2026-01-21 23:19:50 -06:00
|
|
|
#include "pxl8_lights.h"
|
|
|
|
|
#include "pxl8_mem.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
struct pxl8_lights {
|
|
|
|
|
pxl8_light* data;
|
|
|
|
|
u32 capacity;
|
|
|
|
|
u32 count;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pxl8_lights* pxl8_lights_create(u32 capacity) {
|
|
|
|
|
if (capacity > PXL8_LIGHTS_MAX) capacity = PXL8_LIGHTS_MAX;
|
|
|
|
|
|
|
|
|
|
pxl8_lights* lights = pxl8_calloc(1, sizeof(pxl8_lights));
|
|
|
|
|
if (!lights) return NULL;
|
|
|
|
|
|
|
|
|
|
lights->data = pxl8_calloc(capacity, sizeof(pxl8_light));
|
|
|
|
|
if (!lights->data) {
|
|
|
|
|
pxl8_free(lights);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lights->capacity = capacity;
|
|
|
|
|
lights->count = 0;
|
|
|
|
|
|
|
|
|
|
return lights;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pxl8_lights_destroy(pxl8_lights* lights) {
|
|
|
|
|
if (!lights) return;
|
|
|
|
|
pxl8_free(lights->data);
|
|
|
|
|
pxl8_free(lights);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pxl8_lights_add(pxl8_lights* lights, f32 x, f32 y, f32 z, u8 r, u8 g, u8 b, u8 intensity, f32 radius) {
|
|
|
|
|
if (!lights || lights->count >= lights->capacity) return;
|
|
|
|
|
|
|
|
|
|
f32 radius_sq = radius * radius;
|
|
|
|
|
pxl8_light* l = &lights->data[lights->count++];
|
|
|
|
|
l->position.x = x;
|
|
|
|
|
l->position.y = y;
|
|
|
|
|
l->position.z = z;
|
|
|
|
|
l->r = r;
|
|
|
|
|
l->g = g;
|
|
|
|
|
l->b = b;
|
|
|
|
|
l->intensity = intensity;
|
|
|
|
|
l->radius = radius;
|
|
|
|
|
l->radius_sq = radius_sq;
|
|
|
|
|
l->inv_radius_sq = radius_sq > 0.0f ? 1.0f / radius_sq : 0.0f;
|
2026-02-02 17:48:25 -06:00
|
|
|
|
|
|
|
|
l->constant = 1.0f;
|
|
|
|
|
if (radius <= 7.0f) {
|
|
|
|
|
l->linear = 0.7f;
|
|
|
|
|
l->quadratic = 1.8f;
|
|
|
|
|
} else if (radius <= 13.0f) {
|
|
|
|
|
l->linear = 0.35f;
|
|
|
|
|
l->quadratic = 0.44f;
|
|
|
|
|
} else if (radius <= 20.0f) {
|
|
|
|
|
l->linear = 0.22f;
|
|
|
|
|
l->quadratic = 0.20f;
|
|
|
|
|
} else if (radius <= 32.0f) {
|
|
|
|
|
l->linear = 0.14f;
|
|
|
|
|
l->quadratic = 0.07f;
|
|
|
|
|
} else if (radius <= 50.0f) {
|
|
|
|
|
l->linear = 0.09f;
|
|
|
|
|
l->quadratic = 0.032f;
|
|
|
|
|
} else if (radius <= 65.0f) {
|
|
|
|
|
l->linear = 0.07f;
|
|
|
|
|
l->quadratic = 0.017f;
|
|
|
|
|
} else if (radius <= 100.0f) {
|
|
|
|
|
l->linear = 0.045f;
|
|
|
|
|
l->quadratic = 0.0075f;
|
|
|
|
|
} else if (radius <= 160.0f) {
|
|
|
|
|
l->linear = 0.027f;
|
|
|
|
|
l->quadratic = 0.0028f;
|
|
|
|
|
} else {
|
|
|
|
|
l->linear = 0.022f;
|
|
|
|
|
l->quadratic = 0.0019f;
|
|
|
|
|
}
|
2026-01-21 23:19:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pxl8_lights_clear(pxl8_lights* lights) {
|
|
|
|
|
if (!lights) return;
|
|
|
|
|
lights->count = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u32 pxl8_lights_count(const pxl8_lights* lights) {
|
|
|
|
|
return lights ? lights->count : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pxl8_light* pxl8_lights_data(const pxl8_lights* lights) {
|
|
|
|
|
return lights ? lights->data : NULL;
|
|
|
|
|
}
|