Can't manage to do a RTT with an FBO

Started by
4 comments, last by carmellose 15 years, 6 months ago
hi guys, the tite says it all : i cant manage to render to texture with an FBO. Here's the function in my code, i dont understand why it doesn't work. I only get the texture render to the whole quad, but not the triangle i draw in the RTT procedure ...

void DrawObject(void) {

  glClearColor(0.1, 0.3, 0.6, 0.5f);  /* Blue background */
  glBindTexture(GL_TEXTURE_2D, TEX_OBJ_SOURCE_IMAGE );

  /* draw a simple triangle and bind texture coordinates */
  glBegin(GL_TRIANGLES);
  glTexCoord3f(0, 0, -1);
  glVertex3f(-0.8, 0.8, -1);

  glTexCoord3f(1, 0, -1);
  glVertex3f(0.8, 0.8, -1);

  glTexCoord3f(0.5, 1, -1);
  glVertex3f(0.0, -0.8, -1);
  glEnd();

}

void RenderToTexture (void) {

  // create objects
  glGenFramebuffersEXT(1, &myFBO);// frame buffer
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO);

  //initialize the texture to render to 
  glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE );
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 
      SX, SY, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

  // attach the texture to framebuffercolor buffer
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
      GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE, 0);

  // (draw something here, rendering to texture)
  glPushAttrib(GL_VIEWPORT_BIT);
  glViewport(0,0,sourceW,sourceH);
  // Render as normal here
  // output goes to the FBO and it\u2019s attached buffers
  DrawObject();
  glPopAttrib();

  glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE );
  
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  glDeleteFramebuffersEXT(1, &myFBO);
}
/*
 * main routine to display openGL scene
 * */
void DrawGLScene(void) {

  CGpass pass;

  if ( t++ == 0) {
    printf("[+] Render To Texture ...\n");
    RenderToTexture(); 

  }

  pass = cgGetFirstPass(cgTechnique);
  while(pass) {
    cgSetPassState(pass);

    /* Draw the scene as a texture */
    glBegin(GL_QUADS);
    glTexCoord3f (0, 0, -1);
    glVertex3f	 (-1,1 , -1);

    glTexCoord3f (1, 0, -1);
    glVertex3f	 (1, 1, -1);

    glTexCoord3f (1, 1, -1);
    glVertex3f	 (1, -1, -1);

    glTexCoord3f (0, 1, -1);
    glVertex3f	 (-1, -1, -1);
    glEnd();

    cgResetPassState(pass);
    pass = cgGetNextPass(pass);
  }

  glutSwapBuffers();
  return;	
}

/*
 * Init the GL runtime
 * */
bool GLInit(void) {

  glClearColor(0.1, 0.3, 0.6, 0.5f);  /* Blue background */
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* Tightly packed texture data. */
  glClearDepth(1.0f);	
  glEnable(GL_TEXTURE_2D);	

  glBindTexture(GL_TEXTURE_2D, TEX_OBJ_SOURCE_IMAGE);

  //TO ADAPT IN OPTIM
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);// Linear Filtering
  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);// Linear Filtering
  glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

  glTexImage2D(
      GL_TEXTURE_2D,
      0,
      GL_RGBA8,
      sourceW,
      sourceH,
      0,
      GL_RGBA,
      GL_UNSIGNED_BYTE, 
      data);

  return true;
}


what's wrong ? i'm lost ...how do you get this FBO to work ? thanks in advance. cheers
Advertisement
http://www.opengl.org/wiki/index.php/Common_Mistakes#Creating_a_Texture

http://www.opengl.org/wiki/index.php/GL_EXT_framebuffer_object
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
hello,

thank you! that helped a lot.

Still, i can't manage to do it :(

I see in your example links that there's a depth buffer involved, is this mandatory ? in my example i guess i don't need it (at least at that point)

Also, does glReadPixels() has to be used to do an RTT/FBO ? Where should i use it ?

Here's my code where i try to rtt, it still does not work since i see a black screen :((((( another problem is that i want to do a render-to-texture of a texture :/

