diff --git a/orgfile.org b/orgfile.org index 6638eb6..aa739b5 100644 --- a/orgfile.org +++ b/orgfile.org @@ -46,7 +46,8 @@ while using as few libraries as possible. ** TODO Materials ** TODO Mesh/Model ** TODO Add modifiers to input maps to enable combinations for example, c-x, m-k etc -** TODO Heirarchical Transforms +** DONE Heirarchical Transforms +- State "DONE" from "TODO" [2015-09-14 Mon 20:55] ** TODO 2d drawing routines ** TODO Gui ** TODO Physics diff --git a/src/game.c b/src/game.c index d276429..0ce43c0 100644 --- a/src/game.c +++ b/src/game.c @@ -70,9 +70,6 @@ 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); */ entity_component_add(player, C_CAMERA, 800, 600); struct Entity* new_ent = scene_add_new("Model_Entity", NULL); diff --git a/src/linmath.h b/src/linmath.h index 8340f75..e2b8521 100644 --- a/src/linmath.h +++ b/src/linmath.h @@ -1,8 +1,6 @@ #ifndef LINMATH_H #define LINMATH_H -/* Credit : https://github.com/datenwolf/linmath.h */ - #include "../include/kazmath/kazmath/kazmath.h" #include "../include/kazmath/kazmath/vec4.h" #include @@ -65,662 +63,9 @@ typedef kmQuaternion quat; #define quat_get_up kmQuaternionGetUpVec3 #define quat_get_right kmQuaternionGetRightVec3 - +/* Conversions */ #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]; \ */ -/* static inline void vec##n##_add(vec##n r, vec##n const a, vec##n const b) \ */ -/* { \ */ -/* int i; \ */ -/* for(i=0; i 1e-4) { */ -/* vec3_norm(u, u); */ -/* mat4 T; */ -/* mat4_from_vec3_mul_outer(T, u, u); */ - -/* mat4 S = { */ -/* { 0, u[2], -u[1], 0}, */ -/* {-u[2], 0, u[0], 0}, */ -/* { u[1], -u[0], 0, 0}, */ -/* { 0, 0, 0, 0} */ -/* }; */ -/* mat4_scale(S, S, s); */ - -/* mat4 C; */ -/* mat4_identity(C); */ -/* mat4_sub(C, C, T); */ - -/* mat4_scale(C, C, c); */ - -/* mat4_add(T, T, C); */ -/* mat4_add(T, T, S); */ - -/* T[3][3] = 1.; */ -/* mat4_mul(R, M, T); */ -/* } else { */ -/* mat4_dup(R, M); */ -/* } */ -/* } */ -/* static inline void mat4_rotate_X(mat4 Q, mat4 M, float angle) */ -/* { */ -/* float s = sinf(angle); */ -/* float c = cosf(angle); */ -/* mat4 R = { */ -/* {1.f, 0.f, 0.f, 0.f}, */ -/* {0.f, c, s, 0.f}, */ -/* {0.f, -s, c, 0.f}, */ -/* {0.f, 0.f, 0.f, 1.f} */ -/* }; */ -/* mat4_mul(Q, M, R); */ -/* } */ -/* static inline void mat4_rotate_Y(mat4 Q, mat4 M, float angle) */ -/* { */ -/* float s = sinf(angle); */ -/* float c = cosf(angle); */ -/* mat4 R = { */ -/* { c, 0.f, s, 0.f}, */ -/* { 0.f, 1.f, 0.f, 0.f}, */ -/* { -s, 0.f, c, 0.f}, */ -/* { 0.f, 0.f, 0.f, 1.f} */ -/* }; */ -/* mat4_mul(Q, M, R); */ -/* } */ -/* static inline void mat4_rotate_Z(mat4 Q, mat4 M, float angle) */ -/* { */ -/* float s = sinf(angle); */ -/* float c = cosf(angle); */ -/* mat4 R = { */ -/* { c, s, 0.f, 0.f}, */ -/* { -s, c, 0.f, 0.f}, */ -/* { 0.f, 0.f, 1.f, 0.f}, */ -/* { 0.f, 0.f, 0.f, 1.f} */ -/* }; */ -/* mat4_mul(Q, M, R); */ -/* } */ -/* static inline void mat4_invert(mat4 T, mat4 M) */ -/* { */ -/* float s[6]; */ -/* float c[6]; */ -/* s[0] = M[0][0]*M[1][1] - M[1][0]*M[0][1]; */ -/* s[1] = M[0][0]*M[1][2] - M[1][0]*M[0][2]; */ -/* s[2] = M[0][0]*M[1][3] - M[1][0]*M[0][3]; */ -/* s[3] = M[0][1]*M[1][2] - M[1][1]*M[0][2]; */ -/* s[4] = M[0][1]*M[1][3] - M[1][1]*M[0][3]; */ -/* s[5] = M[0][2]*M[1][3] - M[1][2]*M[0][3]; */ - -/* c[0] = M[2][0]*M[3][1] - M[3][0]*M[2][1]; */ -/* c[1] = M[2][0]*M[3][2] - M[3][0]*M[2][2]; */ -/* c[2] = M[2][0]*M[3][3] - M[3][0]*M[2][3]; */ -/* c[3] = M[2][1]*M[3][2] - M[3][1]*M[2][2]; */ -/* c[4] = M[2][1]*M[3][3] - M[3][1]*M[2][3]; */ -/* c[5] = M[2][2]*M[3][3] - M[3][2]*M[2][3]; */ - -/* /\* Assumes it is invertible *\/ */ -/* float idet = 1.0f/( s[0]*c[5]-s[1]*c[4]+s[2]*c[3]+s[3]*c[2]-s[4]*c[1]+s[5]*c[0] ); */ - -/* T[0][0] = ( M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet; */ -/* T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet; */ -/* T[0][2] = ( M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet; */ -/* T[0][3] = (-M[2][1] * s[5] + M[2][2] * s[4] - M[2][3] * s[3]) * idet; */ - -/* T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet; */ -/* T[1][1] = ( M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet; */ -/* T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet; */ -/* T[1][3] = ( M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet; */ - -/* T[2][0] = ( M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet; */ -/* T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet; */ -/* T[2][2] = ( M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet; */ -/* T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet; */ - -/* T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet; */ -/* T[3][1] = ( M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet; */ -/* T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet; */ -/* T[3][3] = ( M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet; */ -/* } */ -/* static inline void mat4_orthonormalize(mat4 R, mat4 M) */ -/* { */ -/* mat4_dup(R, M); */ -/* float s = 1.; */ -/* vec3 h; */ - -/* vec3_norm(R[2], R[2]); */ - -/* s = vec3_mul_inner(R[1], R[2]); */ -/* vec3_scale(h, R[2], s); */ -/* vec3_sub(R[1], R[1], h); */ -/* vec3_norm(R[2], R[2]); */ - -/* s = vec3_mul_inner(R[1], R[2]); */ -/* vec3_scale(h, R[2], s); */ -/* vec3_sub(R[1], R[1], h); */ -/* vec3_norm(R[1], R[1]); */ - -/* s = vec3_mul_inner(R[0], R[1]); */ -/* vec3_scale(h, R[1], s); */ -/* vec3_sub(R[0], R[0], h); */ -/* vec3_norm(R[0], R[0]); */ -/* } */ - -/* static inline void mat4_frustum(mat4 M, float l, float r, float b, float t, float n, float f) */ -/* { */ -/* M[0][0] = 2.f*n/(r-l); */ -/* M[0][1] = M[0][2] = M[0][3] = 0.f; */ - -/* M[1][1] = 2.*n/(t-b); */ -/* M[1][0] = M[1][2] = M[1][3] = 0.f; */ - -/* M[2][0] = (r+l)/(r-l); */ -/* M[2][1] = (t+b)/(t-b); */ -/* M[2][2] = -(f+n)/(f-n); */ -/* M[2][3] = -1.f; */ - -/* M[3][2] = -2.f*(f*n)/(f-n); */ -/* M[3][0] = M[3][1] = M[3][3] = 0.f; */ -/* } */ -/* static inline void mat4_ortho(mat4 M, float l, float r, float b, float t, float n, float f) */ -/* { */ -/* M[0][0] = 2.f/(r-l); */ -/* M[0][1] = M[0][2] = M[0][3] = 0.f; */ - -/* M[1][1] = 2.f/(t-b); */ -/* M[1][0] = M[1][2] = M[1][3] = 0.f; */ - -/* M[2][2] = -2.f/(f-n); */ -/* M[2][0] = M[2][1] = M[2][3] = 0.f; */ - -/* M[3][0] = -(r+l)/(r-l); */ -/* M[3][1] = -(t+b)/(t-b); */ -/* M[3][2] = -(f+n)/(f-n); */ -/* M[3][3] = 1.f; */ -/* } */ -/* static inline void mat4_perspective(mat4 m, float y_fov, float aspect, float n, float f) */ -/* { */ -/* /\* NOTE: Degrees are an unhandy unit to work with. */ -/* * linmath.h uses radians for everything! *\/ */ -/* float const a = 1.f / tan(y_fov / 2.f); */ - -/* m[0][0] = a / aspect; */ -/* m[0][1] = 0.f; */ -/* m[0][2] = 0.f; */ -/* m[0][3] = 0.f; */ - -/* m[1][0] = 0.f; */ -/* m[1][1] = a; */ -/* m[1][2] = 0.f; */ -/* m[1][3] = 0.f; */ - -/* m[2][0] = 0.f; */ -/* m[2][1] = 0.f; */ -/* m[2][2] = -((f + n) / (f - n)); */ -/* m[2][3] = -1.f; */ - -/* m[3][0] = 0.f; */ -/* m[3][1] = 0.f; */ -/* m[3][2] = -((2.f * f * n) / (f - n)); */ -/* m[3][3] = 0.f; */ -/* } */ -/* static inline void mat4_look_at(mat4 m, vec3 eye, vec3 lookat, vec3 up) */ -/* { */ -/* /\* Adapted from Android's OpenGL Matrix.java. *\/ */ -/* /\* See the OpenGL GLUT documentation for gluLookAt for a description *\/ */ -/* /\* of the algorithm. We implement it in a straightforward way: *\/ */ - -/* /\* TODO: The negation of of can be spared by swapping the order of */ -/* * operands in the following cross products in the right way. *\/ */ -/* vec3 f; */ -/* vec3_sub(f, lookat, eye); */ -/* vec3_norm(f, f); */ - -/* vec3 s; */ -/* vec3_mul_cross(s, f, up); */ -/* vec3_norm(s, s); */ - -/* vec3 t; */ -/* vec3_mul_cross(t, s, f); */ - -/* m[0][0] = s[0]; */ -/* m[1][0] = s[1]; */ -/* m[2][0] = s[2]; */ - -/* m[0][1] = t[0]; */ -/* m[1][1] = t[1]; */ -/* m[2][1] = t[2]; */ - -/* m[0][2] = -f[0]; */ -/* m[1][2] = -f[1]; */ -/* m[2][2] = -f[2]; */ - -/* m[0][3] = 0.f; */ -/* m[1][3] = 0.f; */ -/* m[2][3] = 0.f; */ - -/* m[3][0] = -vec3_mul_inner(s, eye); */ -/* m[3][1] = -vec3_mul_inner(t, eye); */ -/* m[3][2] = vec3_mul_inner(f, eye); */ -/* m[3][3] = 1.f; */ - -/* //mat4_translate_in_place(m, -eye[0], -eye[1], -eye[2]); */ -/* } */ - -/* typedef float quat[4]; */ -/* static inline void quat_identity(quat q) */ -/* { */ -/* q[0] = q[1] = q[2] = 0.f; */ -/* q[3] = 1.f; */ -/* } */ -/* static inline void quat_add(quat r, quat a, quat b) */ -/* { */ -/* int i; */ -/* for(i=0; i<4; ++i) */ -/* r[i] = a[i] + b[i]; */ -/* } */ -/* static inline void quat_sub(quat r, quat a, quat b) */ -/* { */ -/* int i; */ -/* for(i=0; i<4; ++i) */ -/* r[i] = a[i] - b[i]; */ -/* } */ -/* 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); *\/ */ - -/* 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) */ -/* { */ -/* int i; */ -/* for(i=0; i<4; ++i) */ -/* r[i] = v[i] * s; */ -/* } */ -/* static inline float quat_inner_product(quat a, quat b) */ -/* { */ -/* float p = 0.f; */ -/* int i; */ -/* for(i=0; i<4; ++i) */ -/* p += b[i]*a[i]; */ -/* return p; */ -/* } */ -/* static inline void quat_conj(quat r, quat q) */ -/* { */ -/* int i; */ -/* for(i=0; i<3; ++i) */ -/* r[i] = -q[i]; */ -/* r[3] = q[3]; */ -/* } */ -/* static inline void quat_rotate(quat r, float angle, vec3 axis) { */ -/* vec3 v; */ -/* 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.f); */ -/* } */ -/* #define quat_norm vec4_norm */ -/* static inline void quat_mul_vec3(vec3 r, quat q, vec3 v) */ -/* { */ -/* /\* */ -/* * Method by Fabian 'ryg' Giessen (of Farbrausch) */ -/* t = 2 * cross(q.xyz, v) */ -/* v' = v + q.w * t + cross(q.xyz, t) */ -/* *\/ */ -/* vec3 t = {q[0], q[1], q[2]}; */ -/* vec3 u = {q[0], q[1], q[2]}; */ - -/* vec3_mul_cross(t, t, v); */ -/* vec3_scale(t, t, 2.f); */ - -/* vec3_mul_cross(u, u, t); */ -/* vec3_scale(t, t, q[3]); */ - -/* 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; *\/ */ - -/* /\* 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 yy = q[1] * q[1]; */ -/* float yz = q[1] * q[2]; */ - -/* float zz = q[2] * q[2]; */ -/* float wz = q[3] * q[2]; */ -/* float wy = q[3] * q[1]; */ -/* float wx = q[3] * q[0]; */ - -/* M[0][0] = 1 - 2 * (yy + zz); */ -/* M[0][1] = 2 * (xy + wz); */ -/* M[0][2] = 2 * (xz - wy); */ -/* M[0][3] = 0; */ - -/* M[1][0] = 2 * (xy - wz); */ -/* M[1][1] = 1 - 2 * (xx + zz); */ -/* M[1][2] = 2 * (yz + wx); */ -/* M[1][3] = 0.0; */ - -/* M[2][0] = 2 * (xz + wy); */ -/* M[2][1] = 2 * (yz - wx); */ -/* 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; */ -/* } */ - -/* static inline void mat4o_mul_quat(mat4 R, mat4 M, quat q) */ -/* { */ -/* /\* XXX: The way this is written only works for othogonal matrices. *\/ */ -/* /\* TODO: Take care of non-orthogonal case. *\/ */ -/* quat_mul_vec3(R[0], q, M[0]); */ -/* quat_mul_vec3(R[1], q, M[1]); */ -/* quat_mul_vec3(R[2], q, M[2]); */ - -/* R[3][0] = R[3][1] = R[3][2] = 0.f; */ -/* R[3][3] = 1.f; */ -/* } */ -/* static inline void quat_from_mat4(quat q, mat4 M) */ -/* { */ -/* float r=0.f; */ -/* int i; */ - -/* int perm[] = { 0, 1, 2, 0, 1 }; */ -/* int *p = perm; */ - -/* for(i = 0; i<3; i++) { */ -/* float m = M[i][i]; */ -/* if( m < r ) */ -/* continue; */ -/* m = r; */ -/* p = &perm[i]; */ -/* } */ - -/* r = sqrtf(1.f + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]] ); */ - -/* if(r < 1e-6) { */ -/* q[0] = 1.f; */ -/* q[1] = q[2] = q[3] = 0.f; */ -/* return; */ -/* } */ - -/* q[0] = r/2.f; */ -/* q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]])/(2.f*r); */ -/* q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]])/(2.f*r); */ -/* q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]])/(2.f*r); */ -/* } */ - #endif diff --git a/src/transform.c b/src/transform.c index ee3d453..c078de1 100644 --- a/src/transform.c +++ b/src/transform.c @@ -219,4 +219,3 @@ void transform_get_absolute_rot(struct Transform* transform, quat* res) } quat_mul(res, res, &transform->rotation); } -