Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In OpenGL, if I'm rendering to a texture using a framebuffer, do I have to have that texture bound while I'm rendering to it? In code:

glBindTexture(...);
glTexImage2D(...);

glBindFramebuffer(...);
glFrameBufferTexture2D(...);

//START DRAWING HERE: does the texture I'm rendering to need to be bound here?
share|improve this question
2  
You can't have the texture bound if you want to use some other texture while drawing to your framebuffer. –  Nathan Reed Jun 9 '12 at 8:04

2 Answers 2

up vote 5 down vote accepted

No.

When you bind textures to a GL_FRAMEBUFFER object (FBO), those textures get associated with it. Later, when you bind the FBO, the associated textures get bound as well.

An example:

// a framebuffer with a color texture and a depth texture

m_TexWidth = 1024;
m_TexHeight = 1024;

m_TextureTotal = 2;
m_Texture = new GLuint[m_TextureTotal];
glGenTextures(m_TextureTotal, m_Texture);

glGenFramebuffers(1, &m_Target);
glBindFramebuffer(GL_FRAMEBUFFER, m_Target);

// color

glBindTexture(GL_TEXTURE_2D, m_Texture[0]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_TexWidth, m_TexHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_Texture[0], 0);

// depth

glBindTexture(GL_TEXTURE_2D, m_Texture[1]);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_TexWidth, m_TexHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_Texture[1], 0);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
{
    LOG_FATAL("Could not validate framebuffer");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

Now, if you want to use that framebuffer so you can render to it:

glBindFramebuffer(GL_FRAMEBUFFER, m_Target);

And back to the backbuffer:

glBindFramebuffer(GL_FRAMEBUFFER, 0);

EDIT: Earlier I claimed that textures are created per FBO. That is wrong, all textures are always created global in OpenGL. The difference is that these textures are now associated with the FBO.

share|improve this answer
    
You are right. I got a bit confused. Fixed it to make more sense. –  knight666 Jun 9 '12 at 12:16
4  
"the associated textures get bound as well." No, they don't. "Bind" always refers to associating an object with the OpenGL context. AKA, glBindTexture. Binding an FBO does not bind the textures attached to it. The textures are still attached, and thus rendering to the FBO means rendering to those textures. –  Nicol Bolas Jun 9 '12 at 12:29
    
Thanks, these answers and comments were exactly what I was looking for. It is particularly helpful that binding the FBO does not bind the textures. Does that mean if I have a rendered-to texture that I bound to GL_TEXTURE0 I can still use GL_TEXTURE0 for drawing while the framebuffer is bound? –  Avi Jun 10 '12 at 6:24

No, the texture doesn't need to be bound.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.