schwenk
schwenk

Reputation: 360

Bind (screen)-framebuffer to texture

How can I catch/copy/grab the currently displayed screen in OpenGL and read it into a GLuint Texture for using it in the next frame again as Texture ?

// Set random seed
program.setUniformValue("seed", ((float)qrand()/RAND_MAX));

//Set number of alredy rendered passes
program.setUniformValue("numRenderPass",mRenderPasses);

//Bind last rendered Image
//pixelsRenderedImage = bindTexture(*renderedImage);

//Load Identity
glLoadIdentity();

//Move to rendering point
glTranslatef( -1.0, -1.0, 0.0f );

glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pixelsScene);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, pixelsRenderedImage);

// Draw geometry
//Render textured quad
glBegin( GL_QUADS );
    glTexCoord2f( 0.f, 0.f ); glVertex2f( 0, 0);
    glTexCoord2f( 1.f, 0.f ); glVertex2f( 2.0, 0);
    glTexCoord2f( 1.f, 1.f ); glVertex2f( 2.0, 2.0);
    glTexCoord2f( 0.f, 1.f ); glVertex2f( 0, 2.0);
 glEnd();

 //PseudoCode
 Read_Back_screen_to -> pixelsRenderedImage

I want to bypass glReadPixels (the CPU) to speed up rendering "a little" ;-).

Upvotes: 0

Views: 771

Answers (1)

Reto Koradi
Reto Koradi

Reputation: 54652

glCopyTexImage2D() (man page) is the call you're looking for. It is used to copy data from the current framebuffer into a texture.

I don't think this call Is used much anymore. The preferred approach for cases where you want to use the result of rendering as texture content are Framebuffer Objects (FBO). They allow you to render to textures directly, avoiding an extra copy operation. FBO support was introduced in OpenGL 3.0.

Upvotes: 2

Related Questions