OpenGL Programming Guide- Red Book 例子程序库 -系列- 1-Introduction to OpenGL-Part1
来源:互联网 发布:excel找出重复数据行 编辑:程序博客网 时间:2024/05/07 06:01
Example 1-1 : A Simple OpenGL Program
#include <whateverYouNeed.h>main() { OpenAWindowPlease(); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); KeepTheWindowOnTheScreenForAWhile();}
此程序无法编译通过,但把GL编程的核心内容和架构体现出来了
glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush();
以上代码结构在后续例子中将反复出现
Example 1-2 : A Simple OpenGL Program Using the Auxiliary Library: simple.c
原始程序
#include <GL/gl.h>#include "aux.h"int main(int argc, char** argv){ auxInitDisplayMode (AUX_SINGLE | AUX_RGBA); auxInitPosition (0, 0, 500, 500); auxInitWindow (argv[0]); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); sleep(10);}
VC项目创建步骤
新建Win32 Console 项目
选择类型,建议选择2nd or 3rd
创建完毕的状态
复制例子原始代码,覆盖main函数所在cpp文件全部
第一轮编译错误
--------------------Configuration: Example_1_2 - Win32 Debug--------------------Compiling...StdAfx.cppCompiling...Example_1_2.cppe:\openglrb\example_1_2\example_1_2.cpp(29) : fatal error C1010: unexpected end of file while looking for precompiled header directiveError executing cl.exe.Example_1_2.exe - 1 error(s), 0 warning(s)解决方法,在example_1_2.cpp的头部第一行包含stdafx.h
第二轮编译错误
--------------------Configuration: Example_1_2 - Win32 Debug--------------------Compiling...Example_1_2.cppc:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2144: syntax error : missing ';' before type 'void'c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2501: 'WINGDIAPI' : missing storage-class or type specifiersc:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : fatal error C1004: unexpected end of file foundError executing cl.exe.Example_1_2.exe - 3 error(s), 0 warning(s)百度搜索WINGDIAPI 可见该错误,原因是VC INCLUDE的gl.h文件是被Windows调整过的,和Qt开发库里面的gl.h内容上有区别
WINGDIAPI void APIENTRY glAccum (GLenum op, GLfloat value);WINGDIAPI void APIENTRY glAlphaFunc (GLenum func, GLclampf ref);WINGDIAPI GLboolean APIENTRY glAreTexturesResident (GLsizei n, const GLuint *textures, GLboolean *residences);WINGDIAPI void APIENTRY glArrayElement (GLint i);WINGDIAPI void APIENTRY glBegin (GLenum mode);WINGDIAPI void APIENTRY glBindTexture (GLenum target, GLuint texture);WINGDIAPI void APIENTRY glBitmap (GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, const GLubyte *bitmap);WINGDIAPI void APIENTRY glBlendFunc (GLenum sfactor, GLenum dfactor);WINGDIAPI void APIENTRY glCallList (GLuint list);
此处解决方案: 在stdafx.h中增加包含 windows.h,如果在example_1_2.cpp中包含,需要在包含gl.h之前
第三轮编译错误
--------------------Configuration: Example_1_2 - Win32 Debug--------------------Compiling...Example_1_2.cppE:\OpenGLRB\Example_1_2\Example_1_2.cpp(6) : fatal error C1083: Cannot open include file: 'aux.h': No such file or directoryError executing cl.exe.Example_1_2.exe - 1 error(s), 0 warning(s)
没有aux.h,此文件已被更名为glaux.h,和gl.h在同一文件夹
此处解决方案:将aux.h修改为GLAUX.H #include <GL/glaux.h>
至此cpp包含的头文件如下
#include "StdAfx.h"#include "windows.h"#include <GL/gl.h>#include <GL/glaux.h>
第四轮编译错误
--------------------Configuration: Example_1_2 - Win32 Debug--------------------Compiling...Example_1_2.cppE:\OpenGLRB\Example_1_2\Example_1_2.cpp(28) : error C2065: 'sleep' : undeclared identifierE:\OpenGLRB\Example_1_2\Example_1_2.cpp(29) : warning C4508: 'main' : function should return a value; 'void' return type assumedError executing cl.exe.Example_1_2.exe - 1 error(s), 1 warning(s)
解决方案, sleep函数首字母大写,main 函数结尾增加return 语句,或者将函数返回类型改为void, sleep(10)中的10是窗口生存时间,毫秒,需要改大一些
第五轮编译错误
--------------------Configuration: Example_1_2 - Win32 Debug--------------------Compiling...Example_1_2.cppLinking...Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glFlush@0Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glEnd@0Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glVertex2f@8Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glBegin@4Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glOrtho@48Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glLoadIdentity@0Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glMatrixMode@4Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glColor3f@12Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glClear@4Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glClearColor@16Example_1_2.obj : error LNK2001: unresolved external symbol _auxInitWindowA@4Example_1_2.obj : error LNK2001: unresolved external symbol _auxInitPosition@16Example_1_2.obj : error LNK2001: unresolved external symbol _auxInitDisplayMode@4Debug/Example_1_2.exe : fatal error LNK1120: 13 unresolved externalsError executing link.exe.Example_1_2.exe - 14 error(s), 0 warning(s)
此处错误为链接错误,源文件的语法、拼写错误已解决
解决方案为引入OpenGL相关的lib文件
#pragma comment(lib,"opengl32.lib")#pragma comment(lib,"glaux.lib")
至此编译链接全部通过,当前main函数cpp文件内容如下
// Example_1_2.cpp : Defines the entry point for the console application.//#include "StdAfx.h"#include "windows.h"#include <GL/gl.h>#include <GL/glaux.h>#pragma comment(lib,"opengl32.lib")#pragma comment(lib,"glaux.lib")int main(int argc, char** argv){ auxInitDisplayMode (AUX_SINGLE | AUX_RGBA); auxInitPosition (0, 0, 500, 500); auxInitWindow (argv[0]); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON);glVertex2f(-0.5, -0.5);glVertex2f(-0.5, 0.5);glVertex2f(0.5, 0.5);glVertex2f(0.5, -0.5); glEnd(); glFlush(); Sleep(1000); return 0;}
运行截图
此截图需要将sleep参数调整足够大!
后记,本文针对第一章第二个例子给出了完整的编译排错流程,后续各例子将仅提供原始代码。调整后的代码、运行结果及必要的说明。
0 0
- OpenGL Programming Guide- Red Book 例子程序库 -系列- 1-Introduction to OpenGL-Part1
- OpenGL Programming Guide- Red Book 例子程序库 -系列- 2-Introduction to OpenGL-Part2
- OpenGL Programming Guide- Red Book 例子程序库 -系列- 3-Chapter 2Drawing Geometric Objects-Part1
- OpenGL Programming Guide- Red Book 例子程序库 -系列- 0
- OpenGL Programming Guide or 'The Red Book'
- OpenGL Programming Guide (Red Book)——坐标变换
- OpenGL ES2.0 Programming Guide - chapter 1:Introduction to OpenGL ES 2.0
- OpenGL ES2.0 Programming Guide - chapter 3:An introduction to EGL
- OpenGL Red Book的一个例子代码 crash
- OpenGL Programming Guide
- OpenGL Programming Guide
- Introduction to WebKit Objective-C Programming Guide
- Introduction to Core Data Programming Guide
- Introduction to Stream Programming Guide for Cocoa
- Introduction to CFNetwork Programming Guide(译)
- OpenGL(R) ES 2.0 Programming Guide
- OpenGL Programming Guide 6th done, roughly
- OpenGL ES Programming Guide for iOS 第一章
- 青蛙的约会 POJ1061
- 文件及文件夹
- 不要做一个浮躁的程序员
- Android 使用 Ant 批量打包
- MVC和MVP
- OpenGL Programming Guide- Red Book 例子程序库 -系列- 1-Introduction to OpenGL-Part1
- [LeetCode]Search a 2D Matrix
- 父类指针向子类指针的强制类型转换
- 编程珠玑中关于二分查找的使用
- Java设计模式之代理模式
- 如何让优秀的程序员更进一步
- 技术网站网站推荐
- 80X86寄存器介绍
- 手机病毒满10年 高风险移动app突破二百万