Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Transparency & depth

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    3

    Question 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:
    Code :
    /**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);

  2. #2
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    12
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    3
    Quote Originally Posted by cmaster.matso View Post
    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.

    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?

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    12
    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

  5. #5
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    445
    Quote Originally Posted by knight666 View Post

    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
    Code :
    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.

  6. #6
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    3
    Quote Originally Posted by Dan Bartlett View Post
    You could either provide a depth for each element, or use
    Code :
    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •