PDA

View Full Version : Transparency & depth



knight666
06-26-2012, 12:57 AM
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.

http://img507.imageshack.us/img507/9652/imagejx.png (http://imageshack.us/photo/my-images/507/imagejx.png/)

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

cmaster.matso
06-26-2012, 01:11 AM
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.

knight666
06-26-2012, 01:23 AM
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?

cmaster.matso
06-26-2012, 01:32 AM
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

Dan Bartlett
06-26-2012, 04:49 AM
: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?

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.

knight666
06-26-2012, 05:56 AM
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.

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

Thank you very much. Worked perfectly.