GL_DEPTH_TEST dilemma

Wheatie
Unregistered
 
Post: #1
In order for my explosion particles to draw correctly, I have to disable GL_DEPTH_TEST. But when I do this, if the explosion occurs behind a hill, I see them drawn on top of the terrain. When I don't disable it, they don't show up correctly when drawn over each other, but the hill correctly masks the explosion. Any ideas of how to fix this?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
You need to draw your transparent objects from farthest to nearest.
Quote this message in a reply
Member
Posts: 45
Joined: 2006.11
Post: #3
Just to clarify, you need to draw your particles from farthest to nearest, with the depth test on.
Quote this message in a reply
Member
Posts: 177
Joined: 2002.08
Post: #4
Try disabling depth *writes* with glDepthMask instead of the entire depth test.
Quote this message in a reply
Member
Posts: 157
Joined: 2002.12
Post: #5
All you need to do Is for every frame:
Enable depth test
Enable depth write glDepthMask(GL_TRUE);
Draw everything that IS NOT using transparencies first.
Then disable depth writting ( glDepthMask(GL_FALSE); ) and draw your explosion.

happy coding!...
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
The depth mask trick won't work unless all your sprites are uniform in color and opacity, since the blending equation is not symmetric.

This is why you need to draw them farthest to nearest.

If you draw them after all opaque objects, whether you write depth or not should be irrelevant, except perhaps to performance.
Quote this message in a reply
Member
Posts: 157
Joined: 2002.12
Post: #7
OneSadCookie Wrote:If you draw them after all opaque objects, whether you write depth or not should be irrelevant, except perhaps to performance.

If your explosion is nothing more than fire, and smoke you don't care about back-front because explosions will blend together.

If there's more than one explosion going on and the closest explosion is draw fefore the second one you may not get the desired results if writes to depth buffer is ON when drawing.

[Image: explode.jpg]
multiple explosions
Quote this message in a reply
Wheatie
Unregistered
 
Post: #8
Thanks for all the tips you guys have given me, but I'm having a hard time figuring out how I'm actually going to fix it.

See, I'm using the code from the OpenGL Game Programming Book for windows. I'm using my own "engine" code plus the game code from the book. I'm doing everything the same way that the book does it (or so I think) but it isn't working correctly. Anybody have experience with this code? I'm referring to the game that involves shooting the MD2 models on the terrain.

Thanks again,
Wheatie
Quote this message in a reply
Member
Posts: 157
Joined: 2002.12
Post: #9
Seems like you're doing everything ok.

when you call glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
depth is reset properly, so I don't think you need to call glClearDepth(1.0f); // that sets all the bits in the depth buffer to it's highest value.

when you call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); the depth
buffer is set to it's highest value(1.0).
so when you set glDepthFunc(GL_LESS); depth test will return true if the pixel z value is less than the one stored in the depth buffer.

I think the problem is on the glDepthFunc(GL_GREATER);
you should use glDepthFunc(GL_LESS);

this is a fragment from my enterframe function in my engine.

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);

glDepthMask(GL_TRUE);
glColorMask(1,1,1,1);
glDepthMask(GL_TRUE);
glShadeModel( GL_SMOOTH );

objHandle = modelSearchResult[0];
n = modelSearchResult.GetIndex();

for(i=0; i<n; i++)
( (S3Model3D*) objHandle[i] )->BeginDraw();

UpdateGameObjectSearchStack();
S3Physics::EnterFrame();

-----------------
another way it may work is calling glClearDepth(0.0f);
glDepthFunc(GL_GREATER);
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  GL_DEPTH_TEST and or Blending Volte 1 2,417 May 7, 2009 04:49 PM
Last Post: AnotherJake
  GLSL dilemma (in my Objective-C application) arrelid 21 7,599 Sep 8, 2006 07:02 AM
Last Post: arrelid