#ifndef VMATH_H_
#define VMATH_H_
+#include <math.h>
+
#ifdef __GNUC__
#define INLINE __inline
#define INLINE
#endif
+typedef struct { float x, y; } vec2_t;
typedef struct { float x, y, z; } vec3_t;
typedef struct { float x, y, z, w; } vec4_t;
return res;
}
+static INLINE void v3_negate(vec3_t *v)
+{
+ v->x = -v->x;
+ v->y = -v->y;
+ v->z = -v->z;
+}
+
static INLINE float v3_dot(vec3_t v1, vec3_t v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
+static INLINE vec3_t v3_cross(vec3_t v1, vec3_t v2)
+{
+ vec3_t res;
+ res.x = v1.y * v2.z - v1.z * v2.y;
+ res.y = v1.z * v2.x - v1.x * v2.z;
+ res.z = v1.x * v2.y - v1.y * v2.x;
+ return res;
+}
+
+static INLINE void v3_normalize(vec3_t *v)
+{
+ float mag = sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
+ if(mag != 0.0f) {
+ float s = 1.0f / mag;
+ v->x *= s;
+ v->y *= s;
+ v->z *= s;
+ }
+}
+
/* quaternion functions */
static INLINE quat_t quat_cons(float s, float x, float y, float z)
{
return quat_mul(q, rq);
}
+static INLINE void mat4_transpose(float *mat)
+{
+ int i, j;
+
+ for(i=0; i<4; i++) {
+ for(j=0; j<i; j++) {
+ int rowidx = i * 4 + j;
+ int colidx = j * 4 + i;
+ float tmp = mat[rowidx];
+ mat[rowidx] = mat[colidx];
+ mat[colidx] = tmp;
+ }
+ }
+}
+
+/* misc stuff */
+static vec3_t INLINE sphrand(float rad)
+{
+ vec3_t res;
+
+ float u = (float)rand() / RAND_MAX;
+ float v = (float)rand() / RAND_MAX;
+
+ float theta = 2.0f * M_PI * u;
+ float phi = acos(2.0f * v - 1.0f);
+
+ res.x = rad * cos(theta) * sin(phi);
+ res.y = rad * sin(theta) * sin(phi);
+ res.z = rad * cos(phi);
+
+ return res;
+}
+
#endif /* VMATH_H_ */