在 OpenGL 上绘制点的意外 gluLookAt 行为

Unexpected gluLookAt behavior with a point draw on OpenGL

我目前正在研究读取 3D obj 文件的软件的打印循环。 我已将读取的 obj 文件存储在变量 tie 中。该变量包含一个 OpenGL 列表。我的 objective 是能够使用键盘在读取的对象周围移动。键盘读取正确实现(我可以通过日志看到)。

问题

当我编译以下代码循环时,gluLookAt 正确执行并且我可以通过更改参数值来移动我的对象。

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 and glEnd delimit the vertices that define a primitive or a group of like primitives. You have to ensure, the each glBegin is followed by a glEnd.
这意味着,如果您的 Display Lists contains a glBegin then it should contain a glEnd, too. I strongly recommend to do it this way. The other possibility would be to do it manually after glCallList:

glCallList(tie);
glEnd();


glPushMatrix and glPopMatrix 用于将矩阵压入矩阵栈和从矩阵栈中弹出矩阵。如果要将模型矩阵添加到视图矩阵,则必须执行以下步骤。

  • 推送视图矩阵glPushMatrix。这会将视图矩阵的副本推送到堆栈上。
  • 将模型矩阵添加到当前视图矩阵(glRotatedglTranslated、...)
  • 画模型。 (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();