Advertisement

ARRRGG!!! Depth-testing and Transparency

Started by November 17, 2003 06:28 AM
10 comments, last by Telamon 21 years, 3 months ago
Hi. I''m working on a school project with two other people and we are adding a particle engine to our game. As a base for this, we are using the code from NeHe''s particles tutorial (I think it''s number 19). Anyways, when we turn on depth testing, the particles don''t blend right - you can see where the edges are. NeHe uses a weird alpha blend function to blend the black pixels of bitmaps onto the background (his particle images don''t have an alpha channel). When we turn depth testing off in our game to render particles, they look great. But with depth testing they don''t draw completely transparently onto what''s behind them. This is a problem because we want the particles to be occuldable. I tried turning on depth testing in NeHe''s lesson 9 (the one with the spinning particles) just to see what would happen, and it completely messed up his drawing code, just like it is in our game. What gives? Any advice would be greatly appreciated! ---------------------------------------- Let be be finale of seem, seems to me. ---------------------------------------- Coding: http://www.stanford.edu/~jjshed/coding Miscellany: http://www.stanford.edu/~jjshed

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

I''ve had trouble with multiple transparent objects drawn out of order before which is (I think) what you''re having a problem with. What I did to fix it was to use alpha testing (look up glAlphaFunc) to test each fragments alpha value against a constant value. Hope that helps
Advertisement
The normal way of drawing transparant stuff is:
1. Draw all solid stuff (with z-check and z-write)
2. Sort all you transparant stuff
3. Draw all you transparant stuff, back to front (with z-check, but not z-write)

Problem with this is that the transparant stuff don't occlude, but since you draw them last it usually isn't a problem.

Edit: Another thing, if you use additive blending, sorting isn't needed.

[edited by - Jolle on November 17, 2003 1:35:35 PM]

I understand the problem: NeHe uses something called masking to get rid of the edges. The problem is when you change the color of a transparent object, the black changes color slightly to what you are changing the color to (im assuming you are using a white particle with a black background and want to turn it another color to simular fire or water or something). Also, if you have fog the color will be changed. Tell me if either of these things im saying is true then I can post a resolution to one/both
My fellow Americans I have just signed legislation that outlaws Russia forever. Bombing will commence in five minutes.
I am having the EXACT same problem with my game right now too, I know exactly what that looks like. I think the way to do it is just what Jolle said, but can I get a description of what Z-check & Z-write mean? Is that just depth testing of some sort?

Thanks
Whatsoever you do, do it heartily as to the Lord, and not unto men.
Hey, this is just off the top of my head so i may be wrong but I think if you do this:

glDepthMask(false);
//Draw Particles
glDepthMask(true);

that should solve the problem.

Luke.

Hyperdev

"To err is human, to really mess up requires a computer"
"To err is human, to really mess up requires a computer"
Advertisement
Z-check: Compare z-value against the value in the z-buffer and only draw the fragment if it is greater/smaller/whatever. To disable z-check, glDepthFunc(GL_ALWAYS) can be used. To turn it back on, use glDepthFunc(GL_LEQUAL) (or GL_LESS/GL_GREATER/whatever).

Z-write: Whatever you write the fragments z-value to the z-buffer or not. To disable, use what Lukerd said.

Another note: On a second thought, if you actually sort the transparant stuff, disabling Z-write isn''t needed.

So you shouldn''t then have to worry at all about z-write/z-check. Simply use what you usually use.
quote:
Original post by Lukerd
Hey, this is just off the top of my head so i may be wrong but I think if you do this:

glDepthMask(false);
//Draw Particles
glDepthMask(true);

that should solve the problem.

Luke.



what he said.


if its mixed with solid objects, try this


glEnable(GL_DEPTH_TEST);
DrawNonAlphaObjects();
glDepthMask(false);
DrawAlphaObjects();
glDepthMask(true);
glDisable(GL_DEPTH_TEST);


using teh depth mask tells it to check, but not update the depth buffer. Very handy (and it also increases speed).
Beer - the love catalystgood ol' homepage
Also, the Blending Factors in BlendFunc that are used in NeHe's code are kind of stupid. Try out different Blending Factors to see what works best.

The eight blending factors are:

GL_ZERO
GL_ONE
GL_SRC_COLOR
GL_ONE_MINUS_SRC_COLOR
GL_SRC_ALPHA
GL_ONE_MINUS_SRC_ALPHA
GL_DST_ALPHA
GL_ONE_MINUS_DST_ALPHA

Or something like that.

If it has no Alpha Channel, you are somewhat limited, but if you want black to be transparent, use (GL_ONE,GL_ONE_MINUS_SRC_COLOR). I think that should work.

Edit: You could also use (GL_ONE,GL_ONE) which would be like screening in Photoshop. The Dark Colors would not show up at all, but the Light ones will. This would probably be better.

--------------------------------------
I am the master of stories.....
If only I could just write them down...

[edited by - Nathaniel Hammen on November 21, 2003 12:49:20 PM]
I am the master of ideas.....If only I could write them down...
EXCELLENT, I will test out some of those blending equations... I think the (GL_ONE,GL_ONE_MINUS_SRC_COLOR) is the setup I want... so that the black doesn''t show up. I guess if I wasn''t a lazy bumb I could have looked it up myself. THANKS!!!!

This topic is closed to new replies.

Advertisement