diff --git a/CMakeLists.txt b/CMakeLists.txt index dca805a..8ff0cbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,11 @@ project(Informatikprojekt C) set(CMAKE_C_STANDARD 11) -add_executable(Informatikprojekt main.c particlesystem.h particlesystem.c) +add_executable(Informatikprojekt main.c particlesystem.h particlesystem.c init.h) # GLFW add_subdirectory(./glfw-3.3) include_directories(./glfw-3.3/include) -target_link_libraries(Informatikprojekt -lOpenGL32 glfw ${GLFW_LIBRARIES}) \ No newline at end of file +target_link_libraries(Informatikprojekt -lOpenGL32 glfw ${GLFW_LIBRARIES}) + + diff --git a/init.h b/init.h new file mode 100644 index 0000000..e69de29 diff --git a/main.c b/main.c index 2afa190..7a6550b 100644 --- a/main.c +++ b/main.c @@ -20,7 +20,7 @@ int main() glfwSetErrorCallback(error_callback); - GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL); + GLFWwindow* window = glfwCreateWindow(1920, 1080, "My Title", NULL, NULL); if (!window) { @@ -30,32 +30,46 @@ int main() glfwMakeContextCurrent(window); + vector3f *epos = initVector3f(0, 0, 0); + emitter *e = initEmitter(epos, 1000); + particle_system *ps = initParticleSystem(1); + (ps->emitters)[0] = e; + + initRandomParticles(e); + while (!glfwWindowShouldClose(window)) { - float ratio; int width, height; glfwGetFramebufferSize(window, &width, &height); - ratio = width / (float) height; + + int b = 0; + for (int i = 0; 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); + } glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f); - glMatrixMode(GL_MODELVIEW); + glBegin(GL_POINTS); + for (int i = 0; i < e->pamount; i++) + { - glLoadIdentity(); - glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f); - - glBegin(GL_TRIANGLES); - glColor3f(1.f, 0.f, 0.f); - glVertex3f(-0.6f, -0.4f, 0.f); - glColor3f(0.f, 1.f, 0.f); - glVertex3f(0.6f, -0.4f, 0.f); - glColor3f(0.f, 0.f, 1.f); - glVertex3f(0.f, 0.6f, 0.f); + 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); + } glEnd(); glfwSwapBuffers(window); @@ -66,20 +80,8 @@ int main() glfwDestroyWindow(window); glfwTerminate(); - /*vector3f *epos = initVector3f(0, 0, 0); - emitter *e = initEmitter(epos, 10); - - initRandomParticles(e); - - printEmitter(e); - - for (int i = 0; i < e->pamount; i++) - { - free((e->particles)[i]); - } - free(epos); - free(e);*/ + freeEmitter(e); return 0; } @@ -89,12 +91,20 @@ void error_callback(int error, const char* description) fputs(description, stderr); } +/*************************/ +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((float) rand() / RAND_MAX, (float) rand() / RAND_MAX, (float) rand() / RAND_MAX); + vector3f *dir = initVector3f(rv(), rv(), rv()); (e->particles)[i] = initParticle(pos, dir); } } diff --git a/particlesystem.c b/particlesystem.c index b907d18..c28ce2d 100644 --- a/particlesystem.c +++ b/particlesystem.c @@ -40,9 +40,22 @@ particle_system *initParticleSystem(int eamount) /* * Updates particles */ -int updateParticles(float dt, particle_system *particleSystem) +int updateParticles(float dt, particle_system *ps) { - + emitter *e; + float t = 1 / dt; + 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; + } + } } /* @@ -64,4 +77,37 @@ vector3f *initVector3f(float x, float y, float z) vector->y = y; vector->z = z; return vector; -} \ No newline at end of file +} + +/* + * Frees a given emitter and all corresponding particles + */ +void freeEmitter(emitter *e) +{ + for (int j = 0; j < e->pamount; j++) + { + free((e->particles)[j]); + } + + free(e); +} + +/* + * Frees all emitters within a particle system + */ +void freeEmitters(particle_system *ps) +{ + for (int i = 0; i < ps->eamount; i++) + { + freeEmitter((ps->emitters)[i]); + } +} + +/* + * Frees all emitters and particles within a particle system + */ +void freeParticleSystem(particle_system *ps) +{ + freeEmitters(ps); + free(ps); +} diff --git a/particlesystem.h b/particlesystem.h index 5f99776..dbf6806 100644 --- a/particlesystem.h +++ b/particlesystem.h @@ -67,4 +67,17 @@ int drawParticles(particle_system *particleSystem); */ vector3f *initVector3f(float x, float y, float z); +/* + * Frees an given emitter with all particles + */ +void freeEmitter(emitter *e); +/* + * Frees all emitters within an particle system + */ +void freeEmitters(particle_system *ps); + +/* + * Frees all emitter and particles within a particle system + */ +void freeParticleSystem(particle_system *ps); \ No newline at end of file