From b5c1f8c85f37f0da96b0a3001265aa14b12aae43 Mon Sep 17 00:00:00 2001 From: shariq Date: Thu, 22 Jun 2017 00:52:09 +0500 Subject: [PATCH] Added inspectors for entity transformation --- orgfile.org | 1 + src/editor.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++- src/game.c | 15 ++++++----- src/renderer.c | 7 +++--- src/transform.c | 54 ++++++++++++++++++++++++++++----------- 5 files changed, 118 insertions(+), 26 deletions(-) diff --git a/orgfile.org b/orgfile.org index 22276c6..f54f26d 100644 --- a/orgfile.org +++ b/orgfile.org @@ -224,6 +224,7 @@ x Font atlas proper cleanup ** TODO Remove components and switch to "Fat Entities" i.e. one entity struct contains all combinations ** TODO Use variants for material params ** TODO Improve Material Parameters/Pipeline Uniforms/Instance Uniforms are handled +** TODO Fix light rotation/direction bugs ** DONE In second refactor pass, use entities everywhere, no need to pass in transform and model separately for example since they're both part of the same entity anyway - State "DONE" from "TODO" [2017-05-31 Wed 21:44] ** DONE Show SDL dialogbox if we cannot launch at all? diff --git a/src/editor.c b/src/editor.c index ef0911f..c483c1b 100644 --- a/src/editor.c +++ b/src/editor.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include struct Editor_State { @@ -45,6 +47,16 @@ static struct Debug_Variable* debug_vars_list = NULL; static int* empty_indices = NULL; static void editor_color_combo(struct nk_context* context, vec4* color, int width, int height); +static bool editor_widget_vec3(struct nk_context* context, + vec3* value, + const char* name_x, + const char* name_y, + const char* name_z, + float min, + float max, + float step, + float inc_per_pixel, + int row_height); void editor_init(void) { @@ -200,7 +212,6 @@ void editor_update(float dt) nk_layout_row_dynamic(context, 20, 1); if(nk_selectable_label(context, entity->name, NK_TEXT_ALIGN_LEFT, &entity->editor_selected)) { - log_message(entity->editor_selected ? "selected" : "deselected"); if(editor_state.selected_entity_id != -1) { struct Entity* currently_selected = entity_get(editor_state.selected_entity_id); @@ -264,6 +275,47 @@ void editor_update(float dt) nk_label(context, "Name", NK_TEXT_ALIGN_LEFT); nk_label(context, entity->name, NK_TEXT_ALIGN_RIGHT); nk_layout_row_dynamic(context, row_height, 2); nk_label(context, "ID", NK_TEXT_ALIGN_LEFT); nk_labelf(context, NK_TEXT_ALIGN_RIGHT, "%d", entity->id); + + /* Transform */ + nk_layout_row_dynamic(context, row_height, 1); nk_label(context, "Position", NK_TEXT_ALIGN_CENTERED); + vec3 abs_pos = {0.f, 0.f, 0.f}; + transform_get_absolute_pos(entity, &abs_pos); + if(editor_widget_vec3(context, &abs_pos, "Px", "Py", "Pz", -FLT_MAX, FLT_MAX, 5.f, 1.f, row_height)) transform_set_position(entity, &abs_pos); + + nk_layout_row_dynamic(context, row_height, 1); nk_label(context, "Rotation", NK_TEXT_ALIGN_CENTERED); + quat abs_rot = {0.f, 0.f, 0.f, 1.f}; + transform_get_absolute_rot(entity, &abs_rot); + vec3 rot_angles = {0.f, 0.f, 0.f}; + rot_angles.x = TO_DEGREES(quat_get_pitch(&abs_rot)); + rot_angles.y = TO_DEGREES(quat_get_yaw(&abs_rot)); + rot_angles.z = TO_DEGREES(quat_get_roll(&abs_rot)); + vec3 curr_rot = {rot_angles.x, rot_angles.y, rot_angles.z}; + + nk_layout_row_dynamic(context, row_height, 1); nk_property_float(context, "Rx", -FLT_MAX, &curr_rot.x, FLT_MAX, 5.f, 1.f); + nk_layout_row_dynamic(context, row_height, 1); nk_property_float(context, "Ry", -FLT_MAX, &curr_rot.y, FLT_MAX, 5.f, 1.f); + nk_layout_row_dynamic(context, row_height, 1); nk_property_float(context, "Rz", -FLT_MAX, &curr_rot.z, FLT_MAX, 5.f, 1.f); + + vec3 delta = {0.f, 0.f, 0.f}; + vec3_sub(&delta, &rot_angles, &curr_rot); + + vec3 AXIS_X = {1.f, 0.f, 0.f}; + vec3 AXIS_Y = {0.f, 1.f, 0.f}; + vec3 AXIS_Z = {0.f, 0.f, 1.f}; + + const float epsilon = 0.0001f; + if(fabsf(delta.x) > epsilon) transform_rotate(entity, &AXIS_X, delta.x, TS_WORLD); + if(fabsf(delta.y) > epsilon) transform_rotate(entity, &AXIS_Y, delta.y, TS_WORLD); + if(fabsf(delta.z) > epsilon) transform_rotate(entity, &AXIS_Z, delta.z, TS_WORLD); + + nk_layout_row_dynamic(context, row_height, 1); nk_label(context, "Scale", NK_TEXT_ALIGN_CENTERED); + vec3 abs_scale = {0.f, 0.f, 0.f}; + transform_get_absolute_scale(entity, &abs_scale); + if(editor_widget_vec3(context, &abs_scale, "SX", "SY", "SZ", 0.1f, FLT_MAX, 1.f, 0.1f, row_height)) + { + entity->transform.scale = abs_scale; + transform_update_transmat(entity); + } + } else { @@ -409,3 +461,16 @@ void editor_cleanup(void) array_free(debug_vars_list); array_free(empty_indices); } + + +bool editor_widget_vec3(struct nk_context* context, vec3* value, const char* name_x, const char* name_y, const char* name_z, float min, float max, float step, float inc_per_pixel, int row_height) +{ + bool changed = false; + vec3 val_copy = {0.f, 0.f, 0.f}; + vec3_assign(&val_copy, value); + nk_layout_row_dynamic(context, row_height, 1); nk_property_float(context, name_x, min, &value->x, max, step, inc_per_pixel); + nk_layout_row_dynamic(context, row_height, 1); nk_property_float(context, name_y, min, &value->y, max, step, inc_per_pixel); + nk_layout_row_dynamic(context, row_height, 1); nk_property_float(context, name_z, min, &value->z, max, step, inc_per_pixel); + if(!vec3_equals(&val_copy, value)) changed = true; + return changed; +} diff --git a/src/game.c b/src/game.c index 0b03def..a533b0f 100644 --- a/src/game.c +++ b/src/game.c @@ -186,7 +186,6 @@ void scene_setup(void) /* entity_save(light_ent, "ground.ent", DT_INSTALL); */ struct Entity* light = entity_load("light.ent", DT_INSTALL); - log_message("asdas;"); } void debug(float dt) @@ -280,8 +279,8 @@ void debug(float dt) if(input_is_key_pressed(KEY_SPACE)) { - struct Entity* model = scene_find("Model_Entity"); - vec3 x_axis = {1, 0, 0}; + struct Entity* model = scene_find("Light_Ent"); + vec3 x_axis = {0, 1, 0}; transform_rotate(model, &x_axis, 25.f * dt, TS_WORLD); } @@ -303,11 +302,11 @@ void debug(float dt) transform_translate(model, &amount, TS_LOCAL); } - struct Entity* model = scene_find("Model_Entity"); - vec3 x_axis = {1, 0, 0}; - transform_rotate(model, &x_axis, 25.f * dt, TS_WORLD); - vec3 amount = {0, 0, -5 * dt}; - transform_translate(model, &amount, TS_LOCAL); + /* struct Entity* model = scene_find("Light_Ent"); */ + /* vec3 x_axis = {0, 1, 0}; */ + /* transform_rotate(model, &x_axis, 25.f * dt, TS_WORLD); */ + /* vec3 amount = {0, 0, -5 * dt}; */ + /* transform_translate(model, &amount, TS_LOCAL); */ } int run(void) diff --git a/src/renderer.c b/src/renderer.c index e1783d2..465f186 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -170,9 +170,10 @@ void renderer_draw(struct Entity* active_viewer) if(light->type != LT_POINT) { snprintf(uniform_name, MAX_UNIFORM_NAME_LEN, "lights[%d].direction", i); - transform_get_absolute_lookat(light_entity, &light_pos); - vec3_norm(&light_pos, &light_pos); - shader_set_uniform_vec3(material->shader, uniform_name, &light_pos); + vec3 light_dir = {0.f, 0.f, 0.f}; + transform_get_absolute_lookat(light_entity, &light_dir); + vec3_norm(&light_dir, &light_dir); + shader_set_uniform_vec3(material->shader, uniform_name, &light_dir); memset(uniform_name, '\0', MAX_UNIFORM_NAME_LEN); } diff --git a/src/transform.c b/src/transform.c index 9ecf388..af5c088 100644 --- a/src/transform.c +++ b/src/transform.c @@ -241,27 +241,53 @@ void transform_set_position(struct Entity* entity, vec3* new_position) void transform_get_absolute_pos(struct Entity* entity, vec3* res) { - struct Transform* transform = &entity->transform; - struct Entity* parent = entity_get(transform->parent); - if(parent) - transform_get_absolute_pos(parent, res); - vec3_add(res, res, &transform->position); + vec3_assign(res, &entity->transform.position); + bool done = false; + struct Entity* parent = entity_get(entity->transform.parent); + while(!done) + { + if(!parent) + { + done = true; + break; + } + vec3_add(res, res, &parent->transform.position); + parent = entity_get(parent->transform.parent); + } } void transform_get_absolute_scale(struct Entity* entity, vec3* res) { struct Transform* transform = &entity->transform; - struct Entity* parent = entity_get(transform->parent); - if(parent) - transform_get_absolute_scale(parent, res); - vec3_add(res, res, &transform->scale); + vec3_assign(res, &transform->scale); + bool done = false; + struct Entity* parent = entity_get(transform->parent); + while(!done) + { + if(!parent) + { + done = true; + break; + } + vec3_mul(res, res, &parent->transform.scale); + parent = entity_get(parent->transform.parent); + } + } void transform_get_absolute_rot(struct Entity* entity, quat* res) { - struct Transform* transform = &entity->transform; - struct Entity* parent = entity_get(transform->parent); - if(parent) - transform_get_absolute_rot(parent, res); - quat_mul(res, res, &transform->rotation); + quat_assign(res, &entity->transform.rotation); + bool done = false; + struct Entity* parent = entity_get(entity->transform.parent); + while(!done) + { + if(!parent) + { + done = true; + break; + } + quat_mul(res, res, &parent->transform.rotation); + parent = entity_get(parent->transform.parent); + } }