Hey guys, I have some questions about how an FBO may work.
I'm trying to incorporate FBOs into an existing rendering pipeline. To maintain current architecture I'm trying to use only one FBO for the program. I know it may not be ideal, but it should still work, (unless someone knows a better way to do MRT in OpenGL). I really can't know about any other targets I may be bound with when I create the render target, so I'm creating them and attaching them to the FBO later on.
So my current path is like such:
If it's a color target: Create a texture.
If it's a depth target: Create a renderbuffer.
Then when I bind the targets for a draw:
Bind a framebuffer
For each color target
If it's a color target.
Attach my texture to the frame buffer
If it's a depth target
Attach my renderbuffer to the frame buffer
Enable the draw buffer for any color target attachments
This runs with no problems, but in gDebugger I don't see anything being drawn to my color target's texture. Any ideas?
//
// Create our gl texture handle
//
glGenTextures( 1, &texture->m_Texture );
GL_ERROR( "Failed to generate render target texture" );
glBindTexture( GL_TEXTURE_2D, texture->m_Texture );
GL_ERROR( "Failed to bind the render target texture when initializing" );
//
// Set the texture data parameters and some filtering data.
// TODO: We need to pass in platform agnostic filtering information here.
//
GLenum platformFormat = GetPlatformTextureFormat( format );
GLenum imageFormat = GetOpenGLImageFormat( platformFormat );
GLenum componentType = GetOpenGLComponentType( platformFormat );
glTexImage2D( GL_TEXTURE_2D, 0, platformFormat, width, height, 0, imageFormat, componentType, 0 );
GL_ERROR( "Failed to set the texture image parameters for a render target texture" );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
GL_ERROR( "Failed to set the filtering parameters for a render target texture" );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GL_ERROR( "Failed to set the filtering parameters for a render target texture" );
and for depth stencil render targets I use this code:
pglGenRenderbuffers( 1, &m_PlatformData.m_TargetHandle );
GL_ERROR( "Failed to generate render buffer handle" );
pglBindRenderbuffer( GL_RENDERBUFFER, m_PlatformData.m_TargetHandle );
GL_ERROR( "Failed to bind the render buffer" );
pglRenderbufferStorage( GL_RENDERBUFFER, platformFormat, width, height );
GL_ERROR( "Failed to set render buffer storage" );
pglBindRenderbuffer( GL_RENDERBUFFER, 0 );
pglBindFramebuffer( GL_FRAMEBUFFER, m_FrameBufferObject );
GL_ERROR( "Failed to bind context frame buffer" );
int numDrawBuffers = 0;
GLenum drawBuffers[ VTG_MAX_RENDER_TARGETS ] = {GL_COLOR_ATTACHMENT0};
for( int currentTarget = 0; currentTarget < VTG_MAX_RENDER_TARGETS; ++currentTarget )
{
if( m_BoundRenderTargets[ currentTarget ] )
{
m_BoundRenderTargets[ currentTarget ]->Bind( currentTarget );
drawBuffers[ currentTarget ] = GL_COLOR_ATTACHMENT0 + currentTarget;
numDrawBuffers++;
}
}
pglDrawBuffers( numDrawBuffers, drawBuffers );
GL_ERROR( "Failed to specify draw buffers" );
if( m_BoundDepthTarget )
{
m_BoundDepthTarget->Bind( -1 );
}
//
// Now we need to make sure we are complete.
//
CheckForCompleteness();
The bind function for a render target looks like this:
if( m_Texture )
{
//glBindTexture( GL_TEXTURE_2D, m_Texture->m_Texture );
GL_ERROR( "Failed to bind the texture" );
pglFramebufferTexture2D( GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, m_Texture->m_Texture, 0 );
GL_ERROR( "Failed to set the render target as a texture" );
}
else
{
//
// Attach to the currently bound frame buffer object.
// TODO: Need some way to ensure there is an FBO bound...
//
pglFramebufferRenderbuffer( GL_FRAMEBUFFER,
attachment,
GL_RENDERBUFFER,
m_PlatformData.m_TargetHandle );
GL_ERROR( "Failed to attach the render buffer to the frame buffer" );
}
GL_ERROR( "Failed to bind the default back buffer" );
This code executes with no problem. Then when I update all render targets I do this code: