Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: How to copy backbuffer contents into a texture?

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2010
    Posts
    13

    How to copy backbuffer contents into a texture?

    Hi,

    I am trying to copy the contents of the backbuffer into a texture to reuse it later. However, all my attempts failed, because I have not enough know-how of how to capture textures from the backbuffer.

    Could some please fill-in/correct the code below? I need help with steps 4 and 6.

    Code :
    #define CX 144
    #define CY 144
     
    GLuint img;
     
    //	Step 1: create window
    glutInitWindowSize (CX, CY);
    glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow ("glut");
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
     
    //	Step 2: clear the scene
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    //	Step 3: draw a red square
    glColor3f(1, 0, 0);
    glMatrixMode (GL_MODELVIEW);
    glRectf (-0.2f, -0.2f, 0.2f, 0.2f);
     
    //	Step 4: save backbuffer contents into texture (save the whole visible area)
    glGenTextures(1,&img);
    glBindTexture(GL_TEXTURE_2D,img);
    // And now? Use glCopyTexImage2D? Use glCopyTexSubImage2D? How does it work?
     
    //	Step 5: clear the scene again
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    //	Step 6: draw a quad with the size of the whole screen using the captured texture
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,img);
    glBegin(GL_QUADS);
    	glTexCoord2f(?,?); glVertex2i(-1,1);
    	glTexCoord2f(?,?); glVertex2i(1,1);
    	glTexCoord2f(?,?); glVertex2i(1,-1);				
    	glTexCoord2f(?,?); glVertex2i(-1,-1);
    glEnd();
    glDisable(GL_TEXTURE_2D);

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,655

    Re: How to copy backbuffer contents into a texture?

    Step 4: it works like that :
    http://www.opengl.org/sdk/docs/man/x...TexImage2D.xml

    Step 6: depends on your projection, but with default projection x and y will be [-1;1] accross the screen

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2010
    Posts
    13

    Re: How to copy backbuffer contents into a texture?

    Quote Originally Posted by ZbuffeR
    Sorry, I should have informed you that I have passed the RTFM phase already ;-) Seriously, I have read the manual, but I don't understand it. I am a complete beginner.
    I have googled for examples and I have tried some code I found on the internet, but I just can't seem to make it work. I miss some fundamental understanding of OpenGL here.

    Therefore I'd really appreciate if someone could post some example code for step 4 and 6 to help better understand how it works.

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    965

    Re: How to copy backbuffer contents into a texture?

    Tell us about the texture you're copying it into. What's it's format, it's width, it's height? Also, can you show us the glCopyTexSubimage2D line that didn't work?

  5. #5
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: How to copy backbuffer contents into a texture?

    Step 4 - read back buffer into a texture is easy:

    Bind (texture);
    glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0, 0,0,texture.image.sizex, texture.image.sizey);


    Step 6 - display the quad/texture
    You can't do what you have done:
    glBegin(GL_QUADS);
    glTexCoord2f(?,?); glVertex2i(-1,1);
    glTexCoord2f(?,?); glVertex2i(1,1);
    glTexCoord2f(?,?); glVertex2i(1,-1);
    glTexCoord2f(?,?); glVertex2i(-1,-1);
    glEnd();

    ...because the vertex positions will get multiplied by the ModelView matrix and projection matrices by the fixed function pipeline and or shaders. Are you using GL CORE profile or COMPATABILITY?

    Instead, you need to pass in the width and height of the quad you are drawing:

    glBegin (GL_Quads);
    glTexCoord2f (0,1); glVertex2f (x,y); //top left
    glTexCoord2f (1,1); glVertex2f (x+w,y); //top right
    glTexCoord2f (1,0); glVertex2f (x+w,y+h); //bottom right
    glTexCoord2f (0,0); glVertex2f (x,y+h); //bottom left
    glEnd;

  6. #6
    Junior Member Newbie
    Join Date
    Aug 2010
    Posts
    13

    Re: How to copy backbuffer contents into a texture?

    Thanks for your comments. I tried to apply what you have said. Now I see something on the screen, but it is now what I had expected. My code looks like this now:

    Code :
    //	Step 4: save backbuffer contents into texture (save the whole visible area)
    glGenTextures(1,&img);
    glBindTexture(GL_TEXTURE_2D,img);
    glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0, 0,0,CX,CY);
     
    //	Step 5: clear the scene again
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    //	Step 6: draw a quad with the size of the whole screen using the captured texture
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,img);
    glBegin (GL_QUADS);
      glTexCoord2f (0,1); glVertex2f (-1,1); //top left
      glTexCoord2f (1,1); glVertex2f (1,1); //top right
      glTexCoord2f (1,0); glVertex2f (1,-1); //bottom right
      glTexCoord2f (0,0); glVertex2f (-1,-1); //bottom left
    glEnd();
    glDisable(GL_TEXTURE_2D);
     
    glutSwapBuffers();

    Since I want to capture the whole visible area of my window, I set the coordinates of glVertex2f accordingly (applying the width of the texture as advised).

    However, the result is that the red sqare is extended over the whole visible area. Actually I wanted to capture also the black part, that's why I have set the width and height in glCopyTexSubImage2D to CX and CY (width and height of the window)

    See the attached images (I expected that "before" and "after" look equally)

    Before the capture:


    After rendering the captured texture:


    Thanks in advance for any help!


    EDIT - @BionicBytes: How can I tell whether I am using GL CORE profile or COMPATABILITY?

  7. #7
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: How to copy backbuffer contents into a texture?

    Compatability or Core is an option set in the context creation code. You had to have specified this in your initialisation. How did you do that?
    I guess you have compatibility because you don't appear to have shaders running and are using immediate mode (with quads). These were removed from the 'core' profile!

  8. #8
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    965

    Re: How to copy backbuffer contents into a texture?

    What's your viewport x, y, width and height? And the projection matrix you're using when drawing? Also, the values of CX and CY? And also the img texture should be just created once during startup (or if the window size changes), not every frame.

  9. #9
    Junior Member Newbie
    Join Date
    Aug 2010
    Posts
    13

    Re: How to copy backbuffer contents into a texture?

    Quote Originally Posted by BionicBytes
    Compatability or Core is an option set in the context creation code. You had to have specified this in your initialisation. How did you do that?
    All initialization I do is described in step 1. Do I have do do more than that?

    Code :
    //	Step 1: create window
    glutInitWindowSize (CX, CY);
    glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow ("glut");
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);


    Quote Originally Posted by mhagain
    What's your viewport x, y, width and height? And the projection matrix you're using when drawing? Also, the values of CX and CY? And also the img texture should be just created once during startup (or if the window size changes), not every frame.
    Viewport: Not specified. I really use only the code posted above. Should I set the viewport to the visible area of the window?

    Projection matrix: Not specified. What would be a useful value here?

    CX, CY: Both are 144. That is also the width and height of the window. I want to capture the complete client area of the window.

    Regarding the texture: I will change it so that the texture is created only once. What is the problem if it is created every frame? Memory leaks?

  10. #10
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: How to copy backbuffer contents into a texture?

    Your using glut - a helper package. I guess that does some defaults.
    I suggest you google HeNe which has some excellent begginer tutorials which will take you from the absolute basics to intermediate level. You are missing the fundaments of the whole GL thing and it would take a series of tutorials to explain what those things are!
    I suggest you read up on projection matrix, modelview matrix and view port. Then you'll be able to anser your own questions!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •