#include <Imagine/Graphics.h>
#ifdef IMAGINE_OPENGL
struct ParticleParameters {
int amount;
int life;
float speed;
float weight;
int size;
int quality;
int* currentLife;
float radius;
float angle;
float sharpness;
};
float theta = 0.0;
class Particle {
private:
bool active;
ParticleParameters p;
void reset(int id);
void initialize();
void makeAxes();
public:
Particle(int amount=30, int life=50, float speed=0.1f, float weight=1,
float angle=0.2f, float sharpness=0,
void draw();
void start();
void stop();
};
Particle::Particle(
int amount,
int life,
float speed,
float weight,
int size,
int quality,
Color color,
FloatPoint3 source,
float radius,
FloatVector3 direction,
float angle,
float sharpness,
FloatVector3 forces)
{
p.amount = amount;
p.life = life;
p.speed = speed;
p.weight = weight;
p.size = size;
p.quality = quality + 1;
p.source = source;
p.radius = radius;
p.direction = direction;
p.angle = tan(angle);
p.sharpness = sharpness;
p.forces = forces;
initialize();
}
void Particle::start()
{
active = true;
for (
int i(0) ; i < p.amount ; i++)
showMesh(meshes[i],
false);
}
void Particle::stop()
{
active = false;
for (
int i(0) ; i < p.amount ; i++)
hideMesh(meshes[i],
false);
}
void Particle::makeAxes()
{
axe1 = p.direction / norm(p.direction);
axe3 = axe1^axe2;
}
void Particle::initialize()
{
makeAxes();
p.currentLife = new int[p.amount];
meshes =
new Mesh[p.amount];
for (int i(0) ; i < p.amount ; i++)
{
meshes[i] =
Mesh::Sphere(p.source + randomVector(p.radius), p.size, p.quality);
meshes[i].setColor(p.color);
reset(i);
p.currentLife[i] = 1 + rand()%p.life;
}
}
void Particle::draw()
{
if (active == true) for (int i(0) ; i < p.amount ; i++)
{
if (--p.currentLife[i] == 0) reset(i);
p.currentSpeed[i] += p.forces / p.weight;
p.currentPosition[i] += p.currentSpeed[i];
meshes[i] =
Mesh::Sphere(p.currentPosition[i], p.size, p.quality);
meshes[i].setColor(p.color);
meshes[i].setOpacity(p.currentLife[i] / (float)p.life);
}
}
float randomValue(float max)
{
int randomSign = (rand() % 2 == 0) ? 1 : -1;
return float(rand() % (int) (100 * max)) * randomSign / 100;
}
float randomAngle()
{
return ((float) (rand() % 1000)) / 100.0f;
}
{
if (value == 0)
return randomValue(value) * axe2 + randomValue(value) * axe3;
}
void Particle::reset(int id)
{
p.currentLife[id] = p.life;
p.currentPosition[id] = p.source + randomVector(p.radius);
float theta = randomAngle();
p.currentSpeed[id] = (axe1 + (p.angle * cos(theta)) * axe2 + (p.angle * sin(theta)) * axe3 + randomVector(p.sharpness));
p.currentSpeed[id] *= p.speed / norm(p.currentSpeed[id]);
}
void animateCamera(){
for (int i=0;i<200;i++){
theta +=0.1f;
}
}
int main(int argc, char* argv[])
{
(void)argc;(void)argv;
std::string fileName =
srcPath(
"bunny.obj");
std::cout << fileName << std::endl;
readMeshFromObjFile(mesh, fileName);
animateCamera();
Particle particles1;
particles1.start();
for (int i=0;i<200;i++)
{
particles1.draw();
}
particles1.stop();
return 0;
}
#else
int main(int argc, char** argv)
{
drawString(0,20,
"Imagine++ was configured with no OpenGL support",
RED);
return 0;
}
#endif