How can I use depth test without changing the depth buffer?

Started by
4 comments, last by silvermace 18 years, 5 months ago
I'm currently implementing a particle system with alpha blending. When depth test is disabled, everything works fine. But when depth test is enabled, the textures all messed up. I've tried the alpha test method but the result is not quite good. I've also found some articles stating that drawing in reverse order will have the best visual effect, but it will need to sort all particles for every single frame, which doesn't sound good when hundreds of particles need to be sorted. So I'm looking for a way to draw particles with depth test without modifying the values in depth buffer. Is it achievable in OpenGL? Or is there a better alternative?
Advertisement
Use glDepthMask.
Keys to success: Ability, ambition and opportunity.
Quote:Original post by LilBudyWizer
Use glDepthMask.
That will just give you incorrect results, you still want the depth test you just want the particles to be correctly ordered.
RenderScene();glEnable( GL_BLEND );glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );// 1. sort by Z// 2. render particles in order of://    furthest away to closest to camera/eye.RenderParticlesByZ();
you probably iterate the list of particles per frame at least once, you could do a simple counting sort (or radix) to order the Z values loosely.

EDIT:
this just came to mind; i'm not sure if it will work but its worth a try. Render the particles to the frame buffer but disable color writes then re-draw the particles with color write & alpha blending on and the depth buffer off. so something like this:
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);RenderParticles();glDepthMask(GL_FALSE);glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);RenderParticles();glDepthMask(GL_TRUE);
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
It seems that glDepthMask will prevent depth buffer from changing while still performing depth test. Anyway thanx alot to you both :D
silvermane, I think you're confusing disabling writes with turning off the depth test. If you just try to draw a decal on a polygon you get some of the pixels of the decal and some of the polygon because of rounding errors. They should both be the same depth, but they aren't. If you turn off the depth write then it writes the colors for both of them in the order you tell it to. Then you turn off the color buffer and write the depth values. You don't want to overlay the image you drew, but you want the correct depth values for the rest of your rendering.

Writing the depth values then the colors would pretty well require that you turn off depth testing for writing the colors. The only time you should turn off depth testing is when the stencil buffer is set to keep you from writing where you shouldn't.
Keys to success: Ability, ambition and opportunity.
Hey LilBudyWizer, yea i think i see what you're saying; FlyinDeath you can ignore my "EDIT:" solution - like it said, it was just a stab in the dark [wink]. I found a particle system example over at GLFW and they simply disable depth-write and draw the particles..
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website

This topic is closed to new replies.

Advertisement