Fixed minor bugs in input code

dev
Shariq Shah 10 years ago
parent c97dbf5777
commit 16dca4f9ca
  1. 5
      src/camera.c
  2. 9
      src/entity.c
  3. 1
      src/entity.h
  4. 71
      src/game.c
  5. 8
      src/input.c
  6. 1
      src/input.h
  7. 135
      src/linmath.h
  8. 9
      src/scene.c
  9. 1
      src/scene.h
  10. 79
      src/transform.c
  11. 5
      src/transform.h

@ -3,6 +3,9 @@
#include "transform.h"
#include "array.h"
#include "utils.h"
#include "log.h"
static struct Camera* camera_list;
static int* empty_indices;
@ -78,7 +81,7 @@ void camera_update_view(struct Camera* camera)
struct Entity* entity = entity_get(camera->node);
struct Transform* transform = entity_component_get(entity, C_TRANSFORM);
vec3 lookat = {0.f}, up = {0.f}, position = {0.f};
transform_get_absolute_lookat(transform, lookat);
transform_get_lookat(transform, lookat);
transform_get_up(transform, up);
transform_get_absolute_pos(transform, position);
mat4_look_at(camera->view_mat, position, lookat, up);

@ -32,6 +32,7 @@ void entity_cleanup(void)
void entity_remove(int index)
{
struct Entity* entity = &entity_list[index];
if(entity->node == -1) return;
for(int i = 0; i < MAX_COMPONENTS; i++)
{
@ -233,3 +234,11 @@ struct Entity* entity_get_all(void)
{
return entity_list;
}
struct Entity* entity_get_parent(int node)
{
struct Entity* parent = NULL;
struct Entity* entity = entity_get(node);
if(entity) parent = entity_get(entity->node);
return parent;
}

@ -20,6 +20,7 @@ struct Entity* entity_create(const char* name, const char* tag);
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, ...);

@ -17,6 +17,7 @@
#include "camera.h"
#include "model.h"
#include "scene.h"
#include "utils.h"
void run(void);
void update(float dt);
@ -69,9 +70,10 @@ void game_init(void)
vec3 viewer_pos = {0, 0, 10};
struct Transform* viewer_tran = entity_component_get(player, C_TRANSFORM);
transform_set_position(viewer_tran, viewer_pos);
struct Entity* player_pitch = scene_add_as_child("player_pitch", NULL, player);
player_pitch_node = player_pitch->node;
entity_component_add(player_pitch, C_CAMERA, 800, 600);
/* struct Entity* player_pitch = scene_add_as_child("player_pitch", NULL, player); */
/* player_pitch_node = player_pitch->node; */
/* entity_component_add(player_pitch, C_CAMERA, 800, 600); */
struct Camera* camera = entity_component_add(player, C_CAMERA, 800, 600);
struct Entity* new_ent = scene_add_new("Model_Entity", NULL);
struct Transform* tran = entity_component_get(new_ent, C_TRANSFORM);
@ -81,7 +83,7 @@ void game_init(void)
struct Transform* model_tran = entity_component_get(new_ent, C_TRANSFORM);
//vec3 axis = {0.f, 1.f, 0.f};
//transform_rotate(model_tran, axis, (45.f), TS_WORLD);
vec3 scale = {1, 1, 5};
vec3 scale = {1, 1, 2};
transform_scale(model_tran, scale);
struct Entity* suz = scene_add_as_child("Suzanne", NULL, new_ent);
@ -90,10 +92,8 @@ void game_init(void)
vec3 s_pos = {3, 0, 0};
transform_translate(s_tran, s_pos, TS_WORLD);
vec4 temp = {1, 0, 0, 1};
mat4 mat;
mat4_identity(mat);
mat4_mul_vec4(temp, mat, temp);
/* Set cursor mode */
input_cursor_mode_set(CM_LOCKED);
run();
}
@ -101,9 +101,9 @@ void game_init(void)
void debug(float dt)
{
struct Entity* entity = entity_get(player_node);
struct Entity* entity_pitch = entity_get(player_pitch_node);
//struct Entity* entity_pitch = entity_get(player_pitch_node);
struct Transform* transform = entity_component_get(entity, C_TRANSFORM);
struct Transform* pitch_transform = entity_component_get(entity_pitch, C_TRANSFORM);
//struct Transform* pitch_transform = entity_component_get(entity_pitch, C_TRANSFORM);
float move_speed = 5.f, turn_speed = 50.f;
vec3 offset = {0, 0, 0};
float turn_up_down = 0.f;
@ -119,8 +119,22 @@ void debug(float dt)
if(input_map_state_get("Turn_Right", GLFW_PRESS)) turn_left_right += turn_speed;
if(input_map_state_get("Turn_Left", GLFW_PRESS)) turn_left_right -= turn_speed;
double cursor_lr, cursor_ud;
input_cursor_pos_get(&cursor_lr, &cursor_ud);
input_cursor_pos_set(0.0, 0.0);
if(input_mousebutton_state_get(GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS))
{
const double scale = 0.5;
turn_up_down = cursor_ud * turn_speed * dt * scale;
turn_left_right = cursor_lr * turn_speed * dt * scale;
}
else
{
turn_up_down *= dt;
turn_left_right *= dt;
}
total_up_down_rot += turn_up_down;
if(total_up_down_rot >= max_up_down)
{
@ -140,18 +154,27 @@ void debug(float dt)
vec3 forward = {0.f};
transform_get_up(transform, up);
transform_get_forward(transform, forward);
//log_message("Up : %.3f, %.3f, %.3f", up[0], up[1], up[2]);
log_message("Forward : %.3f, %.3f, %.3f", forward[0], forward[1], forward[2]);
/* log_message("Up : %.3f, %.3f, %.3f", up[0], up[1], up[2]); */
/* log_message("Forward : %.3f, %.3f, %.3f", forward[0], forward[1], forward[2]); */
/* log_message("ROT : %.3f, %.3f, %.3f", */
/* TO_DEGREES(quat_pitch(transform->rotq)), */
/* TO_DEGREES(quat_yaw(transform->rotq)), */
/* TO_DEGREES(quat_roll(transform->rotq))); */
}
if(turn_up_down != 0.f)
{
transform_rotate(pitch_transform, rot_axis_up_down, -turn_up_down, TS_LOCAL);
transform_rotate(transform, rot_axis_up_down, -turn_up_down, TS_LOCAL);
//transform_rotate(pitch_transform, rot_axis_up_down, -turn_up_down, TS_LOCAL);
/* log_message("ROT : %.3f, %.3f, %.3f", */
/* TO_DEGREES(quat_pitch(pitch_transform->rotq)), */
/* TO_DEGREES(quat_yaw(pitch_transform->rotq)), */
/* TO_DEGREES(quat_roll(pitch_transform->rotq))); */
vec3 up = {0.f};
vec3 forward = {0.f};
transform_get_up(transform, up);
transform_get_forward(transform, forward);
//log_message("Up : %.3f, %.3f, %.3f", up[0], up[1], up[2]);
log_message("Forward : %.3f, %.3f, %.3f", forward[0], forward[1], forward[2]);
log_message("Up : %s", tostr_vec3(up));
log_message("FR : %s", tostr_vec3(forward));
}
/* Movement */
@ -166,15 +189,15 @@ void debug(float dt)
if(offset[0] != 0 || offset[2] != 0 || offset[1] != 0)
{
transform_translate(transform, offset, TS_LOCAL);
/* log_message("Position : %.3f, %.3f, %.3f", */
/* transform->position[0], */
/* transform->position[1], */
/* transform->position[2]); */
log_message("Position : %.3f, %.3f, %.3f",
transform->position[0],
transform->position[1],
transform->position[2]);
}
if(input_key_state_get(GLFW_KEY_SPACE, GLFW_PRESS))
{
struct Entity* model = entity_get(3);
struct Entity* model = entity_get(2);
struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM);
vec3 x_axis = {1, 0, 0};
transform_rotate(mod_tran, x_axis, 25.f * dt, TS_WORLD);
@ -187,6 +210,14 @@ void debug(float dt)
vec3 y_axis = {0, 1, 0};
transform_rotate(mod_tran, y_axis, 25.f * dt, TS_LOCAL);
}
if(input_key_state_get(GLFW_KEY_N, GLFW_PRESS))
{
struct Entity* model = entity_get(3);
struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM);
vec3 amount = {0, 0, -5 * dt};
transform_translate(mod_tran, amount, TS_LOCAL);
}
}
void run(void)

