room101/PARTICLE.CPP

78 lines
1.7 KiB
C++
Raw Permalink Normal View History

#include "particle.hpp"
#include "polygon.hpp"
void
ParticleEngineCreate(ParticleEngine* pe, int n, float x, float y, float z)
{
memset(pe, 0, sizeof(ParticleEngine));
VectorCreate(pe->origin, x, y, z);
pe->n = n;
pe->life = (int*) calloc(n, sizeof(int));
pe->pos = (Vector*) calloc(n, sizeof(Vector));
pe->vel = (Vector*) calloc(n, sizeof(Vector));
assert(pe->life && pe->pos && pe->vel);
}
void
ParticleEngineUpdate(ParticleEngine* pe)
{
for (int i = 0; i < pe->n; i++)
{
if (pe->life[i] == 0)
{
pe->life[i] = rand() % 1000;
v3dcpy(pe->pos[i], pe->origin);
VectorCreate(pe->vel[i],
(rand() % 100 - 50) / 100.0f,
(rand() % 100 - 50) / 100.0f,
(rand() % 100 - 50) / 100.0f);
}
else
{
pe->life[i]--;
}
VectorAddition(pe->pos[i], pe->pos[i], pe->vel[i]);
pe->vel[i][1] -= 0.001f;
}
}
void
ParticleEngineRender(ParticleEngine* pe, Camera* camera)
{
Vector tmp[1000];
Pixel** fb;
Pixel** zb;
unsigned x, y, z, width, height;
width = camera->fb->width;
height = camera->fb->height;
fb = camera->fb->pixels;
zb = camera->zb->pixels;
PolygonTransform(tmp, pe->pos, pe->n, camera->matrix);
PolygonProject(tmp, pe->n, camera);
for (int i = 0; i < pe->n; i++)
{
z = Round(tmp[i][2]);
if (z > 0)
{
x = Round(tmp[i][0]);
y = Round(tmp[i][1]);
if (x < width && y < height && z < zb[y][x])
{
fb[y][x] = PIXEL_GREEN;
}
}
}
}