English 中文(简体)
Coloring grayscale image in OpenGL
原标题:

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 :)

最佳回答

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!

问题回答

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 );




相关问题
OpenGL 3D Selection

I am trying to create a 3D robot that should perform certain actions when certain body parts are clicked. I have successfully (sort of) implemented picking in that if you click on any x-plane part, it ...

CVDisplayLink instead of NSTimer

I have started to implement cvDisplayLink to drive the render loop instead of nstimer, as detailed in this technical note https://developer.apple.com/library/archive/qa/qa1385/_index.html Is it ...

Can the iPhone simulator handle PVR textures?

I have a really weird problem with PVR textures on the iPhone simulator- the framerate falls through the floor on the iPhone simulator, but on the iPhone itself it works just fine. Has anyone had any ...

Calculate fps (frames per second) for iphone app

I am using an opengl es iphone application. What is the most accurate way to calculate the frames per second of my application for performance tuning?

Java - Zoom / 3D Data Visualization Libraries

What are the best libraries/frameworks for doing 3D and/or Zoom interfaces in Java? I d like to be able to do some prototyping of creating new types of interfaces for navigating within data and ...

FLTK in Cygwin using Eclipse (Linking errors)

I have this assignment due that requires the usage of FLTK. The code is given to us and it should compile straight off of the bat, but I am having linking errors and do not know which other libraries ...

热门标签