Rendering to a texture (How ?)

Started by
5 comments, last by Heretic 22 years, 8 months ago
How can I render a scene to a texture using OGL ?
Advertisement
u can copy a part or all the screen to a texture with glCopyTexSubImage2D(..)
to update an already existing texture with pixels (from any source) use glTexSubImage2D(..)
That''s slooooowwwww... Almost all newer 3D hardware use shared framebuffer and texture memory. That means, that you could *directly* render to a texture, without having to copy data around. I think there is a function in D3D that supports this, but I''m not sure (i don''t use D3D). There must be a way to do this under OpenGL ? Some extension perhaps ?

- JL
Try looking into pixel buffers (aka pbuffers). http://oss.sgi.com/projects/ogl-sample/registry/EXT/wgl_pbuffer.txt

Maybe that''ll give you what you need.
Ok this works for me (and ISNT slow, for me anyway), and should work for you ok:


int teximgnum = 0;

void InitRenderTexture()
{
glGenTextures(1, teximgnum);
glBindTexture(GL_TEXTURE_2D, teximgnum);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 512, 512, 0);
}

void RenderScene()
{
RenderTextureScene(); // render scene to be displayed on a texture
glBindTexture(GL_TEXTURE_2D, teximgnum);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, 512, 512);

RenderView(); // render real scene
}
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
glCopyTexSubImage2D(..) shouldnt be slow at all, but u need the same data formats in the screen as with the texture (so theres no expensive converting)

btw theres an arb_render_to_texture extension on the horizon
> glCopyTexSubImage2D(..) shouldnt be slow at all,

Ofcourse it is slow, even with the same data formats. The frambuffer data has to be transfered over the AGP bus down to host memory and back to the 3D card as a texture. Even if this is done as a simple block transfer (no processing, same data format) it still needs the data to be transfered over the AGP bus twice per texture each frame.

> btw theres an arb_render_to_texture extension on the horizon

This would be the ultimate solution. Do you have any more detailed info on this one ?

- JL

This topic is closed to new replies.

Advertisement