时髦的OpenGL立方体

时间:2013-01-27 20:33:32

标签: c++ opengl blending cubes

啊哈!看来我的问题是我给gluPerspective的zNear值必须大于0,我必须启用深度缓冲来使它工作。我已经更新了以下代码以便工作。

我已经尝试过这么做了,并且一直以为我是以错误的顺序定义我的四边形顶点,但是现在,我知道它的其他东西。

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

what the cube looks like

希望您甚至不必查看我的代码就知道问题是什么,因为这样做并不太难。

如果您不立即知道问题所在,这里是用于设置和绘制多维数据集的代码。

// 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);
}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

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