Jump to content
  • Advertisement
Sign in to follow this  
pondwater

Trouble getting back buffer with glCopyTexImage2D

This topic is 2072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!