particle effects billboarding issue[C++]

Started by
3 comments, last by GeoKureli 11 years, 8 months ago
I've included pictures of whats happening as well as my "particle" image. The issue is i have a bunch a billboards showing a particle, but the alpha doesn't seem to be working in some cases, but it works in some cases. for instance if i stack the particleThis is an assignment I have for class

[source lang="cpp"]
// render the grid
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
render_grid();

d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);

// calculate elapsed time
static DWORD starting_point = GetTickCount(), time;
time = GetTickCount() - starting_point;
starting_point = GetTickCount();

addParticles();

// prepare particle for rendering
Node<Particle> *particle = particles.first;
D3DXMATRIX patMat;
float y = 0;
while (particle != NULL){
particle->value->run_particle(time / 1000.0f);
//particle->value->position.y = y;
if(particle->value->isDead()) particle->value->reset();
patMat = particle->value->set_particle(camx, camy, camz);
d3ddev->SetTransform(D3DTS_WORLD, &patMat);

particle->value->render_particle();

// --- NEXT
particle = particle->next;
y += .05;
}
d3ddev->EndScene();
[/source]
"particles" is a linked list, addparticles adds some particles to the list until the cap is reached, and when particle die they get reset. when i draw X amount of the particles at the same position, they all draw, andit looks like a brighter version of the particle, however when i move them randomly they loook like the images below heres the particle class:
[source lang="cpp"]#include <d3dx9.h>
float random(float max, bool negative = true){ if(negative) return cos(float(rand()))*max; /*else*/ return abs(cos(float(rand())))*max;}
// the Particle class
class Particle{
protected:
D3DXVECTOR3 velocity;
D3DXVECTOR3 acceleration;
float radius;
D3DXMATRIX matRotateX;
D3DXMATRIX matRotateY;

public:
D3DXVECTOR3 position;
Particle();
void reset();
void render_particle();
D3DXMATRIX set_particle(float camx, float camy, float camz);
void run_particle(float seconds);
bool isDead();
};

Particle :: Particle(){ reset(); }
void Particle :: reset(){
position = D3DXVECTOR3(random(0.5f), 0.0, random(0.5f));
velocity = D3DXVECTOR3(random(0.5f), random(1.0f, false)+0.5f, random(0.5f));
acceleration = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
radius = random(0.5f, false)+0.5f;
}

bool Particle :: isDead(){ return radius <= 0.0f; }

// this is the function that positions, rotates, scales and renders the particle
D3DXMATRIX Particle::set_particle(float camx, float camy, float camz)
{
// Before setting the world transform, do the intense mathematics!
// a. Calculate the Differences
static float difx, dify, difz;
difx = camx - position.x;
dify = camy - position.y;
difz = camz - position.z;
// ooh, intense!

// b. Calculate the Distances
static float FlatDist, TotalDist;
FlatDist = sqrt(difx * difx + difz * difz);
TotalDist = sqrt(FlatDist * FlatDist + dify * dify);

// c. Y Rotation
D3DXMatrixIdentity(&matRotateY);
matRotateY._11 = matRotateY._33 = difz / FlatDist; // cosY
matRotateY._31 = difx / FlatDist; // sinY
matRotateY._13 = -matRotateY._31; // -sinY

// d. X Rotation
D3DXMatrixIdentity(&matRotateX);
matRotateX._22 = matRotateX._33 = FlatDist / TotalDist; // cosX
matRotateX._32 = dify / TotalDist; // sinX
matRotateX._23 = -matRotateX._32; // -sinX

// e. Tranlation
static D3DXMATRIX matTranslate;
D3DXMatrixTranslation(&matTranslate, position.x, position.y, position.z);

// f. Scaling
static D3DXMATRIX matScale;
D3DXMatrixIdentity(&matScale);
matScale._11 = matScale._22 = matScale._33 = radius;

return matScale * matRotateX * matRotateY * matTranslate;
}

// this function updates the particle
void Particle::run_particle(float seconds)
{
velocity += acceleration * seconds;
position += velocity * seconds;
radius -= seconds;
return;
}[/source]
Advertisement
i think that has to do with the zbuffer and the blending state settings.
have a look at this article for the renderstate settings he uses for particles :)

here is the 'generic' settings

lpDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, true);
lpDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DBLEND_ONE);
lpDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DBLEND_ONE);
lpDevice->SetRenderState(D3DRENDERSTATE_LIGHTING, false);
lpDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false);


Hope that helps.

Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

You need to sort each particle Back-To-Front to have a correct render.
You don't need to do that for additive blending because each component is added to have the final color.
Use the bubble sort which is good for particle.
If you don't want to sort your particles back-to-front, you can use alpha-testing to reduce this effect:

dev->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x00000001);
dev->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
dev->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);[/quote]

This will skip completely transparent pixels from being written to the z-buffer.
Thanks guys, it works great, and your suggestions added a lot of flair to the image. the issue was with:
d3ddev->SetRenderState(D3DRS_ZWRITEENABLE, false);
and these really made it look great!
d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE);
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);

This topic is closed to new replies.

Advertisement