7 #define INLINE __inline
9 #elif defined(__WATCOMC__)
10 #define INLINE __inline
16 typedef struct { float x, y; } vec2_t;
17 typedef struct { float x, y, z; } vec3_t;
18 typedef struct { float x, y, z, w; } vec4_t;
20 typedef vec4_t quat_t;
22 /* vector functions */
23 static INLINE vec3_t v3_cons(float x, float y, float z)
32 static INLINE void v3_negate(vec3_t *v)
39 static INLINE float v3_dot(vec3_t v1, vec3_t v2)
41 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
44 static INLINE vec3_t v3_cross(vec3_t v1, vec3_t v2)
47 res.x = v1.y * v2.z - v1.z * v2.y;
48 res.y = v1.z * v2.x - v1.x * v2.z;
49 res.z = v1.x * v2.y - v1.y * v2.x;
53 static INLINE void v3_normalize(vec3_t *v)
55 float mag = sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
64 /* quaternion functions */
65 static INLINE quat_t quat_cons(float s, float x, float y, float z)
75 static INLINE vec3_t quat_vec(quat_t q)
84 static INLINE quat_t quat_mul(quat_t q1, quat_t q2)
87 vec3_t v1 = quat_vec(q1);
88 vec3_t v2 = quat_vec(q2);
90 res.w = q1.w * q2.w - v3_dot(v1, v2);
91 res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
92 res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
93 res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
97 static INLINE void quat_to_mat(float *res, quat_t q)
99 res[0] = 1.0f - 2.0f * q.y*q.y - 2.0f * q.z*q.z;
100 res[1] = 2.0f * q.x * q.y - 2.0f * q.w * q.z;
101 res[2] = 2.0f * q.z * q.x + 2.0f * q.w * q.y;
103 res[4] = 2.0f * q.x * q.y + 2.0f * q.w * q.z;
104 res[5] = 1.0f - 2.0f * q.x*q.x - 2.0f * q.z*q.z;
105 res[6] = 2.0f * q.y * q.z - 2.0f * q.w * q.x;
107 res[8] = 2.0f * q.z * q.x - 2.0f * q.w * q.y;
108 res[9] = 2.0f * q.y * q.z + 2.0f * q.w * q.x;
109 res[10] = 1.0f - 2.0f * q.x*q.x - 2.0f * q.y*q.y;
111 res[12] = res[13] = res[14] = 0.0f;
115 static INLINE quat_t quat_rotate(quat_t q, float angle, float x, float y, float z)
118 float half_angle = angle * 0.5;
119 float sin_half = sin(half_angle);
121 rq.w = cos(half_angle);
126 return quat_mul(q, rq);
129 static INLINE void mat4_transpose(float *mat)
135 int rowidx = i * 4 + j;
136 int colidx = j * 4 + i;
137 float tmp = mat[rowidx];
138 mat[rowidx] = mat[colidx];
145 static vec3_t INLINE sphrand(float rad)
149 float u = (float)rand() / RAND_MAX;
150 float v = (float)rand() / RAND_MAX;
152 float theta = 2.0f * M_PI * u;
153 float phi = acos(2.0f * v - 1.0f);
155 res.x = rad * cos(theta) * sin(phi);
156 res.y = rad * sin(theta) * sin(phi);
157 res.z = rad * cos(phi);
162 #endif /* VMATH_H_ */