0

啊哈!看来我的问题是我给 gluPerspective 的 zNear 值必须大于 0,并且我必须启用深度缓冲区才能使其工作。我已经更新了下面的代码以使其正常工作。

我已经尝试过很多次,并且一直认为我以错误的顺序定义了我的四边形顶点,但现在,我知道它是另外一回事了。

我试过启用剔除,将frontFace更改为顺时针,禁用混合,添加法线,但我总是得到一个看起来像这样的立方体;

立方体是什么样子的

希望您甚至不必查看我的代码即可知道问题所在,因为这样得到它并不难。

如果您不立即知道问题出在哪里,这里是用于设置和绘制立方体的代码。

// FIXED CODE.
// reshape, called on init, and window resize
void reshape(int w, int h) {
     scrw=w;
     scrh=h;
     glClearColor(0.8,0.8,0.8,1.0);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluPerspective(cfov,(float) scrw/ (float) scrh,1,1000); // this is also a part of the fix.
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
     glEnable(GL_BLEND);
     glEnable(GL_DEPTH_TEST); // this is a part of the fix
     glEnable(GL_CULL_FACE);
     glCullFace(GL_FRONT);
     glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
     glViewport(0,0,scrw,scrh);
}
// drawQuadCube(), called every frame.
void drawQuadCube() {
     glPushMatrix();
     glTranslated(0.5,0.5,0.5);
     glRotated(xangle,0,1,0);
     glRotated(yangle,1,0,0);
     glRotated(zangle,0,0,1);
     glTranslated(-0.5,-0.5,-0.5);
     glBegin(GL_QUADS);
               //   bottom
               glColor4ub(30,30,255,255);
          glVertex3f(0.0f, 0.0f, 1.0f);
          glVertex3f(1.0f, 0.0f, 1.0f);
          glVertex3f(1.0f, 0.0f, 0.0f);
          glVertex3f(0.0f, 0.0f, 0.0f);
               //   top
               glColor4ub(40,40,255,255);
          glVertex3f(0.0f, 1.0f, 1.0f);
          glVertex3f(1.0f, 1.0f, 1.0f);
          glVertex3f(1.0f, 1.0f, 0.0f);
          glVertex3f(0.0f, 1.0f, 0.0f);
               //   left
               glColor4ub(60,60,255,255);
          glVertex3f(0.0f, 0.0f, 1.0f);
          glVertex3f(0.0f, 0.0f, 0.0f);
          glVertex3f(0.0f, 1.0f, 0.0f);
          glVertex3f(0.0f, 1.0f, 1.0f);
               //   right
               glColor4ub(60,60,200,255);
          glVertex3f(1.0f, 0.0f, 1.0f);
          glVertex3f(1.0f, 0.0f, 0.0f);
          glVertex3f(1.0f, 1.0f, 0.0f);
          glVertex3f(1.0f, 1.0f, 1.0f);
               //   near
               glColor4ub(70,70,100,255);
          glVertex3f(0.0f, 0.0f, 0.0f);
          glVertex3f(1.0f, 0.0f, 0.0f);
          glVertex3f(1.0f, 1.0f, 0.0f);
          glVertex3f(0.0f, 1.0f, 0.0f);
               //   far
               glColor4ub(20,20,90,255);
          glVertex3f(0.0f, 0.0f, 1.0f);
          glVertex3f(1.0f, 0.0f, 1.0f);
          glVertex3f(1.0f, 1.0f, 1.0f);
          glVertex3f(0.0f, 1.0f, 1.0f);
          glNormal3f(0,0,0);
          glNormal3f(0,0,1);
          glNormal3f(0,1,0);
          glNormal3f(1,0,0);
          glNormal3f(1,0,1);
          glNormal3f(1,1,0);
          glNormal3f(1,1,1);
          glNormal3f(0,1,1);
     glEnd();
     glPopMatrix();

}
// if that isn't enough, this is the function used to set up the view.
void setView(void) {
     glLoadIdentity();
     gluLookAt(0.5,0.5,-5,0.5,0.5,0.5,0,1,0);
}
4

3 回答 3

1

你的缠绕模式不正确。

glFrontFace 默认为 GL_CCW,但您的“正面四边形”,在本例中为“近”边,顺时针缠绕(从您的相机位置的参考系开始;注意它位于负 Z,并沿正 Z 看) . glCullFace默认为 GL_BACK,因此被剔除。正确设置:

glFrontFace(GL_CW);

另见http://www.opengl.org/sdk/docs/man/xhtml/glFrontFace.xml

完成该设置后,您将需要启用深度缓冲,以便您的四边形正确地覆盖而不依赖于绘制顺序。见:http ://www.opengl.org/archives/resources/faq/technical/depthbuffer.htm

于 2013-01-27T21:26:02.347 回答
0

尝试:

 glCullFace(GL_FRONT);

http://www.opengl.org/sdk/docs/man/xhtml/glCullFace.xml

或者:

glEnable(GL_CULL_FACE);

http://www.opengl.org/sdk/docs/man/xhtml/glEnable.xml

于 2013-01-27T21:19:29.080 回答
0

您没有指定您使用的窗口机制,但如果您使用 glut,请不要忘记在创建窗口时设置 GLUT_DEPTH 标志。这是一个经常被忽视的简单常见错误。

于 2014-04-07T01:02:27.957 回答