1
0

139 lines
2.9 KiB
C
Raw Normal View History

2019-12-16 19:12:24 +01:00
#include <stdio.h>
2019-12-17 22:29:36 +01:00
#include <malloc.h>
#include <stdlib.h>
2019-12-18 18:18:55 +01:00
#include <GLFW/glfw3.h>
2019-12-17 22:29:36 +01:00
#include "particlesystem.h"
char *printVector(vector3f *v);
void printParticle(particle *v);
void printEmitter(emitter *e);
void initRandomParticles(emitter *e);
2019-12-18 18:18:55 +01:00
void error_callback(int error, const char* description);
2019-12-16 19:12:24 +01:00
int main()
{
2019-12-18 18:18:55 +01:00
if (!glfwInit())
{
exit(EXIT_FAILURE);
}
glfwSetErrorCallback(error_callback);
2019-12-20 14:24:52 +01:00
GLFWwindow* window = glfwCreateWindow(1920, 1080, "My Title", NULL, NULL);
2019-12-18 18:18:55 +01:00
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
2019-12-20 14:24:52 +01:00
vector3f *epos = initVector3f(0, 0, 0);
emitter *e = initEmitter(epos, 1000);
particle_system *ps = initParticleSystem(1);
(ps->emitters)[0] = e;
initRandomParticles(e);
2019-12-18 18:18:55 +01:00
while (!glfwWindowShouldClose(window))
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
2019-12-20 14:24:52 +01:00
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);
}
2019-12-18 18:18:55 +01:00
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
2019-12-20 14:24:52 +01:00
glBegin(GL_POINTS);
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);
}
2019-12-18 18:18:55 +01:00
glEnd();
glfwSwapBuffers(window);
glfwPollEvents();
}
//END
glfwDestroyWindow(window);
glfwTerminate();
2019-12-17 22:29:36 +01:00
free(epos);
2019-12-20 14:24:52 +01:00
freeEmitter(e);
2019-12-18 18:18:55 +01:00
2019-12-16 19:12:24 +01:00
return 0;
}
2019-12-17 22:29:36 +01:00
2019-12-18 18:18:55 +01:00
void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
2019-12-20 14:24:52 +01:00
/*************************/
float rv()
{
int i = rand()%2 ? -1 : 1;
return (float) i * rand() / RAND_MAX;
}
/*************************/
2019-12-17 22:29:36 +01:00
void initRandomParticles(emitter *e)
{
for (int i = 0; i < e->pamount; i++)
{
vector3f *pos = initVector3f(e->position->x, e->position->y, e->position->z);
2019-12-20 14:24:52 +01:00
vector3f *dir = initVector3f(rv(), rv(), rv());
2019-12-17 22:29:36 +01:00
(e->particles)[i] = initParticle(pos, dir);
}
}
char *printVector(vector3f *v)
{
char *c = malloc(100);
sprintf(c, "(%f, %f, %f)", v->x, v->y, v->z);
return c;
}
void printParticle(particle *v)
{
printf(" Particle {\n");
printf(" position = %s", printVector(v->position));
printf("\n direction = %s", printVector(v->direction));
printf("\n }");
}
void printEmitter(emitter *e)
{
printf("Emitter {\n");
for (int i = 0; i < e->pamount; i++)
{
printParticle((e->particles)[i]);
printf("\n");
}
printf("\n}");
}