Can't understand the logic behind glBlendFunc()

Started by
4 comments, last by dpadam450 11 years, 9 months ago
I've been reading this chapter about blending in OpenGL:
http://fly.cc.fer.hr.../chapter07.html

Can anyone explain me (in a better way than in the article) the next blending formula, and how I have to use it in order to achieve my wanted results?
(RsSr+RdDr, GsSg+GdDg, BsSb+BdDb, AsSa+AdDa)

A link to a better article about OpenGL blending would be appreciated as well. rolleyes.gif
Advertisement
That is pretty confusing.
That would be additive blending with GL_SRC_ALPHA, GL_DST_ALPHA as the glBLendFunc.

The equation written nicely would be
(R,G,B)*SRC_ALPHA + (R,G,B)*DST_ALPHA
source dest

If the incoming pixel and current pixel on screen have an alpha of 1, then the two pixels getting completely added (R+R, G+G, B+B).
Most useful glBlendFunc is GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA. As incoming source alpha is more opaque, the current pixel gets covered up.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Where's the best place to learn blending?
There isn't anything to learn.
glEnable(GL_BLEND);
glBlendFunc(..........,.........);
//draw stuff
glDisable(GL_BLEND);

optionally glBlendEquation() but I still have yet to use that for anything other than default.

It comes down to what you want to do with glBlendFunc(), If you want a cloud that has alpha, the GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA. If you want a glowing effect, GL_ONE, GL_ONE works. Thats pretty much it.

If your cloud is glColor4f(1,1,1,.7), then
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
will make the pixel drawn .7*incoming pixel + (1.0-.7) = .3*current pixel. = mostly cloud.
if the cloud is not very dense, then

If your cloud is glColor4f(1,1,1,.2), then
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
will make the pixel drawn .2*incoming pixel + (1.0-.2) = .8*current pixel. = not much cloud showing.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

What's a glowing effect? Will I ever need something other than GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA if I want to draw just tree / grass textures with transparency? (not sure if it's even related to the blending function, but still asking)
Nope. you may want to do GL_ALPHA_TEST instead of blending, or GL_ALPHA_TEST with blending. No point in blending pixels that have a 0 alpha so they get discarded by the alpha test.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement