diff --git a/main.c b/main.c index 9df2899..09fdfa0 100644 --- a/main.c +++ b/main.c @@ -46,7 +46,6 @@ int main() tFrame = time - tLast; tLast = time; - glClearColor(0.05f, 0.05f, 0.05f, 0); glClear(GL_COLOR_BUFFER_BIT); glfwGetFramebufferSize(window, &width, &height); @@ -62,12 +61,16 @@ int main() glColor3f(p->color->x, p->color->y, p->color->z); pos = p->position; - glBegin(GL_QUADS); + glBegin(GL_POINTS); glVertex3f(pos->x, pos->y, pos->z); - glVertex3f(pos->x+.1, pos->y, pos->z); - glVertex3f(pos->x+.1, pos->y-.1, pos->z); - glVertex3f(pos->x, pos->y-.1, pos->z); glEnd(); + + /*glBegin(GL_QUADS); + glVertex3f(pos->x, pos->y, pos->z); + glVertex3f(pos->x+.01, pos->y, pos->z); + glVertex3f(pos->x+.01, pos->y-.01, pos->z); + glVertex3f(pos->x, pos->y-.01, pos->z); + glEnd();*/ } glfwSwapBuffers(window); @@ -92,28 +95,22 @@ void calcPos(particle *p, float dt) void calcCol(particle *p) { - p->color->x = 0.5f; - p->color->y = 0.01f; - p->color->z = 0.9f; + p->color->x = 1; + p->color->y = 1; + p->color->z = 1; } /*************************************************************************************************************/ -/*************************/ -float rv() -{ - int i = rand()%2 ? -1 : 1; - return (float) i * rand() / RAND_MAX; -} -/*************************/ - void initRandomParticles(emitter *e) { for (int i = 0; i < e->pamount; i++) { vector3f *pos = initVector3f(e->position->x, e->position->y, e->position->z); - vector3f *dir = initVector3f(rv(), rv(), rv()); + vector3f *dir = initVector3f(((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX, + ((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX, + ((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX); vector3f *color = initVector3f(1, 1, 1); - (e->particles)[i] = initParticle(pos, dir, color, 100.f); + (e->particles)[i] = initParticle(pos, dir, color, rand() / 100.0f); } } diff --git a/particlesystem.c b/particlesystem.c index cb707e0..0644d4b 100644 --- a/particlesystem.c +++ b/particlesystem.c @@ -1,4 +1,5 @@ #include +#include #include "particlesystem.h" @@ -51,12 +52,34 @@ int updateParticles(float dt, particle_system *ps, CalculatePositionFunction cal for (int j = 0; j < e->pamount; j++) { p = (e->particles)[j]; - calculatePosition(p, dt); - calculateColor(p); + + if (p->age < 0) + { + resetParticle(e, p); + } + else + { + calculatePosition(p, dt); + calculateColor(p); + p->age -= 0.1f; + } } } } +void resetParticle(emitter *e, particle *p) +{ + p->position->x = e->position->x; + p->position->y = e->position->y; + p->position->z = e->position->z; + + p->direction->x = ((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX; + p->direction->y = ((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX; + p->direction->z = ((float) (rand()%2 ? -1 : 1) * rand()) / RAND_MAX; + + p->age = rand() / 100.0f; +} + /* * Draws particles */ diff --git a/particlesystem.h b/particlesystem.h index 1d8eb7e..adef1e3 100644 --- a/particlesystem.h +++ b/particlesystem.h @@ -68,6 +68,11 @@ particle_system *initParticleSystem(int eamount); */ int updateParticles(float dt, particle_system *particleSystem, CalculatePositionFunction calculatePosition, CalculateColorFunction calculateColor); +/* + * Resets a particle to seed at emitter's position + */ +void resetParticle(emitter *e, particle *p); + /* * Updates particle */