Masking with GL_ALPHA texture

I have a RGB color texture that needs to be masked by a greyscale alpha mask. Ordinarily I would just combine to two textures in a single RGBA texture and call glAlphaFunc( GL_GREATER, 0 ). Unfortunately using a single RGBA is not practical/feasible for what I’m doing.

I cannot use any proprietary OpenGL extensions. Video card is a Geforce MX /w OpenGL v1.2.1.

I have already posted this topic in the Beginners forum with the topic name “Masking” dated 12-07-2000. I have also posted two tries (code) which logically should have worked but failed. To date I have not been unable to solve the problem on my own or based on replied suggestions.

Summary of first posted attempt-
bind the mask as a one component GL_ALPHA texture
bind the color bmp as a 3 component GL_BGR_EXT texture
use a display list to store one GL_QUADS for texture to site on. One glColor4f and 4 glTexCoord & glVertex used.
disable depth testing so mask does not fight with color texture
disable drawing to RGB layers and enable drawing to alpha layer
draw the mask
enable lighting, GL_MODULATE and blending
set glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA )
draw color texture
> The idea was to have the mask values drawn into the alpha layer and use the blend function do the masking when the color texture was drawn. Problem was OpenGL drew the alpha value of my quad color (glColor4f) and ignored the alphas in the mask. glColor4f( , alpha) controlled the blending of my entire color texture.

Summary of second posted attempt-
bind the mask as a one component GL_ALPHA texture
bind the color bmp as a 3 component GL_BGR_EXT texture
use a display list to store one GL_QUADS for texture to site on. One glColor4f and 4 glTexCoord & glVertex used.
set default stencil value to zero
clear color and stencil buffer
disable depth testing so mask does not fight with color texture
disable drawing RGBA layers of color buffer
set alpha tests to pass any pixel not black (zero)
set stencil tests to always pass and set to white
draw mask
disable updates to stencil buffer
disable alpha testing
turn on lights, GL_MODULATE
enable writing to RGB layers and disable writing to alpha layer
draw color texture
> The idea here was to use the stencil buffer to store my mask. All non-black alpha pixels would pass the alpha test and go onto become white pixels in the stencil buffer. All black pixels would be discarded by alpha testing thereby leaving their corresponding stencil pixel also black. Again the alphas in my mask were ignored and instead glColor4f( , alpha ) were used. By varing glColor4f( , alpha ) my color texture was completely drawn or not present.

Please help.

Method 1 looks simpler, so I’ll just forget about method 2 for the moment…

You are talking about “1 component” and “3 component” textures. This is obsolete OpenGL 1.0 terminology. I assume this means you are passing 1 and 3 as the components argument to TexImage2D.

Well, 1, 2, 3, and 4 are equivalent to the values GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, and GL_RGBA.

So when you say you are setting up an alpha texture, what you are really doing is setting up a luminance texture and passing in alpha data.

The pixel path will expand the alpha data to RGBA: a --> (0,0,0,a).

Then the luminance component will be taken from the red component of the RGBA value: 0.

So you just created a luminance texture of all zeros.

So change 1 to GL_ALPHA, and you’ll get an alpha texture with the alpha value you are passing in. (You probably also want to change the 3 to GL_RGB to avoid confusion on this in the future.)

  • Matt

Matt, I have been having difficulty constructing a GL_ALPHA texture. It appears that if I do not select a destination alpha buffer (8 bits) when setting the pixel format, then the texture ends up just being a black texture with the alpha channel also being all zeroed out. And no, I am not using any destination alpha blending at all so it seems really strange to me. On the other hand, I have no problem creating a GL_LUMINANCE_ALPHA texture in the absence of a destination alpha buffer. And this is all on a TNT using the 6.31 reference drivers.

That sounds extremely strange. What is the exact GL call you are using to create the texture?

  • Matt

Whoops, I spoke too soon. I discovered a typo, I did not have the internal format set to GL_ALPHA8 the way it should have. It works fine now.

[This message has been edited by DFrey (edited 12-12-2000).]

Originally posted by mcraighead:
[b]You are talking about “1 component” and “3 component” textures. This is obsolete OpenGL 1.0 terminology. I assume this means you are passing 1 and 3 as the components argument to TexImage2D.

Well, 1, 2, 3, and 4 are equivalent to the values GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, and GL_RGBA.[/b]

