1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
/*
* ============================================================
* FILE: math3d.h
*
* PURPOSE: The small amount of vector/matrix math the game needs, written
* by hand so we don't pull in a math library.
*
* FITS IN: Header-only (every function is `static inline`). Included by
* any file that does math. Depends only on <math.h>.
*
* INTERFACE: Use these exact names:
*
* Constants
* M3D_PI 3.14159265358979f (C99 doesn't guarantee M_PI)
*
* Types (plain structs, passed and returned BY VALUE since they're small)
* typedef struct { float x, y; } Vec2;
* typedef struct { float x, y, z; } Vec3;
* typedef struct { float m[16]; } Mat4; COLUMN-MAJOR, element (row r,
* col c) is m[c*4 + r], so it can
* go straight to glUniformMatrix4fv
* with transpose = GL_FALSE.
*
* Scalar helpers
* float clampf(float v, float lo, float hi);
* float approachf(float current, float target, float max_step);
* moves current toward target by at most max_step, never overshoots.
* (Used for acceleration/friction.)
* float wrap_angle(float a);
* returns a in (-PI, PI].
* float approach_angle(float current, float target, float max_step);
* like approachf but takes the SHORT way around the circle.
* (Used for turning the player.)
*
* Vec2
* Vec2 vec2_make(float x, float y);
* float vec2_length(Vec2 v);
*
* Vec3
* Vec3 vec3_make(float x, float y, float z);
* Vec3 vec3_add(Vec3 a, Vec3 b);
* Vec3 vec3_sub(Vec3 a, Vec3 b);
* Vec3 vec3_scale(Vec3 v, float s);
* float vec3_dot(Vec3 a, Vec3 b);
* Vec3 vec3_cross(Vec3 a, Vec3 b);
* float vec3_length(Vec3 v);
* Vec3 vec3_normalize(Vec3 v); returns (0,0,0) for zero-length input
* Vec3 vec3_lerp(Vec3 a, Vec3 b, float t);
*
* Mat4
* Mat4 mat4_identity(void);
* Mat4 mat4_mul(Mat4 a, Mat4 b); result = a * b (b is applied first)
* Mat4 mat4_translate(Vec3 t);
* Mat4 mat4_scale(Vec3 s);
* Mat4 mat4_rotate_y(float angle); rotates +Z toward +X, i.e.
* (0,0,1) -> (sin a, 0, cos a)
* Mat4 mat4_perspective(float fovy_rad, float aspect, float near, float far);
* standard OpenGL (gluPerspective) form
* Mat4 mat4_look_at(Vec3 eye, Vec3 target, Vec3 up);
* standard OpenGL (gluLookAt) form
*
* CONVENTIONS: Right-handed, Y up. Radians everywhere. Keep each function a
* few lines with a short comment; this is a learning resource.
* Include guard: MATH3D_H.
*
* NOT IN SCOPE: Quaternions, matrix inverse, SIMD, Vec4, generic N-dim math.
* Add a function only when a caller needs it.
*
* DONE WHEN: All functions above compile cleanly under -std=c99 -Wall
* -Wextra, and mat4_perspective * mat4_look_at produces a
* correct view of the ground grid in render.c.
* ============================================================
*/
#ifndef MATH3D_H
#define MATH3D_H
#endif /* MATH3D_H */
|