PDA

View Full Version : How to get colored texture with a for-white back-black one?



suniuin
12-11-2005, 08:12 AM
I simply want to draw disks with different colors. One image file(.tga) is acted as a texture on which a white disk is painted on black background, definitely the texture is a square thing. The purpose of using texture instead of calling display list of dist is that I only need to replace the .tga file when other objects(person face etc.) are wanted :) .

So the question is how can I get different colored disks by this method? I think that the GL_BLEND must be enabled because the disk actually is a square(black backgroud). The state of blend is glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR), GL_SRC_COLOR is used to remove the black background, but I do't know what state I should choose for the destination color factor, I try GL_ONE_MINUS_SRC_COLOR that when a red disk with smaller z value( to the viewer ) placed before a green disk (higher z value), the color red will blend with color green, when this happens, it is not a red disk any more. How can I get the pure red disk?

Thanks! :)

memfr0b
12-11-2005, 12:33 PM
You shouldn't use blending in this case. Instead of uploading a grayscale/black-and-white texture, upload an alpha-only texture, set the texture environment mode to replace and enable alpha testing.

Using an alpha texture with GL_REPLACE simply replaces the fragments alpha values with the textures alpha values, but leaves the original color. You can then use the alpha test with an appropriate reference value to select which fragments to discard and which to keep. The kept fragments are still completely opaque if you keep blending disabled. Blending is (almost) only necessary if you need semi-transparent objects.

suniuin
12-11-2005, 05:46 PM
Thanks very much, I understand your point.
But How to get a alpha texture? Previously I only use RGB image, also the function glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) also replace the current color with the new texture color. If texture is a alpha, help page says that the RGB color is (0 0 0) (black), so will I only get the black disks?
How to get red disk?

memfr0b
12-12-2005, 08:27 AM
The manual page for glTexEnv (http://pyopengl.sourceforge.net/documentation/manual/glTexEnv.3G.html) has an overview over texture environment modes, and how they transform the fragments' color and alpha values depending on the type of the bound texture. For GL_ALPHA textures, the fragment color is never changed.

You get an alpha texture by uploading alpha channel data (formats GL_ALPHA, GL_LUMINANCE_ALPHA, GL_RGBA, GL_BGRA) and storing it in a texture with an internal format of GL_ALPHA.


glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels);

suniuin
12-12-2005, 06:20 PM
Thanks,
From now I think a texture with a alpha channel is more flexible in fragment test.
Make sure another problem, can I use alpha channel to update the stencil buffer? I think the answer is yes because alpha test is done before stencil test, so alpha mask can work on stencil!

memfr0b
12-12-2005, 08:18 PM
Only fragments that pass the alpha test will affect the stencil buffer. In your case (circle texture on quad) this means that you'll get an circle area in the stencil buffer when alpha test enabled, and a quad area when you disable the alpha test.

suniuin
12-13-2005, 01:50 AM
memfr0b, thank you a lot.

Now I understand. Opengl is so amazing, guys like you are even better. :)