Jump to content
  • Advertisement
Sign in to follow this  

particle effects billboarding issue[C++]

This topic is 2124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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


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


// prepare particle for rendering
Node<Particle> *particle = particles.first;
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);


// --- NEXT
particle = particle->next;
y += .05;
"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{
D3DXVECTOR3 velocity;
D3DXVECTOR3 acceleration;
float radius;
D3DXMATRIX matRotateX;
D3DXMATRIX matRotateY;

D3DXVECTOR3 position;
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
matRotateY._11 = matRotateY._33 = difz / FlatDist; // cosY
matRotateY._31 = difx / FlatDist; // sinY
matRotateY._13 = -matRotateY._31; // -sinY

// d. X Rotation
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;
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;
}[/source] Edited by GeoKureli

Share this post

Link to post
Share on other sites
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_LIGHTING, false);
lpDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, false);

Hope that helps.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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_ALPHAFUNC, D3DCMP_GREATEREQUAL);[/quote]

This will skip completely transparent pixels from being written to the z-buffer.

Share this post

Link to post
Share on other sites
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);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!