Sign in to follow this  
carmellose

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

Recommended Posts

carmellose    122
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
riku    194
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
carmellose    122
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
V-man    813
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
carmellose    122
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
carmellose    122
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
ma_hty    100
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
Graz    122
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
carmellose    122
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
carmellose    122
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
V-man    813
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
ma_hty    100
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  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now