PDA

View Full Version : Render to RenderBuffer with Depth Test



Limdor
03-06-2011, 11:58 AM
Hi guys,

I am rendering a box on the screen and it works but if I render it to a a render buffer it seems that the depth test doesn't work. What am I doing wrong? I attach the code and the images.

Initialization of renderbuffer and framebuffer:

GLuint renderBuffer;
GLuint frameBuffer;

glGenRenderbuffers( 1, &renderBuffer );
glBindRenderbuffer( GL_RENDERBUFFER, renderBuffer );
glRenderbufferStorage( GL_RENDERBUFFER, GL_RGBA32F, mWinWidth, mWinHeight );
CHECK_GL_ERROR();

glGenFramebuffers( 1, &frameBuffer );
glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);
if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
CECK_GL_ERROR();
glBindFramebuffer( GL_FRAMEBUFFER, 0 );

Preparing for drawing:

glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glViewport( 0, 0, mWinWidth, mWinHeight );
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

McLeary
03-06-2011, 12:24 PM
You need to generate a render buffer for depth buffer in order to allow the framebuffer object to render the depth component (my head is gonna blow if I write "buffer" one more time, oops). Something like:



// create a renderbuffer object to store depth info
GLuint rboId;
glGenRenderbuffersEXT(1, &rboId);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rboId);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, TEXTURE_WIDTH, TEXTURE_HEIGHT);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);


And then attach the buffer to the framebuffer object



// attach the renderbuffer to depth attachment point
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, rboId);

Limdor
03-06-2011, 12:49 PM
Thanks, it works!

This is the final code:

Initialization of renderbuffer and framebuffer:

GLuint renderBuffer;
GLuint frameBuffer;
GLuint depthRenderBuffer;

glGenRenderbuffers( 1, &renderBuffer );
glBindRenderbuffer( GL_RENDERBUFFER, renderBuffer );
glRenderbufferStorage( GL_RENDERBUFFER, GL_RGB32F, mWinWidth, mWinHeight );
CHECK_GL_ERROR();

glGenRenderbuffers( 1, &depthRenderBuffer );
glBindRenderbuffer( GL_RENDERBUFFER, depthRenderBuffer );
glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, mWinWidth, mWinHeight );
CHECK_GL_ERROR();

glGenFramebuffers( 1, &frameBuffer );
glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBuffer);
glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRenderBuffer);
if( glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
CHECK_GL_ERROR();
glBindFramebuffer( GL_FRAMEBUFFER, 0 );

Preparing for drawing:

glBindFramebuffer( GL_FRAMEBUFFER, frameBuffer );
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glViewport( 0, 0, mWinWidth, mWinHeight );
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );