#ifndef LINMATH_H #define LINMATH_H #if __STDC_VERSION__ < 199901L # ifndef inline # if defined(_MSC_VER) # define inline __inline # elif defined(__GNUC__) # define inline __inline__ # else # define inline # endif # endif #endif #include #include #define LINMATH_H_DEFINE_VEC(n) \ typedef float vec##n[n]; \ static inline void vec##n##_add(vec##n r, vec##n a, vec##n b) \ { \ int i; \ for(i=0; i 1e-4) { vec3_norm(u, u); mat4x4 T; mat4x4_from_vec3_mul_outer(T, u, u); mat4x4 S = { { 0, u[2], -u[1], 0}, {-u[2], 0, u[0], 0}, { u[1], -u[0], 0, 0}, { 0, 0, 0, 0} }; mat4x4_scale(S, S, s); mat4x4 C; mat4x4_identity(C); mat4x4_sub(C, C, T); mat4x4_scale(C, C, c); mat4x4_add(T, T, C); mat4x4_add(T, T, S); T[3][3] = 1.; mat4x4_mul(R, M, T); } else { mat4x4_dup(R, M); } } static inline void mat4x4_rotate_X(mat4x4 Q, mat4x4 M, float angle) { float s = sinf(angle); float c = cosf(angle); mat4x4 R = { {1, 0, 0, 0}, {0, c, s, 0}, {0,-s, c, 0}, {0, 0, 0, 1} }; mat4x4_mul(Q, M, R); } static inline void mat4x4_rotate_Y(mat4x4 Q, mat4x4 M, float angle) { float s = sinf(angle); float c = cosf(angle); mat4x4 R = { { c, 0, s, 0}, { 0, 1, 0, 0}, {-s, 0, c, 0}, { 0, 0, 0, 1} }; mat4x4_mul(Q, M, R); } static inline void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle) { float s = sinf(angle); float c = cosf(angle); mat4x4 R = { { c, s, 0, 0}, {-s, c, 0, 0}, { 0, 0, 1, 0}, { 0, 0, 0, 1} }; mat4x4_mul(Q, M, R); } static inline void mat4x4_row(vec4 r, mat4x4 M, int i) { int k; for(k=0; k<4; ++k) r[k] = M[k][i]; } static inline void mat4x4_col(vec4 r, mat4x4 M, int i) { int k; for(k=0; k<4; ++k) r[k] = M[i][k]; } static inline void mat4x4_transpose(mat4x4 M, mat4x4 N) { int i, j; mat4x4 R; for(j=0; j<4; ++j) { for(i=0; i<4; ++i) { R[i][j] = N[j][i]; } } memcpy(M, R, sizeof(R)); } static inline void mat4x4_invert(mat4x4 T, mat4x4 M) { mat4x4 R; R[0][0] = M[1][1]*(M[2][2]*M[3][3] - M[2][3]*M[3][2]) - M[2][1]*(M[1][2]*M[3][3] - M[1][3]*M[3][2]) - M[3][1]*(M[1][3]*M[2][2] - M[1][2]*M[2][3]); R[0][1] = M[0][1]*(M[2][3]*M[3][2] - M[2][2]*M[3][3]) - M[2][1]*(M[0][3]*M[3][2] - M[0][2]*M[3][3]) - M[3][1]*(M[0][2]*M[2][3] - M[0][3]*M[2][2]); R[0][2] = M[0][1]*(M[1][2]*M[3][3] - M[1][3]*M[3][2]) - M[1][1]*(M[0][2]*M[3][3] - M[0][3]*M[3][2]) - M[3][1]*(M[0][3]*M[1][2] - M[0][2]*M[1][3]); R[0][3] = M[0][1]*(M[1][3]*M[2][2] - M[1][2]*M[2][3]) - M[1][1]*(M[0][3]*M[2][2] - M[0][2]*M[2][3]) - M[2][1]*(M[0][2]*M[1][3] - M[0][3]*M[1][2]); R[1][0] = M[1][0]*(M[2][3]*M[3][2] - M[2][2]*M[3][3]) - M[2][0]*(M[1][3]*M[3][2] - M[1][2]*M[3][3]) - M[3][0]*(M[1][2]*M[2][3] - M[1][3]*M[2][2]); R[1][1] = M[0][0]*(M[2][2]*M[3][3] - M[2][3]*M[3][2]) - M[2][0]*(M[0][2]*M[3][3] - M[0][3]*M[3][2]) - M[3][0]*(M[0][3]*M[2][2] - M[0][2]*M[2][3]); R[1][2] = M[0][0]*(M[1][3]*M[3][2] - M[1][2]*M[3][3]) - M[1][0]*(M[0][3]*M[3][2] - M[0][2]*M[3][3]) - M[3][0]*(M[0][2]*M[1][3] - M[0][3]*M[1][2]); R[1][3] = M[0][0]*(M[1][2]*M[2][3] - M[1][3]*M[2][2]) - M[1][0]*(M[0][2]*M[2][3] - M[0][3]*M[2][2]) - M[2][0]*(M[0][3]*M[1][2] - M[0][2]*M[1][3]); R[2][0] = M[1][0]*(M[2][1]*M[3][3] - M[2][3]*M[3][1]) - M[2][0]*(M[1][1]*M[3][3] - M[1][3]*M[3][1]) - M[3][0]*(M[1][3]*M[2][1] - M[1][1]*M[2][3]); R[2][1] = M[0][0]*(M[2][3]*M[3][1] - M[2][1]*M[3][3]) - M[2][0]*(M[0][3]*M[3][1] - M[0][1]*M[3][3]) - M[3][0]*(M[0][1]*M[2][3] - M[0][3]*M[2][1]); R[2][2] = M[0][0]*(M[1][1]*M[3][3] - M[1][3]*M[3][1]) - M[1][0]*(M[0][1]*M[3][3] - M[0][3]*M[3][1]) - M[3][0]*(M[0][3]*M[1][1] - M[0][1]*M[1][3]); R[2][3] = M[0][0]*(M[1][3]*M[2][1] - M[1][1]*M[2][3]) - M[1][0]*(M[0][3]*M[2][1] - M[0][1]*M[2][3]) - M[2][0]*(M[0][1]*M[1][3] - M[0][3]*M[1][1]); R[3][0] = M[1][0]*(M[2][2]*M[3][1] - M[2][1]*M[3][2]) - M[2][0]*(M[1][2]*M[3][1] - M[1][1]*M[3][2]) - M[3][0]*(M[1][1]*M[2][2] - M[1][2]*M[2][1]); R[3][1] = M[0][0]*(M[2][1]*M[3][2] - M[2][2]*M[3][1]) - M[2][0]*(M[0][1]*M[3][2] - M[0][2]*M[3][1]) - M[3][0]*(M[0][2]*M[2][1] - M[0][1]*M[2][2]); R[3][2] = M[0][0]*(M[1][2]*M[3][1] - M[1][1]*M[3][2]) - M[1][0]*(M[0][2]*M[3][1] - M[0][1]*M[3][2]) - M[3][0]*(M[0][1]*M[1][2] - M[0][2]*M[1][1]); R[3][3] = M[0][0]*(M[1][1]*M[2][2] - M[1][2]*M[2][1]) - M[1][0]*(M[0][1]*M[2][2] - M[0][2]*M[2][1]) - M[2][0]*(M[0][2]*M[1][1] - M[0][1]*M[1][2]); memcpy(T, R, 16*sizeof(float)); } static inline void mat4x4_orthonormalize(mat4x4 R, mat4x4 M) { mat4x4_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 mat4x4_frustum(mat4x4 M, float l, float r, float b, float t, float n, float f) { M[0][0] = 2.*n/(r-l); M[0][1] = M[0][2] = M[0][3] = 0.; M[1][1] = 2.*n/(t-b); M[1][0] = M[1][2] = M[1][3] = 0.; 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; M[3][2] = -2.*(f*n)/(f-n); M[3][0] = M[3][1] = M[3][3] = 0.; } static inline void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f) { M[0][0] = 2./(r-l); M[0][1] = M[0][2] = M[0][3] = 0.; M[1][1] = 2./(t-b); M[1][0] = M[1][2] = M[1][3] = 0.; M[2][2] = -2./(f-n); M[2][0] = M[2][1] = M[2][3] = 0.; 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.; } typedef float quat[4]; static inline void quat_identity(quat q) { q[0] = q[1] = q[2] = 0.; q[3] = 1.; } 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); } 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.; 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_norm(quat r, quat v) { vec4_norm(r, v); } static inline void quat_mul_vec3(vec3 r, quat q, vec3 v) { quat q_; quat v_ = {v[0], v[1], v[2], 0.}; quat_conj(q_, q); quat_norm(q_, q_); quat_mul(q_, v_, q_); quat_mul(q_, q, q_); memcpy(r, q_, 3*sizeof(float)); } static inline void mat4x4_from_quat(mat4x4 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*(b*c + a*d); M[0][2] = 2*(b*d - a*c); M[0][3] = 0.; M[1][0] = 2*(b*c - a*d); M[1][1] = a2 - b2 + c2 - d2; M[1][2] = 2*(c*d + a*b); M[1][3] = 0.; M[2][0] = 2*(b*d + a*c); M[2][1] = 2*(c*d - a*b); M[2][2] = a2 - b2 - c2 + d2; M[2][3] = 0.; M[3][0] = M[3][1] = M[3][2] = 0.; M[3][3] = 1.; } static inline void mat4x4_mul_quat(mat4x4 R, mat4x4 M, quat q) { quat_mul_vec3(R[0], M[0], q); quat_mul_vec3(R[1], M[1], q); quat_mul_vec3(R[2], M[2], q); R[3][0] = R[3][1] = R[3][2] = 0.; R[3][3] = 1.; } static inline void quat_from_mat4x4(quat q, mat4x4 M) { float r=0.; 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. + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]] ); q[0] = r/2.; q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]])/(2.*r); q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]])/(2.*r); q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]])/(2.*r); } static inline void mat4x4_arcball(mat4x4 R, mat4x4 M, vec2 _a, vec2 _b, float s) { vec2 a; memcpy(a, _a, sizeof(a)); vec2 b; memcpy(b, _b, sizeof(b)); float z_a = 0.; float z_b = 0.; if(vec2_len(a) < 1.) { z_a = sqrtf(1. - vec2_mul_inner(a, a)); } else { vec2_norm(a, a); } if(vec2_len(b) < 1.) { z_b = sqrtf(1. - vec2_mul_inner(b, b)); } else { vec2_norm(b, b); } vec3 a_ = {a[0], a[1], z_a}; vec3 b_ = {b[0], b[1], z_b}; vec3 c_; vec3_mul_cross(c_, a_, b_); float const angle = acos(vec3_mul_inner(a_, b_)) * s; mat4x4_rotate(R, M, c_[0], c_[1], c_[2], angle); } #endif