Problem, getting black window with depth testing
10:10 19 Nov 2025

I have a problem that my window becomes black when I enable GL_DEPTH_TEST. I suspect that my framebuffers aren't properly setup. I have included code that I believe to be minimal. As the code is right now an image appears. By removing the // before glEnable(GL_DEPTH_TEST); Screen goes black. I have tested with GL_ALWAYS and an image appears, but obviously no depth test.

I have looked up other questions about the same problem, but it was not the same cause or not enough information. I have been looking through many tutorials and I cannot find the solution to this problem.

To compile the code in gcc I use the following command.

gcc -o minimum main.c -lglut -lGL

#include 
#include 
#include 
#include 

extern void glGenFramebuffers (GLsizei n, GLuint *framebuffers);
extern void glBindFramebuffer (GLenum target, GLuint framebuffer);
extern void glFramebufferTexture2D (GLenum target, GLenum attachment, GLenum textar
get, GLuint texture , GLint level);
extern void glGenRenderbuffers (GLsizei n, GLuint *renderbuffers);
extern void glBindRenderbuffer (GLenum target, GLuint renderbuffer);
extern void glRenderbufferStorage (GLenum target, GLenum internalformat, GLsizei wi
dth, GLsizei height);
extern void glFramebufferRenderbuffer (GLenum target, GLenum attachment, GLenum ren
derbuffertarget, GLuint renderbuffer);
extern void glGenerateMipmap (GLenum target);
extern GLenum glCheckFramebufferStatus (GLenum target);

int main(int argc, char** argv)
{
        GLuint t_color, t_depth, b_frame, b_render, t_sides[3];
        unsigned char image1[16], image2[16], image3[16];

        // Init images for textures on cube
        image1[0]  = 0;   image1[1]  = 0;   image1[2]  = 255; image1[3]  = 0;
        image1[4]  = 255; image1[5]  = 255; image1[6]  = 0;   image1[7]  = 0;
        image1[8]  = 255; image1[9]  = 255; image1[10] = 0;   image1[11] = 0;
        image1[12] = 0;   image1[13] = 0;   image1[14] = 255; image1[15] = 0;

        image2[0]  = 0;   image2[1]  = 0;   image2[2]  = 255; image2[3]  = 0;
        image2[4]  = 255; image2[5]  = 0;   image2[6]  = 0;   image2[7]  = 0;
        image2[8]  = 255; image2[9]  = 0;   image2[10] = 0; image2[11] = 0;
        image2[12] = 0;   image2[13] = 0;   image2[14] = 255; image2[15] = 0;

        image3[0]  = 0;   image3[1]  = 0;   image3[2]  = 255; image3[3]  = 0;
        image3[4]  = 0;   image3[5]  = 255; image3[6]  = 0;   image3[7]  = 0;
        image3[8]  = 0;   image3[9]  = 255; image3[10] = 0;   image3[11] = 0;
        image3[12] = 0;   image3[13] = 0;   image3[14] = 255; image3[15] = 0;

        // Init window
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);

        glutInitWindowPosition(-1, -1);
        glutInitWindowSize(640, 480);
        glutCreateWindow("Test");

        glClearColor(0.95f, 0.95f, 0.65f, 0.0f);
        // glClearDepth(1.0f);

        // glEnable(GL_DEPTH_TEST);
        // glDepthFunc(GL_LESS);
        // glDepthFunc(GL_ALWAYS);
        // glEnable(GL_CULL_FACE);

        // Generate color texture for framebuffer
        glGenTextures(1, &t_color);
        glBindTexture(GL_TEXTURE_2D, t_color);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 640, 480, 0, GL_RGBA, GL_UNSIGNED_B
YTE, NULL);
        glBindTexture(GL_TEXTURE_2D, 0);

        // Generate depth texture for framebuffer
        glGenTextures(1, &t_depth);
        glBindTexture(GL_TEXTURE_2D, t_depth);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 640, 480, 0, GL_DEPTH_CO
MPONENT, GL_UNSIGNED_BYTE, NULL);
        glBindTexture(GL_TEXTURE_2D, 0);

        // Generate framebuffer
        glGenFramebuffers(1, &b_frame);
        glBindFramebuffer(GL_FRAMEBUFFER, b_frame);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
 t_color, 0);
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 
t_depth, 0);

        glGenRenderbuffers(1, &b_render);
        glBindRenderbuffer(GL_RENDERBUFFER, b_render);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 640, 480);
        glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_
