OpenGL Programming Guide- Red Book 例子程序库 -系列- 1-Introduction to OpenGL-Part1
2014-07-29 14:49
513 查看
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.cpp Compiling... Example_1_2.cpp e:\openglrb\example_1_2\example_1_2.cpp(29) : fatal error C1010: unexpected end of file while looking for precompiled header directive Error 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.cpp c:\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 specifiers c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : fatal error C1004: unexpected end of file found Error 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.cpp E:\OpenGLRB\Example_1_2\Example_1_2.cpp(6) : fatal error C1083: Cannot open include file: 'aux.h': No such file or directory Error 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.cpp E:\OpenGLRB\Example_1_2\Example_1_2.cpp(28) : error C2065: 'sleep' : undeclared identifier E:\OpenGLRB\Example_1_2\Example_1_2.cpp(29) : warning C4508: 'main' : function should return a value; 'void' return type assumed Error 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.cpp Linking... Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glFlush@0 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glEnd@0 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glVertex2f@8 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glBegin@4 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glOrtho@48 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glLoadIdentity@0 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glMatrixMode@4 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glColor3f@12 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glClear@4 Example_1_2.obj : error LNK2001: unresolved external symbol __imp__glClearColor@16 Example_1_2.obj : error LNK2001: unresolved external symbol _auxInitWindowA@4 Example_1_2.obj : error LNK2001: unresolved external symbol _auxInitPosition@16 Example_1_2.obj : error LNK2001: unresolved external symbol _auxInitDisplayMode@4 Debug/Example_1_2.exe : fatal error LNK1120: 13 unresolved externals Error 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参数调整足够大!
后记,本文针对第一章第二个例子给出了完整的编译排错流程,后续各例子将仅提供原始代码。调整后的代码、运行结果及必要的说明。
相关文章推荐
- 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 Red Book的一个例子代码 crash
- OpenGL Programming Guide (Red Book)——坐标变换
- Linq to Sql 学习系列之十 分层构架的例子
- PowerShell 学习_Part1_Introduction to Toolmaking
- wx网罗系列之翔实:An Introduction To wxWidgets
- CUDA系列学习(一)An Introduction to GPU and CUDA
- 细分球代码 (参考opengl redbook 的第二章)
- 一个大牛的系列文章:An intro to modern OpenGL. Chapter 4: Rendering a Dynamic 3D Scene with Phong Shading
- OpenGL入门系列6 - 累积缓冲区用法例子
- Win32 OpenGL编程系列 2D例子 -- 七巧板图形绘制
- Introduction to Algorithms Chapter 13: Red Black Tree
- opencv中svm支持向量机c++简单例子introduction_to_svm.cpp坐标次序问题
- IBM Redbook之Seven keys to improving business value(seven attributes)
- 一个大牛的系列文章:An intro to modern OpenGL. Chapter 2.2: Shaders
- OpenGL SuperBible读书笔记(Introduction to 3D Graphics and OpenGL)
- 一个大牛的系列文章:An intro to modern OpenGL. Chapter 1: The Graphics Pipeline
- Joe's OpenGL ES 2.0 系列经典入门教程(第一课:Introduction)