Question

Aha! It seems my problem was that my zNear value given to gluPerspective had to be greater than 0, and I had to enable the depth buffer to get it working. Ive updated the code below to be working.

I've tried to do this a lot, and always thought I was defining my quad vertices in the wrong order, but now, I know its something else.

I've tried enabling Culling, changing frontFace to clockwise, disabling Blending, adding normals, but I always get a cube that looks like this;

what the cube looks like

Hopefully, you won't even have to look at my code to know what the problem is, as it wasn't too hard to get it like this.

If you don't immediately know what the problem is, here's the code used to set up and draw the cube.

// 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);
}
Was it helpful?

Solution

Your winding mode is incorrect.

glFrontFace defaults to GL_CCW, but your "front-facing quad", in this example the "near" one, is wound clockwise (from the frame of reference of your camera position; note that it's at negative Z, and looking along positive Z). glCullFace defaults to GL_BACK, so it's getting culled. Set it correctly with:

glFrontFace(GL_CW);

See also http://www.opengl.org/sdk/docs/man/xhtml/glFrontFace.xml

Once you've got that setup, then you'll want to enable depth-buffering, so your quads overpaint correctly without relying on paint ordering. See: http://www.opengl.org/archives/resources/faq/technical/depthbuffer.htm

OTHER TIPS

You didn't specify what windowing mechanism you were using but incase you are using glut, don't forget to set the GLUT_DEPTH flag while creating the window. Thats a simple common error frequently overlooked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top