Sign in to follow this  
robustpotato

DirectX Particles depth/blend problem

Recommended Posts

robustpotato    124
This is a topic Ive seen tonnes of threads about. im still not sure about how to go about solving it.

Ok i have a particle engine. the particle im emitting a smoke texture with alpha channel (just a circular gradient).

the emitter works fine. when i coded it i had no scene. just black background. I knew that i would encounter problems because i was disabling z buffer. but thats the only way i could blend them properly.

so i added some models. And im sure you know what happens. without the z buffer objects draw in the wrong order, with it, the blending doesnt work.

im using this as my blending parameters

device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);


im drawing my models 1st with z buffer on, and then turning it off for the particles.

problem is if there is a model positioned in front of the particles, the particles always draw in front when they should be behind it.

ive read about alpha testing or something but im not really sure how to do it, or what the theory is behind it. Anychance anyone can offer me a solution.

Share this post


Link to post
Share on other sites
MJP    19754
Typically for particles and other transparent geometry you want to leave z-testing on, but turn off z-writes. This way the particles don't occlude each other, but can still be occluded by opaque geometry.

Share this post


Link to post
Share on other sites
belfegor    2834
Alpha blending:

device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
DrawParticles();
device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);


I dont think you want alpha test on particles, but you can/should use it for
grass, fences...:

device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
device->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
device->SetRenderState(D3DRS_ALPHAREF, 150);//for example cut below alpha value of 150
DrawGrass();

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this