andyvn22
andyvn22

Reputation: 14824

Discoloration in OpenGL

I'm using OpenGL to draw in 2D. I'm trying to overlay textures with alpha. I have done this:

glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);

glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

And then I draw in reverse z-order. However, I'm getting strange discolorations. Here's an example of something that should smoothly fade from one image to another (in fact, the images are seamless in this particular case, but that won't always happen (so, no, I can't just not have alpha)): Strange discoloration from alpha blending

See the grey patch in the middle? That patch is in neither of the source PNGs. Does anyone know what's causing this and how to fix it? Perhaps a completely different alpha strategy?

EDIT: For reference, here are the two textures being blended:

enter image description here

Upvotes: 7

Views: 894

Answers (3)

holtavolt
holtavolt

Reputation: 4468

Is there any change if you use this as your blending function?

glBlendFunc(GL_SRC_ONE,GL_ONE_MINUS_SRC_ALPHA);

EDIT/SOLUTION:

Pre-multiplied alpha in the PNG was the culprit. Alpha needed to be divided back out of RGB to correct the image and remove the gray artifact (see comment chain for details)

Upvotes: 2

datenwolf
datenwolf

Reputation: 162164

I'm now wildly guessing here, but if Z fighting and color modulation are ruled out this may be some kind of filtering artifact. For a quick test switch between

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

and

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

if the effect changes between the two you, some filtering somehow messes up your coloration. Solution: Use mipmaping and generate the mipmap levels with a downsampling method that keeps the desired features. Standard gluBuildMipmaps uses a rather stupid box filter, so don't use that.

Upvotes: 0

genpfault
genpfault

Reputation: 52082

If your GL_TEXTURE_ENV is the default GL_MODULATE you may have some old color state floating around.

Try resetting that with glColor3ub(255,255,255) before you render your textured geometry.

Upvotes: 0

Related Questions