opengl depth

An other of my openGl questions. How can I create depth ?
Here is an image to illustrate the problem. Or the Quads mapped with a pink gradient is over the black particles or below. I would like it to be in the center of the sphere formed by the particles.

I found those two interesting threads - 1 and 2, and that page, but I’m still unclear on how to use it.

Any thoughts on how to do this would be great.



Hi Smallfly, the threads you link to shouldn’t be very related to your problem. The problem you have is because you have transparency in your gradient. Unfortunately depth testing doesn’t work very well when you have semi-transparent objects, and the only main solution is sorting and drawing your semi-transparent polygons from back to front.

One thing you could try which sometimes works (but I doubt it will work in your situation) is GL_ALPHA_TEST and glAlphaFunc. These tell opengl to skip pixels if they dont meet specified alpha criteria.

So you can try:

glEnable(GL_DEPTH_TEST);   // enable use of depth buffer  
glEnable(GL_ALPHA_TEST);   // enable alpha testing  
glAlphaFunc(GL_GREATER, 0.5);   // if incoming alpha value is greater than 0.5 (tweak this number), draw pixel to screen and depth buffer; if its less, dont draw at all.  

In your situation, this will look a bit weird because your gradient is so spread. If your gradient didn’t have such feathered edges you could use the above code and it should work. If you do want to keep such a feathered gradient, you need to first draw the back half of the sphere, then the gradient, then the front half unfortunately…

Hi Memo. I knew that the blend mode and transparency had something to do with my problem. I’m going to try both of the solutions you proposed.

I was also thinking to try to replace the pink gradient with the a light source, but this is again a new territory :slight_smile:


So I tried both solutions.

  1. alpha testing
    I draw first the black particles and than the pink gradient. I changed the pink gradient so that it goes from alpha 80% to 0% (instead of 100% to 0%). I set up the alpha threshold to 0.74, like this “glAlphaFunc(GL_GREATER, 0.74)”. Here is what I get, which makes sense. As you mentioned the feathered edges of the pink gradient are cut off, but the part of the gradient (over alpha .74) is draw over the particles. How can the particles be drawn over the pink gradient ?

  2. order of drawing
    This works, even though I have to go through the list of particles twice. But running it, I realized that I’m using a camera, and than it doesn’t-work that much :). Maybe the way I do the billboarding, for the pink gradient, via a shader, could solve this. Any thoughts ?