First attempt at adding John Tsiombikas' spaceball support for X11. It compiles...
[freeglut] / progs / demos / spaceball / vmath.inl
diff --git a/progs/demos/spaceball/vmath.inl b/progs/demos/spaceball/vmath.inl
new file mode 100644 (file)
index 0000000..82bbea0
--- /dev/null
@@ -0,0 +1,68 @@
+/* vector functions */\r
+static inline vec3_t v3_cons(float x, float y, float z)\r
+{\r
+       vec3_t res;\r
+       res.x = x;\r
+       res.y = y;\r
+       res.z = z;\r
+       return res;\r
+}\r
+\r
+static inline vec3_t quat_vec(quat_t q)\r
+{\r
+       vec3_t v;\r
+       v.x = q.x;\r
+       v.y = q.y;\r
+       v.z = q.z;\r
+       return v;\r
+}\r
+\r
+static inline float v3_dot(vec3_t v1, vec3_t v2)\r
+{\r
+       return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;\r
+}\r
+\r
+/* quaternion functions */\r
+static inline quat_t quat_cons(float s, float x, float y, float z)\r
+{\r
+       quat_t q;\r
+       q.x = x;\r
+       q.y = y;\r
+       q.z = z;\r
+       q.w = s;\r
+       return q;\r
+}\r
+\r
+static inline quat_t quat_mul(quat_t q1, quat_t q2)\r
+{\r
+       quat_t res;\r
+       vec3_t v1 = quat_vec(q1);\r
+       vec3_t v2 = quat_vec(q2);\r
+\r
+       res.w = q1.w * q2.w - v3_dot(v1, v2);\r
+       res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);\r
+       res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);\r
+       res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);\r
+       return res;\r
+}\r
+\r
+static inline void quat_to_mat(mat4_t res, quat_t q)\r
+{\r
+       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,\r
+                                       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,\r
+                                       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,\r
+                                       0, 0, 0, 1);\r
+}\r
+\r
+/* matrix functions */\r
+static inline void m4_cons(mat4_t m,\r
+               float m11, float m12, float m13, float m14,\r
+               float m21, float m22, float m23, float m24,\r
+               float m31, float m32, float m33, float m34,\r
+               float m41, float m42, float m43, float m44)\r
+{\r
+       m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;\r
+       m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;\r
+       m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;\r
+       m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;\r
+}\r