RENDERBUFFER, b_render);
        glBindRenderbuffer(GL_RENDERBUFFER, 0);

        GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
        fprintf(stdout, "Status: %d\n", status);
        fprintf(stdout, "Facit: %d\n", GL_FRAMEBUFFER_COMPLETE);
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

        // Generate textures for the sides of the cube
        glGenTextures(3, t_sides);
        glBindTexture(GL_TEXTURE_2D, t_sides[0]);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_BGRA, GL_UNSIGNED_BYTE, 
image1);
        glBindTexture(GL_TEXTURE_2D, 0);

        glBindTexture(GL_TEXTURE_2D, t_sides[1]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_BGRA, GL_UNSIGNED_BYTE, 
image2);
        glBindTexture(GL_TEXTURE_2D, 0);

        glBindTexture(GL_TEXTURE_2D, t_sides[2]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_BGRA, GL_UNSIGNED_BYTE, 
image3);
        glBindTexture(GL_TEXTURE_2D, 0);

        glBindFramebuffer(GL_FRAMEBUFFER, b_frame);
        glBindRenderbuffer(GL_RENDERBUFFER, b_render);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

        glPushMatrix();
        glRotatef(-5.0, 1, 0, 0);
        glRotatef(5.0, 0, 1, 0);

        // Bind the textures for the sides of the cube
        glBindTexture(GL_TEXTURE_2D, t_sides[0]);
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
                glTexCoord2f(0.0f,0.0f); glVertex3f(-0.5, -0.5, -0.25);
                glTexCoord2f(1.0f,0.0f); glVertex3f( 0.5, -0.5, -0.25);
                glTexCoord2f(1.0f,1.0f); glVertex3f( 0.5,  0.5, -0.25);
                glTexCoord2f(0.0f,1.0f); glVertex3f(-0.5,  0.5, -0.25);
        glEnd();
        glBindTexture(GL_TEXTURE_2D, 0);

        glBindTexture(GL_TEXTURE_2D, t_sides[1]);
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
                glTexCoord2f(0.0f,0.0f); glVertex3f(-0.5, -0.5, -0.75);
                glTexCoord2f(1.0f,0.0f); glVertex3f(-0.5, -0.5, -0.25);
                glTexCoord2f(1.0f,1.0f); glVertex3f(-0.5,  0.5, -0.25);
                glTexCoord2f(0.0f,1.0f); glVertex3f(-0.5,  0.5, -0.75);
        glEnd();
        glBindTexture(GL_TEXTURE_2D, 0);

        glBindTexture(GL_TEXTURE_2D, t_sides[2]);
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
                glTexCoord2f(0.0f,0.0f); glVertex3f(-0.5,  0.5, -0.25);
                glTexCoord2f(1.0f,0.0f); glVertex3f( 0.5,  0.5, -0.25);
                glTexCoord2f(1.0f,1.0f); glVertex3f( 0.5,  0.5, -0.75);
                glTexCoord2f(0.0f,1.0f); glVertex3f(-0.5,  0.5, -0.75);
        glEnd();
        glBindTexture(GL_TEXTURE_2D, 0);

        glPopMatrix();
        glBindRenderbuffer(GL_RENDERBUFFER, 0);
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

        glBindTexture(GL_TEXTURE_2D, t_color);
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
                glTexCoord2f(0.0f,0.0f); glVertex3f(-1.0, -1.0, 0.0);
                glTexCoord2f(1.0f,0.0f); glVertex3f( 1.0, -1.0, 0.0);
                glTexCoord2f(1.0f,1.0f); glVertex3f( 1.0,  1.0, 0.0);
                glTexCoord2f(0.0f,1.0f); glVertex3f(-1.0,  1.0, 0.0);

        glEnd();
        glBindTexture(GL_TEXTURE_2D, 0);
        glFlush();

        sleep(3);
}

Note: The reason I do not include GL/glext.h, is that it does nothing on my ubuntu system, I have to copy the function prototypes from the header file to get it to work.

opengl depth-testing