Trouble getting back buffer with glCopyTexImage2D

Started by
0 comments, last by Brother Bob 11 years, 3 months ago

I'm attempting to capture the back buffer on a texture, and then in an attempt to make sure its working, simply redraw the texture on the screen. I've copied the appropriate sections of code line for line from a tutorial example. The example compiles and works, mine doesn't. I can't for the life of me understand why.

Here the is initialization code that creates the empty texture:


glClearColor( 0.0f, 0.0f, 0.0f, 1.0f);  // set clear color to black

unsigned int *empty = new unsigned int[((glutGet(GLUT_WINDOW_WIDTH) * glutGet(GLUT_WINDOW_HEIGHT))* 4 * sizeof(unsigned int))];

glEnable( GL_TEXTURE_2D );
    
glGenTextures( 1, &texture_buffer );
glBindTexture( GL_TEXTURE_2D, texture_buffer);
glTexImage2D( GL_TEXTURE_2D, 0, 4, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT), 0, GL_RGBA, GL_UNSIGNED_BYTE, empty);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glBindTexture(GL_TEXTURE_2D, 0);

glBindTexture( GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
  
delete [] empty;

Here is the code that draws the scene, grabs it off the back buffer, then for testing, attempts to draw the texture to the screen:


glClear( GL_COLOR_BUFFER_BIT );   

state->draw();
glFlush();
    
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_buffer);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT), 0);
        
glClear( GL_COLOR_BUFFER_BIT );
   
glBegin( GL_QUADS );
    glTexCoord2f(0,1); glVertex2f(WORLD_LEFT, WORLD_TOP);     
    glTexCoord2f(1,1); glVertex2f(WORLD_RIGHT, WORLD_TOP);    
    glTexCoord2f(1,0); glVertex2f(WORLD_RIGHT, WORLD_BOTTOM); 
    glTexCoord2f(0,0); glVertex2f(WORLD_LEFT, WORLD_BOTTOM);  
glEnd();
    
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
    
glutSwapBuffers();

Anything seem out of place?

Advertisement

Some general comment...

  1. You don't need to allocate an empty buffer to create an empty texture. You can just pass a null pointer to glTexImage to just allocate the texture memory.
  2. You should use glCopyTexSubImage to update an existing texture. glCopyTexImage creates an entirely new texture from scratch which is not what you want to do in this case.
  3. You don't need to enable texturing just to bind and create your texture. The texture enable state is only used for applying texturing when drawing, but not for managing the texture object.

... and a possible cause of your error.

  1. You should clear the depth buffer also. What you draw to the frame buffer is cleared after copying to the texture and before drawing the quad, but the depth buffer is not cleared and what remains in there from rendering the texture-image bay prevent the quad from being drawn.

This topic is closed to new replies.

Advertisement