FBO rendering behavior

Started by
3 comments, last by Seabolt 10 years, 5 months ago

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:


Perception is when one imagination clashes with another
Advertisement

First, i'd check whether your framebuffer is proper ...


printf("%i\n",glCheckFramebufferStatus(GL_FRAMEBUFFER)==GL_FRAMEBUFFER_COMPLETE);

Also, I'd avoid using a renderbuffer for the depth.. just use a regular GL_DEPTH_COMPONENT texture....

Lastly, probably can try to avoid re-attaching the textures to the framebuffer each time. Just attach them once when you create it and they're still attached next time you bind the framebuffer.

I am check the framebuffer completeness, it's all good.
I'm using a renderbuffer for the depth because it's faster and I want to test renderbuffer functionality as well.

My problem is that I don't know what render targets are going to be bound to a framebuffer each frame, so I have to rebind the texture/renderbuffers each frame.

Perception is when one imagination clashes with another

So it seems that it will write to the render target texture as long as the texture is bound. If I bind another texture it seems to overwrite it. Should that happen? What if I want to bind a texture for something I want to draw into the render target, how would I do that?

Perception is when one imagination clashes with another

Jesus christ, I apologize guys, I'm all over the place right now.

I can get the model to draw if I don't use a depth stencil buffer.

Is it possible the texture and renderbuffer are conflicting somehow? The renderbuffer is being bound to the GL_DEPTH_STENCIL_ATTACHMENT point of the FBO so it shouldn't be overwriting anything...

Perception is when one imagination clashes with another

This topic is closed to new replies.

Advertisement