Matt, there’s something I don’t get…

Wherever I go (MSDN, any internet site), here is what I find:


void glTexImage2D(
GLenum target,
GLint level,
GLint components,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const GLvoid *pixels
);

components:

The number of color components in the texture. Must be 1, 2, 3, or 4.

format:

Specifies the format of the pixel data. The following symbolic values are accepted: GL_COLOR_INDEX, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, and GL_LUMINANCE_ALPHA.


Are you saying that the “components” parameter should not be 1,2,3,4 but GL_xxxx ??? Or are you talking about the “format” parameter ?

Regards

Eric

Originally posted by mcraighead:
So change 1 to GL_ALPHA, and you’ll get an alpha texture with the alpha value you are passing in. (You probably also want to change the 3 to GL_RGB to avoid confusion on this in the future.)

Could my mistake really have been so simple? Well it was. One little parameter hung me up for a whole week. Thank you so much!!

I am talking about the components parameter. Your documentation is very much out of date. The redbook has the correct info. I looked for a reference on opengl.org and found this in the man pages:
http://trant.sgi.com/opengl/docs/man_pages/hardcopy/GL/html/gl/teximage2d.html

In fact, the “components” parameter has now been renamed to “internalformat”, as you’ll note. It hasn’t been changed to a GLenum from a GLint, but that’s only because it could break API compatibility if a particular architecture had different sizes for the two.

  • Matt

I’ll paste in the relevant quotes:

(The representations specified by GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, and GL_RGBA must match exactly. The numeric values 1, 2, 3, and 4 may also be used to specify the above representations.)

A one-component texture image uses only the red component of the RGBA color extracted from pixels. A two-component image uses the R and A values. A three-component image uses the R, G, and B values. A four-component image uses all of the RGBA components.

  • Matt

Originally posted by Eric:
[b] void glTexImage2D(
GLenum target,
GLint level,
GLint components,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const GLvoid *pixels
);

         components:
         The number of color components in the texture. Must be 1, 2, 3, or 4. 
         format:
         Specifies the format of the pixel data. The following symbolic values are accepted: GL_COLOR_INDEX, GL_RED,
         GL_GREEN, GL_BLUE, GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, and GL_LUMINANCE_ALPHA. 

[/b]

You must have old documentation. The latest MSDN says this…

void glTexImage2D(
GLenum target,
GLint level,
GLint internalformat,
GLsizei width,
GLsizei height,
GLint border,
GLenum format,
GLenum type,
const GLvoid *pixels
);

Parameters
clip
internalformat
The number of color components in the texture. Must be 1, 2, 3, or 4, or one of the following symbolic constants: GL_ALPHA, GL_ALPHA4,
GL_ALPHA8, GL_ALPHA12, GL_ALPHA16, GL_LUMINANCE, GL_LUMINANCE4, GL_LUMINANCE8, GL_LUMINANCE12,
GL_LUMINANCE16, GL_LUMINANCE_ALPHA, GL_LUMINANCE4_ALPHA4, GL_LUMINANCE6_ALPHA2,
GL_LUMINANCE8_ALPHA8, GL_LUMINANCE12_ALPHA4, GL_LUMINANCE12_ALPHA12, GL_LUMINANCE16_ALPHA16,
GL_INTENSITY, GL_INTENSITY4, GL_INTENSITY8, GL_INTENSITY12, GL_INTENSITY16, GL_R3_G3_B2, GL_RGB, GL_RGB4,
GL_RGB5, GL_RGB8, GL_RGB10, GL_RGB12, GL_RGB16, GL_RGBA, GL_RGBA2, GL_RGBA4, GL_RGB5_A1, GL_RGBA8, GL_RGB10_A2,
GL_RGBA12, or GL_RGBA16.

clip
format
The format of the pixel data. It can assume one of nine symbolic values:
clip <Large list of values with detailed info on each>

Originally posted by Deiussum:
You must have old documentation. The latest MSDN says this…

I guess I should subscribe to MSDN to receive the updated CDs… The other thing is that all the On-Line versions of the Red Book I have state the (1,2,3,4) thing !

I have never had any problem with glTexImage2D but it would have come without knowing this !

Thanks Matt and Deiussum !!!

Regards.

Eric

P.S. : just checked the MSDN that comes with Visual Studio.NET (VS 7.0) and I have the correct info !!!