您的位置:首页 > 运维架构

如何配置OpenGL库详解

2013-03-26 21:07 134 查看
如何配置OpenGL库详解

关于OpenGL和directx的争论一直没有停息过,我并不想告诉你哪个更好,但是比较公认的是OpenGL对于一个初学者更容易理解。下面主要说一下OpenGL的可配置(当然这很简单,但有时候库配置好了仍然出错,希望我的方法可以帮到大家)

复制.dll文件(包括glu.dll,glut.dll glut32.lib opengl.dll四个文件)到系统盘c:\windows\system32中

复制.h文件(包括gl.h glaux.h glext.h glu.h glut.h wglext.h六个文件)到Visual c++ 的安装目录下

以我的为例C:\program files\Microsoft visual stdio\VC98\include\GL下,一般都会有GL文件夹的,没有的话新建一个就行了。

复制.lib文件(包括glaux.lib glu.lib glu32.lib glut.lib glut32.lib opengl.lib opengl32.lib 七个文件到C:\program files\Microsoft visual stdio\VC98\lib中

然后就可以开始编写OpenGL程序了

打开VC,新建一个 win32,console application 工程。我用的是中文版,点击“工程(project)”->“设置(setting)”,中选“连接(link)”在“对象\库模块”中添加“opengl32.lib glu32.lib glaux.lib”中间用空格隔开。



新建一个source Files 添加以下代码:

#include<GL\glut.h>

#include <stdlib.h>

void display()

{

glClear(GL_COLOR_BUFFER_BIT);

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();

}

void init()

{

glClearColor(0.0,0.0,0.0,0.0);

glColor3f(1.0,0.0,0.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(-1.0,1.0,-1.0,1.0);

}

void myKeyboard(unsigned char key,int x,int y)

{

if(key==27)exit(0);

}

int main(int argc, char** argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(500,500);

glutInitWindowPosition(0,0);

glutCreateWindow("OpenGL程序");

glutDisplayFunc(display);

glutKeyboardFunc(myKeyboard);

init();

glutMainLoop();

return 0;

}

看看运行结果:



一般来说这样就行了,但是我看到有很多网友按照上面的设置仍然不能运行出正确的OpenGL程序,出现以下错误:

error LNK2001: unresolved external symbol
___glutInitWithExit@12

到v.obj : error LNK2001: unresolved external symbol
___glutCreateWindowWithExit@8

Debug/大法官.exe : fatal error LNK1120: 2 unresolved externals

执行 link.exe 时出错

如果用的是gl.h等头文件也有可能出现以下错误:

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

执行 cl.exe 时出错.

如果不是库的问题就应该是路径设置

那么你可以试试以下方法:

点击“开始”->"附件”->"命令提示符"输入"path"看看有没有VC的路径(如图):

如果没有则进行设置,分别输入:set
include=C:\Program Files\Microsoft Visual Studio\VC98\Include\GL(点击回车)

接着输入:set lib=C:\Program Files\Microsoft Visual Studio\VC98\Lib(点击回车)

(即:include和Lib文件的路径),这样就应该OK了。

或者在#inlcude<glut.h>之前加上#define GLUT_DISABLE_ATEXIT_HACK

感谢百度网友ponglang,在OpenGL方面对我的帮助很多。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: