masking overlapping?

I put a quad as a mask and another quad as the texture onto each other. Almost like the NeHe tutorial teaches about masking. NeHe writes there:
“…we draw this image right on top of the mask. Same texture coordinates, same vertices.”
I tried that in my own program, but after that I got a strange result. I only saw the mask (black/white textured quad). The “foreground” texture wasn’t there. I changed the vertices’ coordinates and put the quad (coloured) nearer the viewer and the “foreground” texture appeared, the masking was made.
I think this error came of the fact that the two quads were overlapping each other. But if I draw the b/w mask first and after that the coloured one, the second should appear and the first should disappear.
My code looks the following:

(in the main loop)
glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR, GL_ZERO);
draw_mask(4);//draws the b/w mask
glBlendFunc(GL_ONE,GL_ONE);
draw_mask(5);//draws the image
glDisable(GL_BLEND);

//****************************************************

(the drawing funcion)
void draw_mask(int num){

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, background[num]);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0, 0);
glVertex3f(218.419,-60.65,492.828);
glTexCoord2f(0, 1);
glVertex3f(218.731, -51.77, 494.277);
glTexCoord2f(1, 1);
glVertex3f( 227.529,-51.72, 492.38);
glTexCoord2f(1, 0);
glVertex3f(227.216, -60.65, 490.93);
glEnd();}

After that I see only the b/w mask. How can this be?

Turn off depth test with glDisable(GL_DEPTH_TEST);

I disabled the depth test like this:

glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR, GL_ZERO);
draw_mask(4);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_ONE,GL_ONE);
draw_mask(5);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);

I had to keep the depth test by the first draw_mask(), so it masks the 3D object according to the depth value.
Now it has one more bug left: when a 3D object comes between the camera and the textured quad, the quad’s texture shines through the object (according to the alpha value of the object’s pixels).
If the 3D object is behind the textured quad (it is masked by the quad) there is no problem.
What should I do to have the 3D object rotating correctly around the textured quad?

Damn that NeHe, everybody is using masking when should use alpha blending/testing…

Take your mask texture and put it into your main texture’s alpha channel. Then draw your object with this texture using one of the following:

  1. Alpha test: sharp edges, compatible with depth test
  2. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA): soft edges, not compatible with depth test

Even with 2 you can have the depth test enabled, just set glDepthMask(false).

-Ilkka

ps. I think NeHe’s pages are great