FBO .... i hate you!!!

Started by
11 comments, last by ma_hty 15 years, 5 months ago
hello, i'm trying to do a very simple render to texture, but it does not work. I'm just trying to render a square to a texture and display it onto the screen. There's no depth buffer involved, just an FBO .... The background is supposed to be blue and the square somehow has RGB colors. However, i only see the blue background and my square is black!!! :( seems like i'm missing something, but what ? ... here's my code where i RTT :

void RenderToTexture (void) {

  //bind texture
  glGenTextures(1, &TEX_SCENE_SOURCE_IMAGE);
  glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE);

  printf(" texId(TEX_SCENE_SOURCE_IMAGE)=%d\n",TEX_SCENE_SOURCE_IMAGE );

    //initialize the texture to render to 
  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, 
      sourceW, sourceH, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

 // glTexEnvi(GL_TEXTURE_ENV,TEX_SCENE_SOURCE_IMAGE,GL_MODULATE);

  // create FBO 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);

  CHECK_FRAMEBUFFER_STATUS();

  // Start rendering to the FBO
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO); 

  glPushAttrib(GL_VIEWPORT_BIT);
  glViewport(0,0,sourceW, sourceH);

  glBegin(GL_QUADS);
  glColor3f      (0.0f, 1.0, 0.0f);
  glVertex3f	 (+.8f, +.8 , -2);
  glColor3f      (1.0f, 0.0, 0.0f);
  glVertex3f	 (-.8f, +.8,  -2);
  glColor3f      (0.0f, 0.0, 1.0f);
  glVertex3f	 (-.8f, -.8,  -2);
  glColor3f      (1.0f, 0.0, 1.0f);
  glVertex3f	 (+.8f, -.8,  -2);
  glEnd();

  glPopAttrib();

  //Bind 0, which means strop rendering to the FBO
  glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

}
/*
 * main routine to display openGL scene
 * */
void DrawGLScene(void) {
 
  glClear(GL_COLOR_BUFFER_BIT);

  if (i++ == 1) { //static int i = 1 at startup
    glEnable(GL_TEXTURE_2D);	
    RenderToTexture(); 
  }

  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE);
  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();


  //Dettach, textureID = 0
  /* glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0, 0);
     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0);
  //Delete resources
  //glDeleteTextures(1, &TEX_SCENE_SOURCE_IMAGE);
  glDeleteFramebuffersEXT(1, &myFBO);
  glDeleteFramebuffersEXT(1, &myDBO);
  */

  glutSwapBuffers();
  return;	
}

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

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
  glClearDepth(1.0f);	
  glClearColor(0, 0, 1, 1);  /* Blue background */

  return true;
}



i can't seem to figure this out ... square's black :( can you help me please :p thanks in advance
Advertisement
It looks like you have GL_TEXTURE_2D enabled when you call RenderToTexture. The drawn polygons get textured with the your texture which has undefined content (NULL given to glTexImage2D, usually black). Additionally, the texturing uses a texture that is attached to the current frame buffer. This has undefined results too.

So changing glEnable(GL_TEXTURE_2D) to glDisable(GL_TEXTURE_2D) in the beginning of DrawGLScene should do the trick. I assume that the rendering works (matrices and render state set properly) otherwise.

-Riku
hello

thank you for your answer.

Im confused :/ why do i need to disable GL_TEXTURE_2D if i want to render to a texture ? It doesn't seems logical to me ...

However, i have tryed to disable this parameter in the beginning of DrawGLScene() and now i have a square rendered on the screen, but its color is pink!!! how can it be so since i have never told this square to be pink ? where could that come from ?

thank you in advance,

cheers
If you want to render to the texture, call glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO), not glEnable(GL_TEXTURE_2D)
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 V-man
If you want to render to the texture, call glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO), not glEnable(GL_TEXTURE_2D)


hi,
actually, this is what i've tried to do : disable GL_TEXTURE_2D and then render to the FBO. The square renders to texture, but i've noticed that it only has the last color mentioned in GlColor3f(). For example :

 glBegin(GL_QUADS);  glColor3f      (0.0f, 1.0f, 0.0f);  glVertex3f	 (+.8f, +.8 , -2);  glColor3f      (1.0f, 0.0f, 0.0f);  glVertex3f	 (-.8f, +.8,  -2);  glColor3f      (0.0f, 0.0f, 1.0f);  glVertex3f	 (-.8f, -.8,  -2);  glColor3f      (1.0f, 0.0f, 1.0f);  glVertex3f	 (+.8f, -.8,  -2);  glEnd();


appears pink, because the last color is pink. Other colors seem not to be taken into account. Why is this so ? what did i missed to do ??

Also, when i've rendered the square to the texture, i guess i should (re)enable GL_TEXTURE_2D, right ?

when i do this, the square appears totally black!!!

for example :

    glEnable(GL_TEXTURE_2D); //enable texture after RTT  glBindTexture(GL_TEXTURE_2D, TEX_SCENE_SOURCE_IMAGE); //the texture RTT'ed  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();


blue background, but black square ... kesako ?

thank you in advance ...
Could be a bug. Try running on some other system and compare.
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 V-man
Could be a bug. Try running on some other system and compare.


ok :( would that be related the the card drivers ?

im runing on a linux gentoo, nvidia geforce 9200m gs, latest drivers (177.80)
When you have GL_TEXTURE_2D disabled, the textured quad in DrawGLScene() are drew without texture mapping. Instead, only the vertex colors have effect. And, the last vertex color you passed to OpenGL is pink. Thats why you have a pink quad if you draw the textured quad in DrawGLScene() with GL_TEXTURE_2D disabled.

When you do the correct thing (i.e. enable GL_TEXTURE_2D before drawing the the textured quad in DrawGLScene()), you have a black quad. So, you are probably still having a black rendered texture. Very likely, the display card you used are not supporting FBO.

If you are using glew library, you can check the availability of FBO entry functions by running the program glewinfo.exe. glewinfo.exe can be found in the glew library. When you executed it, it will generate a text file glewinfo.txt in the working directory which listing the availability all entry functions (including those FBO related).
Quote:Original post by carmellose
The square renders to texture, but i've noticed that it only has the last color mentioned in GlColor3f(). For example :

*** Source Snippet Removed ***

appears pink, because the last color is pink. Other colors seem not to be taken into account. Why is this so ? what did i missed to do ??

I know it doesn't solve your texture problem, but glShadeModel(GL_SMOOTH) is probably what you missed. However, this is the default setting, so you'd have had to have called glShadeModel(GL_FLAT) somewhere else to cause this problem.
Quote:Original post by ma_hty
When you have GL_TEXTURE_2D disabled, the textured quad in DrawGLScene() are drew without texture mapping. Instead, only the vertex colors have effect. And, the last vertex color you passed to OpenGL is pink. Thats why you have a pink quad if you draw the textured quad in DrawGLScene() with GL_TEXTURE_2D disabled.

When you do the correct thing (i.e. enable GL_TEXTURE_2D before drawing the the textured quad in DrawGLScene()), you have a black quad. So, you are probably still having a black rendered texture. Very likely, the display card you used are not supporting FBO.

If you are using glew library, you can check the availability of FBO entry functions by running the program glewinfo.exe. glewinfo.exe can be found in the glew library. When you executed it, it will generate a text file glewinfo.txt in the working directory which listing the availability all entry functions (including those FBO related).


thank you for the explanation.

Actually, i do use glew and do a test with gluCheckExtension() to see if it supports FBO. Since it passes the test i believe my video card can handle FBO....it's rather new so it would surprise me it doesn't.

This topic is closed to new replies.

Advertisement