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:
Paint my trail-sprites once to the screen(?)
Use glCopyTexSubImage2D for the whole screen to copy the trail-sprites into the 1024x1024 texture
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.
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
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 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?
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!
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.