Screwed up depth buffer?

I have been having problems with OpenGL in XCode (written in C++). The following code yields different results in Visual Studio and XCode and I cannot figure out why, somehow the z-depth is screwed up so the cube looks weird in XCode.

Any suggestion is appreciated! Happy holidays everyone!

Following is the source code (main.cpp):

#include <iostream>
#include <GLUT/glut.h>

/* Define color vectors */
GLfloat RED[3] = {1.0f, 0.0f, 0.0f};
GLfloat GREEN[3] = {0.0f, 1.0f, 0.0f};
GLfloat BLUE[3] = {0.0f, 0.0f, 1.0f};
GLfloat ORANGE[3] = {1.0f, 0.5f, 0.0f};
GLfloat YELLOW[3] = {1.0f, 1.0f, 0.0f};
GLfloat VIOLET[3] = {1.0f, 0.0f, 1.0f};
GLfloat CYAN[3] = {0.0f, 1.0f, 1.0f};

static GLfloat spin = 0.0;

void init() {
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, -1.0f, -10.0f);
glRotatef(spin, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glColor3fv(GREEN); // Set The Color To Green
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
//glColor3fv(BLUE);
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
//glColor3fv(RED);
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
//glColor3fv(VIOLET);
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3fv(BLUE); // Set The Color To Orange
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
//glColor3fv(YELLOW);
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
//glColor3fv(ORANGE);
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
//glColor3fv(CYAN);
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
glColor3fv(VIOLET);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
//glColor3fv(RED);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
//glColor3fv(YELLOW);
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
//glColor3fv(BLUE);
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3fv(CYAN);
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
//glColor3fv(ORANGE);
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
//glColor3fv(BLUE);
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
//glColor3fv(GREEN);
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
#if 0
glColor3fv(RED);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
//glColor3fv(BLUE);
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
//glColor3fv(ORANGE);
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
//glColor3fv(YELLOW);
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3fv(GREEN);
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
//glColor3fv(VIOLET);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
//glColor3fv(BLUE);
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
//glColor3fv(CYAN);
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
#endif
glEnd();
glutSwapBuffers();
}

void spinDisplay() {
spin += 0.5;
spin > 360.0? (spin - 360.0) : spin;
glutPostRedisplay();
}

void reshape(int width, int height)
{
height == 0? 1 : height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse(int button, int state, int x, int y) {
switch(button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}

int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutCreateWindow(“Rotating Cube”);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

You need to add GLUT_DEPTH to glutInitDisplayMode…

thank you very much!!

Actually I would suggest you to don’t use gluInitDisplayMode on MAC, cuz by default the depth buffer is 16 bits, and will cause major zfighting all over the place (if you are rendering a decent scene), I would suggest you to initialize your window with this line:

glutInitDisplayString(“double rgba depth=24”); // samples

Add samples if you want to use multisampling.

Cheers!

thanks! I didn’t know that :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.