2
\$\begingroup\$

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?
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can't have the texture bound if you want to use some other texture while drawing to your framebuffer. \$\endgroup\$ Jun 9, 2012 at 8:04

2 Answers 2

5
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ You are right. I got a bit confused. Fixed it to make more sense. \$\endgroup\$
    – knight666
    Jun 9, 2012 at 12:16
  • 4
    \$\begingroup\$ "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. \$\endgroup\$ Jun 9, 2012 at 12:29
  • \$\begingroup\$ 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? \$\endgroup\$
    – Publius
    Jun 10, 2012 at 6:24
5
\$\begingroup\$

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .