PDA

View Full Version : OpenGL Particle Engine Demo



frenchfry164
11-24-2003, 09:03 PM
My first extremely basic demo! It just shows a brief particle engine. It shows 52 red particles move positively on the x and z axes, and then fade out when they get close to the screen. It is completely crappy, but I just hacked it together in like a half hour. No, I won't release source, because, as I said, it was a hack and I'd be embarrassed to show this crap.

edit: If you're browser gets confused and thinks the filename is particle and not particle engine.zip, open it with a zip file thing-a-ma-bob

PS: You need GLUT to get this to work.

Lurker
11-24-2003, 09:05 PM
Not really too much you can say about it WITHOUT the code...umm maybe good job :D ?

frenchfry164
11-24-2003, 09:07 PM
#include <cstdlib>
#include <ctime>
#include "particle.h"
#include <gl/gl.h>
inline float FloatRand(float MaxVal)
{
return float(rand()%100)/((100/MaxVal)+0.1);
}

ParticleEffect::ParticleEffect(int no, float x, float y, float z, float size,
float speedx, float speedy, float speedz, float life,
float fade, float r, float g, float b, float a, float perm,
bool forever)
{
srand(time(0));
particles = new Particle[no];
for (int i=0;i<no;i++)
{
particles[i].x = (x-(0.5*size))+FloatRand(size);
particles[i].y = (y-(0.5*size))+FloatRand(size);
particles[i].z = (z-(0.5*size))+FloatRand(size);
particles[i].life = (life-(0.5*fade))+FloatRand(fade);
particles[i].r = r;
particles[i].g = g;
particles[i].b = b;
particles[i].a = a;
particles[i].speedx = (speedx-(0.25*speedx))+FloatRand(0.25*speedx);
particles[i].speedy = (speedy-(0.25*speedy))+FloatRand(0.25*speedy);
particles[i].speedz = (speedz-(0.25*speedz))+FloatRand(0.25*speedz);
particles[i].fade = (fade-(0.15*fade))+FloatRand(fade*0.15);
particles[i].perimeter = perm;
}
count = no;
endless = forever;
this->x=x;
this->y=y;
this->z=z;
this->life=life;
this->fade=fade;
this->size=size;
this->r=r;
this->g=g;
this->b=b;
this->a=a;
this->speedx = speedx;
this->speedy = speedy;
this->speedz = speedz;
this->perimeter = perimeter;
}

void ParticleEffect::Render()
{
for (int i=0;i<count;i++)
{
if (particles[i].life <= 0)
{
particles[i].x = (x-(0.5*size))+FloatRand(size);
particles[i].y = (y-(0.5*size))+FloatRand(size);
particles[i].z = (z-(0.5*size))+FloatRand(size);
particles[i].life = (life-(0.5*fade))+FloatRand(fade);
particles[i].r = r;
particles[i].g = g;
particles[i].b = b;
particles[i].a = a;
particles[i].speedx = (speedx-(0.5*speedx))+FloatRand(0.5*speedx);
particles[i].speedy = (speedy-(0.5*speedy))+FloatRand(0.5*speedy);
particles[i].speedz = (speedz-(0.5*speedz))+FloatRand(0.5*speedz);
particles[i].fade = (fade-(fade))+FloatRand(fade);
particles[i].perimeter = perimeter;
}
glBegin(GL_QUADS);
glColor4f(particles[i].r, particles[i].g, particles[i].b, particles[i].a + (particles[i].life/2));
glVertex3f(particles[i].x - (particles[i].perimeter*0.5),
particles[i].y + (particles[i].perimeter*0.5), particles[i].z);
glVertex3f(particles[i].x + (particles[i].perimeter*0.5),
particles[i].y + (particles[i].perimeter*0.5), particles[i].z);
glVertex3f(particles[i].x + (particles[i].perimeter*0.5),
particles[i].y - (particles[i].perimeter*0.5), particles[i].z);
glVertex3f(particles[i].x - (particles[i].perimeter*0.5),
particles[i].y - (particles[i].perimeter*0.5), particles[i].z);
glEnd();

particles[i].life -= particles[i].fade;
particles[i].x += particles[i].speedx;
particles[i].y += particles[i].speedy;
particles[i].z += particles[i].speedz;
}
}

As you can see, it's a hack. It's extremely ugly. It makes me want to puke just seeing it.

Sebastiani
11-25-2003, 09:10 AM
>> PS: You need GLUT to get this to work.


Next time, include it with the package. ;)