PDA

View Full Version : FBO problem



apapaxionga
11-15-2011, 08:02 AM
When i using FBO to draw a teapot to a texture and bind the texture to a QUADS. BUt the texture seems have nothing,it is white. Why?


const GLenum buffers[] = {
GL_COLOR_ATTACHMENT0_EXT,
GL_COLOR_ATTACHMENT1_EXT,
GL_COLOR_ATTACHMENT2_EXT,
GL_COLOR_ATTACHMENT3_EXT
};
void INIT_FBO()
{
glGenFramebuffersEXT(1, &fb);
printf("FBO object is - glGenFramebuffersEXT => %d\n",fb);
}
void BIND_AND_SETUP_FBO()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GPU_TEXTURE_FORMAT, tex2, 0 );
glDrawBuffers(1, buffers);
}
void DONT_USE_FBO()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void idle()
{
glutPostRedisplay();
}
void initFx()
{
glClearColor (.1, .1,.1, 1.0);
glViewport(0, 0, (GLsizei) imageWidth, (GLsizei) imageHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); gluPerspective(45,(double)imageWidth/(double)imageHeight,0.1,100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();
}
void InitTextures()
{

glGenTextures(1, &tex2);

glBindTexture(GL_TEXTURE_2D, tex2);
// set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,imageWidth,ima geHeight,0,GL_RGB,GL_UNSIGNED_BYTE,0);
}
void Draw()
{

BIND_AND_SETUP_FBO();
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0,0,-5);
glutSolidTeapot(1);
glPopMatrix();
DONT_USE_FBO();

glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertex3f(0,0,-20);
glTexCoord2i(1,0);
glVertex3f(5,0,-20);
glTexCoord2i(1,1);
glVertex3f(5,5,-20);
glTexCoord2i(0,1);
glVertex3f(0,5,-20);

glEnd();
glutSwapBuffers();
}
void init(int argc, char *argv[]) {

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA );
glutInitWindowSize(imageWidth, imageHeight);
glutCreateWindow("Morphology: Opening");
glutDisplayFunc(Draw);
glutIdleFunc(idle);

glewInit();

InitTextures();
initFx();
INIT_FBO();
}
int main( int ArgN , char* ArgV[] ) {
init(ArgN,ArgV);
glutMainLoop();
}