PDA

View Full Version : "Render-to-texture" with glCopyTexSubImage2D



glClewbie
10-29-2007, 09:23 PM
I have a 2D-scene that consists of a simple textured background and a bunch of moving sprites. Now I need to add 'trails' (simply sprites) for my objects, and as there's gonna be a lot of them - and they are persistent, I need to render them into a texture.

So from what I've gathered I could use glCopyTexSubImage2D to achieve this - instead of the 'heavier' and less supported PBuffers & FBOs.

I can use a 1024x1024 texture, which is enough to fill the whole scene and this simplifies things quite a bit.

As I've understood it should be nearly this simple:

1. Paint my trail-sprites _once_ to the screen(?)
2. Use glCopyTexSubImage2D for the whole screen to copy the trail-sprites into the 1024x1024 texture
3. Simply paint the final texture over my scene

But I'm very confused as of how to achieve the first step so that glCopyTexSubImage2D does NOT include anything else from the scene, but just these trail sprites. So I guess I should somehow render the trails into 'a transparent offscreen buffer', and the 'blit' them into the texture.

In another words, I want the texture to be transparent and copy only the trail-sprites into it.

Thanks in advance.

Mars_999
10-30-2007, 01:07 AM
what screen resolution you going to run at? If your size is less than 1024 with either height or width you will have problems. FBO's eliminate this headache. IMO use FBOs

ZbuffeR
10-30-2007, 03:35 AM
To Mars_9999 : as the OP is a beginner, I think it is better to start with a simple solution, then when it works, try with FBO.

To glClewbie:
If I get it correctly (assuming tex is cleared):

:loop
- draw tex
- draw particles only
- copy to tex
- draw background
- draw tex with appropriate blend
- swapbuffers
goto :loop

glClewbie
10-30-2007, 07:45 AM
Hi.

I'm testing these things in a custom size window (not fullscreen), around 820x680.

I actually used FBOs (GL_FRAMEBUFFER_EXT) and they were quite easy to use and worked just right.

Thanks for the steps, I tried something along those lines yesterday, and got the trails copied into the texture, but the texture also always got filled with black color (I do clear the OpenGL window at the beginning of a frame with black color, did it come from this?). Maybe I did something stupid, I think I still try this again.

Also, hmm, how can I clear a texture? With FBOs I just created a 1024x1024 RGBA texture, and it was/is always automatically transparent.

I'm very happy with the FBO solution as I don't need to care about the rendering order at all, but I'm afraid about the extension; what OpenGL version is required by GL_FRAMEBUFFER_EXT? I know it's part of OpenGL 2.0, but as I've understood many older cards also get it by updating their drivers. This link (http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt) says that "OpenGL 1.1 is required", does it mean that even most older cards support FBOs (at least in theory - and if drivers are updated)? Does anyone have some handy link where I can find more info about what card supports what?

Thanks for the replies.

Mars_999
10-30-2007, 09:23 AM
http://www.delphi3d.net/hardware/index.php

and IIRC GL1.5 is needed for FBO but I am not 100% sure anymore been to long since I been knee deep in coding to remember all this for sure.... Hope to change that soon! :)

Relic
10-30-2007, 10:00 AM
I actually used FBOs (GL_FRAMEBUFFER_EXT) and they were quite easy to use and worked just right.
If you have FBOs running already I would recommend a different approach. See below.


Also, hmm, how can I clear a texture? With FBOs I just created a 1024x1024 RGBA texture, and it was/is always automatically transparent.

If you need a specific blending, watch your glClearColor alpha value! Do not assume freshly allocated FBOs are initialized. You must initialize things with a clear or render operation!

Instead of rendering to and copying from the backbuffer you could do this:

glClear render-to-texture FBO once;
loop:
- draw additional trail particles into render-to-texture FBO only
- draw tex backgound
- draw tex (from FBO) with appropriate blend
- swapbuffers
goto loop

With a simple fragment shader you could also composite the two draw operations to one.


I'm very happy with the FBO solution as I don't need to care about the rendering order at all, but I'm afraid about the extension; what OpenGL version is required by GL_FRAMEBUFFER_EXT? I know it's part of OpenGL 2.0, but as I've understood many older cards also get it by updating their drivers.

No, framebuffer objects are not inside the OpenGL 2.0 core. They are an extension.