/* DrawObject(void) is called by RenderToTexture()*/void DrawObject(void) {  glClearColor(0.1, 0.3, 0.6, 0.5f);  /* Blue background */  glClear(GL_COLOR_BUFFER_BIT);    glViewport(0,0,sourceW,sourceH);   glBindTexture(GL_TEXTURE_2D, TEX_OBJ_SOURCE_IMAGE );  glLoadIdentity();  /* draw a simple triangle and bind texture coordinates */  glBegin(GL_TRIANGLES);  glColor4f(1.0, 1.0, 0.0,.5f);    //glTexCoord3f(0, 0, -1);  glVertex3f(-0.8, 0.8, -1);  //glTexCoord3f(1, 0, -1);  glVertex3f(0.8, 0.8, -1);  //glTexCoord3f(0.5, 1, -1);  glVertex3f(0.0, -0.8, -1);  glEnd();  glViewport(0,0,SX,SY); }/*Render to texture*/void RenderToTexture (void) {   //initialize the texture to render to   glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE );  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,       SX, SY, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);  glGenerateMipmapEXT(GL_TEXTURE_2D);     // create objects  glGenFramebuffersEXT(1, &myFBO);// frame buffer  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO);  // attach the texture to framebuffercolor buffer  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,      GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE, 0);  // (draw something here, rendering to texture)  DrawObject();    //Bind 0, which means render to back buffer  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);    glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE );}/* * main routine to display openGL scene * */void DrawGLScene(void) {  CGpass pass;  pass = cgGetFirstPass(cgTechnique);  while(pass) {    cgSetPassState(pass);      RenderToTexture();        cgResetPassState(pass);    pass = cgGetNextPass(pass);  }  glutSwapBuffers();  return;	}/* * Init the GL runtime * */bool GLInit(void) {  glClearColor(0.1, 0.3, 0.6, 0.5f);  /* Blue background */  glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* Tightly packed texture data. */  glClearDepth(1.0f);	  glEnable(GL_TEXTURE_2D);	  glBindTexture(GL_TEXTURE_2D, TEX_OBJ_SOURCE_IMAGE);  //TO ADAPT IN OPTIM  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);// Linear Filtering  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);// Linear Filtering  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);  glTexImage2D(      GL_TEXTURE_2D,      0,      GL_RGBA8,      sourceW,      sourceH,      0,      GL_RGBA,      GL_UNSIGNED_BYTE,       data);  glGenerateMipmapEXT(GL_TEXTURE_2D);  return true;}


thanks by advance .... i'm still lost :/
Like the example shows, you should check if everything went ok with glCheckFramebufferStatusEXT. You might want to insert some glGetError() as well.

This
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

and this
glGenerateMipmapEXT(GL_TEXTURE_2D);

don't go together. If you need mipmaps, then it need to be
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);

else no need for glGenerateMipmapEXT()

Quote:Also, does glReadPixels() has to be used to do an RTT/FBO ? Where should i use it ?

You have a RTT there, you are rendering to a texture.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by carmellose
I see in your example links that there's a depth buffer involved, is this mandatory ? in my example i guess i don't need it (at least at that point)

Yes, OpenGL needs the depth buffer for depth test, even if you do not need it. For correct depth test, you also have to attach a depth buffer (e.g: a renderbuffer object) into the FBO.

I notice several issues in your code. Please check the following list:

DrawObject()
1. No projection setting. Add a valid projection, for example,
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, (float)sourceW/sourceH, 0.1f, 5.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

2. The winding of the triangle is clockwise. Change it to counter clock wise;
glVertex3f(-0.8f, 0.8f, -1.0f);
glVertex3f(0, -0.8f, -1.0f);
glVertex3f(0.8f, 0.8f, -1.0f);

3. Use the texture object ID that is created by OpenGL instead of using a constant, TEX_OBJ_SOURCE_IMAGE.

RenderToTexture()
1. It seems to me you call this function repeatedly. If so, create a FBO outside of this function once. (you create a FBO in this function, but you did not delete it.)

2. You must check the FBO completeness after attaching images as V-man mentioned;
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
if(status == GL_FRAMEBUFFER_COMPLETE_EXT)
{
// success
}
else
{
// failed
}

Also, there are some redundant OpenGL functions. Remove unnecessary calls.
hi guys,

thank you so much for your input !!

it now works :)))

I will post my code as soon as it's written clean :o

The main error was that i called the RenderToTexture() method outside the shader pass loop in DrawGLScene(). I've put RenderToTexture() inside the while(pass) and now ...tada!!

thanks again for all your advice!

more to come, soon ;)

cheers

This topic is closed to new replies.

Advertisement