Jump to content
  • Advertisement
Sign in to follow this  
Ashaira

Rendering to FBO and Post Processing Quad

This topic is 2257 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

so i thought i understood how FBO work but it seems not. ive been trying to render to a FBO and then using the texture to render a quad that i will use for post processing effects. I went through several tutorials and everything "seems" right but then again it always does.

This is how i implemented my FBO in the scene manager constructor


glGenRenderbuffers(1, &RBO); // Generate one render buffer and store the ID in fbo_depth
glBindRenderbuffer(GL_RENDERBUFFER, RBO); // Bind the fbo_depth render buffer
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 1024, 780); // Set the render buffer storage to be a depth component, with a width and height of the window
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, RBO); // Set the render buffer of this buffer to the depth buffer
glBindRenderbuffer(GL_RENDERBUFFER, 0); // Unbind the render buffer
glGenTextures(1, &FBO_Texture); // Generate one texture
glBindTexture(GL_TEXTURE_2D, FBO_Texture); // Bind the texture fbo_texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 780, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // Create a standard texture with the width and height of our window
// Setup the basic texture parameters
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// Unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);
glGenFramebuffers(1, &FBO); // Generate one frame buffer and store the ID in fbo
glBindFramebuffer(GL_FRAMEBUFFER, FBO); // Bind our frame buffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, FBO_Texture, 0); // Attach the texture fbo_texture to the color buffer in our frame buffer
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, RBO); // Attach the depth buffer fbo_depth to our frame buffer
/*switch (glCheckFramebufferStatus(GL_FRAMEBUFFER)){
case GL_FRAMEBUFFER_COMPLETE:
std::cout << "Complete." << std::endl;
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
std::cout << "Incomplete Attachment" << std::endl;
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
std::cout << "Incomplete Missing Attachment" << std::endl;
break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
std::cout << "Not all attached images have the same width and height." << std::endl;
break;
case GL_FRAMEBUFFER_UNSUPPORTED:
std::cout << "Framebuffer Unsupported" << std::endl;
break;
}*/
glBindFramebuffer(GL_FRAMEBUFFER, 0);


this should be ok as i have used glCheckFramebufferStatus and it didnt return any errors.

now for the rendering. This is the draw call in my scene manager (which works fine if i remove the FBO so the error cant be in a diff place)


glViewport ( 0, 0, esContext->width, esContext->height );
// Clear the color buffer
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
for(int j=0; j<Resources->ShaderNum ; j++)
{
userData->programObject = Resources->Programs[j];
glUseProgram ( Resources->Programs[j] );
for(int i=0; i<ObjectsLoaded; i++)
{
if(GameObjects->shader == j+1)
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

if(Resources->Shaders[j].Culling && Culling == false)
{
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
Culling = true;
}
if(Resources->Shaders[j].Blending && Blending == false)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Blending = true;
}
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
GameObjects->Draw(esContext);
if(Culling == true)
{
glDisable(GL_CULL_FACE);
Culling = false;
}
if(Blending == true)
{
glDisable(GL_BLEND);
Blending = false;
}
}
}
}
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
glBindFramebuffer(GL_FRAMEBUFFER, 0); // unbind
// Clear the color buffer
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
userData->programObject = Resources->PostProcessingPrograms[0];
Quad->Draw(esContext);


and this is what the post processing quad draw call looks like


GLfloat vVertices[] = { 512.0f, 390.0f, 0.0f,
-512.0f, 390.0f, 0.0f,
-512.0f, -390.0f, 0.0f,
512.0f, -390.0f, 0.0f};
GLfloat vTexCoord[] = { 1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f};
// Use the program object
glUseProgram ( userData->programObject );
glBindAttribLocation ( userData->programObject, 0, "vPosition" );
glBindAttribLocation ( userData->programObject, 1, "a_texCoord" );
glActiveTexture(GL_TEXTURE0 + 1);
Texture_Uniform_Loc = glGetUniformLocation ( userData->programObject, "texture1" );
glBindTexture(GL_TEXTURE_2D, Scene->FBO_Texture);
glUniform1i(Texture_Uniform_Loc, 1);
// Load the vertex data
glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices );
glEnableVertexAttribArray ( 0 );
glVertexAttribPointer ( 1, 3, GL_FLOAT, GL_FALSE, 0, vTexCoord );
glEnableVertexAttribArray ( 1 );
glDrawArrays ( GL_TRIANGLES, 0, 3 );
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );


there are no errors but everything on the screen is just black.

i tried making the quad smaller reversing the vertex order (Clockwise and CounterClockwise) i tried using the perspective matrix and rotated around to try and find the quad in case i misplaced it but nothing works the screen just stays black. im pretty sure im simply not rendering to the FBO right or im not applying the texture to the quad right but im not sure what i did wrong.

Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Hi,

I also implemented FBOs in my game yesterday (but it is not tested yet biggrin.png ).
My source looks like this:

//Init
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glGenRenderbuffers(1, &depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size, size);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
glGenTextures(1, &colorBuffer);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if(resizedDisplay) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
}else{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
//Render into
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, size, size);
//Render from
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, colorBuffer);



There are some differences between our codes:
1. My program first creates the Framebuffer and than the Renderbuffers.
2. Your texture is 1024x780 but non square textures are not supported on every hardware (it is a extension).
3. My program clears the Renderbuffers after it has bound them

And if nothing helps, here is a nice tutorial about FBOs: http://wiki.delphigl...amebufferobject
It is German but I think you can read the source code at least.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!