View Full Version : Creating motion trails with glCopyTexSubImage2D

05-04-2006, 11:59 AM
Hi, basically what I want to do is draw my scene, copy it to a texture, and then draw the texture before I draw the scene the next time. Basically what I want to do is create motion trails behind my objects. Here's my code:

glBindTexture(GL_TEXTURE_2D, texture[0]);
glColor4f(1.f, 1.f, 1.f, 0.97f);
glTexCoord2f(0.0, 1.0); glVertex2f(0.f, windowH-textureSize);
glTexCoord2f(0.0, 0.0); glVertex2f(0.f, windowH);
glTexCoord2f(1.0, 0.0); glVertex2f(textureSize, windowH);
glTexCoord2f(1.0, 1.0); glVertex2f(textureSize, windowH-textureSize);

// draw some spheres or whatever

glBindTexture(GL_TEXTURE_2D, texture[0]);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, windowW, windowH);

This mostly works, but if I set the alpha value high (you'll notice it's at .97 above), the trails don't fade out all the way to black. They sort of fade to a dark grey. Obviously I want them to disappear completely after a second or so.

I can't for the life of me figure out why this happens. Here's a magnified screenshot:


I've got a GeForce 7800GT on WinXP.

05-04-2006, 12:53 PM
Colors get rounded to the nearest value before writting to framebuffer.
round(16 * 97%) = round(15,52) = 16

http://www.opengl.org/documentation/spec...000000000000000 (http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node40.html#SECTION005139000000000000000)

And by the way - if you blend your texture with framebuffer using alpha=0.97 then you get additional 3% of previous framebuffer content added to you final color.

05-04-2006, 03:57 PM
ok, so is there any way around that? or what would be your recommended way for creating motion trails?

05-05-2006, 05:57 AM
You can paint that texture multiple times - every time using alpha = 0.5. Painted 2 times gives 75%, 3 times gives 87,5%, and so on.
However, you will pay for this with increased datarate.
You could also use a very simple fragment shader if that's not a problem.

05-05-2006, 02:06 PM
The multiple texturing with .5 alpha doesn't work - it obscures the trails completely (they're gone after only a few frames). I want them to last for 60 frames or even longer. So I settled on using a shader which works fine on my desktop but my laptop doesn't support it. Oh well.

05-05-2006, 04:22 PM
Let's try completely different approach... ;)

Step 1:
-now render texture that's behind your scene

Step 2:
glBlendFunc(GL_ONE, GL_ONE);
glColor3ub(1, 1, 1);
-draw fullscreen quad (no texture)

Step 3:
glCopyTexSubImage2D(...); //copy framebuffer to your texture

Step 4:
-now render texture that's behind your scene

Step 5:
-now render scene

Step 6:
glCopyTexSubImage2D(...); //copy framebuffer to your texture

As you can see I inverse the texture 2 times - when it's iversed I can use blending to add (1,1,1) to inversed texture wchich is eqivalent to adding (-1, -1, -1) to normal texture :)