转载 http://blog.csdn.net/xiajun07061225/article/details/6938740
我在运行《OpenGL编程指南(第六版)》中文版第142页的程序blendeqn.c时,遇到错误:
1>------ 已启动生成: 项目: blendeqn, 配置: Debug Win32 ------
1>正在编译...
1>blendeqn.cpp
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(23) : error C2065:
“GL_FUNC_ADD”: 未声明的标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(23) : error C3861:
“glBlendEquation”: 找不到标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(26) : error C2065:
“GL_FUNC_SUBTRUCT”: 未声明的标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(26) : error C3861:
“glBlendEquation”: 找不到标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(29) : error C2065:
“GL_FUNC_REVERSE_SUBTRUCT”: 未声明的标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(29) : error C3861:
“glBlendEquation”: 找不到标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(32) : error C2065:
“GL_FUNC_MIN”: 未声明的标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(32) : error C3861:
“glBlendEquation”: 找不到标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(35) : error C2065:
“GL_FUNC_MAX”: 未声明的标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(35) : error C2143: 语法错
误 : 缺少“)”(在“;”的前面)
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(35) : error C3861:
“glBlendEquation”: 找不到标识符
1>e:\学习\程序练习\opengl\blendeqn\blendeqn\blendeqn.cpp(38) : error C3861:
“exit”: 找不到标识符
1>生成日志保存在“file://e:\学习\程序练习\OpenGL\blendeqn\blendeqn\Debug
\BuildLog.htm”
1>blendeqn - 12 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
出错原因是:glBlendEquation是OpenGL扩展部分的函数。解决方法是安装OpenGL的glew扩展库。
我的编译环境:VS2008。
首先下载glew库:http://glew.sourceforge.net/
然后进行安装:
拷贝glew32.dll至system32文件夹下,拷贝glew32.lib至VC目录下lib文件夹,拷贝glew.h和wglew.h至VC目录下include/GL目录下。
在程序开始要导入头文件:#include <GL/glew.h>。
其次还要导入库文件glew32.lib:
初始化glew:
可以在init函数开始处初始化(注意:这一步是非常必要的,不然会出现奇怪的错误!):
glewInit();
不加这句代码的话,应用程序会出现下列错误:
还可以加入错误处理代码。
编译运行,成功。
完整代码:
- #include <GL/glew.h>
- #include <GL/glut.h>
- #include <stdlib.h>
- void init()
- {
- glewInit();
- glClearColor(1.0,1.0,0.0,0.0);
- glBlendFunc(GL_ONE,GL_ONE);
- glEnable(GL_BLEND);
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0.0,0.0,1.0);
- glRectf(-0.5,-0.5,0.5,0.5);
- glFlush();
- }
- void keyboard(unsigned char key,int x,int y)
- {
- switch(key)
- {
- case 'a':case 'A':
- glBlendEquation(GL_FUNC_ADD);
- break;
- case 's':case 'S':
- glBlendEquation(GL_FUNC_SUBTRACT);
- break;
- case 'r':case 'R':
- glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
- break;
- case 'm':case 'M':
- glBlendEquation(GL_MIN);
- break;
- case 'x':case 'X':
- glBlendEquation(GL_MAX);
- break;
- case 27:
- exit(0);
- break;
- }
- glutPostRedisplay();
- }
- int main(int argc,char** argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(500,500);
- glutInitWindowPosition(100,100);
- glutCreateWindow(argv[0]);
- init();
- glutDisplayFunc(display);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- return 0;
- }
运行结果: