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 |
#ifndef ENTITY_H |
||||||
#define 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 |
struct Entity |
||||||
{ |
{ |
||||||
int node; |
int id; |
||||||
char* name; |
int type; |
||||||
char* tag; |
char* name; |
||||||
int components[NUM_COMPONENTS]; |
bool is_listener; /* TODO: Replace all booleans with flags */ |
||||||
int parent; |
bool marked_for_deletion; |
||||||
int* children; |
bool renderable; |
||||||
int is_listener; |
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_init(void); |
||||||
void entity_cleanup(void); |
void entity_cleanup(void); |
||||||
void entity_remove(int index); |
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_get(int index); |
||||||
struct Entity* entity_find(const char* name); |
struct Entity* entity_find(const char* name); |
||||||
struct Entity* entity_get_all(void); |
struct Entity* entity_get_all(void); |
||||||
struct Entity* entity_get_parent(int node); |
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 |
#endif |
||||||
|
@ -1,117 +1,70 @@ |
|||||||
#include "light.h" |
#include "light.h" |
||||||
#include "log.h" |
|
||||||
#include "array.h" |
#include "array.h" |
||||||
#include <stdio.h> |
#include "entity.h" |
||||||
#include <string.h> |
|
||||||
|
|
||||||
static struct Light* light_list; |
#include <assert.h> |
||||||
static int* empty_indices; |
|
||||||
static int* valid_light_indices; |
|
||||||
static int max_lights; |
|
||||||
|
|
||||||
struct Light* light_get(int index) |
static int* light_list = NULL; |
||||||
{ |
|
||||||
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) |
void light_init(void) |
||||||
{ |
{ |
||||||
max_lights = 128; |
light_list = array_new(int); |
||||||
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); |
|
||||||
} |
} |
||||||
|
|
||||||
void light_cleanup(void) |
void light_cleanup(void) |
||||||
{ |
{ |
||||||
for(int i = 0; i < array_len(light_list); i++) |
|
||||||
light_remove(i); |
|
||||||
array_free(light_list); |
array_free(light_list); |
||||||
array_free(empty_indices); |
|
||||||
array_free(valid_light_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) |
void light_create(struct Light* light, int entity_id, int light_type) |
||||||
{ |
{ |
||||||
int index = -1; |
assert(light && entity_id > -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 |
|
||||||
{ |
|
||||||
for(index = 0; index < max_lights; index++) |
|
||||||
{ |
|
||||||
if(!light_list[index].valid) |
|
||||||
break; |
|
||||||
} |
|
||||||
|
|
||||||
if(index == max_lights - 1) |
|
||||||
{ |
|
||||||
index = -1; |
|
||||||
log_warning("Max light limit(%d) reached, cannot add light", max_lights); |
|
||||||
return index; |
|
||||||
} |
|
||||||
|
|
||||||
new_light = &light_list[index]; |
light->valid = true; |
||||||
} |
light->cast_shadow = 0; |
||||||
new_light->node = node; |
light->depth_bias = 0.0005f; |
||||||
new_light->valid = 1; |
light->type = light_type; |
||||||
new_light->cast_shadow = 0; |
light->pcf_enabled = false; |
||||||
vec3_fill(&new_light->color, 1.f, 1.f, 1.f); |
light->intensity = 1.f; |
||||||
new_light->depth_bias = 0.0005f; |
light->falloff = 1.5f; |
||||||
new_light->type = light_type; |
light->outer_angle = TO_RADIANS(30.f); |
||||||
new_light->pcf_enabled = 0; |
light->inner_angle = TO_RADIANS(20.f); |
||||||
new_light->intensity = 1.f; |
light->radius = 20; |
||||||
new_light->falloff = 1.5f; |
vec3_fill(&light->color, 1.f, 1.f, 1.f); |
||||||
new_light->outer_angle = TO_RADIANS(30.f); |
int* new_index = array_grow(light_list, int); |
||||||
new_light->inner_angle = TO_RADIANS(20.f); |
*new_index = entity_id; |
||||||
new_light->radius = 20; |
|
||||||
return index; |
|
||||||
} |
} |
||||||
|
|
||||||
int light_get_max_lights(void) |
void light_destroy(struct Light* light, int entity_id) |
||||||
{ |
{ |
||||||
return max_lights; |
assert(light && entity_id > -1); |
||||||
} |
|
||||||
|
|
||||||
int* light_get_valid_indices(int* valid_light_count) |
int index_to_remove = -1; |
||||||
{ |
|
||||||
/* 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++) |
for(int i = 0; i < array_len(light_list); i++) |
||||||
{ |
{ |
||||||
if(light_list[i].valid) |
if(light_list[i] == entity_id) |
||||||
{ |
{ |
||||||
valid_light_indices[light_count] = i; |
index_to_remove = i; |
||||||
light_count++; |
break; |
||||||
} |
} |
||||||
} |
} |
||||||
*valid_light_count = light_count; |
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); |
||||||
|
} |
||||||
|
|
||||||
return valid_light_indices; |
int* light_get_valid_indices(int* out_count) |
||||||
|
{ |
||||||
|
*out_count = array_len(light_list); |
||||||
|
return light_list; |
||||||
} |
} |
||||||
|
@ -1,43 +1,14 @@ |
|||||||
#ifndef LIGHT_H |
#ifndef LIGHT_H |
||||||
#define LIGHT_H |
#define LIGHT_H |
||||||
|
|
||||||
#include "num_types.h" |
|
||||||
#include "linmath.h" |
|
||||||
|
|
||||||
#define MAX_SHADOWMAPS 4 |
#define MAX_SHADOWMAPS 4 |
||||||
|
|
||||||
enum LightType |
struct Light; |
||||||
{ |
|
||||||
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* light_get(int index); |
|
||||||
struct Light* light_get_all(void); |
|
||||||
void light_init(void); |
void light_init(void); |
||||||
void light_cleanup(void); |
void light_cleanup(void); |
||||||
void light_remove(int index); |
void light_destroy(struct Light* light, int entity_id); |
||||||
int light_create(int node, int light_type); |
void light_create(struct Light* light, int entity_id, int light_type); |
||||||
void light_set_radius(struct Light* light, int radius); |
int* light_get_valid_indices(int* out_count); |
||||||
int light_get_max_lights(void); |
|
||||||
int* light_get_valid_indices(int* valid_light_count); |
|
||||||
|
|
||||||
#endif |
#endif |
||||||
|
Loading…
Reference in new issue