diff --git a/src/light.c b/src/light.c new file mode 100644 index 0000000..ff6f10b --- /dev/null +++ b/src/light.c @@ -0,0 +1,72 @@ +#include "light.h" +#include "array.h" +#include + +static struct Light* light_list; +static int* empty_indices; + +struct Light* light_get(int index) +{ + struct Light* light = NULL; + if(index > -1 && index < array_len(light_list)) + light = &light_list[index]; + return light; +} + +struct Light* light_get_all(void) +{ + return light_list; +} + +void light_init(void) +{ + light_list = array_new(struct Light); + empty_indices = array_new(int); +} + +void light_cleanup(void) +{ + for(int i = 0; i < array_len(light_list); i++) + light_remove(i); + array_free(light_list); + array_free(empty_indices); +} + +void light_remove(int index) +{ + if(index > -1 && index < array_len(light_list)) + { + light_list[index].valid = 0; + array_push(empty_indices, index, int); + } +} + +int light_create(int node, int light_type) +{ + int index = -1; + struct Light* new_light = NULL; + if(array_len(empty_indices) > 0) + { + index = *array_get_last(empty_indices, int); + new_light = &light_list[index]; + array_pop(empty_indices); + } + else + { + new_light = array_grow(light_list, struct Light); + index = array_len(light_list) - 1; + } + new_light->node = node; + new_light->valid = 1; + new_light->cast_shadow = 0; + vec4_fill(&new_light->color, 1.f, 1.f, 1.f, 1.f); + new_light->depth_bias = 0.0005f; + new_light->type = light_type; + new_light->pcf_enabled = 0; + new_light->intensity = 1.f; + new_light->falloff = 1.5f; + new_light->outer_angle = TO_RADIANS(30.f); + new_light->inner_angle = TO_RADIANS(20.f); + new_light->radius = 30; + return index; +} diff --git a/src/light.h b/src/light.h new file mode 100644 index 0000000..89244a8 --- /dev/null +++ b/src/light.h @@ -0,0 +1,41 @@ +#ifndef LIGHT_H +#define LIGHT_H + +#include "num_types.h" +#include "linmath.h" + +#define MAX_SHADOWMAPS 4 + +enum LightType +{ + LT_SPOT = 0, + LT_DIR, + LT_POINT +}; + +struct Light +{ + float outer_angle; + float inner_angle; + float falloff; + float intensity; + vec4 color; + int32 node; + uint8 cast_shadow; + uint8 pcf_enabled; + uint8 valid; + int type; + int radius; + int shadow_map[4]; + float depth_bias; +}; + +struct Light* light_get(int index); +struct Light* light_get_all(void); +void light_init(void); +void light_cleanup(void); +void light_remove(int index); +int light_create(int node, int light_type); +void light_set_radius(struct Light* light, int radius); + +#endif diff --git a/src/num_types.h b/src/num_types.h index 2559886..7e8ad85 100644 --- a/src/num_types.h +++ b/src/num_types.h @@ -3,10 +3,12 @@ #include +typedef int8_t int8; typedef int32_t int32; typedef int64_t int64; typedef unsigned int uint; typedef uint32_t uint32; typedef uint64_t uint64; +typedef uint8_t uint8; #endif