1
0

Init particle creation

This commit is contained in:
Niklas Birk 2019-12-17 22:29:36 +01:00
parent 8079f1809f
commit 95e874e74e
4 changed files with 164 additions and 19 deletions

View File

@ -3,4 +3,4 @@ project(Informatikprojekt C)
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD 11)
add_executable(Informatikprojekt main.c particlesystem.h) add_executable(Informatikprojekt main.c particlesystem.h particlesystem.c)

61
main.c
View File

@ -1,7 +1,66 @@
#include <stdio.h> #include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include "particlesystem.h"
char *printVector(vector3f *v);
void printParticle(particle *v);
void printEmitter(emitter *e);
void initRandomParticles(emitter *e);
int main() int main()
{ {
printf("Hello, World!\n"); 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);
return 0; return 0;
} }
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);
(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}");
}

65
particlesystem.c Normal file
View File

@ -0,0 +1,65 @@
#include "particlesystem.h"
#include <malloc.h>
/*
* Initializes a particle
*/
particle *initParticle(vector3f *pos, vector3f *dir)
{
particle *p = malloc(sizeof(particle));
p->position = pos;
p->direction = dir;
return p;
}
/*
* Initializes an emitter
*/
emitter *initEmitter(vector3f *pos, int pamount)
{
emitter *e = malloc(sizeof(emitter));
e->position = pos;
e->particles = malloc(sizeof(particle *) * pamount);
e->pamount = pamount;
return e;
}
/*
* Initializes a particle system
*/
particle_system *initParticleSystem(int eamount)
{
particle_system *ps = malloc(sizeof(particle_system));
ps->emitters = malloc(sizeof(emitter *) * eamount);
ps->eamount = eamount;
return ps;
}
/*
* Updates particles
*/
int updateParticles(float dt, particle_system *particleSystem)
{
}
/*
* Draws particles
*/
int drawParticles(particle_system *particleSystem)
{
}
/*
* Initializes a vector
* For that it allocates memory that must be freed later
*/
vector3f *initVector3f(float x, float y, float z)
{
vector3f *vector = malloc(sizeof(vector3f));
vector->x = x;
vector->y = y;
vector->z = z;
return vector;
}

View File

@ -1,49 +1,70 @@
/* /*
* A vector with three floats * A vector with three floats
*/ */
struct vector3f typedef struct vector3f
{ {
float x; float x;
float y; float y;
float z; float z;
}; } vector3f;
/* /*
* A particle has a position and a direction * A particle has a position and a direction
*/ */
struct particle typedef struct particle
{ {
struct vector3f position; vector3f *position;
struct vector3f direction; vector3f *direction;
}; } particle;
/* /*
* An emitter has a position and contains an array of particles * An emitter has a position and contains an array of particles
*/ */
struct emitter typedef struct emitter
{ {
struct vector3f position; vector3f *position;
struct particle *particles; particle **particles;
int pamount; int pamount;
}; } emitter;
/* /*
* A particle system consists of one or more emitter * A particle system consists of one or more emitter
*/ */
struct particle_system typedef struct particle_system
{ {
struct emitter *emitters; emitter **emitters;
int eamount; int eamount;
}; } particle_system;
/* /*
* * Initializes a particle
*/ */
int initParticle(struct vector3f pos, struct vector3f dir); particle *initParticle(vector3f *pos, vector3f *dir);
/* /*
* * Initializes an emitter
*/ */
int initVector3f(int x, int y, int z); emitter *initEmitter(vector3f *pos, int pamount);
/*
* Initializes a particle system
*/
particle_system *initParticleSystem(int eamount);
/*
* Updates particle
*/
int updateParticles(float dt, particle_system *particleSystem);
/*
* Updates particle
*/
int drawParticles(particle_system *particleSystem);
/*
* Initializes a vector
* For that it allocates memory that must be freed later
*/
vector3f *initVector3f(float x, float y, float z);