Hello,
I’m having some trouble rendering to a texture. I’m not quite sure what’s going on: http://i43.tinypic.com/1rq9nm.png
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.
FBO setup:
// generate a framebuffer object
glGenFramebuffers(1, &fbo_);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
// generate the texture for rendering
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture_);
glBindTexture(GL_TEXTURE_2D, texture_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// attach the texture to the FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_, 0);
// generate a renderbuffer for the depth info
glGenRenderbuffers(1, &rbo_);
glBindRenderbuffer(GL_RENDERBUFFER, rbo_);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 512, 512);
// attach the renderbuffer
//glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo_);
// bind the fragment shader output
GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, drawBuffers);
// switch to the default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
main render function:
// bind to the dummy texture
program_.setUniform("texture_", 1);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_);
setupMatrices();
program_.setUniform("texRenderPass_", true);
model_.render();
program_.setUniform("texRenderPass_", false);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
program_.setUniform("texture_", 0);
setupMatrices();
glBindVertexArray(cube_);
glDrawArrays(GL_TRIANGLES, 0, CUBE_VERTICES);
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:
vec3 ambDiff;
vec3 spec;
calculatePhongShading(ambDiff, spec);
if(texRenderPass_) {
fragColor_ = vec4(ambDiff + spec, 1.0);
}
else {
vec4 texColor = texture(texture_, texCoord_);
fragColor_ = vec4(ambDiff, 1.0) * texColor + vec4(spec, 1.0);
}
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.
Thanks in advance!