Ylva And Malin
ym_math.h
Go to the documentation of this file.
1 #pragma once
2 #include <math.h>
3 
4 #define YM_PI 3.14159265358979323846f
5 #define YM_DEG_TO_RAD(deg) (deg * YM_PI / 180.0f)
6 #define YM_RAD_TO_DEG(rad) (180.0f * rad / YM_PI)
7 
8 typedef struct
9 {
10  float x;
11  float y;
12 } ym_vec2;
13 
14 typedef struct
15 {
16  union
17  {
18  float val[3];
19  struct
20  {
21  float x;
22  float y;
23  float z;
24  };
25  };
26 } ym_vec3;
27 
28 typedef struct
29 {
30  union
31  {
32  float val[4];
33  struct
34  {
35  float x;
36  float y;
37  float z;
38  float w;
39  };
40  };
41 } ym_vec4;
42 
43 
44 typedef struct
45 {
46  float val[9];
47 } ym_mat3;
48 
49 typedef struct
50 {
51  float val[16];
52 } ym_mat4;
53 
54 //static const ym_mat4 ym_identity =
55 //{
56 // .val =
57 // {
58 // 1.0f, 0.0f, 0.0f, 0.0f,
59 // 0.0f, 1.0f, 0.0f, 0.0f,
60 // 0.0f, 0.0f, 1.0f, 0.0f,
61 // 0.0f, 0.0f, 0.0f, 1.0f,
62 // },
63 //};
64 
66 ym_mat4
68 {
69  ym_mat4 res;
70  for (int i = 0; i < 4; i++)
71  {
72  for (int j = 0; j < 4; j++)
73  {
74  res.val[i * 4 + j] = 0;
75  for (int k = 0; k < 4; k++)
76  res.val[i * 4 + j] += lhs.val[i * 4 + k] * rhs.val[k * 4 + j];
77  }
78  }
79  return res;
80 }
81 
83 ym_mat3
85 {
86  ym_mat3 res;
87  for (int i = 0; i < 3; i++)
88  {
89  for (int j = 0; j < 3; j++)
90  {
91  res.val[i * 3 + j] = 0;
92  for (int k = 0; k < 3; k++)
93  res.val[i * 3 + j] += lhs.val[i * 3 + k] * rhs.val[k * 3 + j];
94  }
95  }
96  return res;
97 }
98 
100 ym_vec4
102 {
103  ym_vec4 res;
104  for (int i = 0; i < 4; i++)
105  {
106  res.val[i] = 0;
107  for (int j = 0; j < 4; j++)
108  res.val[i] += lhs.val[i * 4 + j] * rhs.val[j];
109  }
110 
111  return res;
112 }
113 
114 YM_INLINE
115 ym_vec3
117 {
118  ym_vec3 res;
119  for (int i = 0; i < 3; i++)
120  {
121  res.val[i] = 0;
122  for (int j = 0; j < 3; j++)
123  res.val[i] += lhs.val[i * 3 + j] * rhs.val[j];
124  }
125 
126  return res;
127 }
128 
129 YM_INLINE
130 ym_mat3
132 {
133  ym_mat3 mat =
134  {
135  .val =
136  {
137  1.0f, 0.0f, 0.0f,
138  0.0f, 1.0f, 0.0f,
139  vec.x, vec.y, 1.0f,
140  },
141  };
142 
143  return mat;
144 }
145 
146 YM_INLINE
147 ym_mat3
149 {
150  ym_mat3 mat =
151  {
152  .val =
153  {
154  vec.x, 0.0f, 0.0f,
155  0.0f, vec.y, 0.0f,
156  0.0f, 0.0f, 1.0f,
157  },
158  };
159 
160  return mat;
161 }
162 
163 
164 YM_INLINE
165 ym_mat4
167 {
168  ym_mat4 mat =
169  {
170  .val =
171  {
172  1.0f, 0.0f, 0.0f, 0.0f,
173  0.0f, 1.0f, 0.0f, 0.0f,
174  0.0f, 0.0f, 1.0f, 0.0f,
175  vec.x, vec.y, vec.z, 1.0f,
176  },
177  };
178 
179  return mat;
180 }
181 
182 YM_INLINE
183 ym_mat4
185 {
186  ym_mat4 mat =
187  {
188  .val =
189  {
190  vec.x, 0.0f, 0.0f, 0.0f,
191  0.0f, vec.y, 0.0f, 0.0f,
192  0.0f, 0.0f, vec.z, 0.0f,
193  0.0f, 0.0f, 0.0f, 1.0f,
194  },
195  };
196 
197  return mat;
198 }
199 
200 // Taken from: http://antongerdelan.net/teaching/3dprog1/maths_cheat_sheet.pdf
201 YM_INLINE
202 ym_mat4
203 ym_lookat(ym_vec4 camera_pos, ym_vec4 target_pos, ym_vec4 up_direction)
204 {
205  // rx, ry, rz, -px,
206  // ux, uy, uz, -py,
207  // -fx, -fy, -fz, -pz,
208  // 0, 0, 0, 1,
209 
210  // rx, ux, -fx, 0,
211  // ry, uy, -fy, 0,
212  // rz, uz, -fz, 0,
213  // -px, -py, -pz, 1,
214 
215  ym_vec4 camera_neg =
216  {
217  .x = -camera_pos.x,
218  .y = -camera_pos.y,
219  .z = -camera_pos.z,
220  .w = 1.0f,
221  };
222 
223  ym_mat4 trans = ym_translate_vec4(camera_neg);
224 
225  ym_vec4 distance =
226  {
227  .x = target_pos.x - camera_pos.x,
228  .y = target_pos.y - camera_pos.y,
229  .z = target_pos.z - camera_pos.z,
230  .w = 1.0f,
231  };
232 
233  float magn = sqrtf(distance.x * distance.x +
234  distance.y * distance.y +
235  distance.z * distance.z);
236 
237  distance.x /= magn;
238  distance.y /= magn;
239  distance.z /= magn;
240 
241  // Cross product of forward and upward vector
242  ym_vec4 r =
243  {
244  .x = distance.y * up_direction.z - distance.z * up_direction.y,
245  .y = distance.z * up_direction.x - distance.x * up_direction.z,
246  .z = distance.x * up_direction.y - distance.y * up_direction.x,
247  .w = 1.0f,
248  };
249 
250  ym_mat4 mat =
251  {
252  .val =
253  {
254  r.x, up_direction.x, -distance.x, 0.0f,
255  r.y, up_direction.y, -distance.y, 0.0f,
256  r.z, up_direction.z, -distance.z, 0.0f,
257  0.0f, 0.0f, 0.0f, 1.0f,
258  },
259  };
260 
261  return ym_mul_mat4_mat4(trans, mat);
262 }
263 
264 // Taken from: http://antongerdelan.net/teaching/3dprog1/maths_cheat_sheet.pdf
265 YM_INLINE
266 ym_mat4
267 ym_project(float near, float far, float fov, float aspect)
268 {
269  float range = tanf(fov * 0.5f) * near;
270  float sx = (2.0f * near) / (range * aspect + range * aspect);
271  float sy = near / range;
272  float sz = -(far + near) / (far - near);
273  float pz = -(2 * far * near) / (far - near);
274 
275  ym_mat4 mat =
276  {
277  .val =
278  {
279  sx, 0.0f, 0.0f, 0.0f,
280  0.0f, sy, 0.0f, 0.0f,
281  0.0f, 0.0f, sz, -1.0f,
282  0.0f, 0.0f, pz, 0.0f,
283  },
284  };
285 
286  return mat;
287 }
288 
289 YM_INLINE
290 void
292 {
293  for (int i = 0; i < 3; i++)
294  {
295  for (int j = 0; j < 3; j++)
296  printf("%f ", mat.val[i * 3 + j]);
297  printf("\n");
298  }
299 }
float z
Definition: ym_math.h:37
float z
Definition: ym_math.h:23
#define YM_INLINE
YM_INLINE is a platform independent macro that forces a function to be inlined.
Definition: ym_attributes.h:31
float w
Definition: ym_math.h:38
Definition: ym_math.h:44
float val[9]
Definition: ym_math.h:46
YM_INLINE ym_vec4 ym_mul_mat4_vec4(ym_mat4 lhs, ym_vec4 rhs)
Definition: ym_math.h:101
float y
Definition: ym_math.h:36
Definition: ym_math.h:8
float val[3]
Definition: ym_math.h:18
YM_INLINE void ym_print_mat3(ym_mat3 mat)
Definition: ym_math.h:291
YM_INLINE ym_mat4 ym_lookat(ym_vec4 camera_pos, ym_vec4 target_pos, ym_vec4 up_direction)
Definition: ym_math.h:203
YM_INLINE ym_mat4 ym_project(float near, float far, float fov, float aspect)
Definition: ym_math.h:267
Definition: ym_math.h:28
YM_INLINE ym_mat4 ym_mul_mat4_mat4(ym_mat4 lhs, ym_mat4 rhs)
Definition: ym_math.h:67
YM_INLINE ym_mat4 ym_translate_vec4(ym_vec4 vec)
Definition: ym_math.h:166
float x
Definition: ym_math.h:10
float x
Definition: ym_math.h:21
float y
Definition: ym_math.h:22
YM_INLINE ym_mat3 ym_mul_mat3_mat3(ym_mat3 lhs, ym_mat3 rhs)
Definition: ym_math.h:84
YM_INLINE ym_mat3 ym_scale_vec3(ym_vec3 vec)
Definition: ym_math.h:148
Definition: ym_math.h:14
float val[4]
Definition: ym_math.h:32
YM_INLINE ym_vec3 ym_mul_mat3_vec3(ym_mat3 lhs, ym_vec3 rhs)
Definition: ym_math.h:116
YM_INLINE ym_mat3 ym_translate_vec3(ym_vec3 vec)
Definition: ym_math.h:131
float val[16]
Definition: ym_math.h:51
float x
Definition: ym_math.h:35
Definition: ym_math.h:49
YM_INLINE ym_mat4 ym_scale_vec4(ym_vec4 vec)
Definition: ym_math.h:184
float y
Definition: ym_math.h:11