@ -58,6 +58,12 @@ void input_cursor_pos_get(double* xpos, double* ypos)
glfwGetCursorPos(window, xpos, ypos);
}
void input_cursor_pos_set(double xpos, double ypos)
{
GLFWwindow* window = window_get_active();
glfwSetCursorPos(window, xpos, ypos);
}
static void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods)
{
for(int i = 0; i < array_len(input_map_list); i++)
@ -87,7 +93,7 @@ void input_cursor_mode_set(enum Cursor_Mode mode)
int cursor_mode = GLFW_CURSOR_NORMAL;
if(mode == CM_HIDDEN)
cursor_mode = GLFW_CURSOR_HIDDEN;
else if(mode == CM_HIDDEN)
else if(mode == CM_LOCKED)
cursor_mode = GLFW_CURSOR_DISABLED;
glfwSetInputMode(window, GLFW_CURSOR, cursor_mode);

@ -18,6 +18,7 @@ void input_cleanup(void);
int input_mousebutton_state_get(int button, int state_type);
int input_key_state_get(int key, int state_type);
void input_cursor_pos_get(double* xpos, double* ypos);
void input_cursor_pos_set(double xpos, double ypos);
void input_cursor_mode_set(enum Cursor_Mode mode);
void input_update(void);
int input_map_state_get(const char* map_name, int state);

@ -7,6 +7,7 @@
#define M_PI 3.14159265358979323846
#define TO_RADIANS(degrees) ((degrees * M_PI) / 180.0)
#define TO_DEGREES(radians) ((radians * 180.0) / M_PI)
#define LINMATH_H_DEFINE_VEC(n) \
typedef float vec##n[n]; \
@ -135,13 +136,18 @@ static inline void mat4_scale(mat4 M, mat4 a, float k)
}
static inline void mat4_scale_aniso(mat4 M, mat4 a, float x, float y, float z)
{
int i;
vec4_scale(M[0], a[0], x);
vec4_scale(M[1], a[1], y);
vec4_scale(M[2], a[2], z);
for(i = 0; i < 4; ++i) {
M[3][i] = a[3][i];
}
/* int i; */
/* vec4_scale(M[0], a[0], x); */
/* vec4_scale(M[1], a[1], y); */
/* vec4_scale(M[2], a[2], z); */
/* for(i = 0; i < 4; ++i) { */
/* M[3][i] = a[3][i]; */
/* } */
M[0][0] = a[0][0] * x;
M[1][1] = a[1][1] * y;
M[2][2] = a[2][2] * z;
M[3][3] = 1.f;
}
static inline void mat4_mul(mat4 M, mat4 a, mat4 b)
{
@ -458,13 +464,18 @@ static inline void quat_sub(quat r, quat a, quat b)
}
static inline void quat_mul(quat r, quat p, quat q)
{
vec3 w;
vec3_mul_cross(r, p, q);
vec3_scale(w, p, q[3]);
vec3_add(r, r, w);
vec3_scale(w, q, p[3]);
vec3_add(r, r, w);
r[3] = p[3]*q[3] - vec3_mul_inner(p, q);
/* vec3 w; */
/* vec3_mul_cross(r, p, q); */
/* vec3_scale(w, p, q[3]); */
/* vec3_add(r, r, w); */
/* vec3_scale(w, q, p[3]); */
/* vec3_add(r, r, w); */
/* r[3] = p[3]*q[3] - vec3_mul_inner(p, q); */
r[0] = p[3] * q[0] + p[0] * q[3] + p[1] * q[2] - p[2] * q[1];
r[1] = p[3] * q[1] + p[1] * q[3] + p[2] * q[0] - p[0] * q[2];
r[2] = p[3] * q[2] + p[2] * q[3] + p[0] * q[1] - p[1] * q[0];
r[3] = p[3] * q[3] - p[0] * q[0] - p[1] * q[1] - p[2] * q[2];
}
static inline void quat_scale(quat r, quat v, float s)
{
@ -489,11 +500,11 @@ static inline void quat_conj(quat r, quat q)
}
static inline void quat_rotate(quat r, float angle, vec3 axis) {
vec3 v;
vec3_scale(v, axis, sinf(angle / 2));
vec3_scale(v, axis, sinf(angle / 2.f));
int i;
for(i=0; i<3; ++i)
r[i] = v[i];
r[3] = cosf(angle / 2);
r[3] = cosf(angle / 2.f);
}
#define quat_norm vec4_norm
static inline void quat_mul_vec3(vec3 r, quat q, vec3 v)
@ -515,31 +526,77 @@ v' = v + q.w * t + cross(q.xyz, t)
vec3_add(r, v, t);
vec3_add(r, r, u);
}
static inline float quat_pitch(quat q)
{
float result = atan2(2 * (q[1] * q[2] + q[3] * q[0]), q[3] * q[3] - q[0] * q[0] - q[1] * q[1] + q[2] * q[2]);
return result;
}
static inline float quat_yaw(quat q)
{
float result = asin(-2 * (q[0] * q[2] - q[3] * q[1]));
return result;
}
static inline float quat_roll(quat q)
{
float result = atan2(2 * (q[0] * q[1] + q[3] * q[2]), q[3] * q[3] + q[0] * q[0] - q[1] * q[1] - q[2] * q[2]);
return result;
}
static inline void mat4_from_quat(mat4 M, quat q)
{
float a = q[3];
float b = q[0];
float c = q[1];
float d = q[2];
float a2 = a*a;
float b2 = b*b;
float c2 = c*c;
float d2 = d*d;
M[0][0] = a2 + b2 - c2 - d2;
M[0][1] = 2.f*(b*c + a*d);
M[0][2] = 2.f*(b*d - a*c);
M[0][3] = 0.f;
M[1][0] = 2*(b*c - a*d);
M[1][1] = a2 - b2 + c2 - d2;
M[1][2] = 2.f*(c*d + a*b);
M[1][3] = 0.f;
M[2][0] = 2.f*(b*d + a*c);
M[2][1] = 2.f*(c*d - a*b);
M[2][2] = a2 - b2 - c2 + d2;
M[2][3] = 0.f;
/* float a = q[3]; */
/* float b = q[0]; */
/* float c = q[1]; */
/* float d = q[2]; */
/* float a2 = a*a; */
/* float b2 = b*b; */
/* float c2 = c*c; */
/* float d2 = d*d; */
/* M[0][0] = a2 + b2 - c2 - d2; */
/* M[0][1] = 2.f*(b*c + a*d); */
/* M[0][2] = 2.f*(b*d - a*c); */
/* M[0][3] = 0.f; */
/* M[1][0] = 2*(b*c - a*d); */
/* M[1][1] = a2 - b2 + c2 - d2; */
/* M[1][2] = 2.f*(c*d + a*b); */
/* M[1][3] = 0.f; */
/* M[2][0] = 2.f*(b*d + a*c); */
/* M[2][1] = 2.f*(c*d - a*b); */
/* M[2][2] = a2 - b2 - c2 + d2; */
/* M[2][3] = 0.f; */
/* M[3][0] = M[3][1] = M[3][2] = 0.f; */
/* M[3][3] = 1.f; */
float xx = q[0] * q[0];
float xy = q[0] * q[1];
float xz = q[0] * q[2];
float xw = q[0] * q[3];
float yy = q[1] * q[1];
float yz = q[1] * q[2];
float yw = q[1] * q[3];
float zz = q[2] * q[2];
float zw = q[2] * q[3];
M[0][0] = 1 - 2 * (yy + zz);
M[0][1] = 2 * (xy + zw);
M[0][2] = 2 * (xz - yw);
M[0][3] = 0;
M[1][0] = 2 * (xy - zw);
M[1][1] = 1 - 2 * (xx + zz);
M[1][2] = 2 * (yz + xw);
M[1][3] = 0.0;
M[2][0] = 2 * (xz + yw);
M[2][1] = 2 * (yz - xw);
M[2][2] = 1 - 2 * (xx + yy);
M[2][3] = 0.0;
M[3][0] = M[3][1] = M[3][2] = 0.f;
M[3][3] = 1.f;

@ -117,3 +117,12 @@ struct Entity* scene_get_child_by_name(struct Entity* parent, const char* name)
}
return child;
}
struct Entity* scene_get_parent(struct Entity* entity)
{
assert(entity);
struct Entity* parent = NULL;
if(entity->parent != -1)
parent = entity_get(entity->parent);
return parent;
}

