5 #define INLINE __inline
7 #elif defined(__WATCOMC__)
8 #define INLINE __inline
14 typedef struct { float x, y, z; } vec3_t;
15 typedef struct { float x, y, z, w; } vec4_t;
17 typedef vec4_t quat_t;
19 /* vector functions */
20 static INLINE vec3_t v3_cons(float x, float y, float z)
29 static INLINE float v3_dot(vec3_t v1, vec3_t v2)
31 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
34 /* quaternion functions */
35 static INLINE quat_t quat_cons(float s, float x, float y, float z)
45 static INLINE vec3_t quat_vec(quat_t q)
54 static INLINE quat_t quat_mul(quat_t q1, quat_t q2)
57 vec3_t v1 = quat_vec(q1);
58 vec3_t v2 = quat_vec(q2);
60 res.w = q1.w * q2.w - v3_dot(v1, v2);
61 res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
62 res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
63 res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
67 static INLINE void quat_to_mat(float *res, quat_t q)
69 res[0] = 1.0f - 2.0f * q.y*q.y - 2.0f * q.z*q.z;
70 res[1] = 2.0f * q.x * q.y - 2.0f * q.w * q.z;
71 res[2] = 2.0f * q.z * q.x + 2.0f * q.w * q.y;
73 res[4] = 2.0f * q.x * q.y + 2.0f * q.w * q.z;
74 res[5] = 1.0f - 2.0f * q.x*q.x - 2.0f * q.z*q.z;
75 res[6] = 2.0f * q.y * q.z - 2.0f * q.w * q.x;
77 res[8] = 2.0f * q.z * q.x - 2.0f * q.w * q.y;
78 res[9] = 2.0f * q.y * q.z + 2.0f * q.w * q.x;
79 res[10] = 1.0f - 2.0f * q.x*q.x - 2.0f * q.y*q.y;
81 res[12] = res[13] = res[14] = 0.0f;
85 static INLINE quat_t quat_rotate(quat_t q, float angle, float x, float y, float z)
88 float half_angle = angle * 0.5;
89 float sin_half = sin(half_angle);
91 rq.w = cos(half_angle);
96 return quat_mul(q, rq);