#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);
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].
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
Vector of fixed size.
Definition: FVector.h:17
const T & z() const
Read alias.
Definition: FVector.h:132
const T & y() const
Read alias.
Definition: FVector.h:118
const T & x() const
Read alias.
Definition: FVector.h:104
A mesh representing a 3D object.
Definition: Mesh.h:151
void setColor(Color col)
Set color.
void setOpacity(float opacity)
Set opacity.
RED GREEN BLUE color.
Definition: Color.h:26
#define srcPath(s)
Transform relative file path to absolute path.
Definition: Base.h:51
RGB< byte > Color
RGB<byte> alias.
Definition: Color.h:281
const Color RED
Predefined color.
Definition: Color.h:305
void closeWindow(Window w)
Close window.
void setBackGround(const Color &col)
Set ackground color of window.
void hideMesh(const Mesh &M, bool reinitCam=true)
Hide mesh.
FVector< float, 3 > FloatPoint3
Float space point.
Definition: Types.h:23
Window openWindow(int w, int h, const std::string &windowTitle="Imagine++", int x=-1, int y=-1)
New window for 2D graphics.
void setActiveWindow(Window w, int subWin=0)
Set active window.
Window openWindow3D(int w, int h, const std::string &windowTitle="Imagine++", int x=-1, int y=-1)
New window for 3D graphics.
void showMesh(const Mesh &M, bool reinitCam=true)
Show mesh.
FloatPoint3 FloatVector3
Float space vector.
Definition: Types.h:25
void drawString(int x, int y, const std::string &s, const AlphaColor &col, int fontSize=12, double alpha=0, bool italic=false, bool bold=false, bool underlined=false, bool xorMode=false)
String.
WindowInternal * Window
Handle to a display window.
Definition: Types.h:48
void endGraphics()
Terminate graphics application.
int anyClick()
Wait for mouse click in any window.
void milliSleep(int msec)
... Pause program for a certain period.
void setCamera(const DoublePoint3 &pos, const DoublePoint3 &dir, const DoubleVector3 &up)
Sets the camera pose.
Image< Color, dim > color(const Image< RGB< T >, dim > &I, const RGB< T > &m, const RGB< T > &M)
Color representation.
Definition: IO.h:322