# OpenGL OpenGL Blending (I want white overlays to get lighter, not darker)

This topic is 3496 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a green image and I'm putting a semi-opaque white image on top of it. I'm trying to get a result that's light green. The initial blending func I had was: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); This ends up making a dark-green color. Then I tried: glBlendFunc(GL_SRC_COLOR, GL_DST_ALPHA); And I end up getting a bright-green color (it's way over-exposed). Help? :) Screenshot:

##### Share on other sites
prehaps what you wan't is glBlendFunc(GL_SRC_ALPHA, GL_ONE); if that isn't what your looking for maybe try playing around with the parameters, I believe the function used to calc the final color looks something like this (in a very simplified form) finalColor = (sfacter * newColor) + (dfacter * oldColor)

##### Share on other sites
Supposedly, that's the exact formula. Okay. I have a color in RGBA:

(244, 179, 15, 1)

And I'm overlaying a 50% white image on top of it:

(255, 255, 255, .5)

According to Photoshop I should get a resulting color of:

(250, 217, 135, 1)

Which, if you notice, is simply half-way between the original color and pure white.

Now according to this site, http://jerome.jouvie.free.fr/OpenGl/Lessons/Lesson3.php (and a couple of others), the formula that OpenGL uses to calculate the final color is:

srcColor*srcFactor + destColor*destFactor

I'm using:

glBlendFunc(GL_DST_ALPHA, GL_DST_ALPHA);

which basically means srcFactor and destFactor are both:

(.5, .5, .5, .5)

Which, if you do the math, comes out to what photoshop gives me, except for the alpha channel:

R = 244*.5 + 255*.5 = 250
G = 179*.5 + 255*.5 = 217
B = 15*.5 + 255*.5 = 135
A = 1*.5 + .5*.5 = .75

Now I'm not quite sure what effect having a delta alpha of .75 has... but OpenGL is giving me this color, measured in photoshop:

(255, 255, 206, 1)

Which is nowhere near what I want!

ARGGGHHHH!!!

##### Share on other sites
are you sure you don't want:

glBlendFunc(GL_SRC_ALPHA, GL_SRC_ALPHA);

GL_SRC_ALPHA is the alpha from glColor and/or your texture
GL_DST_ALPHA is the alpha value thats already in the pixel buffer, from whatever you drew before, using this could explain the high numbers your getting

##### Share on other sites
Yeah, I do... but for whatever reason GL_SRC_ALPHA makes the fully transparent areas of my image completely black, and darkens the rest... It's basically taking the initial image and making it closer to black.

##### Share on other sites
well that makes sense, since when alpha = 0, srcColor * 0 + dstColor * 0 = 0 = black
in your example you only used 0.5 for your alpha value which works because when a = 0.5, it is the same as averaging the two colors, since your textures alpha is not all 0.5, you have to figure out how you want it to behave when a != 0.5, if you want it to always take the average, you could try glBlendFunc(GL_CONSTANT_ALPHA, GL_CONSTANT_ALPHA) with glBlendColor(1.0f, 1.0f, 1.0f, 0.5f)

##### Share on other sites
yeah, but even so it's making the part that has A=.5 closer to black as well, not closer to white.

##### Share on other sites
I do want it to always take the average, but the image I'm trying to "lighten" is an irregular shape, so the mask is the same shape. I -really- don't want to define the shape in code :)

##### Share on other sites
so your image alpha is always 0.5 or 0? in that case you might want to try alpha testing, with the GL_CONSTANT_ALPHA I mentioned earlier:

glEnable(GL_BLEND);
glBlendFunc(GL_CONSTANT_ALPHA, GL_CONSTANT_ALPHA);
glBlendColor(1.0f, 1.0f, 1.0f, 0.5f);

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0);

##### Share on other sites
as u can see I wrote that 8 years ago (so blendig modes etc a bit out of date)
www.zedzeek.com/APPS/opengl_blending.rar

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011385
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!