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)
- Load image as texture
- Draw image on screen
- Enable blending
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);and draw rectangle with color1glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE);and draw rectangle with color2
But… 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)
- Save image as texture but don’t use grayscale image, use white image with alpha channel that is same as grayscale
- Draw rectangle with color1
- Draw image
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 🙂
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.
The math for this actually is something like:
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 thatglEnable(GL_BLEND)enables).So… something like
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!