Capture scene and draw it back as texture

Hi there,

I just want to capture the actual scene and use it as a texture. This works fine if width and height of the viewport is a power of two when using

gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 0, 0, width, height, 0);

But for width=640 and height=480 I first have to set the viewport to the next power of two:

gl.glViewport(0,0,1024,512);
gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 0, 0, 1024, 512, 0);

Is it also possible to render the complete scene to a much smaller texture, e.g 64x64? Calling

gl.glViewport(0,0,64,64);
gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 0, 0, 64, 64, 0);

doesn’t work, because only a small section of the scene is captured.

Has somebody a simple code sample for doing this task?

[quote]But for width=640 and height=480 I first have to set the viewport to the next power of two:

gl.glViewport(0,0,1024,512);
gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 0, 0, 1024, 512, 0);
[/quote]
You probably shouldn’t set the viewport larger than the actual screen, the results are probably undefined. Instead create a texture big enough (so probably next power of two in size) and then use glCopyTexSubImage with actual screen width and height.

[quote]Is it also possible to render the complete scene to a much smaller texture, e.g 64x64? Calling

gl.glViewport(0,0,64,64);
gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 0, 0, 64, 64, 0);
[/quote]
That should work fine, but you have to get your ordering right - glViewport only affects any rendering after it, so you have to set the small viewport, render your scene, then capture the viewport area to texture.

Oh thanks!
I hoped that there is a hardware accelerated functionality to render a W1xH1-sized scene to any W2xH2-sized texture by interpolation. Is this not possible with OpenGL?

Hello,

I am struggeling at this moment with jogl for capturing the content of the framebuffer and copying it to texture.
I am using the glCopyTexImage2D and it seems that is not doing anything.

I was wondering if anybody has a simple example which can show me how it works.

Thanks for your help.

See this thread at LWJGL forum:

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=LWJGL;action=display;num=1098038144

…it contains source code for a motion blur effect which I wrote some time ago. Basically it shows how to make use of non-power-of-two textures. Hopefully you can modify it to fit your needs.