iDevGames Forums
Render to texture and Blending - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Render to texture and Blending (/thread-6140.html)



Render to texture and Blending - reubert - Oct 29, 2004 02:22 PM

This is driving me nuts...

If I render all my cloud particles using blend mode GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, They look how I want.

If I then call glCopyTexSubImage2D() to create an imposter texture and render the result with the same alpha mode, the result looks way too transparent.

I've tried premultiplying the particle texture - (R,G,B * alpha). This looks wrong with an assortment of blend modes, including GL_ONE, GL_ONE_MINUS_SRC_ALPHA, which I believe should work. And this is just rendering the particles, without worrying about copying to the imposter texture. In this case, everything is either full white or zero alpha. No color, no partial alpha.

Can anyone tell me the proper way of dealing with alpha when rendering to a texture? There seems to be absolutely no information about this anywhere. It's like it should "just work" or something.

Cheers,
Frustrated Dave.


Render to texture and Blending - arekkusu - Oct 29, 2004 02:44 PM

You probably want a saturated alpha mode.

Download KochPaint and play with the layer blending for a while. The UI is very non-intuitive but:

1) all controls have tooltips Wink
2) at the top left is a checkbox that exposes additional layer blending controls for control of the glblendfunc when mixing the offscreen particle render into the canvas. checking this box also draws a few colored blobs onto the background so you have something to blend against.
3) the two blendfunc rows are the full GL_COMBINE controls for RGB, ALPHA
4) on the right are checkboxes for premultiply (this sets the layer RGBA to alpha, alpha, alpha, alpha instead of 1,1,1,alpha) and Saturate. Saturate toggles the particle (not layer) blendfunc between (GL_SRC_ALPHA_SATURATE, GL_ONE) and (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).

If you use the bottom left popup to set the primitive mode to points and make a big mess, you should see that the following settings produce what you want:

particles blended with (GL_SRC_ALPHA_SATURATE, GL_ONE) into offscreen texture. The particle color is always premultiplied by the paint alpha.
texture composited to viewport with (GL_ONE, GL_ONE_MINUS_SRC_ALPHA) and premultiplication (the default setting.)

Dumping out your rendered texture and examining the alpha channel is probably also a good idea.

And, btw, this is where a pbuffer (or an offscreen window context in 10.2) is good. Copying from framebuffer to texture over and over is just burning fillrate.

[Edit: noted that particle RGB is premultiplied]


Render to texture and Blending - reubert - Oct 29, 2004 11:16 PM

Thanks again, the saturated alpha worked perfectly.

I'll have to have a good play with KochPaint at some stage. I'm still not sure why saturated alpha is what I needed, but at least it works now Smile

Cheers
Happy Dave


Render to texture and Blending - arekkusu - Oct 30, 2004 04:48 AM

The "why" is best explained by just looking at your texture's alpha channel. Saturation will build up an alpha mask of overlapping particles correctly; other modes will instead replace the alpha channel with the values from the last drawn primitive, this comes out looking too "light".