Added transform component

dev
Shariq Shah 10 years ago
parent f7e1887028
commit cfd810f020
  1. 38
      src/entity.c
  2. 4
      src/entity.h
  3. 19
      src/game.c
  4. 114
      src/transform.c
  5. 18
      src/transform.h
  6. 3
      src/window_system.c

@ -2,6 +2,7 @@
#include "array.h" #include "array.h"
#include "log.h" #include "log.h"
#include "string_utils.h" #include "string_utils.h"
#include "transform.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -32,11 +33,9 @@ void entity_remove(int index)
for(int i = 0; i < MAX_COMPONENTS; i++) for(int i = 0; i < MAX_COMPONENTS; i++)
{ {
enum Component component = entity->components[i]; switch((enum Component)i)
switch(component)
{ {
case C_TRANSFORM: case C_TRANSFORM: transform_remove(entity->components[i]); break;
break;
case C_MODEL: case C_MODEL:
break; break;
case C_RIGIDBODY: case C_RIGIDBODY:
@ -47,37 +46,42 @@ void entity_remove(int index)
/* Only called for MAX_COMPONENTS, do nothing */ /* Only called for MAX_COMPONENTS, do nothing */
break; break;
} }
entity->components[i] = -1;
} }
entity->node = -1;
free(entity->name);
free(entity->tag);
entity->name = entity->tag = NULL;
array_push(empty_indices, index, int);
} }
struct Entity* entity_create(const char* name, const char* tag) struct Entity* entity_create(const char* name, const char* tag)
{ {
struct Entity* new_entity = NULL; struct Entity* new_entity = NULL;
int index = -1; int index = -1;
int empty_len = array_len(empty_indices);
if(empty_len > 0) if(array_len(empty_indices) > 0)
{ {
index = empty_indices[empty_len - 1]; index = *array_get_last(empty_indices, int);
array_pop(empty_indices); array_pop(empty_indices);
new_entity = &entity_list[index]; new_entity = &entity_list[index];
} }
else else
{ {
new_entity = array_grow(entity_list, struct Entity); new_entity = array_grow(entity_list, struct Entity);
new_entity->name = new_entity->tag = NULL;
index = array_len(entity_list) - 1; index = array_len(entity_list) - 1;
} }
if(new_entity->name) free(new_entity->name); if(new_entity->name) free(new_entity->name);
if(new_entity->name) free(new_entity->tag); if(new_entity->tag) free(new_entity->tag);
new_entity->name = name ? str_new(name) : str_new("DEFAULT_NAME"); new_entity->name = name ? str_new(name) : str_new("DEFAULT_NAME");
new_entity->tag = tag ? str_new(tag) : str_new("DEFAULT_TAG"); new_entity->tag = tag ? str_new(tag) : str_new("DEFAULT_TAG");
new_entity->node = index; new_entity->node = index;
new_entity->components[C_TRANSFORM] = transform_create(new_entity->node);
/* TODO: Add transform here by default maybe? */
return new_entity; return new_entity;
} }
struct Entity* entity_get(int index) struct Entity* entity_get(int index)
@ -97,13 +101,14 @@ struct Entity* entity_find(const char* name)
for(int i = 0; i < array_len(entity_list); i++) for(int i = 0; i < array_len(entity_list); i++)
{ {
struct Entity* curr_ent = &entity_list[i]; struct Entity* curr_ent = &entity_list[i];
if(!entity->name)
continue;
if(strcmp(curr_ent->name, name) == 0) if(strcmp(curr_ent->name, name) == 0)
{ {
entity = curr_ent; entity = curr_ent;
break; break;
} }
} }
return entity; return entity;
} }
int entity_component_remove(struct Entity* entity, enum Component component) int entity_component_remove(struct Entity* entity, enum Component component)
@ -113,6 +118,8 @@ int entity_component_remove(struct Entity* entity, enum Component component)
switch(component) switch(component)
{ {
case C_TRANSFORM: case C_TRANSFORM:
log_error("entity:remove", "Cannot remove Tranform component");
success = 0;
break; break;
case C_MODEL: case C_MODEL:
break; break;
@ -134,8 +141,7 @@ void* entity_component_get(struct Entity* entity, enum Component component)
assert(entity); assert(entity);
switch(component) switch(component)
{ {
case C_TRANSFORM: case C_TRANSFORM: comp_obj = transform_get(entity->components[C_TRANSFORM]); break;
break;
case C_MODEL: case C_MODEL:
break; break;
case C_RIGIDBODY: case C_RIGIDBODY:
@ -156,7 +162,7 @@ void* entity_component_add(struct Entity* entity, enum Component component)
switch(component) switch(component)
{ {
case C_TRANSFORM: case C_TRANSFORM:
break; log_error("entity:add_component", "Entity already has Transform component");
case C_MODEL: case C_MODEL:
break; break;
case C_RIGIDBODY: case C_RIGIDBODY:

@ -4,11 +4,9 @@
#include "components.h" #include "components.h"
#include "num_types.h" #include "num_types.h"
typedef int32 Node;
struct Entity struct Entity
{ {
Node node; int node;
char* name; char* name;
char* tag; char* tag;
int components[MAX_COMPONENTS]; int components[MAX_COMPONENTS];

@ -13,10 +13,14 @@
#include "entity.h" #include "entity.h"
#include "geometry.h" #include "geometry.h"
#include "array.h" #include "array.h"
#include "transform.h"
void run(void); void run(void);
void update(void); void update(void);
void render(void); void render(void);
void debug(void);
struct Entity* entity = NULL;
void game_init(void) void game_init(void)
{ {
@ -26,6 +30,7 @@ void game_init(void)
renderer_init(window); renderer_init(window);
io_file_init("/mnt/Dev/Projects/Symmetry/assets/");/* TODO: Implement proper way of getting binary directory */ io_file_init("/mnt/Dev/Projects/Symmetry/assets/");/* TODO: Implement proper way of getting binary directory */
shader_init(); shader_init();
transform_init();
entity_init(); entity_init();
geom_init(); geom_init();
@ -40,9 +45,19 @@ void game_init(void)
input_map_create("Test2", keys4, 1); input_map_create("Test2", keys4, 1);
int shader = shader_create("phong.vert", "phong.frag"); int shader = shader_create("phong.vert", "phong.frag");
entity = entity_create("Test", "None");
run(); run();
} }
void debug(void)
{
struct Transform* transform = entity_component_get(entity, C_TRANSFORM);
vec3 offset = {0, 5, 0};
transform_translate(transform, offset, TS_WORLD);
log_message("Position : %.3f, %.3f, %.3f", transform->position[0], transform->position[1], transform->position[2]);
}
void run(void) void run(void)
{ {
while(!window_should_close()) while(!window_should_close())
@ -59,6 +74,9 @@ void update(void)
input_update(); input_update();
if(input_key_state_get(GLFW_KEY_ESCAPE, GLFW_PRESS)) if(input_key_state_get(GLFW_KEY_ESCAPE, GLFW_PRESS))
window_set_should_close(1); window_set_should_close(1);
if(input_map_state_get("MoveUp", GLFW_PRESS))
debug();
} }
void render(void) void render(void)
@ -70,6 +88,7 @@ void game_cleanup(void)
{ {
entity_cleanup(); entity_cleanup();
geom_cleanup(); geom_cleanup();
transform_cleanup();
input_cleanup(); input_cleanup();
renderer_cleanup(); renderer_cleanup();
io_file_cleanup(); io_file_cleanup();

@ -1,3 +1,117 @@
#include "transform.h" #include "transform.h"
#include "array.h"
#include <assert.h>
struct Transform* transform_list;
int* empty_indices;
void transform_init(void)
{
transform_list = array_new(struct Transform);
empty_indices = array_new(int);
}
void transform_cleanup(void)
{
array_free(transform_list);
array_free(empty_indices);
}
int transform_create(int node)
{
int index = -1;
if(node > -1)
{
struct Transform* new_transform = NULL;
if(array_len(empty_indices) > 0)
{
index = *array_get_last(empty_indices, int);
array_pop(empty_indices);
new_transform = &transform_list[index];
}
else
{
new_transform = array_grow(transform_list, struct Transform);
index = array_len(transform_list) - 1;
}
new_transform->node = node;
new_transform->position[0] = new_transform->position[1] = new_transform->position[2] = 0;
new_transform->scale[0] = new_transform->scale[1] = new_transform->scale[2] = 1;
quat_identity(new_transform->rotation);
transform_update_transmat(new_transform);
}
return index;
}
void transform_translate(struct Transform* transform, vec3 amount, enum Transform_Space space)
{
if(space == TS_LOCAL)
quat_mul_vec3(amount, transform->rotation, amount);
vec3_add(transform->position, transform->position, amount);
transform_update_transmat(transform);
}
void transform_rotate(struct Transform* transform,
vec3 axis,
float angle,
enum Transform_Space space)
{
quat new_rot;
quat_identity(new_rot);
quat_rotate(new_rot, angle, axis);
quat_norm(new_rot, new_rot);
if(space == TS_LOCAL)
quat_mul(transform->rotation, transform->rotation, new_rot);
else if(space == TS_WORLD)
quat_mul(transform->rotation, new_rot, transform->rotation);
transform_update_transmat(transform);
}
void transform_scale(struct Transform* transform, vec3 scale)
{
transform->scale[0] = scale[0];
transform->scale[1] = scale[1];
transform->scale[2] = scale[2];
transform_update_transmat(transform);
}
void transform_get_forward(struct Transform* transform, vec3 res)
{
res[0] = 0; res[1] = 0; res[2] = 1;
quat_mul_vec3(res, transform->rotation, res);
}
void transform_get_up(struct Transform* transform, vec3 res)
{
res[0] = 0; res[1] = 1; res[2] = 0;
quat_mul_vec3(res, transform->rotation, res);
}
void transform_get_right(struct Transform* transform, vec3 res)
{
res[0] = 1; res[1] = 0; res[2] = 0;
quat_mul_vec3(res, transform->rotation, res);
}
void transform_update_transmat(struct Transform* transform)
{
mat4 scale, rot, tran;
mat4_scale_aniso(scale, scale, transform->scale[0], transform->scale[1], transform->scale[2]);
mat4_from_quat(rot, transform->rotation);
mat4_translate(tran, transform->position[0], transform->position[1], transform->position[2]);
mat4_mul(transform->trans_mat, transform->trans_mat, scale);
mat4_mul(transform->trans_mat, transform->trans_mat, rot);
mat4_mul(transform->trans_mat, transform->trans_mat, tran);
}
struct Transform* transform_get(int index)
{
assert(index > -1 && index < array_len(transform_list));
return &transform_list[index];
}
void transform_remove(int index)
{
assert(index > -1 && index < array_len(transform_list));
transform_list[index].node = -1;
array_push(empty_indices, index, int);
}

@ -3,15 +3,31 @@
#include "linmath.h" #include "linmath.h"
enum Transform_Space { TS_LOCAL, TS_WORLD};
struct Transform struct Transform
{ {
int node;
vec3 position; vec3 position;
vec3 scale; vec3 scale;
quat rotation; quat rotation;
mat4 trans_mat;
}; };
void transform_initialize(void); struct Transform* transform_get(int index);
void transform_remove(int index);
void transform_init(void);
void transform_cleanup(void); void transform_cleanup(void);
int transform_create(int node); int transform_create(int node);
void transform_translate(struct Transform* transform, vec3 amount, enum Transform_Space space);
void transform_rotate(struct Transform* transform,
vec3 axis,
float angle,
enum Transform_Space space);
void transform_scale(struct Transform* transform, vec3 scale);
void transform_get_forward(struct Transform* transform, vec3 res);
void transform_get_up(struct Transform* transform, vec3 res);
void transform_get_right(struct Transform* transform, vec3 res);
void transform_update_transmat(struct Transform* transform);
#endif #endif

@ -28,6 +28,9 @@ int window_init(const char* title, int width, int height)
GLFW_VERSION_REVISION); GLFW_VERSION_REVISION);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_FOCUSED, GL_TRUE);
//glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
active_window = glfwCreateWindow(width, height, title, NULL, NULL); active_window = glfwCreateWindow(width, height, title, NULL, NULL);
if(!active_window) if(!active_window)
{ {

Loading…
Cancel
Save