istereo
changeset 29:fb4c9641059f
added more forgotten files
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 08 Sep 2011 08:31:15 +0300 |
parents | c0ae8e668447 |
children | 8dd271942543 |
files | libs/vmath/matrix.inl libs/vmath/quat.inl libs/vmath/ray.inl libs/vmath/vector.inl libs/vmath/vmath.inl sdr/text.p.glsl sdr/text.v.glsl sdr/tunnel.p.glsl sdr/tunnel.v.glsl |
diffstat | 9 files changed, 1156 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/vmath/matrix.inl Thu Sep 08 08:31:15 2011 +0300 1.3 @@ -0,0 +1,147 @@ 1.4 +#include <string.h> 1.5 + 1.6 +#ifdef __cplusplus 1.7 +extern "C" { 1.8 +#endif /* __cplusplus */ 1.9 + 1.10 +/* C matrix 3x3 functions */ 1.11 +static inline void m3_identity(mat3_t m) 1.12 +{ 1.13 + static const mat3_t id = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; 1.14 + memcpy(m, id, sizeof id); 1.15 +} 1.16 + 1.17 +static inline void m3_cons(mat3_t m, 1.18 + scalar_t m11, scalar_t m12, scalar_t m13, 1.19 + scalar_t m21, scalar_t m22, scalar_t m23, 1.20 + scalar_t m31, scalar_t m32, scalar_t m33) 1.21 +{ 1.22 + m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; 1.23 + m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; 1.24 + m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; 1.25 +} 1.26 + 1.27 +static inline void m3_copy(mat3_t dest, mat3_t src) 1.28 +{ 1.29 + memcpy(dest, src, sizeof(mat3_t)); 1.30 +} 1.31 + 1.32 + 1.33 +/* C matrix 4x4 functions */ 1.34 +static inline void m4_identity(mat4_t m) 1.35 +{ 1.36 + static const mat4_t id = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; 1.37 + memcpy(m, id, sizeof id); 1.38 +} 1.39 + 1.40 +static inline void m4_cons(mat4_t m, 1.41 + scalar_t m11, scalar_t m12, scalar_t m13, scalar_t m14, 1.42 + scalar_t m21, scalar_t m22, scalar_t m23, scalar_t m24, 1.43 + scalar_t m31, scalar_t m32, scalar_t m33, scalar_t m34, 1.44 + scalar_t m41, scalar_t m42, scalar_t m43, scalar_t m44) 1.45 +{ 1.46 + m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14; 1.47 + m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24; 1.48 + m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34; 1.49 + m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44; 1.50 +} 1.51 + 1.52 +static inline void m4_copy(mat4_t dest, mat4_t src) 1.53 +{ 1.54 + memcpy(dest, src, sizeof(mat4_t)); 1.55 +} 1.56 + 1.57 +static inline void m4_mult(mat4_t res, mat4_t m1, mat4_t m2) 1.58 +{ 1.59 + res[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m2[3][0]; 1.60 + res[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1] + m1[0][2] * m2[2][1] + m1[0][3] * m2[3][1]; 1.61 + res[0][2] = m1[0][0] * m2[0][2] + m1[0][1] * m2[1][2] + m1[0][2] * m2[2][2] + m1[0][3] * m2[3][2]; 1.62 + res[0][3] = m1[0][0] * m2[0][3] + m1[0][1] * m2[1][3] + m1[0][2] * m2[2][3] + m1[0][3] * m2[3][3]; 1.63 + 1.64 + res[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0] + m1[1][2] * m2[2][0] + m1[1][3] * m2[3][0]; 1.65 + res[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1] + m1[1][2] * m2[2][1] + m1[1][3] * m2[3][1]; 1.66 + res[1][2] = m1[1][0] * m2[0][2] + m1[1][1] * m2[1][2] + m1[1][2] * m2[2][2] + m1[1][3] * m2[3][2]; 1.67 + res[1][3] = m1[1][0] * m2[0][3] + m1[1][1] * m2[1][3] + m1[1][2] * m2[2][3] + m1[1][3] * m2[3][3]; 1.68 + 1.69 + res[2][0] = m1[2][0] * m2[0][0] + m1[2][1] * m2[1][0] + m1[2][2] * m2[2][0] + m1[2][3] * m2[3][0]; 1.70 + res[2][1] = m1[2][0] * m2[0][1] + m1[2][1] * m2[1][1] + m1[2][2] * m2[2][1] + m1[2][3] * m2[3][1]; 1.71 + res[2][2] = m1[2][0] * m2[0][2] + m1[2][1] * m2[1][2] + m1[2][2] * m2[2][2] + m1[2][3] * m2[3][2]; 1.72 + res[2][3] = m1[2][0] * m2[0][3] + m1[2][1] * m2[1][3] + m1[2][2] * m2[2][3] + m1[2][3] * m2[3][3]; 1.73 + 1.74 + res[3][0] = m1[3][0] * m2[0][0] + m1[3][1] * m2[1][0] + m1[3][2] * m2[2][0] + m1[3][3] * m2[3][0]; 1.75 + res[3][1] = m1[3][0] * m2[0][1] + m1[3][1] * m2[1][1] + m1[3][2] * m2[2][1] + m1[3][3] * m2[3][1]; 1.76 + res[3][2] = m1[3][0] * m2[0][2] + m1[3][1] * m2[1][2] + m1[3][2] * m2[2][2] + m1[3][3] * m2[3][2]; 1.77 + res[3][3] = m1[3][0] * m2[0][3] + m1[3][1] * m2[1][3] + m1[3][2] * m2[2][3] + m1[3][3] * m2[3][3]; 1.78 +} 1.79 + 1.80 +static inline void m4_set_column(mat4_t m, vec4_t v, int idx) 1.81 +{ 1.82 + m[0][idx] = v.x; 1.83 + m[1][idx] = v.y; 1.84 + m[2][idx] = v.z; 1.85 + m[3][idx] = v.w; 1.86 +} 1.87 + 1.88 +static inline void m4_set_row(mat4_t m, vec4_t v, int idx) 1.89 +{ 1.90 + m[idx][0] = v.x; 1.91 + m[idx][1] = v.y; 1.92 + m[idx][2] = v.z; 1.93 + m[idx][3] = v.w; 1.94 +} 1.95 + 1.96 +#ifdef __cplusplus 1.97 +} /* extern "C" */ 1.98 + 1.99 + 1.100 +/* unrolled to hell and inline */ 1.101 +inline Matrix4x4 operator *(const Matrix4x4 &m1, const Matrix4x4 &m2) { 1.102 + Matrix4x4 res; 1.103 + 1.104 + res.m[0][0] = m1.m[0][0] * m2.m[0][0] + m1.m[0][1] * m2.m[1][0] + m1.m[0][2] * m2.m[2][0] + m1.m[0][3] * m2.m[3][0]; 1.105 + res.m[0][1] = m1.m[0][0] * m2.m[0][1] + m1.m[0][1] * m2.m[1][1] + m1.m[0][2] * m2.m[2][1] + m1.m[0][3] * m2.m[3][1]; 1.106 + res.m[0][2] = m1.m[0][0] * m2.m[0][2] + m1.m[0][1] * m2.m[1][2] + m1.m[0][2] * m2.m[2][2] + m1.m[0][3] * m2.m[3][2]; 1.107 + res.m[0][3] = m1.m[0][0] * m2.m[0][3] + m1.m[0][1] * m2.m[1][3] + m1.m[0][2] * m2.m[2][3] + m1.m[0][3] * m2.m[3][3]; 1.108 + 1.109 + res.m[1][0] = m1.m[1][0] * m2.m[0][0] + m1.m[1][1] * m2.m[1][0] + m1.m[1][2] * m2.m[2][0] + m1.m[1][3] * m2.m[3][0]; 1.110 + res.m[1][1] = m1.m[1][0] * m2.m[0][1] + m1.m[1][1] * m2.m[1][1] + m1.m[1][2] * m2.m[2][1] + m1.m[1][3] * m2.m[3][1]; 1.111 + res.m[1][2] = m1.m[1][0] * m2.m[0][2] + m1.m[1][1] * m2.m[1][2] + m1.m[1][2] * m2.m[2][2] + m1.m[1][3] * m2.m[3][2]; 1.112 + res.m[1][3] = m1.m[1][0] * m2.m[0][3] + m1.m[1][1] * m2.m[1][3] + m1.m[1][2] * m2.m[2][3] + m1.m[1][3] * m2.m[3][3]; 1.113 + 1.114 + res.m[2][0] = m1.m[2][0] * m2.m[0][0] + m1.m[2][1] * m2.m[1][0] + m1.m[2][2] * m2.m[2][0] + m1.m[2][3] * m2.m[3][0]; 1.115 + res.m[2][1] = m1.m[2][0] * m2.m[0][1] + m1.m[2][1] * m2.m[1][1] + m1.m[2][2] * m2.m[2][1] + m1.m[2][3] * m2.m[3][1]; 1.116 + res.m[2][2] = m1.m[2][0] * m2.m[0][2] + m1.m[2][1] * m2.m[1][2] + m1.m[2][2] * m2.m[2][2] + m1.m[2][3] * m2.m[3][2]; 1.117 + res.m[2][3] = m1.m[2][0] * m2.m[0][3] + m1.m[2][1] * m2.m[1][3] + m1.m[2][2] * m2.m[2][3] + m1.m[2][3] * m2.m[3][3]; 1.118 + 1.119 + res.m[3][0] = m1.m[3][0] * m2.m[0][0] + m1.m[3][1] * m2.m[1][0] + m1.m[3][2] * m2.m[2][0] + m1.m[3][3] * m2.m[3][0]; 1.120 + res.m[3][1] = m1.m[3][0] * m2.m[0][1] + m1.m[3][1] * m2.m[1][1] + m1.m[3][2] * m2.m[2][1] + m1.m[3][3] * m2.m[3][1]; 1.121 + res.m[3][2] = m1.m[3][0] * m2.m[0][2] + m1.m[3][1] * m2.m[1][2] + m1.m[3][2] * m2.m[2][2] + m1.m[3][3] * m2.m[3][2]; 1.122 + res.m[3][3] = m1.m[3][0] * m2.m[0][3] + m1.m[3][1] * m2.m[1][3] + m1.m[3][2] * m2.m[2][3] + m1.m[3][3] * m2.m[3][3]; 1.123 + 1.124 + return res; 1.125 +} 1.126 + 1.127 +inline scalar_t *Matrix3x3::operator [](int index) { 1.128 + return m[index]; 1.129 +} 1.130 + 1.131 +inline const scalar_t *Matrix3x3::operator [](int index) const { 1.132 + return m[index]; 1.133 +} 1.134 + 1.135 +inline void Matrix3x3::reset_identity() { 1.136 + memcpy(this->m, identity.m, 9 * sizeof(scalar_t)); 1.137 +} 1.138 + 1.139 +inline scalar_t *Matrix4x4::operator [](int index) { 1.140 + return m[index]; 1.141 +} 1.142 + 1.143 +inline const scalar_t *Matrix4x4::operator [](int index) const { 1.144 + return m[index]; 1.145 +} 1.146 + 1.147 +inline void Matrix4x4::reset_identity() { 1.148 + memcpy(this->m, identity.m, 16 * sizeof(scalar_t)); 1.149 +} 1.150 +#endif /* __cplusplus */
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/libs/vmath/quat.inl Thu Sep 08 08:31:15 2011 +0300 2.3 @@ -0,0 +1,63 @@ 2.4 +#include "vector.h" 2.5 +#include "matrix.h" 2.6 + 2.7 +#ifdef __cplusplus 2.8 +extern "C" { 2.9 +#endif /* __cplusplus */ 2.10 + 2.11 +static inline quat_t quat_mul(quat_t q1, quat_t q2) 2.12 +{ 2.13 + quat_t res; 2.14 + vec3_t v1 = quat_vec(q1); 2.15 + vec3_t v2 = quat_vec(q2); 2.16 + 2.17 + res.w = q1.w * q2.w - v3_dot(v1, v2); 2.18 + /* resvec = v2 * q1 + v1 * q2 + cross(v1, v2) */ 2.19 + res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y); 2.20 + res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z); 2.21 + res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x); 2.22 + return res; 2.23 +} 2.24 + 2.25 +static inline quat_t quat_conjugate(quat_t q) 2.26 +{ 2.27 + q.x = -q.x; 2.28 + q.y = -q.y; 2.29 + q.z = -q.z; 2.30 + return q; 2.31 +} 2.32 + 2.33 +static inline quat_t quat_inverse(quat_t q) 2.34 +{ 2.35 + scalar_t lensq = quat_length_sq(q); 2.36 + q = quat_conjugate(q); 2.37 + q.x /= lensq; 2.38 + q.y /= lensq; 2.39 + q.z /= lensq; 2.40 + q.w /= lensq; 2.41 + return q; 2.42 +} 2.43 + 2.44 +static inline void quat_to_mat3(mat3_t res, quat_t q) 2.45 +{ 2.46 + m3_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z, 2.0 * q.z * q.x - 2.0 * q.w * q.y, 2.47 + 2.0 * q.x * q.y - 2.0 * q.w * q.z, 1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x, 2.48 + 2.0 * q.z * q.x + 2.0 * q.w * q.y, 2.0 * q.y * q.z - 2.0 * q.w * q.x, 1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y); 2.49 +} 2.50 + 2.51 +static inline void quat_to_mat4(mat4_t res, quat_t q) 2.52 +{ 2.53 + m4_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z, 2.0 * q.z * q.x - 2.0 * q.w * q.y, 0, 2.54 + 2.0 * q.x * q.y - 2.0 * q.w * q.z, 1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x, 0, 2.55 + 2.0 * q.z * q.x + 2.0 * q.w * q.y, 2.0 * q.y * q.z - 2.0 * q.w * q.x, 1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y, 0, 2.56 + 0, 0, 0, 1); 2.57 +} 2.58 + 2.59 +#ifdef __cplusplus 2.60 +} /* extern "C" */ 2.61 + 2.62 +inline Quaternion lerp(const Quaternion &a, const Quaternion &b, scalar_t t) 2.63 +{ 2.64 + return slerp(a, b, t); 2.65 +} 2.66 +#endif /* __cplusplus */
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/libs/vmath/ray.inl Thu Sep 08 08:31:15 2011 +0300 3.3 @@ -0,0 +1,52 @@ 3.4 +#ifdef __cplusplus 3.5 +extern "C" { 3.6 +#endif /* __cplusplus */ 3.7 + 3.8 +static inline ray_t ray_cons(vec3_t origin, vec3_t dir) 3.9 +{ 3.10 + ray_t r; 3.11 + r.origin = origin; 3.12 + r.dir = dir; 3.13 + return r; 3.14 +} 3.15 + 3.16 +#ifdef __cplusplus 3.17 +} 3.18 + 3.19 +inline Ray reflect_ray(const Ray &inray, const Vector3 &norm) 3.20 +{ 3.21 + Ray ray = inray; 3.22 + ray.iter--; 3.23 + ray.dir = ray.dir.reflection(norm); 3.24 + return ray; 3.25 +} 3.26 + 3.27 +inline Ray refract_ray(const Ray &inray, const Vector3 &norm, scalar_t mat_ior, bool entering, scalar_t ray_mag) 3.28 +{ 3.29 + Ray ray = inray; 3.30 + ray.iter--; 3.31 + 3.32 + scalar_t ior = ray.calc_ior(entering, mat_ior); 3.33 + 3.34 + if(entering) { 3.35 + ray.enter(mat_ior); 3.36 + } else { 3.37 + ray.leave(); 3.38 + } 3.39 + 3.40 + if(ray_mag < 0.0) { 3.41 + ray_mag = ray.dir.length(); 3.42 + } 3.43 + ray.dir = (ray.dir / ray_mag).refraction(norm, ior) * ray_mag; 3.44 + 3.45 + /* check TIR */ 3.46 + if(dot_product(ray.dir, norm) > 0.0) { 3.47 + if(entering) { 3.48 + ray.leave(); 3.49 + } else { 3.50 + ray.enter(mat_ior); 3.51 + } 3.52 + } 3.53 + return ray; 3.54 +} 3.55 +#endif /* __cplusplus */
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/libs/vmath/vector.inl Thu Sep 08 08:31:15 2011 +0300 4.3 @@ -0,0 +1,743 @@ 4.4 +#include <math.h> 4.5 + 4.6 +#ifdef __cplusplus 4.7 +extern "C" { 4.8 +#endif /* __cplusplus */ 4.9 + 4.10 +/* C 2D vector functions */ 4.11 +static inline vec2_t v2_cons(scalar_t x, scalar_t y) 4.12 +{ 4.13 + vec2_t v; 4.14 + v.x = x; 4.15 + v.y = y; 4.16 + return v; 4.17 +} 4.18 + 4.19 +static inline void v2_print(FILE *fp, vec2_t v) 4.20 +{ 4.21 + fprintf(fp, "[ %.4f %.4f ]", v.x, v.y); 4.22 +} 4.23 + 4.24 +static inline vec2_t v2_add(vec2_t v1, vec2_t v2) 4.25 +{ 4.26 + vec2_t res; 4.27 + res.x = v1.x + v2.x; 4.28 + res.y = v1.y + v2.y; 4.29 + return res; 4.30 +} 4.31 + 4.32 +static inline vec2_t v2_sub(vec2_t v1, vec2_t v2) 4.33 +{ 4.34 + vec2_t res; 4.35 + res.x = v1.x - v2.x; 4.36 + res.y = v1.y - v2.y; 4.37 + return res; 4.38 +} 4.39 + 4.40 +static inline vec2_t v2_scale(vec2_t v, scalar_t s) 4.41 +{ 4.42 + vec2_t res; 4.43 + res.x = v.x * s; 4.44 + res.y = v.y * s; 4.45 + return res; 4.46 +} 4.47 + 4.48 +static inline scalar_t v2_dot(vec2_t v1, vec2_t v2) 4.49 +{ 4.50 + return v1.x * v2.x + v1.y * v2.y; 4.51 +} 4.52 + 4.53 +static inline scalar_t v2_length(vec2_t v) 4.54 +{ 4.55 + return sqrt(v.x * v.x + v.y * v.y); 4.56 +} 4.57 + 4.58 +static inline scalar_t v2_length_sq(vec2_t v) 4.59 +{ 4.60 + return v.x * v.x + v.y * v.y; 4.61 +} 4.62 + 4.63 +static inline vec2_t v2_normalize(vec2_t v) 4.64 +{ 4.65 + scalar_t len = (scalar_t)sqrt(v.x * v.x + v.y * v.y); 4.66 + v.x /= len; 4.67 + v.y /= len; 4.68 + return v; 4.69 +} 4.70 + 4.71 +static inline vec2_t v2_lerp(vec2_t v1, vec2_t v2, scalar_t t) 4.72 +{ 4.73 + vec2_t res; 4.74 + res.x = v1.x + (v2.x - v1.x) * t; 4.75 + res.y = v1.y + (v2.y - v1.y) * t; 4.76 + return res; 4.77 +} 4.78 + 4.79 + 4.80 +/* C 3D vector functions */ 4.81 +static inline vec3_t v3_cons(scalar_t x, scalar_t y, scalar_t z) 4.82 +{ 4.83 + vec3_t v; 4.84 + v.x = x; 4.85 + v.y = y; 4.86 + v.z = z; 4.87 + return v; 4.88 +} 4.89 + 4.90 +static inline void v3_print(FILE *fp, vec3_t v) 4.91 +{ 4.92 + fprintf(fp, "[ %.4f %.4f %.4f ]", v.x, v.y, v.z); 4.93 +} 4.94 + 4.95 +static inline vec3_t v3_add(vec3_t v1, vec3_t v2) 4.96 +{ 4.97 + v1.x += v2.x; 4.98 + v1.y += v2.y; 4.99 + v1.z += v2.z; 4.100 + return v1; 4.101 +} 4.102 + 4.103 +static inline vec3_t v3_sub(vec3_t v1, vec3_t v2) 4.104 +{ 4.105 + v1.x -= v2.x; 4.106 + v1.y -= v2.y; 4.107 + v1.z -= v2.z; 4.108 + return v1; 4.109 +} 4.110 + 4.111 +static inline vec3_t v3_neg(vec3_t v) 4.112 +{ 4.113 + v.x = -v.x; 4.114 + v.y = -v.y; 4.115 + v.z = -v.z; 4.116 + return v; 4.117 +} 4.118 + 4.119 +static inline vec3_t v3_mul(vec3_t v1, vec3_t v2) 4.120 +{ 4.121 + v1.x *= v2.x; 4.122 + v1.y *= v2.y; 4.123 + v1.z *= v2.z; 4.124 + return v1; 4.125 +} 4.126 + 4.127 +static inline vec3_t v3_scale(vec3_t v1, scalar_t s) 4.128 +{ 4.129 + v1.x *= s; 4.130 + v1.y *= s; 4.131 + v1.z *= s; 4.132 + return v1; 4.133 +} 4.134 + 4.135 +static inline scalar_t v3_dot(vec3_t v1, vec3_t v2) 4.136 +{ 4.137 + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; 4.138 +} 4.139 + 4.140 +static inline vec3_t v3_cross(vec3_t v1, vec3_t v2) 4.141 +{ 4.142 + vec3_t v; 4.143 + v.x = v1.y * v2.z - v1.z * v2.y; 4.144 + v.y = v1.z * v2.x - v1.x * v2.z; 4.145 + v.z = v1.x * v2.y - v1.y * v2.x; 4.146 + return v; 4.147 +} 4.148 + 4.149 +static inline scalar_t v3_length(vec3_t v) 4.150 +{ 4.151 + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 4.152 +} 4.153 + 4.154 +static inline scalar_t v3_length_sq(vec3_t v) 4.155 +{ 4.156 + return v.x * v.x + v.y * v.y + v.z * v.z; 4.157 +} 4.158 + 4.159 +static inline vec3_t v3_normalize(vec3_t v) 4.160 +{ 4.161 + scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 4.162 + v.x /= len; 4.163 + v.y /= len; 4.164 + v.z /= len; 4.165 + return v; 4.166 +} 4.167 + 4.168 +static inline vec3_t v3_transform(vec3_t v, mat4_t m) 4.169 +{ 4.170 + vec3_t res; 4.171 + res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3]; 4.172 + res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3]; 4.173 + res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]; 4.174 + return res; 4.175 +} 4.176 + 4.177 +static inline vec3_t v3_rotate(vec3_t v, scalar_t x, scalar_t y, scalar_t z) 4.178 +{ 4.179 + void m4_rotate(mat4_t, scalar_t, scalar_t, scalar_t); 4.180 + 4.181 + mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; 4.182 + m4_rotate(m, x, y, z); 4.183 + return v3_transform(v, m); 4.184 +} 4.185 + 4.186 +static inline vec3_t v3_rotate_axis(vec3_t v, scalar_t angle, scalar_t x, scalar_t y, scalar_t z) 4.187 +{ 4.188 + void m4_rotate_axis(mat4_t, scalar_t, scalar_t, scalar_t, scalar_t); 4.189 + 4.190 + mat4_t m = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; 4.191 + m4_rotate_axis(m, angle, x, y, z); 4.192 + return v3_transform(v, m); 4.193 +} 4.194 + 4.195 +static inline vec3_t v3_rotate_quat(vec3_t v, quat_t q) 4.196 +{ 4.197 + quat_t quat_rotate_quat(quat_t, quat_t); 4.198 + 4.199 + quat_t vq = v4_cons(v.x, v.y, v.z, 0.0); 4.200 + quat_t res = quat_rotate_quat(vq, q); 4.201 + return v3_cons(res.x, res.y, res.z); 4.202 +} 4.203 + 4.204 +static inline vec3_t v3_reflect(vec3_t v, vec3_t n) 4.205 +{ 4.206 + scalar_t dot = v3_dot(v, n); 4.207 + return v3_sub(v3_scale(n, dot * 2.0), v); 4.208 +} 4.209 + 4.210 +static inline vec3_t v3_lerp(vec3_t v1, vec3_t v2, scalar_t t) 4.211 +{ 4.212 + v1.x += (v2.x - v1.x) * t; 4.213 + v1.y += (v2.y - v1.y) * t; 4.214 + v1.z += (v2.z - v1.z) * t; 4.215 + return v1; 4.216 +} 4.217 + 4.218 +/* C 4D vector functions */ 4.219 +static inline vec4_t v4_cons(scalar_t x, scalar_t y, scalar_t z, scalar_t w) 4.220 +{ 4.221 + vec4_t v; 4.222 + v.x = x; 4.223 + v.y = y; 4.224 + v.z = z; 4.225 + v.w = w; 4.226 + return v; 4.227 +} 4.228 + 4.229 +static inline void v4_print(FILE *fp, vec4_t v) 4.230 +{ 4.231 + fprintf(fp, "[ %.4f %.4f %.4f %.4f ]", v.x, v.y, v.z, v.w); 4.232 +} 4.233 + 4.234 +static inline vec4_t v4_add(vec4_t v1, vec4_t v2) 4.235 +{ 4.236 + v1.x += v2.x; 4.237 + v1.y += v2.y; 4.238 + v1.z += v2.z; 4.239 + v1.w += v2.w; 4.240 + return v1; 4.241 +} 4.242 + 4.243 +static inline vec4_t v4_sub(vec4_t v1, vec4_t v2) 4.244 +{ 4.245 + v1.x -= v2.x; 4.246 + v1.y -= v2.y; 4.247 + v1.z -= v2.z; 4.248 + v1.w -= v2.w; 4.249 + return v1; 4.250 +} 4.251 + 4.252 +static inline vec4_t v4_neg(vec4_t v) 4.253 +{ 4.254 + v.x = -v.x; 4.255 + v.y = -v.y; 4.256 + v.z = -v.z; 4.257 + v.w = -v.w; 4.258 + return v; 4.259 +} 4.260 + 4.261 +static inline vec4_t v4_mul(vec4_t v1, vec4_t v2) 4.262 +{ 4.263 + v1.x *= v2.x; 4.264 + v1.y *= v2.y; 4.265 + v1.z *= v2.z; 4.266 + v1.w *= v2.w; 4.267 + return v1; 4.268 +} 4.269 + 4.270 +static inline vec4_t v4_scale(vec4_t v, scalar_t s) 4.271 +{ 4.272 + v.x *= s; 4.273 + v.y *= s; 4.274 + v.z *= s; 4.275 + v.w *= s; 4.276 + return v; 4.277 +} 4.278 + 4.279 +static inline scalar_t v4_dot(vec4_t v1, vec4_t v2) 4.280 +{ 4.281 + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w; 4.282 +} 4.283 + 4.284 +static inline scalar_t v4_length(vec4_t v) 4.285 +{ 4.286 + return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); 4.287 +} 4.288 + 4.289 +static inline scalar_t v4_length_sq(vec4_t v) 4.290 +{ 4.291 + return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w; 4.292 +} 4.293 + 4.294 +static inline vec4_t v4_normalize(vec4_t v) 4.295 +{ 4.296 + scalar_t len = sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w); 4.297 + v.x /= len; 4.298 + v.y /= len; 4.299 + v.z /= len; 4.300 + v.w /= len; 4.301 + return v; 4.302 +} 4.303 + 4.304 +static inline vec4_t v4_transform(vec4_t v, mat4_t m) 4.305 +{ 4.306 + vec4_t res; 4.307 + res.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w; 4.308 + res.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w; 4.309 + res.z = m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w; 4.310 + res.w = m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w; 4.311 + return res; 4.312 +} 4.313 + 4.314 +#ifdef __cplusplus 4.315 +} /* extern "C" */ 4.316 + 4.317 + 4.318 +/* --------------- C++ part -------------- */ 4.319 + 4.320 +inline scalar_t &Vector2::operator [](int elem) { 4.321 + return elem ? y : x; 4.322 +} 4.323 + 4.324 +inline const scalar_t &Vector2::operator [](int elem) const { 4.325 + return elem ? y : x; 4.326 +} 4.327 + 4.328 +inline Vector2 operator -(const Vector2 &vec) { 4.329 + return Vector2(-vec.x, -vec.y); 4.330 +} 4.331 + 4.332 +inline scalar_t dot_product(const Vector2 &v1, const Vector2 &v2) { 4.333 + return v1.x * v2.x + v1.y * v2.y; 4.334 +} 4.335 + 4.336 +inline Vector2 operator +(const Vector2 &v1, const Vector2 &v2) { 4.337 + return Vector2(v1.x + v2.x, v1.y + v2.y); 4.338 +} 4.339 + 4.340 +inline Vector2 operator -(const Vector2 &v1, const Vector2 &v2) { 4.341 + return Vector2(v1.x - v2.x, v1.y - v2.y); 4.342 +} 4.343 + 4.344 +inline Vector2 operator *(const Vector2 &v1, const Vector2 &v2) { 4.345 + return Vector2(v1.x * v2.x, v1.y * v2.y); 4.346 +} 4.347 + 4.348 +inline Vector2 operator /(const Vector2 &v1, const Vector2 &v2) { 4.349 + return Vector2(v1.x / v2.x, v1.y / v2.y); 4.350 +} 4.351 + 4.352 +inline bool operator ==(const Vector2 &v1, const Vector2 &v2) { 4.353 + return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.x) < XSMALL_NUMBER); 4.354 +} 4.355 + 4.356 +inline void operator +=(Vector2 &v1, const Vector2 &v2) { 4.357 + v1.x += v2.x; 4.358 + v1.y += v2.y; 4.359 +} 4.360 + 4.361 +inline void operator -=(Vector2 &v1, const Vector2 &v2) { 4.362 + v1.x -= v2.x; 4.363 + v1.y -= v2.y; 4.364 +} 4.365 + 4.366 +inline void operator *=(Vector2 &v1, const Vector2 &v2) { 4.367 + v1.x *= v2.x; 4.368 + v1.y *= v2.y; 4.369 +} 4.370 + 4.371 +inline void operator /=(Vector2 &v1, const Vector2 &v2) { 4.372 + v1.x /= v2.x; 4.373 + v1.y /= v2.y; 4.374 +} 4.375 + 4.376 +inline Vector2 operator +(const Vector2 &vec, scalar_t scalar) { 4.377 + return Vector2(vec.x + scalar, vec.y + scalar); 4.378 +} 4.379 + 4.380 +inline Vector2 operator +(scalar_t scalar, const Vector2 &vec) { 4.381 + return Vector2(vec.x + scalar, vec.y + scalar); 4.382 +} 4.383 + 4.384 +inline Vector2 operator -(scalar_t scalar, const Vector2 &vec) { 4.385 + return Vector2(vec.x - scalar, vec.y - scalar); 4.386 +} 4.387 + 4.388 +inline Vector2 operator *(const Vector2 &vec, scalar_t scalar) { 4.389 + return Vector2(vec.x * scalar, vec.y * scalar); 4.390 +} 4.391 + 4.392 +inline Vector2 operator *(scalar_t scalar, const Vector2 &vec) { 4.393 + return Vector2(vec.x * scalar, vec.y * scalar); 4.394 +} 4.395 + 4.396 +inline Vector2 operator /(const Vector2 &vec, scalar_t scalar) { 4.397 + return Vector2(vec.x / scalar, vec.y / scalar); 4.398 +} 4.399 + 4.400 +inline void operator +=(Vector2 &vec, scalar_t scalar) { 4.401 + vec.x += scalar; 4.402 + vec.y += scalar; 4.403 +} 4.404 + 4.405 +inline void operator -=(Vector2 &vec, scalar_t scalar) { 4.406 + vec.x -= scalar; 4.407 + vec.y -= scalar; 4.408 +} 4.409 + 4.410 +inline void operator *=(Vector2 &vec, scalar_t scalar) { 4.411 + vec.x *= scalar; 4.412 + vec.y *= scalar; 4.413 +} 4.414 + 4.415 +inline void operator /=(Vector2 &vec, scalar_t scalar) { 4.416 + vec.x /= scalar; 4.417 + vec.y /= scalar; 4.418 +} 4.419 + 4.420 +inline scalar_t Vector2::length() const { 4.421 + return sqrt(x*x + y*y); 4.422 +} 4.423 + 4.424 +inline scalar_t Vector2::length_sq() const { 4.425 + return x*x + y*y; 4.426 +} 4.427 + 4.428 +inline Vector2 lerp(const Vector2 &a, const Vector2 &b, scalar_t t) 4.429 +{ 4.430 + return a + (b - a) * t; 4.431 +} 4.432 + 4.433 +inline Vector2 catmull_rom_spline(const Vector2 &v0, const Vector2 &v1, 4.434 + const Vector2 &v2, const Vector2 &v3, scalar_t t) 4.435 +{ 4.436 + scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); 4.437 + scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); 4.438 + scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); 4.439 + return Vector2(x, y); 4.440 +} 4.441 + 4.442 + 4.443 +/* ------------- Vector3 -------------- */ 4.444 + 4.445 +inline scalar_t &Vector3::operator [](int elem) { 4.446 + return elem ? (elem == 1 ? y : z) : x; 4.447 +} 4.448 + 4.449 +inline const scalar_t &Vector3::operator [](int elem) const { 4.450 + return elem ? (elem == 1 ? y : z) : x; 4.451 +} 4.452 + 4.453 +/* unary operations */ 4.454 +inline Vector3 operator -(const Vector3 &vec) { 4.455 + return Vector3(-vec.x, -vec.y, -vec.z); 4.456 +} 4.457 + 4.458 +/* binary vector (op) vector operations */ 4.459 +inline scalar_t dot_product(const Vector3 &v1, const Vector3 &v2) { 4.460 + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; 4.461 +} 4.462 + 4.463 +inline Vector3 cross_product(const Vector3 &v1, const Vector3 &v2) { 4.464 + return Vector3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x); 4.465 +} 4.466 + 4.467 + 4.468 +inline Vector3 operator +(const Vector3 &v1, const Vector3 &v2) { 4.469 + return Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z); 4.470 +} 4.471 + 4.472 +inline Vector3 operator -(const Vector3 &v1, const Vector3 &v2) { 4.473 + return Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z); 4.474 +} 4.475 + 4.476 +inline Vector3 operator *(const Vector3 &v1, const Vector3 &v2) { 4.477 + return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); 4.478 +} 4.479 + 4.480 +inline Vector3 operator /(const Vector3 &v1, const Vector3 &v2) { 4.481 + return Vector3(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z); 4.482 +} 4.483 + 4.484 +inline bool operator ==(const Vector3 &v1, const Vector3 &v2) { 4.485 + return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && (fabs(v1.y - v2.y) < XSMALL_NUMBER) && (fabs(v1.z - v2.z) < XSMALL_NUMBER); 4.486 +} 4.487 + 4.488 +inline void operator +=(Vector3 &v1, const Vector3 &v2) { 4.489 + v1.x += v2.x; 4.490 + v1.y += v2.y; 4.491 + v1.z += v2.z; 4.492 +} 4.493 + 4.494 +inline void operator -=(Vector3 &v1, const Vector3 &v2) { 4.495 + v1.x -= v2.x; 4.496 + v1.y -= v2.y; 4.497 + v1.z -= v2.z; 4.498 +} 4.499 + 4.500 +inline void operator *=(Vector3 &v1, const Vector3 &v2) { 4.501 + v1.x *= v2.x; 4.502 + v1.y *= v2.y; 4.503 + v1.z *= v2.z; 4.504 +} 4.505 + 4.506 +inline void operator /=(Vector3 &v1, const Vector3 &v2) { 4.507 + v1.x /= v2.x; 4.508 + v1.y /= v2.y; 4.509 + v1.z /= v2.z; 4.510 +} 4.511 +/* binary vector (op) scalar and scalar (op) vector operations */ 4.512 +inline Vector3 operator +(const Vector3 &vec, scalar_t scalar) { 4.513 + return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar); 4.514 +} 4.515 + 4.516 +inline Vector3 operator +(scalar_t scalar, const Vector3 &vec) { 4.517 + return Vector3(vec.x + scalar, vec.y + scalar, vec.z + scalar); 4.518 +} 4.519 + 4.520 +inline Vector3 operator -(const Vector3 &vec, scalar_t scalar) { 4.521 + return Vector3(vec.x - scalar, vec.y - scalar, vec.z - scalar); 4.522 +} 4.523 + 4.524 +inline Vector3 operator *(const Vector3 &vec, scalar_t scalar) { 4.525 + return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar); 4.526 +} 4.527 + 4.528 +inline Vector3 operator *(scalar_t scalar, const Vector3 &vec) { 4.529 + return Vector3(vec.x * scalar, vec.y * scalar, vec.z * scalar); 4.530 +} 4.531 + 4.532 +inline Vector3 operator /(const Vector3 &vec, scalar_t scalar) { 4.533 + return Vector3(vec.x / scalar, vec.y / scalar, vec.z / scalar); 4.534 +} 4.535 + 4.536 +inline void operator +=(Vector3 &vec, scalar_t scalar) { 4.537 + vec.x += scalar; 4.538 + vec.y += scalar; 4.539 + vec.z += scalar; 4.540 +} 4.541 + 4.542 +inline void operator -=(Vector3 &vec, scalar_t scalar) { 4.543 + vec.x -= scalar; 4.544 + vec.y -= scalar; 4.545 + vec.z -= scalar; 4.546 +} 4.547 + 4.548 +inline void operator *=(Vector3 &vec, scalar_t scalar) { 4.549 + vec.x *= scalar; 4.550 + vec.y *= scalar; 4.551 + vec.z *= scalar; 4.552 +} 4.553 + 4.554 +inline void operator /=(Vector3 &vec, scalar_t scalar) { 4.555 + vec.x /= scalar; 4.556 + vec.y /= scalar; 4.557 + vec.z /= scalar; 4.558 +} 4.559 + 4.560 +inline scalar_t Vector3::length() const { 4.561 + return sqrt(x*x + y*y + z*z); 4.562 +} 4.563 +inline scalar_t Vector3::length_sq() const { 4.564 + return x*x + y*y + z*z; 4.565 +} 4.566 + 4.567 +inline Vector3 lerp(const Vector3 &a, const Vector3 &b, scalar_t t) { 4.568 + return a + (b - a) * t; 4.569 +} 4.570 + 4.571 +inline Vector3 catmull_rom_spline(const Vector3 &v0, const Vector3 &v1, 4.572 + const Vector3 &v2, const Vector3 &v3, scalar_t t) 4.573 +{ 4.574 + scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); 4.575 + scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); 4.576 + scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); 4.577 + scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t); 4.578 + return Vector3(x, y, z); 4.579 +} 4.580 + 4.581 +/* ----------- Vector4 ----------------- */ 4.582 + 4.583 +inline scalar_t &Vector4::operator [](int elem) { 4.584 + return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x; 4.585 +} 4.586 + 4.587 +inline const scalar_t &Vector4::operator [](int elem) const { 4.588 + return elem ? (elem == 1 ? y : (elem == 2 ? z : w)) : x; 4.589 +} 4.590 + 4.591 +inline Vector4 operator -(const Vector4 &vec) { 4.592 + return Vector4(-vec.x, -vec.y, -vec.z, -vec.w); 4.593 +} 4.594 + 4.595 +inline scalar_t dot_product(const Vector4 &v1, const Vector4 &v2) { 4.596 + return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w; 4.597 +} 4.598 + 4.599 +inline Vector4 cross_product(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3) { 4.600 + scalar_t a, b, c, d, e, f; /* Intermediate Values */ 4.601 + Vector4 result; 4.602 + 4.603 + /* Calculate intermediate values. */ 4.604 + a = (v2.x * v3.y) - (v2.y * v3.x); 4.605 + b = (v2.x * v3.z) - (v2.z * v3.x); 4.606 + c = (v2.x * v3.w) - (v2.w * v3.x); 4.607 + d = (v2.y * v3.z) - (v2.z * v3.y); 4.608 + e = (v2.y * v3.w) - (v2.w * v3.y); 4.609 + f = (v2.z * v3.w) - (v2.w * v3.z); 4.610 + 4.611 + /* Calculate the result-vector components. */ 4.612 + result.x = (v1.y * f) - (v1.z * e) + (v1.w * d); 4.613 + result.y = - (v1.x * f) + (v1.z * c) - (v1.w * b); 4.614 + result.z = (v1.x * e) - (v1.y * c) + (v1.w * a); 4.615 + result.w = - (v1.x * d) + (v1.y * b) - (v1.z * a); 4.616 + return result; 4.617 +} 4.618 + 4.619 +inline Vector4 operator +(const Vector4 &v1, const Vector4 &v2) { 4.620 + return Vector4(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w); 4.621 +} 4.622 + 4.623 +inline Vector4 operator -(const Vector4 &v1, const Vector4 &v2) { 4.624 + return Vector4(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w); 4.625 +} 4.626 + 4.627 +inline Vector4 operator *(const Vector4 &v1, const Vector4 &v2) { 4.628 + return Vector4(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z, v1.w * v2.w); 4.629 +} 4.630 + 4.631 +inline Vector4 operator /(const Vector4 &v1, const Vector4 &v2) { 4.632 + return Vector4(v1.x / v2.x, v1.y / v2.y, v1.z / v2.z, v1.w / v2.w); 4.633 +} 4.634 + 4.635 +inline bool operator ==(const Vector4 &v1, const Vector4 &v2) { 4.636 + return (fabs(v1.x - v2.x) < XSMALL_NUMBER) && 4.637 + (fabs(v1.y - v2.y) < XSMALL_NUMBER) && 4.638 + (fabs(v1.z - v2.z) < XSMALL_NUMBER) && 4.639 + (fabs(v1.w - v2.w) < XSMALL_NUMBER); 4.640 +} 4.641 + 4.642 +inline void operator +=(Vector4 &v1, const Vector4 &v2) { 4.643 + v1.x += v2.x; 4.644 + v1.y += v2.y; 4.645 + v1.z += v2.z; 4.646 + v1.w += v2.w; 4.647 +} 4.648 + 4.649 +inline void operator -=(Vector4 &v1, const Vector4 &v2) { 4.650 + v1.x -= v2.x; 4.651 + v1.y -= v2.y; 4.652 + v1.z -= v2.z; 4.653 + v1.w -= v2.w; 4.654 +} 4.655 + 4.656 +inline void operator *=(Vector4 &v1, const Vector4 &v2) { 4.657 + v1.x *= v2.x; 4.658 + v1.y *= v2.y; 4.659 + v1.z *= v2.z; 4.660 + v1.w *= v2.w; 4.661 +} 4.662 + 4.663 +inline void operator /=(Vector4 &v1, const Vector4 &v2) { 4.664 + v1.x /= v2.x; 4.665 + v1.y /= v2.y; 4.666 + v1.z /= v2.z; 4.667 + v1.w /= v2.w; 4.668 +} 4.669 + 4.670 +/* binary vector (op) scalar and scalar (op) vector operations */ 4.671 +inline Vector4 operator +(const Vector4 &vec, scalar_t scalar) { 4.672 + return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar); 4.673 +} 4.674 + 4.675 +inline Vector4 operator +(scalar_t scalar, const Vector4 &vec) { 4.676 + return Vector4(vec.x + scalar, vec.y + scalar, vec.z + scalar, vec.w + scalar); 4.677 +} 4.678 + 4.679 +inline Vector4 operator -(const Vector4 &vec, scalar_t scalar) { 4.680 + return Vector4(vec.x - scalar, vec.y - scalar, vec.z - scalar, vec.w - scalar); 4.681 +} 4.682 + 4.683 +inline Vector4 operator *(const Vector4 &vec, scalar_t scalar) { 4.684 + return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar); 4.685 +} 4.686 + 4.687 +inline Vector4 operator *(scalar_t scalar, const Vector4 &vec) { 4.688 + return Vector4(vec.x * scalar, vec.y * scalar, vec.z * scalar, vec.w * scalar); 4.689 +} 4.690 + 4.691 +inline Vector4 operator /(const Vector4 &vec, scalar_t scalar) { 4.692 + return Vector4(vec.x / scalar, vec.y / scalar, vec.z / scalar, vec.w / scalar); 4.693 +} 4.694 + 4.695 +inline void operator +=(Vector4 &vec, scalar_t scalar) { 4.696 + vec.x += scalar; 4.697 + vec.y += scalar; 4.698 + vec.z += scalar; 4.699 + vec.w += scalar; 4.700 +} 4.701 + 4.702 +inline void operator -=(Vector4 &vec, scalar_t scalar) { 4.703 + vec.x -= scalar; 4.704 + vec.y -= scalar; 4.705 + vec.z -= scalar; 4.706 + vec.w -= scalar; 4.707 +} 4.708 + 4.709 +inline void operator *=(Vector4 &vec, scalar_t scalar) { 4.710 + vec.x *= scalar; 4.711 + vec.y *= scalar; 4.712 + vec.z *= scalar; 4.713 + vec.w *= scalar; 4.714 +} 4.715 + 4.716 +inline void operator /=(Vector4 &vec, scalar_t scalar) { 4.717 + vec.x /= scalar; 4.718 + vec.y /= scalar; 4.719 + vec.z /= scalar; 4.720 + vec.w /= scalar; 4.721 +} 4.722 + 4.723 +inline scalar_t Vector4::length() const { 4.724 + return sqrt(x*x + y*y + z*z + w*w); 4.725 +} 4.726 +inline scalar_t Vector4::length_sq() const { 4.727 + return x*x + y*y + z*z + w*w; 4.728 +} 4.729 + 4.730 +inline Vector4 lerp(const Vector4 &v0, const Vector4 &v1, scalar_t t) 4.731 +{ 4.732 + return v0 + (v1 - v0) * t; 4.733 +} 4.734 + 4.735 +inline Vector4 catmull_rom_spline(const Vector4 &v0, const Vector4 &v1, 4.736 + const Vector4 &v2, const Vector4 &v3, scalar_t t) 4.737 +{ 4.738 + scalar_t spline(scalar_t, scalar_t, scalar_t, scalar_t, scalar_t); 4.739 + scalar_t x = spline(v0.x, v1.x, v2.x, v3.x, t); 4.740 + scalar_t y = spline(v0.y, v1.y, v2.y, v3.y, t); 4.741 + scalar_t z = spline(v0.z, v1.z, v2.z, v3.z, t); 4.742 + scalar_t w = spline(v0.w, v1.w, v2.w, v3.w, t); 4.743 + return Vector4(x, y, z, w); 4.744 +} 4.745 + 4.746 +#endif /* __cplusplus */
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/libs/vmath/vmath.inl Thu Sep 08 08:31:15 2011 +0300 5.3 @@ -0,0 +1,29 @@ 5.4 +#include <stdlib.h> 5.5 + 5.6 +/** Generates a random number in [0, range) */ 5.7 +static inline scalar_t frand(scalar_t range) 5.8 +{ 5.9 + return range * (scalar_t)rand() / (scalar_t)RAND_MAX; 5.10 +} 5.11 + 5.12 +/** Generates a random vector on the surface of a sphere */ 5.13 +static inline vec3_t sphrand(scalar_t rad) 5.14 +{ 5.15 + scalar_t u = (scalar_t)rand() / RAND_MAX; 5.16 + scalar_t v = (scalar_t)rand() / RAND_MAX; 5.17 + 5.18 + scalar_t theta = 2.0 * M_PI * u; 5.19 + scalar_t phi = acos(2.0 * v - 1.0); 5.20 + 5.21 + vec3_t res; 5.22 + res.x = rad * cos(theta) * sin(phi); 5.23 + res.y = rad * sin(theta) * sin(phi); 5.24 + res.z = rad * cos(phi); 5.25 + return res; 5.26 +} 5.27 + 5.28 +/** linear interpolation */ 5.29 +static inline scalar_t lerp(scalar_t a, scalar_t b, scalar_t t) 5.30 +{ 5.31 + return a + (b - a) * t; 5.32 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/sdr/text.p.glsl Thu Sep 08 08:31:15 2011 +0300 6.3 @@ -0,0 +1,22 @@ 6.4 +#ifdef GL_ES 6.5 +precision highp float; 6.6 +#endif 6.7 + 6.8 +uniform sampler2D tex; 6.9 + 6.10 +varying vec4 color; 6.11 +varying vec3 vpos; 6.12 +varying vec2 tc; 6.13 + 6.14 +void main() 6.15 +{ 6.16 + vec4 tcol = texture2D(tex, tc); 6.17 + 6.18 + float fog = exp(-(0.2 * -vpos.z)); 6.19 + 6.20 + float alpha = fog * color.a; 6.21 + 6.22 + vec4 color = vec4(1.0, 0.3, 0.2, tcol.x * alpha); 6.23 + 6.24 + gl_FragColor = color; 6.25 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/sdr/text.v.glsl Thu Sep 08 08:31:15 2011 +0300 7.3 @@ -0,0 +1,21 @@ 7.4 +attribute vec4 attr_vertex, attr_texcoord, attr_color; 7.5 + 7.6 +uniform mat4 matrix_modelview, matrix_projection; 7.7 +uniform float idx; 7.8 + 7.9 +varying vec4 color; 7.10 +varying vec3 vpos; 7.11 +varying vec2 tc; 7.12 + 7.13 +void main() 7.14 +{ 7.15 + mat4 mvp = matrix_projection * matrix_modelview; 7.16 + gl_Position = mvp * attr_vertex; 7.17 + 7.18 + vpos = (matrix_modelview * attr_vertex).xyz; 7.19 + 7.20 + float sz = 1.0 / 17.0; 7.21 + tc = vec2(attr_texcoord.x, (attr_texcoord.y + idx) * sz); 7.22 + 7.23 + color = attr_color; 7.24 +}
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/sdr/tunnel.p.glsl Thu Sep 08 08:31:15 2011 +0300 8.3 @@ -0,0 +1,52 @@ 8.4 +#ifdef GL_ES 8.5 +precision highp float; 8.6 +#endif 8.7 + 8.8 +uniform sampler2D tex, tex_norm; 8.9 + 8.10 +varying vec3 vpos, normal, tangent; 8.11 +varying vec4 color, tc; 8.12 +varying vec3 lpos; 8.13 + 8.14 +void main() 8.15 +{ 8.16 + vec4 fog_color = vec4(0.0, 0.0, 0.0, 1.0); 8.17 + vec3 tcol = texture2D(tex, tc.xy * vec2(1.0, -1.0)).xyz; 8.18 + vec3 tnorm = texture2D(tex_norm, tc.xy * vec2(1.0, -1.0)).xyz; 8.19 + 8.20 + float fog = exp(-(0.2 * -vpos.z)); 8.21 + 8.22 + vec3 ldir = lpos - vpos; 8.23 + float ldist = length(ldir); 8.24 + 8.25 + /* bring the light direction to tangent space */ 8.26 + vec3 norm = normalize(normal); 8.27 + vec3 tang = normalize(tangent); 8.28 + vec3 bitan = cross(norm, tang); 8.29 + 8.30 + mat3 tbn_xform = mat3(tang.x, bitan.x, norm.x, 8.31 + tang.y, bitan.y, norm.y, 8.32 + tang.z, bitan.z, norm.z); 8.33 + 8.34 + vec3 l = normalize(tbn_xform * ldir); 8.35 + 8.36 + /* grab normal from the normalmap */ 8.37 + vec3 n = normalize(tnorm * 2.0 - 1.0); 8.38 + 8.39 + float diffuse = max(dot(n, l), 0.0); 8.40 + 8.41 + /* blinn-phong specular */ 8.42 + vec3 v = normalize(-vpos); 8.43 + vec3 h = normalize(v + l); 8.44 + float specular = pow(max(dot(n, h), 0.0), 60.0); 8.45 + 8.46 + vec3 amb = vec3(0.02, 0.02, 0.02); 8.47 + 8.48 + float att = clamp(1.0 / (0.5 * (ldist * ldist)), 0.0, 1.0); 8.49 + 8.50 + vec3 dif = tcol * diffuse * att; 8.51 + vec3 spec = vec3(0.6, 0.6, 0.6) * specular * att; 8.52 + 8.53 + //gl_FragColor = 0.5 * tcol + 0.5 * tnorm;//mix(fog_color, tcol, fog); 8.54 + gl_FragColor = vec4(fog * (amb + dif + spec), 1.0); 8.55 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/sdr/tunnel.v.glsl Thu Sep 08 08:31:15 2011 +0300 9.3 @@ -0,0 +1,27 @@ 9.4 +attribute vec4 attr_vertex, attr_color, attr_texcoord; 9.5 +attribute vec4 attr_tangent; 9.6 +attribute vec3 attr_normal; 9.7 + 9.8 +uniform mat4 matrix_modelview, matrix_projection, matrix_texture; 9.9 +uniform mat3 normmat; 9.10 +uniform float t; 9.11 +uniform vec4 light_pos; 9.12 + 9.13 +varying vec3 vpos, normal, tangent; 9.14 +varying vec4 tc; 9.15 +varying vec3 lpos; 9.16 + 9.17 +void main() 9.18 +{ 9.19 + mat4 mvp = matrix_projection * matrix_modelview; 9.20 + gl_Position = mvp * attr_vertex; 9.21 + 9.22 + vpos = (matrix_modelview * attr_vertex).xyz; 9.23 + 9.24 + normal = normmat * attr_normal; 9.25 + tangent = normmat * attr_tangent.xyz; 9.26 + 9.27 + lpos = (matrix_modelview * light_pos).xyz; 9.28 + 9.29 + tc = matrix_texture * attr_texcoord; 9.30 +}