Sign in to follow this  
carmellose

OpenGL FBO .... i hate you!!!

Recommended Posts

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("[i] 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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
If you want to render to the texture, call glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, myFBO), not glEnable(GL_TEXTURE_2D)

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
hello again

i might have found one possible error source.

when i use :


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
SX, SY, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);





does SX and SY have to be the size of the Viewport, or do they have to be the size of the frustum ? (e.g. the size of glOrtho() window in my case)

thanks!

Share this post


Link to post
Share on other sites
It can be whatever size you want as long as you don't exceed the implementation limits. glGetIntegerv(GL_TEXTURE_SIZE, ...) or maybe it was glGetIntegerv(GL_TEXTURE_WIDTH, ...)

Share this post


Link to post
Share on other sites
I knew it is strange. However, I found out recently that the texture filter setting can cause FBO fail. Try to change the texture filter to GL_NEAREST instead of GL_LINEAR, it might help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627749
    • Total Posts
      2978913
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now