Bob
Bob

Reputation: 593

Trouble Modifying Contents of Frame Buffer

I am trying to rotate the contents of the frame buffer by putting it into a texture then putting it back into the buffer. I get a white rotated rectangle. I suspect I am overlooking something obvious. Here is the code I am using:

  glReadBuffer(GL_AUX1);
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, textures[1]);
  glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, rect.width(),
        rect.height(), 0);
  glDrawBuffer(GL_AUX1);
  glPushMatrix();
  glRotatef(head - new_head, 0, 0, -1);
  glBegin(GL_POLYGON);
  glTexCoord2f(0, 0);
  glVertex2f(-1, -1);
  glTexCoord2f(0, 1);
  glVertex2f(-1, 1);
  glTexCoord2f(1, 1);
  glVertex2f(1, 1);
  glTexCoord2f(1, 0);
  glVertex2f(1, -1);
  glEnd();
  glPopMatrix();
  glDisable(GL_TEXTURE_2D);

I have solved the problem. Adding these lines to make it work:

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

Upvotes: 0

Views: 298

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56357

You can't create a texture with glCopyTexImage2D, you only copy data to the texture, but the texture is not complete at this stage so you get a black/white texture (an indication of incomplete state).

Solution: Create a texture with:

glGenTextures(....)
glBindTexture(...)
glTexParameter(...) //Set MIN/MAG filter
glTexImage2D(....., NULL)

glTexImage2D defines the texture image, as you want to render to this texture (or copy to it), you pass NULL for the last parameteres so the texture backing store is created, but it contents are left undefined. Another suggestion is to use Framebuffer Objects (FBOs) to draw directly into a texture (without a copy)

Upvotes: 1

Related Questions