PDA

View Full Version : Frame Buffer and render to texture problem.



mickey123
08-27-2009, 07:23 PM
GLuint fbo,texture;
BOOL fbGenerated = false;
LLRenderTarget target;
void render_offscreen_fbo(){
if(!fbGenerated){
fbGenerated = TRUE;
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, 256, 256, 0, GL_RGB, GL_UNSIGNED_INT, NULL);
//glGenerateMipmapEXT(GL_TEXTURE_2D);

glGenFramebuffersEXT(1, &fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);

glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture, 0);

if(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) !=GL_FRAMEBUFFER_COMPLETE_EXT)
{
cout<<"The fbo is not complete"<<endl;
cout<<"Error: "<<glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT)<<endl;
}
}
//bind the FBO, and the associated texture.





glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glClearColor(0.0, 0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);

glPushAttrib(GL_VIEWPORT_BIT);
glViewport(0, 0, 256, 256);

glColor4f(1.0, 1.0, 0.0, 0.0);
glBegin(GL_QUADS);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glEnd();

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glClearColor(0.0, 1.0, 0.0, 0.0);
glEnable(GL_SCISSOR_TEST);
glScissor(0,0,256,256);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);



glColor4f(1.0, 1.0, 1.0, 0.0);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex2f(1.0, 1.0);
glTexCoord2f(0.0, 1.0); glVertex2f(1.0, -1.0);
glTexCoord2f(1.0, 1.0); glVertex2f(-1.0, -1.0);
glTexCoord2f(1.0, 0.0); glVertex2f(-1.0, 1.0);
glEnd();
glPopAttrib();
glDisable(GL_SCISSOR_TEST);

}


I am expecting a texture to be rendered with yellow quads.

But what I am getting is a green screen?

Any help?

scratt
08-27-2009, 07:59 PM
You don't appear to have done anything to setup projection or modelview matrices when drawing to either the FBO or the main framebuffer.. Perhaps you do those elsewhere, but from your code I have no idea.

Also if you want to draw the contents of an FBO texture to the screen you need to bind the texture associated with the FBO. You neglect to do that at the bottom of the code as far as I can see.

I presume you've been looking at something like this..
http://www.gamedev.net/reference/articles/article2331.asp

Try this : http://www.songho.ca/opengl/gl_fbo.html
It has some code to download which may make it easier.

mickey123
08-27-2009, 08:06 PM
You don't appear to have done anything to setup projection or modelview matrices when drawing to either the FBO or the main framebuffer.. Perhaps you do those elsewhere, but from your code I have no idea.


This is just basic gl drawing, so I didnt feel the necessity for them.



Also if you want to draw the contents of an FBO texture to the screen you need to bind the texture associated with the FBO. You neglect to do that at the bottom of the code as far as I can see.


Yeah I realized it late and added it, but couldnt find any effect, except for a small bluedot in the green region.

scratt
08-27-2009, 09:15 PM
This is just basic gl drawing, so I didnt feel the necessity for them.

I am confused by your reply..

You mean you don't have them anywhere in your code? If so, then be aware they *are* necessary for all OpenGL drawing. You may get lucky with their default settings, but their default settings are not Ortho, so you are going to get strange results.

If they are elsewhere in your code, and you are asking for help with a bug / problem it is best to post a complete example, even if it is a slightly cut down version of your problem. Otherwise we'll just be guessing for our replies...


Yeah I realized it late and added it, but couldnt find any effect, except for a small bluedot in the green region.

This tends to suggest something wrong with your matrices... The "small blue dot" is your *entire* FBO texture by the looks of the clear colour you are using! :)

mickey123
08-27-2009, 11:35 PM
@scratt

True regarding blue dot, as a noob have to learn the usage of projections decently.