我目前正在研究读取3D obj文件的软件的打印循环。 我已经存储我的obj文件读取variablestie
。 这个variables包含一个OpenGL列表。 我的目标是能够通过使用键盘来移动读取对象。 键盘阅读正确实施(我可以通过日志看到)。
当我编译下面的代码循环,gluLookAt正常exucute,我可以通过改变参数的值移动我的对象。
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ; glMatrixMode(GL_PROJECTION); glLoadIdentity(); light(); gluPerspective (60.0, 250/(float)250, 0.1, 500.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eyeX,eyeY,eyeZ,eyeX+directionX,eyeY+directionY,eyeZ+directionZ,upX,upY,upZ); glPushMatrix(); glRotated(45,0,0,1); glTranslated(0,0,50); glBindTexture(GL_TEXTURE_2D,texture1); //glCallList(xwing); //ICI glEnd(); glPopMatrix(); glColor3d(1,1,1); glDisable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D,texture2); GLUquadric* params = gluNewQuadric(); gluQuadricDrawStyle(params,GLU_FILL); gluQuadricTexture(params,GL_TRUE); gluSphere(params,100,20,20); gluDeleteQuadric(params); glEnable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D,texture1); glCallList(tie); //ICI glPointSize(5.0); glBegin(GL_POINTS); glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f,0.0f,0.0f); glEnd(); SwapBuffers(hDC); //} //else Sleep(1);
但是当我评论这4行时:
glBegin(GL_POINTS); glColor3f(1.0f,0.0f,0.0f); glVertex3f(-1.0f,0.0f,0.0f); glEnd();
我的对象不再移动。 就好像gluLookAt
没有成功执行。 你有什么想法为什么会发生这种情况。 我在代码中忘记了什么吗?
glBegin
和glEnd
分隔定义一个基元或一组基元的顶点。 你必须确保,每个glBegin
后面跟着一个glEnd
。
这意味着,如果您的显示列表包含一个glBegin
那么它也应该包含一个glEnd
。 我强烈建议这样做。 另一种可能性是在glCallList之后手工完成:
glCallList(tie); glEnd();
glPushMatrix
和glPopMatrix
用于在矩阵堆栈上推送矩阵和弹出矩阵。 如果要将模型矩阵添加到视图矩阵,则必须执行以下步骤。
glPushMatrix
。 这会推送堆栈中视图矩阵的副本。 glRotated
, glTranslated
…) glCallList
, gluSphere
,…) glPopMatrix
)。 以这种方式调整你的代码:
// set up view matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(eyeX,eyeY,eyeZ,eyeX+directionX,eyeY+directionY,eyeZ+directionZ,upX,upY,upZ); // save view matrix glPushMatrix(); // add model matrix glRotated(45,0,0,1); glTranslated(0,0,50); // do the drawing glColor3d(1,1,1); glDisable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D,texture2); GLUquadric* params = gluNewQuadric(); gluQuadricDrawStyle(params,GLU_FILL); gluQuadricTexture(params,GL_TRUE); gluSphere(params,100,20,20); gluDeleteQuadric(params); glEnable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D,texture1); glCallList(tie); glEnd(); // <-- maybe this could be done in "tie" // restore the view matrix glPopMatrix();