Render to texture and Blending

Posts: 320
Joined: 2003.06
Post: #1
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.

Frustrated Dave.

Chopper, iSight Screensavers, DuckDuckDuck:
Quote this message in a reply
Posts: 1,234
Joined: 2002.10
Post: #2
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]
Quote this message in a reply
Posts: 320
Joined: 2003.06
Post: #3
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

Happy Dave

Chopper, iSight Screensavers, DuckDuckDuck:
Quote this message in a reply
Posts: 1,234
Joined: 2002.10
Post: #4
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".
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Render to Texture behaves weirdly ardowz 2 4,328 Feb 17, 2014 04:50 PM
Last Post: ardowz
  Render video (e.g. QuickTime) to buffer or texture? Ingemar 14 18,515 Jun 8, 2011 04:09 PM
Last Post: mdejong1024
  Render to Texture? IBethune 5 8,586 May 2, 2007 04:25 AM
Last Post: IBethune
  general blending versus texture blending questions WhatMeWorry 2 6,988 Dec 7, 2006 02:43 PM
Last Post: arekkusu
  PBuffer & Render to Texture (read & write) habicht 4 6,402 Feb 7, 2005 05:33 PM
Last Post: habicht