The possible causes are:
no glEnable(GL_DEPTH_TEST);
no glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); in every frame
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.0f,100.0f);
0 for near (should be 0.1f)
But the culprit of my problem is:
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE );
which is called before you run openGL
If you don't put GLUT_DEPTH in here, depth won't be calculated!
To check whether you've defined depth or not in your program:
int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);
std::cout << "depth " << depth << std::endl;
if the result is 0, that means the depth is not defined.
This GL_DEPTH_BITS seems to be deprecated in openGL 3.3 / 4, though. I don't know what my version is but it worked (was 0, is now non 0).
I'm using gtkglarea, and the setting for it is:
int attrlist[] = {
GDK_GL_RGBA,
GDK_GL_RED_SIZE,1,
GDK_GL_GREEN_SIZE,1,
GDK_GL_BLUE_SIZE,1,
GDK_GL_DOUBLEBUFFER,
GDK_GL_DEPTH_SIZE,16,
GDK_GL_NONE
};
which is used by glArea = GTK_WIDGET(gtk_gl_area_new(attrlist)); to create new open widget in GTK.
Not sure why the size is 16, though, but putting on 32 results in a blank screen for mine.
Beware that some computer may work by default without this depth setting. Ugh. @_@