From a9609ed770925c3d459e3a2c0b1d45b973721a21 Mon Sep 17 00:00:00 2001 From: Niklas Birk Date: Sat, 28 Dec 2019 13:54:53 +0100 Subject: [PATCH] several things --- main.c | 30 ++++++++++++++++++------------ particlesystem.c | 20 +++++++++++++------- particlesystem.h | 4 +++- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/main.c b/main.c index 7a6550b..b717672 100644 --- a/main.c +++ b/main.c @@ -31,44 +31,49 @@ int main() glfwMakeContextCurrent(window); vector3f *epos = initVector3f(0, 0, 0); - emitter *e = initEmitter(epos, 1000); + emitter *e = initEmitter(epos, 10000); particle_system *ps = initParticleSystem(1); (ps->emitters)[0] = e; initRandomParticles(e); + double time, tFrame, tLast = 0; + int width, height, b = 0; + while (!glfwWindowShouldClose(window)) { - int width, height; + time = glfwGetTime(); + tFrame = time - tLast; + tLast = time; glfwGetFramebufferSize(window, &width, &height); - int b = 0; - for (int i = 0; i < e->pamount; i++) + for (int i = 0; !b && i < e->pamount; i++) { vector3f *p = (e->particles)[i]->position; if (p->x > 1 || p->x < -1 || p->y > 1 || p->y < -1 || p->z > 1 || p->z < -1) { b=1; - break; } } if (!b) { - updateParticles(4000, ps); + updateParticles((float) tFrame, ps); } glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POINTS); + particle *p; + vector3f *pos; for (int i = 0; i < e->pamount; i++) { - - glColor3f((float) rand() / RAND_MAX, (float) rand() / RAND_MAX, (float) rand() / RAND_MAX); - vector3f *p = (e->particles)[i]->position; - glVertex3f(p->x, p->y, p->z); + p = (e->particles)[i]; + glColor3f(p->color->x, p->color->y, p->color->z); + pos = p->position; + glVertex3f(pos->x, pos->y, pos->z); } glEnd(); @@ -81,7 +86,7 @@ int main() glfwTerminate(); free(epos); - freeEmitter(e); + freeParticleSystem(ps); return 0; } @@ -105,7 +110,8 @@ void initRandomParticles(emitter *e) { vector3f *pos = initVector3f(e->position->x, e->position->y, e->position->z); vector3f *dir = initVector3f(rv(), rv(), rv()); - (e->particles)[i] = initParticle(pos, dir); + vector3f *color = initVector3f(255, 255, 255); + (e->particles)[i] = initParticle(pos, dir, color, 100.f); } } diff --git a/particlesystem.c b/particlesystem.c index c28ce2d..408165f 100644 --- a/particlesystem.c +++ b/particlesystem.c @@ -6,11 +6,13 @@ /* * Initializes a particle */ -particle *initParticle(vector3f *pos, vector3f *dir) +particle *initParticle(vector3f *pos, vector3f *dir, vector3f *color, float age) { particle *p = malloc(sizeof(particle)); p->position = pos; p->direction = dir; + p->color = color; + p->age = age; return p; } @@ -43,17 +45,21 @@ particle_system *initParticleSystem(int eamount) int updateParticles(float dt, particle_system *ps) { emitter *e; - float t = 1 / dt; + particle *p; for (int i = 0; i < ps->eamount; i++) { e = (ps->emitters)[i]; for (int j = 0; j < e->pamount; j++) { - vector3f *p = (e->particles)[j]->position; - vector3f *d = (e->particles)[j]->direction; - p->x += d->x * t; - p->y += d->y * t; - p->z += d->z * t; + p = (e->particles)[j]; + + p->position->x += p->direction->x * dt; + p->position->y += p->direction->y * dt; + p->position->z += p->direction->z * dt; + + p->color->x -= 0.25f; + p->color->y -= 0.25f; + p->color->z -= 0.25f; } } } diff --git a/particlesystem.h b/particlesystem.h index dbf6806..910208c 100644 --- a/particlesystem.h +++ b/particlesystem.h @@ -15,6 +15,8 @@ typedef struct particle { vector3f *position; vector3f *direction; + vector3f *color; + float age; } particle; /* @@ -39,7 +41,7 @@ typedef struct particle_system /* * Initializes a particle */ -particle *initParticle(vector3f *pos, vector3f *dir); +particle *initParticle(vector3f *pos, vector3f *dir, vector3f *color, float age); /* * Initializes an emitter