PDA

View Full Version : Creating motion trails with glCopyTexSubImage2D



fast-eric
05-04-2006, 12:59 PM
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]);
glBegin(GL_QUADS);
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);
glEnd();

// draw some spheres or whatever
drawScene();

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:

http://www.mat.ucsb.edu/~e.newman/trails.jpg

I've got a GeForce 7800GT on WinXP.

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

Source:
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.

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

k_szczech
05-05-2006, 06: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.

fast-eric
05-05-2006, 03: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.

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

Step 1:
glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_ZERO);
-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:
glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_ZERO);
-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 :)