struct CFVFVertexParticles
{
float x, y, z;
DWORD color;
};
struct CParticle
{
int xPos, yPos, r1, g1, b1, r2,
g2, b2, LifeTime, VelX, VelY, Speed;
bool Active;
};
CParticle Particles[300]; // 300 = max particles
IDirect3DVertexBuffer8 *ParticleVertBuffer;
CFVFVertexParticles *ParticleVerts;
// Provides fade effect NOTE: Cow_in_the_well gave me this...
// so dont blame me if its wrong ;)
inline float Interpolate(float f1, float f2, float lerp)
{
return f2+((f1-f2)*lerp);
}
// Called at app init...
ParticleVertBuffer = 0;
HRESULT hr = pd3dDevice->CreateVertexBuffer(300*sizeof(CFVFVertexParticles), D3DUSAGE_DYNAMIC, (D3DFVF_XYZ | D3DFVF_DIFFUSE), D3DPOOL_DEFAULT, &ParticleVertBuffer);
if(!ParticleVertBuffer)
{
Log("ERROR: ParticleVertBuffer == NULL");
Log("D3D error returned: ", 0);
Log(DXGetErrorString8(hr));
return false;
}
ParticleVertBuffer->Lock(0, 0, (BYTE**)&ParticleVerts, 0);
for(int g = 0; g < 300; g++)
{
ParticleVerts[g].x = 0.0f;
ParticleVerts[g].y = 0.0f;
ParticleVerts[g].color = 0xffffffff;
ParticleVerts[g].z = 1.0f;
Particles[g].Active = false;
}
ParticleVertBuffer->Unlock();
// Game code...
// r1 etc is first color and r2 etc is color to fade to
void CJester::StartParticle(int xPos, int yPos, int red1, int green1, int blue1, int red2, int green2, int blue2, int LifeTime, int angle, int Speed)
{
for(int i = 0; i < 300; i++)
{
if(!Particles[i].Active)
{
Particles[i].Active = true;
Particles[i].r1 = red1;
Particles[i].g1 = green1;
Particles[i].b1 = blue1;
Particles[i].r2 = red2;
Particles[i].g2 = green2;
Particles[i].b2 = blue2;
Particles[i].VelX = 200; // angle yet to be implimented...
Particles[i].VelY = 50; // so these are temp vals
Particles[i].LifeTime = LifeTime;
Particles[i].Speed = Speed;
Particles[i].xPos = xPos - 400; // switching co-ordinate systems...
Particles[i].yPos = yPos - 300;
}
}
}
// And here is where the work is done...
void CJester::ProcessParticles()
{
int numActive = 0;
int particleIndex = 0;
for(int i = 0; i < 300; i++) if(Particles[i].Active) numActive++;
ParticleVertBuffer->Lock(0, numActive*sizeof(CFVFVertexParticles), (BYTE**)&ParticleVerts, 0);
for(int r = 0; r < 300; r++)
{
if(Particles[r].Active)
{
if(timeGetTime() > Particles[r].LifeTime) Particles[r].Active = false;
Particles[r].yPos += Particles[r].VelY*(1.0f/Jester->GetFPS());
Particles[r].xPos += Particles[r].VelX*(1.0f/Jester->GetFPS());
int a = Interpolate(0, 255, (timeGetTime()-Particles[r].LifeTime)/timeGetTime());
int red = Interpolate(Particles[r].r1, Particles[r].r2, (timeGetTime()-Particles[r].LifeTime)/timeGetTime());
int g = Interpolate(Particles[r].g1, Particles[r].g2, (timeGetTime()-Particles[r].LifeTime)/timeGetTime());
int b = Interpolate(Particles[r].b1, Particles[r].b2, (timeGetTime()-Particles[r].LifeTime)/timeGetTime());
ParticleVerts[particleIndex].color = D3DCOLOR_ARGB(a,red,g,b);
ParticleVerts[particleIndex].x = Particles[r].xPos;
ParticleVerts[particleIndex].y = Particles[r].yPos;
particleIndex++;
}
}
ParticleVertBuffer->Unlock();
pd3dDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_DIFFUSE);
pd3dDevice->SetStreamSource(0, ParticleVertBuffer, sizeof(CFVFVertexParticles));
pd3dDevice->DrawPrimitive(D3DPT_POINTLIST, 0, numActive);
}
Particle system troubles...
My particles dont appear to be showing up... can anyone help?
Edit: im in 16 bit color btw... 565 format... if that helps...
CEO Platoon Studios
[edited by - elis-cool on August 18, 2002 1:28:29 AM]
Ok, tryed that and it didnt help... I outputed the numActive variable onscreen, so I know that there are particles bieng added etc just not rendered... also, the framerate seems to half when theres particles there... so I dont know even more now...
CEO Platoon Studios
CEO Platoon Studios
i think what the problem is, if your doing particles with vertex buffers, you need to lock and fill it every frame. that would be bad on fps. i think you need point sprites or something.
Yeah but thats why I specifyed D3DUSAGE_DYNAMIC, it hasnt seemed to help though...
CEO Platoon Studios
CEO Platoon Studios
This doesn''t help, but why not just have one particle in the VB and render it multiple times by changing the transform matrix?
Steve
DirectX Programmer
Soon to be the new Bill Gates
Steve
DirectX Programmer
Soon to be the new Bill Gates
Well I supose... but I would rather have it so I can have particles of diferent colors etc... and fade them... how are u guys doing yours?
CEO Platoon Studios
CEO Platoon Studios
Make sure you turn hardware lighting off when rendering the particles. If I''m not mistaken primitives that don''t have vertex normals are rendered black with lighting.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement