user3696282
user3696282

Reputation: 35

Render to Texture in opengl

I get ERROR : GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT with the following code. Any ideas on what I might be missing here:

glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 640, 480, 0, GL_RGBA4, GL_UNSIGNED_BYTE, NULL);

glGenFramebuffers(1, &renderTexture);
glBindFramebuffer(GL_FRAMEBUFFER, renderTexture);
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);


GLint val = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if(val != GL_FRAMEBUFFER_COMPLETE)
{
    std::cout <<"\n Error in framebuffer  : 2";
}

Here's rest of the code for rendering and blit to default FB.But doesnt render anything any ideas? Expecting a green clear on the default FB

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f,1.0f,0.0f,0.0f);


glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, renderTexture);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glDrawBuffer(GL_COLOR_ATTACHMENT0); 
val = glGetError();
if( val == GL_NO_ERROR)
    std::cout<<"\n Bind  Fine";

val = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(val != GL_FRAMEBUFFER_COMPLETE)
{
    std::cout <<"\n Error in framebuffer  : 3";
}   
glBlitFramebuffer(0,0,128,128,0,0,128,128, GL_COLOR_BUFFER_BIT, GL_NEAREST);

Upvotes: 0

Views: 770

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54642

You need to bind the FBO before making calls that operate on it. OpenGL calls that modify objects generally operate on a currently bound object. It's the same scheme you follow with the texture at the start of the posted code, where you generate a texture id with glGenTextures(), bind it with glBindTexture(), and then make calls that modify the texture, like glTexParameteri() and glTexImage2D().

In the FBO case, glFrameBufferTexture2D() and glDrawBuffer() operate on the currently bound FBO. So the glBindFramebuffer() call needs to be before those calls:

glGenFramebuffers(...);
glBindFramebuffer(...);
glFramebufferTexture2D(...);
glDrawBuffer(...);

It's always a good idea to call glGetError() if you see any unexpected behavior with your code. You would have seen a GL_INVALID_OPERATION error from the glFramebufferTexture2D() call because no FBO was bound.

A couple more observations on the code:

  • The glDrawBuffer() call is redundant in this case. GL_COLOR_ATTACHMENT0 is the default draw buffer for FBOs.
  • While you can use glFramebufferTexture() here, glFramebufferTexture2D() will work just as well. glFramebufferTexture() requires a newer OpenGL version (3.2), and is only beneficial for layered attachments (texture arrays, etc.).

Upvotes: 6

Related Questions