Cannot seem to get GLFW/OpenGL Depth Testing working

I cannot get Depth testing working with GLFW using codeblocks in any project, even using the default template provided by codeblocks when you start a new project, as below.

Any help is much appreciated as I know I must be doing something wrong but I haven't been able to find it.

#include <GL/glfw.h>

int main()
{
int     width, height;
int     frame = 0;
bool    running = true;

glfwInit();

if( !glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) )
{
    glfwTerminate();
    return 0;
}

glfwSetWindowTitle("GLFW Application");

glEnable(GL_DEPTH_TEST);

while(running)
{
    frame++;

    glfwGetWindowSize( &width, &height );
    height = height > 0 ? height : 1;

    glViewport( 0, 0, width, height );

    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective( 65.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );

    // Draw some rotating garbage
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    gluLookAt(0.0f, -10.0f, 0.0f,
            0.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 1.0f );

    //glTranslatef( 1.0f, 1.0f, 0.0f );
    glRotatef(frame, 0.25f, 1.0f, 0.75f);
    glBegin( GL_TRIANGLES );
      glColor3f(0.1f, 0.0f, 0.0f );
      glVertex3f(0.0f, 3.0f, -4.0f);
      glColor3f(0.0f, 1.0f, 0.0f );
      glVertex3f(3.0f, -2.0f, -4.0f);
      glColor3f(0.0f, 0.0f, 1.0f );
      glVertex3f(-3.0f, -2.0f, -4.0f);
    glEnd();
    glBegin( GL_TRIANGLES );
      glColor3f(0.0f, 0.1f, 0.0f );
      glVertex3f(0.0f, 3.0f, -3.0f);
      glColor3f(0.0f, 0.0f, 1.0f );
      glVertex3f(3.0f, -2.0f, -2.0f);
      glColor3f(1.0f, 0.0f, 0.0f );
      glVertex3f(-3.0f, -2.0f, 2.0f);
    glEnd();
    glfwSwapBuffers();

    // exit if ESC was pressed or window was closed
    running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
}

glfwTerminate();

return 0;

}

Answers


From the GLFW2 documentation (emphasis mine):

int glfwOpenWindow
    (
    int width, int height,
    int redbits, int greenbits, int bluebits,
    int alphabits,
    int depthbits,
    int stencilbits,
    int mode
    )

depthbits: The number of bits to use for the depth buffer (0 means no depth buffer).

And your code:

glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW )
                                      ^ no depth bits

You can't (usefully) use the depth buffer without any depth bits.


Need Your Help

How to merge values of rows from a multiple column list in c#?

c# list multiple-columns

I am getting the data from sql server database and in database, I have column say selectedId. If checkbox1 is true, it assigns selectedId as 1. If checkbox2 is true, it assigns selectedId as 2.

Program to lint YAML files?

yaml lint

So my team is currently working on an application that uses a lot of YAML and we need to enforce some rules for common formatting. The best would be a command-line program so we can plug it into ou...