Jump to content
  • Advertisement
Sign in to follow this  
carmellose

OpenGL Can't manage to do a RTT with an FBO

This topic is 3658 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

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

Share this post


Link to post
Share on other sites
Advertisement
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 :/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!