@ -12,5 +12,6 @@ struct Entity* scene_add_as_child(const char* name, const char* tag, struct Enti
struct Entity* scene_find(const char* name);
struct Entity* scene_get_root(void);
struct Entity* scene_get_child_by_name(struct Entity* parent, const char* name);
struct Entity* scene_get_parent(struct Entity* entity);
#endif

@ -39,7 +39,8 @@ int transform_create(int node)
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;
mat4_identity(new_transform->rotation);
//mat4_identity(new_transform->rotation);
quat_identity(new_transform->rotq);
transform_update_transmat(new_transform);
}
return index;
@ -48,7 +49,21 @@ int transform_create(int node)
void transform_translate(struct Transform* transform, vec3 amount, enum Transform_Space space)
{
if(space == TS_LOCAL)
mat4_mul_vec3(amount, transform->rotation, amount);
{
mat4 temp;
mat4_identity(temp);
mat4_from_quat(temp, transform->rotq);
mat4_mul_vec3(amount, temp, amount);
//mat4_mul_vec3(amount, transform->rotation, amount);
//quat_mul_vec3(amount, transform->rotq, amount);
}
if(space == TS_PARENT)
{
struct Entity* parent = entity_get_parent(transform->node);
struct Transform* parent_tran = entity_component_get(parent, C_TRANSFORM);
//mat4_mul_vec3(amount, parent_tran->rotation, amount);
quat_mul_vec3(amount, parent_tran->rotq, amount);
}
vec3_add(transform->position, transform->position, amount);
transform_update_transmat(transform);
}
@ -57,23 +72,18 @@ void transform_rotate(struct Transform* transform,
float angle,
enum Transform_Space space)
{
mat4 new_rot;
mat4_identity(new_rot);
//mat4_rotate(new_rot, new_rot, axis[0], axis[1], axis[2], TO_RADIANS(angle));
if(axis[0] == 1)
mat4_rotate_X(new_rot, new_rot, TO_RADIANS(angle));
if(axis[1] == 1)
mat4_rotate_Y(new_rot, new_rot, TO_RADIANS(angle));
if(axis[2] == 1)
mat4_rotate_Z(new_rot, new_rot, TO_RADIANS(angle));
//mat4_orthonormalize(new_rot, new_rot);
//mat4_orthonormalize(new_rot, temp);
//mat4 new_rot;
quat new_rotq;
/* mat4_identity(new_rot); */
/* mat4_rotate(new_rot, new_rot, axis[0], axis[1], axis[2], TO_RADIANS(angle)); */
quat_identity(new_rotq);
quat_rotate(new_rotq, TO_RADIANS(angle), axis);
if(space == TS_LOCAL)
mat4_mul(transform->rotation, transform->rotation, new_rot);
quat_mul(transform->rotq, transform->rotq, new_rotq);
else if(space == TS_WORLD)
mat4_mul(transform->rotation, new_rot, transform->rotation);
//mat4_orthonormalize(transform->rotation, transform->rotation);
quat_mul(transform->rotq, new_rotq, transform->rotq);
quat_norm(transform->rotq, transform->rotq);
transform_update_transmat(transform);
}
@ -88,7 +98,12 @@ void transform_scale(struct Transform* transform, vec3 scale)
void transform_get_forward(struct Transform* transform, vec3 res)
{
res[0] = 0; res[1] = 0; res[2] = -1;
mat4_mul_vec3(res, transform->trans_mat, res);
//mat4_mul_vec3(res, transform->rotation, res);
mat4 temp;
mat4_identity(temp);
mat4_from_quat(temp, transform->rotq);
mat4_mul_vec3(res, temp, res);
//quat_mul_vec3(res, transform->rotq, res);
}
void transform_get_lookat(struct Transform* transform, vec3 res)
@ -97,38 +112,54 @@ void transform_get_lookat(struct Transform* transform, vec3 res)
vec3_add(res, transform->position, res);
}
void transform_get_absolute_forward(struct Transform* transform, vec3 res)
{
res[0] = 0.f; res[0] = 0.f; res[0] = -1.f;
//mat4_mul_vec3(res, transform->trans_mat, res);
quat_mul_vec3(res, transform->rotq, res);
vec3_norm(res, res);
}
void transform_get_absolute_lookat(struct Transform* transform, vec3 res)
{
vec3 abs_position = {0.f};
transform_get_absolute_pos(transform, abs_position);
transform_get_forward(transform, res);
transform_get_absolute_forward(transform, res);
vec3_add(res, abs_position, res);
}
void transform_get_up(struct Transform* transform, vec3 res)
{
res[0] = 0; res[1] = 1; res[2] = 0;
mat4_mul_vec3(res, transform->rotation, res);
//mat4_mul_vec3(res, transform->rotation, res);
mat4 temp;
mat4_identity(temp);
mat4_from_quat(temp, transform->rotq);
mat4_mul_vec3(res, temp, res);
//quat_mul_vec3(res, transform->rotq, res);
}
void transform_get_right(struct Transform* transform, vec3 res)
{
res[0] = 1; res[1] = 0; res[2] = 0;
mat4_mul_vec3(res, transform->rotation, res);
//mat4_mul_vec3(res, transform->rotation, res);
quat_mul_vec3(res, transform->rotq, res);
}
void transform_update_transmat(struct Transform* transform)
{
static mat4 scale, translation;
static mat4 scale, translation, rot;
mat4_identity(scale);
mat4_identity(translation);
mat4_identity(rot);
mat4_identity(transform->trans_mat);
mat4_scale_aniso(scale, scale, transform->scale[0], transform->scale[1], transform->scale[2]);
mat4_translate(translation, transform->position[0], transform->position[1], transform->position[2]);
mat4_from_quat(rot, transform->rotq);
mat4_mul(transform->trans_mat, transform->trans_mat, translation);
mat4_mul(transform->trans_mat, transform->trans_mat, transform->rotation);
//mat4_mul(transform->trans_mat, transform->trans_mat, transform->rotation);
mat4_mul(transform->trans_mat, transform->trans_mat, rot);
mat4_mul(transform->trans_mat, transform->trans_mat, scale);
struct Entity* entity = entity_get(transform->node);

@ -3,15 +3,16 @@
#include "linmath.h"
enum Transform_Space { TS_LOCAL, TS_WORLD};
enum Transform_Space { TS_LOCAL, TS_PARENT, TS_WORLD};
struct Transform
{
int node;
vec3 position;
vec3 scale;
mat4 rotation;
//mat4 rotation;
mat4 trans_mat;
quat rotq;
};
struct Transform* transform_get(int index);

Loading…
Cancel
Save