Added inspectors for entity transformation

dev
shariq 8 years ago
parent f721492a91
commit b5c1f8c85f
  1. 1
      orgfile.org
  2. 67
      src/editor.c
  3. 15
      src/game.c
  4. 7
      src/renderer.c
  5. 54
      src/transform.c

@ -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 Remove components and switch to "Fat Entities" i.e. one entity struct contains all combinations
** TODO Use variants for material params ** TODO Use variants for material params
** TODO Improve Material Parameters/Pipeline Uniforms/Instance Uniforms are handled ** 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 ** 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] - State "DONE" from "TODO" [2017-05-31 Wed 21:44]
** DONE Show SDL dialogbox if we cannot launch at all? ** DONE Show SDL dialogbox if we cannot launch at all?

@ -25,6 +25,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <float.h>
#include <math.h>
struct Editor_State struct Editor_State
{ {
@ -45,6 +47,16 @@ static struct Debug_Variable* debug_vars_list = NULL;
static int* empty_indices = NULL; static int* empty_indices = NULL;
static void editor_color_combo(struct nk_context* context, vec4* color, int width, int height); 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) void editor_init(void)
{ {
@ -200,7 +212,6 @@ void editor_update(float dt)
nk_layout_row_dynamic(context, 20, 1); nk_layout_row_dynamic(context, 20, 1);
if(nk_selectable_label(context, entity->name, NK_TEXT_ALIGN_LEFT, &entity->editor_selected)) 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) if(editor_state.selected_entity_id != -1)
{ {
struct Entity* currently_selected = entity_get(editor_state.selected_entity_id); 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_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_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); 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 else
{ {
@ -409,3 +461,16 @@ void editor_cleanup(void)
array_free(debug_vars_list); array_free(debug_vars_list);
array_free(empty_indices); 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;
}

@ -186,7 +186,6 @@ void scene_setup(void)
/* entity_save(light_ent, "ground.ent", DT_INSTALL); */ /* entity_save(light_ent, "ground.ent", DT_INSTALL); */
struct Entity* light = entity_load("light.ent", DT_INSTALL); struct Entity* light = entity_load("light.ent", DT_INSTALL);
log_message("asdas;");
} }
void debug(float dt) void debug(float dt)
@ -280,8 +279,8 @@ void debug(float dt)
if(input_is_key_pressed(KEY_SPACE)) if(input_is_key_pressed(KEY_SPACE))
{ {
struct Entity* model = scene_find("Model_Entity"); struct Entity* model = scene_find("Light_Ent");
vec3 x_axis = {1, 0, 0}; vec3 x_axis = {0, 1, 0};
transform_rotate(model, &x_axis, 25.f * dt, TS_WORLD); transform_rotate(model, &x_axis, 25.f * dt, TS_WORLD);
} }
@ -303,11 +302,11 @@ void debug(float dt)
transform_translate(model, &amount, TS_LOCAL); transform_translate(model, &amount, TS_LOCAL);
} }
struct Entity* model = scene_find("Model_Entity"); /* struct Entity* model = scene_find("Light_Ent"); */
vec3 x_axis = {1, 0, 0}; /* vec3 x_axis = {0, 1, 0}; */
transform_rotate(model, &x_axis, 25.f * dt, TS_WORLD); /* transform_rotate(model, &x_axis, 25.f * dt, TS_WORLD); */
vec3 amount = {0, 0, -5 * dt}; /* vec3 amount = {0, 0, -5 * dt}; */
transform_translate(model, &amount, TS_LOCAL); /* transform_translate(model, &amount, TS_LOCAL); */
} }
int run(void) int run(void)

@ -170,9 +170,10 @@ void renderer_draw(struct Entity* active_viewer)
if(light->type != LT_POINT) if(light->type != LT_POINT)
{ {
snprintf(uniform_name, MAX_UNIFORM_NAME_LEN, "lights[%d].direction", i); snprintf(uniform_name, MAX_UNIFORM_NAME_LEN, "lights[%d].direction", i);
transform_get_absolute_lookat(light_entity, &light_pos); vec3 light_dir = {0.f, 0.f, 0.f};
vec3_norm(&light_pos, &light_pos); transform_get_absolute_lookat(light_entity, &light_dir);
shader_set_uniform_vec3(material->shader, uniform_name, &light_pos); vec3_norm(&light_dir, &light_dir);
shader_set_uniform_vec3(material->shader, uniform_name, &light_dir);
memset(uniform_name, '\0', MAX_UNIFORM_NAME_LEN); memset(uniform_name, '\0', MAX_UNIFORM_NAME_LEN);
} }

@ -241,27 +241,53 @@ void transform_set_position(struct Entity* entity, vec3* new_position)
void transform_get_absolute_pos(struct Entity* entity, vec3* res) void transform_get_absolute_pos(struct Entity* entity, vec3* res)
{ {
struct Transform* transform = &entity->transform; vec3_assign(res, &entity->transform.position);
struct Entity* parent = entity_get(transform->parent); bool done = false;
if(parent) struct Entity* parent = entity_get(entity->transform.parent);
transform_get_absolute_pos(parent, res); while(!done)
vec3_add(res, res, &transform->position); {
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) void transform_get_absolute_scale(struct Entity* entity, vec3* res)
{ {
struct Transform* transform = &entity->transform; struct Transform* transform = &entity->transform;
struct Entity* parent = entity_get(transform->parent); vec3_assign(res, &transform->scale);
if(parent) bool done = false;
transform_get_absolute_scale(parent, res); struct Entity* parent = entity_get(transform->parent);
vec3_add(res, res, &transform->scale); 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) void transform_get_absolute_rot(struct Entity* entity, quat* res)
{ {
struct Transform* transform = &entity->transform; quat_assign(res, &entity->transform.rotation);
struct Entity* parent = entity_get(transform->parent); bool done = false;
if(parent) struct Entity* parent = entity_get(entity->transform.parent);
transform_get_absolute_rot(parent, res); while(!done)
quat_mul(res, res, &transform->rotation); {
if(!parent)
{
done = true;
break;
}
quat_mul(res, res, &parent->transform.rotation);
parent = entity_get(parent->transform.parent);
}
} }

Loading…
Cancel
Save