Weird FBO behaviour

The texture2D attachment gets corrupted image data it seems. Also adding a renderbuffer causes the texture2D attachment to stop working all together :/

At first I thought the problem is that I'm using the texture unit I'm rendering to in the shader (well, not really, but it is bound there, see fragment shader below) so I decided to load a dummy texture into GL_TEXTURE1 and have the shader sampler set to that when rendering the sphere and then when I'm rendering the cube, swap the shader's sampler back. However, that didn't change anything at all. glCheckFramebufferStatus returns all OK as well.

I don't think the problem is with the shaders. In the vertex shader I just find needed data for Phong's lighting model and pass the texture coordinates to the fragment shader, which does the following:

I'm seriously confused with the results I'm getting... To me, it doesn't make any sense that once I attach a renderbuffer, the texturing breaks completely (why is the texture corrupted to begin with?)... Hope you guys can shed some light on this.

Now I get the same result as on the left side of the posted picture: nothing but a box.

Originally Posted by GClements

Does setupMatrices set the viewport to the dimensions of the framebuffer? Also, do you call glClear()?

Viewport is the same as the window size: as soon as I started seeing problems I made sure to make the window the same size as the texture I want to render to. Originally I had different matrices for both renders but for ease of debugging I'm using the same for both.

Now I get the same result as on the left side of the posted picture: nothing but a box.

You'll need to bind the texture when you want to render from it, into the window. But it shouldn't be bound to a texture unit while you're rendering into an FBO to which it's attached. The actual rules are slightly more relaxed, but it's best to play it safe.

Originally Posted by dr4cula

Code :

glClear(GL_COLOR_BUFFER_BIT);

Depth buffer? Your code shows one being attached to the FBO, although I don't know if it's enabled (there's not much point in attaching one if it isn't). If it's present and enabled, it needs to be cleared before use.

Herp-a-derp. I swear I tried glClear() with GL_DEPTH_BUFFER_BIT... Anyways, it seems that was the problem indeed. I don't really need the renderbuffer so I tried removing it but the problem then came back again. I read that glEnable/glDisable(GL_DEPTH_TEST) around the rendering sequence for the texture would fix it but that didn't seem to work for me. Any ideas?

I tried adding another FBO with a different texture attachment but as soon as I call glFramebufferTexture2D() for the second buffer, everything breaks down. I'm not even binding to the created FBO, only using the previous one and the default one for rendering. But like I said, as soon as the texture binding is called for the second FBO, the first one breaks. Any ideas?

EDIT: Nevermind! Since it was confusing to see what was going on when I had the FBO setups one after another, I created a class that encapsulated all of the details and in the process managed to fix the problem.