1
0

145 lines
3.1 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);
2019-12-28 13:54:53 +01:00
emitter *e = initEmitter(epos, 10000);
2019-12-20 14:24:52 +01:00
particle_system *ps = initParticleSystem(1);
(ps->emitters)[0] = e;
initRandomParticles(e);
2019-12-28 13:54:53 +01:00
double time, tFrame, tLast = 0;
int width, height, b = 0;
2019-12-18 18:18:55 +01:00
while (!glfwWindowShouldClose(window))
{
2019-12-28 13:54:53 +01:00
time = glfwGetTime();
tFrame = time - tLast;
tLast = time;
2019-12-18 18:18:55 +01:00
glfwGetFramebufferSize(window, &width, &height);
2019-12-20 14:24:52 +01:00
2019-12-28 13:54:53 +01:00
for (int i = 0; !b && i < e->pamount; i++)
2019-12-20 14:24:52 +01:00
{
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;
}
}
if (!b)
{
2019-12-28 13:54:53 +01:00
updateParticles((float) tFrame, ps);
2019-12-20 14:24:52 +01:00
}
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);
2019-12-28 13:54:53 +01:00
particle *p;
vector3f *pos;
2019-12-20 14:24:52 +01:00
for (int i = 0; i < e->pamount; i++)
{
2019-12-28 13:54:53 +01:00
p = (e->particles)[i];
glColor3f(p->color->x, p->color->y, p->color->z);
pos = p->position;
glVertex3f(pos->x, pos->y, pos->z);
2019-12-20 14:24:52 +01:00
}
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-28 13:54:53 +01:00
freeParticleSystem(ps);
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-28 13:54:53 +01:00
vector3f *color = initVector3f(255, 255, 255);
(e->particles)[i] = initParticle(pos, dir, color, 100.f);
2019-12-17 22:29:36 +01:00
}
}
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}");
}