#include
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();
}
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();
以上代码结构在后续例子中将反复出现
#include#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); }
--------------------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);
--------------------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 #include
#include "StdAfx.h"
#include "windows.h"
#include
#include
--------------------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)
--------------------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")
// Example_1_2.cpp : Defines the entry point for the console application.
//
#include "StdAfx.h"
#include "windows.h"
#include
#include
#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;
}
后记,本文针对第一章第二个例子给出了完整的编译排错流程,后续各例子将仅提供原始代码。调整后的代码、运行结果及必要的说明。