#include "GLUT.H" GLfloat mat_diffuse[] = {1.0,0.0,0.0,0.0}; GLfloat mat_specular[] = {1.0,1.0,1.0,1.0}; GLfloat high_shininess[] = {100.0}; void myInit(void) { GLfloat light_position[] = {0.0,3.0,6.0,0.0}; glLightfv(GL_LIGHT2,GL_POSITION,light_position);//设置光源 glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); //为光照模型指定材质参数 glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); glMaterialfv(GL_FRONT,GL_SHININESS,high_shininess); glColorMaterial(GL_FRONT,GL_AMBIENT);//使材质色跟踪当前颜色 glEnable(GL_COLOR_MATERIAL); glPushMatrix(); glRotatef(40,1.0,1.0,1.0); glutSolidCube(2); glPopMatrix(); glDisable(GL_COLOR_MATERIAL); glPopMatrix(); glFlush(); } void myReshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w <= h) glOrtho(-5.5,5.5,-5.5*(GLfloat)h/(GLfloat)w,5.5*(GLfloat)h/(GLfloat)w,-5.5,5.5); else glOrtho(-5.5*(GLfloat)w/(GLfloat)h,5.5*(GLfloat)w/(GLfloat)h,-5.5,5.5,-5.5,5.5); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(int argc,char ** argv) { //初始化 glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(400,400); glutInitWindowPosition(100,100); //创建窗口 glutCreateWindow("Cube"); //绘制 myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMainLoop(); return 0; }
先上了源程序,用来画一个立方体。
opengl开发环境配置方法为http://hi.baidu.com/amilipan/item/2add3df6a284b9d042c36ace。
刚开始,我按照网上其他的方法配置,没有在预处理器定义栏加入GLUT_BUILDING_LIB,结果出现了如下错误:
1>test.obj : error LNK2019: 无法解析的外部符号 __imp____glutInitWithExit@12,该符号在函数 _glutInit_ATEXIT_HACK@8 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 __imp____glutCreateWindowWithExit@8,该符号在函数 _glutCreateWindow_ATEXIT_HACK@4 中被引用。
针对这个问题,共找到了两种解决方法:
1.如上所述,在预处理器定义栏加入GLUT_BUILDING_LIB。
2.在#include "glut.h"前加一行#define GLUT_DISABLE_ATEXIT_HACK。
另外在配置时,不要以为头文件#include "glut.h"就一定对应glut.lib,而在附加依赖项中添加该引导库,这也会导致上面所提到的错误,因为该头文件对应的引导库为glut32.lib。