View Full Version : Writing to target alpha without changing colour

James Lohr
09-14-2009, 01:07 PM
I have a texture that is a render target which has an alpha channel. I'm trying to get an effect where objects leave a trail when drawn. The idea would be to render to a texture T, and then, instead of clearing T after each frame, simply reduce the alpha level across the entire texture.

T is then rendered to the screen, giving an awesome trail effect.

The problem is that I don't know how to write to the alpha channel without affecting colour.

I can think of a work-around using texture swapping:

Suppose I have a texture B which I wish to draw leaving a trail, and I have two large (screen-sized) textures T1 and T2:

1) T1 is rendered to T2 with alpha set to 0.9
2) draw B to T2.
3) T2 is rendered to screen
4) T2 is rendered to T1 with alpha set to 0.9
5) draw B to T1
6) T1 is rendered to screen
7) return to 1)


0.9 is some decay value, eg for very long trails, set it to 0.995

when rendering from T1 to T2 or T2 to T1, target blend function is ZERO so target data is ignored.

I'm pretty sure this would work, however it seems like a lot of work for something that really should be quite simple: all I want to do is reduce the texture render-target alpha values.

[edit] ooh, on second thought, I think I may have solved the problem myself:

Render a white rectangle with alpha 0.9 to T with the following blend function:


that was easy :D

Ilian Dinev
09-14-2009, 01:17 PM

James Lohr
09-14-2009, 01:47 PM
Heh, I could have sworn that existed, however after 15 minutes of googling for "opengl colour mask" and not finding it, I concluded that I was imagining things and it didn't actually exist. I blame it on the wretched american spelling of colour. :P

Anyway, thanks a lot. It doesn't get much easier than that :)