| 查看: 1044 | 回复: 8 | |||
sukiyq木虫 (小有名气)
|
[求助]
OpenGL里全局变量引起的奇怪的问题
|
|
下边是一个光照的OpenGL示例程序,运行有错误,把程序里完全没有用到的全局变量“GLfloat light_pos[] = {1.0, 2.0, 3.0, 0.0};”一行删除运行就正常了。这是为什么?明明这个全局变量并没有用到,但是设置了它程序就会出错,好奇怪。 #include GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; GLfloat colors[][4] = {{1.0,0.0,0.0,0.5}, {0.0,1.0,1.0,0.5}, {1.0,1.0,0.0,0.5}, {0.0,1.0,0.0,0.5}, {0.0,0.0,1.0,0.5}, {1.0,0.0,1.0,0.5}}; GLfloat light_pos[] = {1.0, 2.0, 3.0, 0.0}; void polygon(int a, int b, int c , int d) { glColor4fv(colors[a]); if(colors[a][4] != 1.0) glDepthMask(GL_TRUE); else glDepthMask(GL_FALSE); glBegin(GL_POLYGON); glVertex3fv(vertices[a]); glVertex3fv(vertices); glVertex3fv(vertices[c]); glVertex3fv(vertices[d]); glEnd(); } void cube() { polygon(0,3,2,1); polygon(2,3,7,6); polygon(3,0,4,7); polygon(1,2,6,5); polygon(4,5,6,7); polygon(5,4,0,1); } static GLfloat theta[] = {0.0,0.0,0.0}; static GLint axis = 2; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); cube(); glutSwapBuffers(); } void spinCube() { theta[axis] += 2.0; if( theta[axis] > 360.0 ) theta[axis] -= 360.0; glutPostRedisplay(); } void mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); } void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("colorcube" ;glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(spinCube); glutMouseFunc(mouse); glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glutMainLoop(); } |
» 猜你喜欢
博士读完未来一定会好吗
已经有14人回复
心脉受损
已经有4人回复
Springer期刊投稿求助
已经有4人回复
读博
已经有3人回复
小论文投稿
已经有3人回复
Bioresource Technology期刊,第一次返修的时候被退回好几次了
已经有9人回复
到新单位后,换了新的研究方向,没有团队,持续积累2区以上论文,能申请到面上吗
已经有8人回复
申请2026年博士
已经有6人回复
请问哪里可以有青B申请的本子可以借鉴一下。
已经有5人回复

conanwj
版主 (著名写手)
- 信息EPI: 1
- 应助: 20 (小学生)
- 贵宾: 0.251
- 金币: 19811.3
- 散金: 732
- 红花: 16
- 沙发: 1
- 帖子: 2951
- 在线: 313.3小时
- 虫号: 706359
- 注册: 2009-02-22
- 性别: GG
- 专业: 模式识别
- 管辖: 信息科学
2楼2012-09-10 20:16:48
sukiyq
木虫 (小有名气)
- 应助: 25 (小学生)
- 金币: 2808.9
- 散金: 10
- 红花: 3
- 帖子: 211
- 在线: 68.5小时
- 虫号: 1397425
- 注册: 2011-09-10
- 专业: 模式识别

3楼2012-09-10 22:12:04
4楼2012-09-11 09:03:36
conanwj
版主 (著名写手)
- 信息EPI: 1
- 应助: 20 (小学生)
- 贵宾: 0.251
- 金币: 19811.3
- 散金: 732
- 红花: 16
- 沙发: 1
- 帖子: 2951
- 在线: 313.3小时
- 虫号: 706359
- 注册: 2009-02-22
- 性别: GG
- 专业: 模式识别
- 管辖: 信息科学
5楼2012-09-11 09:06:43
6楼2012-09-11 09:44:47
sukiyq
木虫 (小有名气)
- 应助: 25 (小学生)
- 金币: 2808.9
- 散金: 10
- 红花: 3
- 帖子: 211
- 在线: 68.5小时
- 虫号: 1397425
- 注册: 2011-09-10
- 专业: 模式识别

7楼2012-09-11 13:09:37
sukiyq
木虫 (小有名气)
- 应助: 25 (小学生)
- 金币: 2808.9
- 散金: 10
- 红花: 3
- 帖子: 211
- 在线: 68.5小时
- 虫号: 1397425
- 注册: 2011-09-10
- 专业: 模式识别

8楼2012-09-11 13:20:49
sukiyq
木虫 (小有名气)
- 应助: 25 (小学生)
- 金币: 2808.9
- 散金: 10
- 红花: 3
- 帖子: 211
- 在线: 68.5小时
- 虫号: 1397425
- 注册: 2011-09-10
- 专业: 模式识别

9楼2012-09-11 13:23:47













;
回复此楼