views:

300

answers:

2

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 color1
  • glBlendFunc(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 :)

A: 

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 );
bmcnett
I have tried that, but I had no luck...
southpolenator
+1  A: 

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!

Bahbar
Actually formula is not so good. I got same result as 1. solution I tried.Formula should be like this:color1 * texValue + color2 * (1 - texValue)
southpolenator
huh ? if you swap color1 and color2, the formulaes are equivalent. This _does_ work, and is the fastest way to do what you're asking for.
Bahbar
btw, with your formula, black does not go to color1. It goes to color2! Mine goes to color1.
Bahbar
Formulas are not equivalent! What happens if color1 is red and color2 is green? out_color will always have red color and it shouldn't. It is not important if color1 maps black or white...
southpolenator
What do you think the -color1 does inside the multiplication ?it removes the red from outcolor as texValue increases.
Bahbar
My bad. I was watching result on different monitor and I saw brighter color so I thought it wasn't subtracting color.
southpolenator