Transparency & depth

Hi there,

I have a bit of trouble getting transparency and depth to work properly.

I have an image which has a transparent background (transparency in the image below shown with color). So only part of the image is actually opaque. Now I want to draw another object(the blue circle) so that it looks like its behind the first object(the rectangle).

In the image, the first two frames shows what I have got so far. I wish to achieve the effect shown in the 3rd frame. The circle image also has a transparent background like the rectangle.

No matter what I try, I just cant get it to work properly. The circle either ends up behind the rectangle or completely in the front.
Any help or pointers will be much appreciated.

Here is a small part of the code I’ve written so far:


/**When cirlce_z > rectangle_z, it is drawn in front
     and behind otherwise
**/
glEnable(GL_DEPTH_TEST);
....
....

glEnable(GL_TEXTURE_2D);
/*Drawing the circle first*/
glBindTexture(GL_TEXTURE_2D, circleImage);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	//drawing code
	...
        ...
        ...

/*drawing the rectangle last (I've tried the reverse order as well)*/
glBindTexture(GL_TEXTURE_2D, rectImage);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        //drawing code
	...

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);

Are the polygons You are drawing the textures on in the same distance form the eye? As far as I know when You draw textures in 2D (aligned to the screen) it is better to disable depth test.

:surprise:
Oh my!

I disabled depth test and re-arranged my drawing order, and it worked!
I will have to keep track of the order, but whatever,

Thanks for the help.

But out of curiosity, is it possible to achieve this with depth test enabled for 2D?

If I remember well there is a problem of z-fighting in such situations. It is caused by the fact that there is a number of polygons drawn in the same exact distance from the eye and GL can’t tell which one of them is to be drawn first, according to the depth of pixels. Thus depth test need to be disabled.

Regards,
MK

Oh my!

I disabled depth test and re-arranged my drawing order, and it worked!
I will have to keep track of the order, but whatever,

Thanks for the help.

But out of curiosity, is it possible to achieve this with depth test enabled for 2D?[/QUOTE]

You could either provide a depth for each element, or use

glDepthFunc(GL_LEQUAL);

instead of the initial value of GL_LESS if you are drawing at the same depth + want it still to be drawn.

[QUOTE=Dan Bartlett;1239301]You could either provide a depth for each element, or use

glDepthFunc(GL_LEQUAL);

instead of the initial value of GL_LESS if you are drawing at the same depth + want it still to be drawn.[/QUOTE]

Great! This is exactly what I was looking for!!

Thank you very much. Worked perfectly.