c++openglframebufferdepth-testingqopenglfunctions

Depth testing doesn't work when using custom framebuffer


I'm stydying framebuffers and I've made a mirror in my scene. It works fine except the depth testing. Got stuck trying to make it work. (when rendering to default frame buffer - depth testing works fine). Would appreciate any help. Here is the code:

glEnable( GL_DEPTH_TEST );

glViewport( 0, 0, 512, 512 );

unsigned int fbo;
glGenFramebuffers( 1, &fbo );
glBindFramebuffer( GL_FRAMEBUFFER, fbo );

unsigned int rbo;
glGenRenderbuffers( 1, &rbo );
glBindRenderbuffer( GL_RENDERBUFFER, rbo );
glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH, 512, 512 );
glBindRenderbuffer( GL_RENDERBUFFER, 0 );

glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, 
GL_RENDERBUFFER, rbo ); //if remove this, mirror works but without depth test
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 
this->mirror->texturePack[0]->textureId(), 0 ); 

//render scene from mirror camera

glBindFramebuffer( GL_FRAMEBUFFER, 0 );
glViewport( 0, 0, this->width(), this->height() );

//render scene from main camera

Solution

  • I've solved it finally. I've added the glClear( GL_DEPTH_BUFFER_BIT ) command right after binding mirror framebuffer and after that it worked.