Reputation:
I'm trying to draw grayscale image in color as texture in OpenGL using two colors. Black goes to color1 and white goes to color2. Texture is loaded using GL_RGBA
.
I have tried two solutions:
1)
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
and draw rectangle with color1glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE);
and draw rectangle with color2But... When I apply first color, there is no black color on screen and when second color is applied it is combined with first color too.
2)
But... When image is drawn it doesn't use color1 where image is transparent, instead it uses current color set with glColor.
Any help will come in handy :)
Upvotes: 1
Views: 3197
Reputation: 18015
In general, when dealing with OpenGL texturing pipeline, I recommend writing down the end result you want. here, you want your grayscale color to be used as an interpolant between your 2 colors.
out_color = mix(color1, color2, texValue)
The math for this actually is something like:
out_color = color1 + (color2 - color1) * texValue
So... is there a texture environment value that helps do that ? Yes, and it's also called GL_BLEND
(not to be confused with the blending to frame buffer that glEnable(GL_BLEND)
enables).
So... something like
// pass color1 as the incoming color to the texture unit
glColor4fv(color1);
GLfloat color2[4] = { ... };
// ask for the texture to be blended/mixed/lerped with incoming color
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_BLEND);
// specify the color2 as per the TexEnv documentation
glTexEnvfv(GL_TEXTURE_2D, GL_TEXTURE_ENV_COLOR, color2)
There is no need to draw multiple times or anything more complicated than this, like you tried to do. The texturing pipeline has plenty of ways to get controlled. Learn them to your advantage!
Upvotes: 1
Reputation:
Your #2 idea would work, but it seems like you didn't set blending correctly. It should be:
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
Upvotes: 0