parent
ede02d950c
commit
8a44bdf4b1
@ -1,31 +0,0 @@ |
||||
#ifndef components_H |
||||
#define components_H |
||||
|
||||
enum Component |
||||
{ |
||||
C_TRANSFORM = 0, |
||||
C_MODEL, |
||||
C_CAMERA, |
||||
C_LIGHT, |
||||
C_SOUND_SOURCE, |
||||
C_RIGIDBODY, |
||||
NUM_COMPONENTS |
||||
}; |
||||
|
||||
inline static const char* comp_to_str(enum Component component) |
||||
{ |
||||
const char* str = 0; |
||||
switch(component) |
||||
{ |
||||
case C_TRANSFORM: str = "TRANSFORM"; break; |
||||
case C_MODEL: str = "MODEL"; break; |
||||
case C_CAMERA: str = "CAMERA"; break; |
||||
case C_LIGHT: str = "LIGHT"; break; |
||||
case C_SOUND_SOURCE: str = "SOUND_SOURCE"; break; |
||||
case C_RIGIDBODY: str = "RIGIDBODY"; break; |
||||
case NUM_COMPONENTS: str = "NUM_COMPONENTS"; break; |
||||
} |
||||
return str; |
||||
} |
||||
|
||||
#endif |
@ -1,31 +1,123 @@ |
||||
#ifndef entity_H |
||||
#define entity_H |
||||
#ifndef ENTITY_H |
||||
#define ENTITY_H |
||||
|
||||
#include "components.h" |
||||
#include "linmath.h" |
||||
#include "num_types.h" |
||||
|
||||
struct Material_Param; |
||||
|
||||
enum Entity_Type |
||||
{ |
||||
ET_PLAYER, |
||||
ET_ROOT, |
||||
ET_CAMERA, |
||||
ET_LIGHT, |
||||
ET_STATIC_MESH, |
||||
ET_SOUND_SOURCE, |
||||
ET_MAX |
||||
}; |
||||
|
||||
enum LightType |
||||
{ |
||||
LT_SPOT = 0, |
||||
LT_DIR, |
||||
LT_POINT, |
||||
LT_INVALID, |
||||
LT_MAX |
||||
}; |
||||
|
||||
struct Transform |
||||
{ |
||||
vec3 position; |
||||
vec3 scale; |
||||
quat rotation; |
||||
mat4 trans_mat; |
||||
int parent; |
||||
int* children; |
||||
bool is_modified; |
||||
}; |
||||
|
||||
struct Model |
||||
{ |
||||
int geometry_index; |
||||
int material; |
||||
struct Material_Param* material_params; |
||||
}; |
||||
|
||||
struct Sound_Source |
||||
{ |
||||
uint al_source_handle; |
||||
uint al_buffer_handle; |
||||
bool active; |
||||
}; |
||||
|
||||
struct Camera |
||||
{ |
||||
mat4 proj_mat; |
||||
mat4 view_mat; |
||||
mat4 view_proj_mat; |
||||
float fov; |
||||
float aspect_ratio; |
||||
float nearz; |
||||
float farz; |
||||
bool ortho; |
||||
int fbo; |
||||
int render_tex; |
||||
int depth_tex; |
||||
vec4 clear_color; |
||||
vec4 frustum[6]; |
||||
bool resizeable; |
||||
}; |
||||
|
||||
struct Light |
||||
{ |
||||
float outer_angle; |
||||
float inner_angle; |
||||
float falloff; |
||||
float intensity; |
||||
vec3 color; |
||||
bool cast_shadow; |
||||
bool pcf_enabled; |
||||
bool valid;
|
||||
int type; |
||||
int radius;
|
||||
int shadow_map[4]; |
||||
float depth_bias; |
||||
}; |
||||
|
||||
struct Entity |
||||
{ |
||||
int node; |
||||
char* name; |
||||
char* tag; |
||||
int components[NUM_COMPONENTS]; |
||||
int parent; |
||||
int* children; |
||||
int is_listener; |
||||
int id; |
||||
int type; |
||||
char* name; |
||||
bool is_listener; /* TODO: Replace all booleans with flags */ |
||||
bool marked_for_deletion; |
||||
bool renderable; |
||||
struct Transform transform; |
||||
|
||||
union |
||||
{ |
||||
struct |
||||
{ |
||||
struct Model model; |
||||
int health; |
||||
} Player; |
||||
|
||||
struct Model model; |
||||
struct Camera camera; |
||||
struct Light light; |
||||
struct Sound_Source sound_source; |
||||
}; |
||||
}; |
||||
|
||||
void entity_init(void); |
||||
void entity_cleanup(void); |
||||
void entity_remove(int index); |
||||
struct Entity* entity_create(const char* name, const char* tag); |
||||
void entity_post_update(void); |
||||
struct Entity* entity_create(const char* name, const int type, int parent_id); |
||||
struct Entity* entity_get(int index); |
||||
struct Entity* entity_find(const char* name); |
||||
struct Entity* entity_get_all(void); |
||||
struct Entity* entity_get_parent(int node); |
||||
int entity_component_remove(struct Entity* entity, enum Component component); |
||||
void* entity_component_get(struct Entity* entity, enum Component component); |
||||
void* entity_component_add(struct Entity* entity, enum Component component, ...); |
||||
int entity_has_component(struct Entity* entity, enum Component component); |
||||
void entity_sync_components(struct Entity* entity); |
||||
|
||||
#endif |
||||
|
@ -1,117 +1,70 @@ |
||||
#include "light.h" |
||||
#include "log.h" |
||||
#include "array.h" |
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
#include "entity.h" |
||||
|
||||
static struct Light* light_list; |
||||
static int* empty_indices; |
||||
static int* valid_light_indices; |
||||
static int max_lights; |
||||
#include <assert.h> |
||||
|
||||
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; |
||||
} |
||||
static int* light_list = NULL; |
||||
|
||||
void light_init(void) |
||||
{ |
||||
max_lights = 128; |
||||
light_list = array_new_cap(struct Light, max_lights); |
||||
for(int i = 0; i < max_lights; i++) light_list[i].valid = 0; |
||||
empty_indices = array_new(int); |
||||
valid_light_indices = array_new_cap(int, max_lights); |
||||
memset(valid_light_indices, -1, max_lights); |
||||
light_list = 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); |
||||
array_free(valid_light_indices); |
||||
} |
||||
|
||||
void light_remove(int index) |
||||
|
||||
void light_create(struct Light* light, int entity_id, int light_type) |
||||
{ |
||||
if(index > -1 && index < array_len(light_list)) |
||||
{ |
||||
light_list[index].valid = 0; |
||||
array_push(empty_indices, index, int); |
||||
} |
||||
assert(light && entity_id > -1); |
||||
|
||||
light->valid = true; |
||||
light->cast_shadow = 0; |
||||
light->depth_bias = 0.0005f; |
||||
light->type = light_type; |
||||
light->pcf_enabled = false; |
||||
light->intensity = 1.f; |
||||
light->falloff = 1.5f; |
||||
light->outer_angle = TO_RADIANS(30.f); |
||||
light->inner_angle = TO_RADIANS(20.f); |
||||
light->radius = 20; |
||||
vec3_fill(&light->color, 1.f, 1.f, 1.f); |
||||
int* new_index = array_grow(light_list, int); |
||||
*new_index = entity_id; |
||||
} |
||||
|
||||
int light_create(int node, int light_type) |
||||
void light_destroy(struct Light* light, int entity_id) |
||||
{ |
||||
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 |
||||
assert(light && entity_id > -1); |
||||
|
||||
int index_to_remove = -1; |
||||
for(int i = 0; i < array_len(light_list); i++) |
||||
{ |
||||
for(index = 0; index < max_lights; index++) |
||||
{ |
||||
if(!light_list[index].valid) |
||||
break; |
||||
} |
||||
|
||||
if(index == max_lights - 1) |
||||
if(light_list[i] == entity_id) |
||||
{ |
||||
index = -1; |
||||
log_warning("Max light limit(%d) reached, cannot add light", max_lights); |
||||
return index; |
||||
index_to_remove = i; |
||||
break; |
||||
} |
||||
|
||||
new_light = &light_list[index]; |
||||
} |
||||
new_light->node = node; |
||||
new_light->valid = 1; |
||||
new_light->cast_shadow = 0; |
||||
vec3_fill(&new_light->color, 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 = 20; |
||||
return index; |
||||
} |
||||
|
||||
int light_get_max_lights(void) |
||||
{ |
||||
return max_lights; |
||||
if(index_to_remove != -1) array_remove_at(light_list, index_to_remove); |
||||
light->valid = false; |
||||
light->cast_shadow = 0; |
||||
light->depth_bias = 0.f; |
||||
light->type = LT_INVALID; |
||||
light->pcf_enabled = false; |
||||
light->intensity = 10.f; |
||||
light->falloff = 0.f; |
||||
light->outer_angle = 0.f; |
||||
light->inner_angle = 0.f; |
||||
light->radius = 0.f; |
||||
vec3_fill(&light->color, 1.f, 0.f, 1.f); |
||||
} |
||||
|
||||
int* light_get_valid_indices(int* valid_light_count) |
||||
int* light_get_valid_indices(int* out_count) |
||||
{ |
||||
/* First, get all the valid(active) lights, then sort them in the
|
||||
order directional, point, spot |
||||
*/ |
||||
int light_count = 0; |
||||
for(int i = 0; i < array_len(light_list); i++) |
||||
{ |
||||
if(light_list[i].valid) |
||||
{ |
||||
valid_light_indices[light_count] = i; |
||||
light_count++; |
||||
} |
||||
} |
||||
*valid_light_count = light_count; |
||||
|
||||
return valid_light_indices; |
||||
*out_count = array_len(light_list); |
||||
return light_list; |
||||
} |
||||
|
@ -1,43 +1,14 @@ |
||||
#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; |
||||
vec3 color; |
||||
int32 node; |
||||
uint8 cast_shadow; |
||||
uint8 pcf_enabled; |
||||
uint8 valid;
|
||||
int type; |
||||
int radius;
|
||||
int shadow_map[4]; |
||||
float depth_bias; |
||||
}; |
||||
struct Light; |
||||
|
||||
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); |
||||
int light_get_max_lights(void); |
||||
int* light_get_valid_indices(int* valid_light_count); |
||||
void light_destroy(struct Light* light, int entity_id); |
||||
void light_create(struct Light* light, int entity_id, int light_type); |
||||
int* light_get_valid_indices(int* out_count); |
||||
|
||||
#endif |
||||
|
Loading…
Reference in new issue