specific.odin 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. package linalg
  2. import "core:math"
  3. // Specific
  4. Float :: f32;
  5. FLOAT_EPSILON :: 1e-7 when size_of(Float) == 4 else 1e-15;
  6. Vector2 :: distinct [2]Float;
  7. Vector3 :: distinct [3]Float;
  8. Vector4 :: distinct [4]Float;
  9. Matrix1x1 :: distinct [1][1]Float;
  10. Matrix1x2 :: distinct [1][2]Float;
  11. Matrix1x3 :: distinct [1][3]Float;
  12. Matrix1x4 :: distinct [1][4]Float;
  13. Matrix2x1 :: distinct [2][1]Float;
  14. Matrix2x2 :: distinct [2][2]Float;
  15. Matrix2x3 :: distinct [2][3]Float;
  16. Matrix2x4 :: distinct [2][4]Float;
  17. Matrix3x1 :: distinct [3][1]Float;
  18. Matrix3x2 :: distinct [3][2]Float;
  19. Matrix3x3 :: distinct [3][3]Float;
  20. Matrix3x4 :: distinct [3][4]Float;
  21. Matrix4x1 :: distinct [4][1]Float;
  22. Matrix4x2 :: distinct [4][2]Float;
  23. Matrix4x3 :: distinct [4][3]Float;
  24. Matrix4x4 :: distinct [4][4]Float;
  25. Matrix1 :: Matrix1x1;
  26. Matrix2 :: Matrix2x2;
  27. Matrix3 :: Matrix3x3;
  28. Matrix4 :: Matrix4x4;
  29. Quaternion :: distinct (quaternion128 when size_of(Float) == size_of(f32) else quaternion256);
  30. MATRIX1_IDENTITY :: Matrix1{{1}};
  31. MATRIX2_IDENTITY :: Matrix2{{1, 0}, {0, 1}};
  32. MATRIX3_IDENTITY :: Matrix3{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
  33. MATRIX4_IDENTITY :: Matrix4{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
  34. QUATERNION_IDENTITY :: Quaternion(1);
  35. VECTOR3_X_AXIS :: Vector3{1, 0, 0};
  36. VECTOR3_Y_AXIS :: Vector3{0, 1, 0};
  37. VECTOR3_Z_AXIS :: Vector3{0, 0, 1};
  38. radians :: proc(degrees: Float) -> Float {
  39. return math.TAU * degrees / 360.0;
  40. }
  41. degrees :: proc(radians: Float) -> Float {
  42. return 360.0 * radians / math.TAU;
  43. }
  44. vector2_orthogonal :: proc(v: Vector2) -> Vector2 {
  45. return {-v.y, v.x};
  46. }
  47. vector3_orthogonal :: proc(v: Vector3) -> Vector3 {
  48. x := abs(v.x);
  49. y := abs(v.y);
  50. z := abs(v.z);
  51. other: Vector3;
  52. if x < y {
  53. if x < z {
  54. other = {1, 0, 0};
  55. } else {
  56. other = {0, 0, 1};
  57. }
  58. } else {
  59. if y < z {
  60. other = {0, 1, 0};
  61. } else {
  62. other = {0, 0, 1};
  63. }
  64. }
  65. return normalize(cross(v, other));
  66. }
  67. vector4_srgb_to_linear :: proc(col: Vector4) -> Vector4 {
  68. r := math.pow(col.x, 2.2);
  69. g := math.pow(col.y, 2.2);
  70. b := math.pow(col.z, 2.2);
  71. a := col.w;
  72. return {r, g, b, a};
  73. }
  74. vector4_linear_to_srgb :: proc(col: Vector4) -> Vector4 {
  75. a :: 2.51;
  76. b :: 0.03;
  77. c :: 2.43;
  78. d :: 0.59;
  79. e :: 0.14;
  80. x := col.x;
  81. y := col.y;
  82. z := col.z;
  83. x = (x * (a * x + b)) / (x * (c * x + d) + e);
  84. y = (y * (a * y + b)) / (y * (c * y + d) + e);
  85. z = (z * (a * z + b)) / (z * (c * z + d) + e);
  86. x = math.pow(clamp(x, 0, 1), 1.0 / 2.2);
  87. y = math.pow(clamp(y, 0, 1), 1.0 / 2.2);
  88. z = math.pow(clamp(z, 0, 1), 1.0 / 2.2);
  89. return {x, y, z, col.w};
  90. }
  91. vector4_hsl_to_rgb :: proc(h, s, l: Float, a: Float = 1) -> Vector4 {
  92. hue_to_rgb :: proc(p, q, t: Float) -> Float {
  93. t := t;
  94. if t < 0 do t += 1;
  95. if t > 1 do t -= 1;
  96. switch {
  97. case t < 1.0/6.0: return p + (q - p) * 6.0 * t;
  98. case t < 1.0/2.0: return q;
  99. case t < 2.0/3.0: return p + (q - p) * 6.0 * (2.0/3.0 - t);
  100. }
  101. return p;
  102. }
  103. r, g, b: Float;
  104. if s == 0 {
  105. r = l;
  106. g = l;
  107. b = l;
  108. } else {
  109. q := l * (1+s) if l < 0.5 else l+s - l*s;
  110. p := 2*l - q;
  111. r = hue_to_rgb(p, q, h + 1.0/3.0);
  112. g = hue_to_rgb(p, q, h);
  113. b = hue_to_rgb(p, q, h - 1.0/3.0);
  114. }
  115. return {r, g, b, a};
  116. }
  117. vector4_rgb_to_hsl :: proc(col: Vector4) -> Vector4 {
  118. r := col.x;
  119. g := col.y;
  120. b := col.z;
  121. a := col.w;
  122. v_min := min(r, g, b);
  123. v_max := max(r, g, b);
  124. h, s, l: Float;
  125. h = 0.0;
  126. s = 0.0;
  127. l = (v_min + v_max) * 0.5;
  128. if v_max != v_min {
  129. d: = v_max - v_min;
  130. s = d / (2.0 - v_max - v_min) if l > 0.5 else d / (v_max + v_min);
  131. switch {
  132. case v_max == r:
  133. h = (g - b) / d + (6.0 if g < b else 0.0);
  134. case v_max == g:
  135. h = (b - r) / d + 2.0;
  136. case v_max == b:
  137. h = (r - g) / d + 4.0;
  138. }
  139. h *= 1.0/6.0;
  140. }
  141. return {h, s, l, a};
  142. }
  143. quaternion_angle_axis :: proc(angle_radians: Float, axis: Vector3) -> Quaternion {
  144. t := angle_radians*0.5;
  145. w := math.cos(t);
  146. v := normalize(axis) * math.sin(t);
  147. return quaternion(w, v.x, v.y, v.z);
  148. }
  149. quaternion_from_euler_angles :: proc(roll, pitch, yaw: Float) -> Quaternion {
  150. x, y, z := roll, pitch, yaw;
  151. a, b, c := x, y, z;
  152. ca, sa := math.cos(a*0.5), math.sin(a*0.5);
  153. cb, sb := math.cos(b*0.5), math.sin(b*0.5);
  154. cc, sc := math.cos(c*0.5), math.sin(c*0.5);
  155. q: Quaternion;
  156. q.x = sa*cb*cc - ca*sb*sc;
  157. q.y = ca*sb*cc + sa*cb*sc;
  158. q.z = ca*cb*sc - sa*sb*cc;
  159. q.w = ca*cb*cc + sa*sb*sc;
  160. return q;
  161. }
  162. euler_angles_from_quaternion :: proc(q: Quaternion) -> (roll, pitch, yaw: Float) {
  163. // roll, x-axis rotation
  164. sinr_cosp: Float = 2 * (q.w * q.x + q.y * q.z);
  165. cosr_cosp: Float = 1 - 2 * (q.x * q.x + q.y * q.y);
  166. roll = math.atan2(sinr_cosp, cosr_cosp);
  167. // pitch, y-axis rotation
  168. sinp: Float = 2 * (q.w * q.y - q.z * q.x);
  169. if abs(sinp) >= 1 {
  170. pitch = math.copy_sign(math.TAU * 0.25, sinp);
  171. } else {
  172. pitch = 2 * math.asin(sinp);
  173. }
  174. // yaw, z-axis rotation
  175. siny_cosp: Float = 2 * (q.w * q.z + q.x * q.y);
  176. cosy_cosp: Float = 1 - 2 * (q.y * q.y + q.z * q.z);
  177. yaw = math.atan2(siny_cosp, cosy_cosp);
  178. return;
  179. }
  180. quaternion_from_forward_and_up :: proc(forward, up: Vector3) -> Quaternion {
  181. f := normalize(forward);
  182. s := normalize(cross(f, up));
  183. u := cross(s, f);
  184. m := Matrix3{
  185. {+s.x, +u.x, -f.x},
  186. {+s.y, +u.y, -f.y},
  187. {+s.z, +u.z, -f.z},
  188. };
  189. tr := trace(m);
  190. q: Quaternion;
  191. switch {
  192. case tr > 0:
  193. S := 2 * math.sqrt(1 + tr);
  194. q.w = 0.25 * S;
  195. q.x = (m[2][1] - m[1][2]) / S;
  196. q.y = (m[0][2] - m[2][0]) / S;
  197. q.z = (m[1][0] - m[0][1]) / S;
  198. case (m[0][0] > m[1][1]) && (m[0][0] > m[2][2]):
  199. S := 2 * math.sqrt(1 + m[0][0] - m[1][1] - m[2][2]);
  200. q.w = (m[2][1] - m[1][2]) / S;
  201. q.x = 0.25 * S;
  202. q.y = (m[0][1] + m[1][0]) / S;
  203. q.z = (m[0][2] + m[2][0]) / S;
  204. case m[1][1] > m[2][2]:
  205. S := 2 * math.sqrt(1 + m[1][1] - m[0][0] - m[2][2]);
  206. q.w = (m[0][2] - m[2][0]) / S;
  207. q.x = (m[0][1] + m[1][0]) / S;
  208. q.y = 0.25 * S;
  209. q.z = (m[1][2] + m[2][1]) / S;
  210. case:
  211. S := 2 * math.sqrt(1 + m[2][2] - m[0][0] - m[1][1]);
  212. q.w = (m[1][0] - m[0][1]) / S;
  213. q.x = (m[0][2] - m[2][0]) / S;
  214. q.y = (m[1][2] + m[2][1]) / S;
  215. q.z = 0.25 * S;
  216. }
  217. return normalize(q);
  218. }
  219. quaternion_look_at :: proc(eye, centre: Vector3, up: Vector3) -> Quaternion {
  220. return quaternion_from_forward_and_up(centre-eye, up);
  221. }
  222. quaternion_nlerp :: proc(a, b: Quaternion, t: Float) -> Quaternion {
  223. c := a + (b-a)*quaternion(t, 0, 0, 0);
  224. return normalize(c);
  225. }
  226. quaternion_slerp :: proc(x, y: Quaternion, t: Float) -> Quaternion {
  227. a, b := x, y;
  228. cos_angle := dot(a, b);
  229. if cos_angle < 0 {
  230. b = -b;
  231. cos_angle = -cos_angle;
  232. }
  233. if cos_angle > 1 - FLOAT_EPSILON {
  234. return a + (b-a)*quaternion(t, 0, 0, 0);
  235. }
  236. angle := math.acos(cos_angle);
  237. sin_angle := math.sin(angle);
  238. factor_a, factor_b: Quaternion;
  239. factor_a = quaternion(math.sin((1-t) * angle) / sin_angle, 0, 0, 0);
  240. factor_b = quaternion(math.sin(t * angle) / sin_angle, 0, 0, 0);
  241. return factor_a * a + factor_b * b;
  242. }
  243. quaternion_from_matrix4 :: proc(m: Matrix4) -> Quaternion {
  244. four_x_squared_minus_1, four_y_squared_minus_1,
  245. four_z_squared_minus_1, four_w_squared_minus_1,
  246. four_biggest_squared_minus_1: Float;
  247. /* xyzw */
  248. /* 0123 */
  249. biggest_index := 3;
  250. biggest_value, mult: Float;
  251. four_x_squared_minus_1 = m[0][0] - m[1][1] - m[2][2];
  252. four_y_squared_minus_1 = m[1][1] - m[0][0] - m[2][2];
  253. four_z_squared_minus_1 = m[2][2] - m[0][0] - m[1][1];
  254. four_w_squared_minus_1 = m[0][0] + m[1][1] + m[2][2];
  255. four_biggest_squared_minus_1 = four_w_squared_minus_1;
  256. if four_x_squared_minus_1 > four_biggest_squared_minus_1 {
  257. four_biggest_squared_minus_1 = four_x_squared_minus_1;
  258. biggest_index = 0;
  259. }
  260. if four_y_squared_minus_1 > four_biggest_squared_minus_1 {
  261. four_biggest_squared_minus_1 = four_y_squared_minus_1;
  262. biggest_index = 1;
  263. }
  264. if four_z_squared_minus_1 > four_biggest_squared_minus_1 {
  265. four_biggest_squared_minus_1 = four_z_squared_minus_1;
  266. biggest_index = 2;
  267. }
  268. biggest_value = math.sqrt(four_biggest_squared_minus_1 + 1) * 0.5;
  269. mult = 0.25 / biggest_value;
  270. switch biggest_index {
  271. case 0:
  272. return quaternion(
  273. biggest_value,
  274. (m[0][1] + m[1][0]) * mult,
  275. (m[2][0] + m[0][2]) * mult,
  276. (m[1][2] - m[2][1]) * mult,
  277. );
  278. case 1:
  279. return quaternion(
  280. (m[0][1] + m[1][0]) * mult,
  281. biggest_value,
  282. (m[1][2] + m[2][1]) * mult,
  283. (m[2][0] - m[0][2]) * mult,
  284. );
  285. case 2:
  286. return quaternion(
  287. (m[2][0] + m[0][2]) * mult,
  288. (m[1][2] + m[2][1]) * mult,
  289. biggest_value,
  290. (m[0][1] - m[1][0]) * mult,
  291. );
  292. case 3:
  293. return quaternion(
  294. (m[1][2] - m[2][1]) * mult,
  295. (m[2][0] - m[0][2]) * mult,
  296. (m[0][1] - m[1][0]) * mult,
  297. biggest_value,
  298. );
  299. }
  300. return 0;
  301. }
  302. quaternion_from_matrix3 :: proc(m: Matrix3) -> Quaternion {
  303. four_x_squared_minus_1, four_y_squared_minus_1,
  304. four_z_squared_minus_1, four_w_squared_minus_1,
  305. four_biggest_squared_minus_1: Float;
  306. /* xyzw */
  307. /* 0123 */
  308. biggest_index := 3;
  309. biggest_value, mult: Float;
  310. four_x_squared_minus_1 = m[0][0] - m[1][1] - m[2][2];
  311. four_y_squared_minus_1 = m[1][1] - m[0][0] - m[2][2];
  312. four_z_squared_minus_1 = m[2][2] - m[0][0] - m[1][1];
  313. four_w_squared_minus_1 = m[0][0] + m[1][1] + m[2][2];
  314. four_biggest_squared_minus_1 = four_w_squared_minus_1;
  315. if four_x_squared_minus_1 > four_biggest_squared_minus_1 {
  316. four_biggest_squared_minus_1 = four_x_squared_minus_1;
  317. biggest_index = 0;
  318. }
  319. if four_y_squared_minus_1 > four_biggest_squared_minus_1 {
  320. four_biggest_squared_minus_1 = four_y_squared_minus_1;
  321. biggest_index = 1;
  322. }
  323. if four_z_squared_minus_1 > four_biggest_squared_minus_1 {
  324. four_biggest_squared_minus_1 = four_z_squared_minus_1;
  325. biggest_index = 2;
  326. }
  327. biggest_value = math.sqrt(four_biggest_squared_minus_1 + 1) * 0.5;
  328. mult = 0.25 / biggest_value;
  329. switch biggest_index {
  330. case 0:
  331. return quaternion(
  332. biggest_value,
  333. (m[0][1] + m[1][0]) * mult,
  334. (m[2][0] + m[0][2]) * mult,
  335. (m[1][2] - m[2][1]) * mult,
  336. );
  337. case 1:
  338. return quaternion(
  339. (m[0][1] + m[1][0]) * mult,
  340. biggest_value,
  341. (m[1][2] + m[2][1]) * mult,
  342. (m[2][0] - m[0][2]) * mult,
  343. );
  344. case 2:
  345. return quaternion(
  346. (m[2][0] + m[0][2]) * mult,
  347. (m[1][2] + m[2][1]) * mult,
  348. biggest_value,
  349. (m[0][1] - m[1][0]) * mult,
  350. );
  351. case 3:
  352. return quaternion(
  353. (m[1][2] - m[2][1]) * mult,
  354. (m[2][0] - m[0][2]) * mult,
  355. (m[0][1] - m[1][0]) * mult,
  356. biggest_value,
  357. );
  358. }
  359. return 0;
  360. }
  361. quaternion_between_two_vector3 :: proc(from, to: Vector3) -> Quaternion {
  362. x := normalize(from);
  363. y := normalize(to);
  364. cos_theta := dot(x, y);
  365. if abs(cos_theta + 1) < 2*FLOAT_EPSILON {
  366. v := vector3_orthogonal(x);
  367. return quaternion(0, v.x, v.y, v.z);
  368. }
  369. v := cross(x, y);
  370. w := cos_theta + 1;
  371. return Quaternion(normalize(quaternion(w, v.x, v.y, v.z)));
  372. }
  373. matrix2_inverse_transpose :: proc(m: Matrix2) -> Matrix2 {
  374. c: Matrix2;
  375. d := m[0][0]*m[1][1] - m[1][0]*m[0][1];
  376. id := 1.0/d;
  377. c[0][0] = +m[1][1] * id;
  378. c[0][1] = -m[0][1] * id;
  379. c[1][0] = -m[1][0] * id;
  380. c[1][1] = +m[0][0] * id;
  381. return c;
  382. }
  383. matrix2_determinant :: proc(m: Matrix2) -> Float {
  384. return m[0][0]*m[1][1] - m[1][0]*m[0][1];
  385. }
  386. matrix2_inverse :: proc(m: Matrix2) -> Matrix2 {
  387. c: Matrix2;
  388. d := m[0][0]*m[1][1] - m[1][0]*m[0][1];
  389. id := 1.0/d;
  390. c[0][0] = +m[1][1] * id;
  391. c[1][0] = -m[0][1] * id;
  392. c[0][1] = -m[1][0] * id;
  393. c[1][1] = +m[0][0] * id;
  394. return c;
  395. }
  396. matrix2_adjoint :: proc(m: Matrix2) -> Matrix2 {
  397. c: Matrix2;
  398. c[0][0] = +m[1][1];
  399. c[0][1] = -m[1][0];
  400. c[1][0] = -m[0][1];
  401. c[1][1] = +m[0][0];
  402. return c;
  403. }
  404. matrix3_from_quaternion :: proc(q: Quaternion) -> Matrix3 {
  405. xx := q.x * q.x;
  406. xy := q.x * q.y;
  407. xz := q.x * q.z;
  408. xw := q.x * q.w;
  409. yy := q.y * q.y;
  410. yz := q.y * q.z;
  411. yw := q.y * q.w;
  412. zz := q.z * q.z;
  413. zw := q.z * q.w;
  414. m: Matrix3;
  415. m[0][0] = 1 - 2 * (yy + zz);
  416. m[1][0] = 2 * (xy - zw);
  417. m[2][0] = 2 * (xz + yw);
  418. m[0][1] = 2 * (xy + zw);
  419. m[1][1] = 1 - 2 * (xx + zz);
  420. m[2][1] = 2 * (yz - xw);
  421. m[0][2] = 2 * (xz - yw);
  422. m[1][2] = 2 * (yz + xw);
  423. m[2][2] = 1 - 2 * (xx + yy);
  424. return m;
  425. }
  426. matrix3_inverse :: proc(m: Matrix3) -> Matrix3 {
  427. return transpose(matrix3_inverse_transpose(m));
  428. }
  429. matrix3_determinant :: proc(m: Matrix3) -> Float {
  430. a := +m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
  431. b := -m[1][0] * (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
  432. c := +m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
  433. return a + b + c;
  434. }
  435. matrix3_adjoint :: proc(m: Matrix3) -> Matrix3 {
  436. adjoint: Matrix3;
  437. adjoint[0][0] = +(m[1][1] * m[2][2] - m[1][2] * m[2][1]);
  438. adjoint[1][0] = -(m[0][1] * m[2][2] - m[0][2] * m[2][1]);
  439. adjoint[2][0] = +(m[0][1] * m[1][2] - m[0][2] * m[1][1]);
  440. adjoint[0][1] = -(m[1][0] * m[2][2] - m[1][2] * m[2][0]);
  441. adjoint[1][1] = +(m[0][0] * m[2][2] - m[0][2] * m[2][0]);
  442. adjoint[2][1] = -(m[0][0] * m[1][2] - m[0][2] * m[1][0]);
  443. adjoint[0][2] = +(m[1][0] * m[2][1] - m[1][1] * m[2][0]);
  444. adjoint[1][2] = -(m[0][0] * m[2][1] - m[0][1] * m[2][0]);
  445. adjoint[2][2] = +(m[0][0] * m[1][1] - m[0][1] * m[1][0]);
  446. return adjoint;
  447. }
  448. matrix3_inverse_transpose :: proc(m: Matrix3) -> Matrix3 {
  449. inverse_transpose: Matrix3;
  450. adjoint := matrix3_adjoint(m);
  451. determinant := matrix3_determinant(m);
  452. inv_determinant := 1.0 / determinant;
  453. for i in 0..<3 {
  454. for j in 0..<3 {
  455. inverse_transpose[i][j] = adjoint[i][j] * inv_determinant;
  456. }
  457. }
  458. return inverse_transpose;
  459. }
  460. matrix3_scale :: proc(s: Vector3) -> Matrix3 {
  461. m: Matrix3;
  462. m[0][0] = s[0];
  463. m[1][1] = s[1];
  464. m[2][2] = s[2];
  465. return m;
  466. }
  467. matrix3_rotate :: proc(angle_radians: Float, v: Vector3) -> Matrix3 {
  468. c := math.cos(angle_radians);
  469. s := math.sin(angle_radians);
  470. a := normalize(v);
  471. t := a * (1-c);
  472. rot: Matrix3 = ---;
  473. rot[0][0] = c + t[0]*a[0];
  474. rot[0][1] = 0 + t[0]*a[1] + s*a[2];
  475. rot[0][2] = 0 + t[0]*a[2] - s*a[1];
  476. rot[1][0] = 0 + t[1]*a[0] - s*a[2];
  477. rot[1][1] = c + t[1]*a[1];
  478. rot[1][2] = 0 + t[1]*a[2] + s*a[0];
  479. rot[2][0] = 0 + t[2]*a[0] + s*a[1];
  480. rot[2][1] = 0 + t[2]*a[1] - s*a[0];
  481. rot[2][2] = c + t[2]*a[2];
  482. return rot;
  483. }
  484. matrix3_look_at :: proc(eye, centre, up: Vector3) -> Matrix3 {
  485. f := normalize(centre - eye);
  486. s := normalize(cross(f, up));
  487. u := cross(s, f);
  488. return Matrix3{
  489. {+s.x, +u.x, -f.x},
  490. {+s.y, +u.y, -f.y},
  491. {+s.z, +u.z, -f.z},
  492. };
  493. }
  494. matrix4_from_quaternion :: proc(q: Quaternion) -> Matrix4 {
  495. m := identity(Matrix4);
  496. xx := q.x * q.x;
  497. xy := q.x * q.y;
  498. xz := q.x * q.z;
  499. xw := q.x * q.w;
  500. yy := q.y * q.y;
  501. yz := q.y * q.z;
  502. yw := q.y * q.w;
  503. zz := q.z * q.z;
  504. zw := q.z * q.w;
  505. m[0][0] = 1 - 2 * (yy + zz);
  506. m[1][0] = 2 * (xy - zw);
  507. m[2][0] = 2 * (xz + yw);
  508. m[0][1] = 2 * (xy + zw);
  509. m[1][1] = 1 - 2 * (xx + zz);
  510. m[2][1] = 2 * (yz - xw);
  511. m[0][2] = 2 * (xz - yw);
  512. m[1][2] = 2 * (yz + xw);
  513. m[2][2] = 1 - 2 * (xx + yy);
  514. return m;
  515. }
  516. matrix4_from_trs :: proc(t: Vector3, r: Quaternion, s: Vector3) -> Matrix4 {
  517. translation := matrix4_translate(t);
  518. rotation := matrix4_from_quaternion(r);
  519. scale := matrix4_scale(s);
  520. return mul(translation, mul(rotation, scale));
  521. }
  522. matrix4_inverse :: proc(m: Matrix4) -> Matrix4 {
  523. return transpose(matrix4_inverse_transpose(m));
  524. }
  525. matrix4_minor :: proc(m: Matrix4, c, r: int) -> Float {
  526. cut_down: Matrix3;
  527. for i in 0..<3 {
  528. col := i if i < c else i+1;
  529. for j in 0..<3 {
  530. row := j if j < r else j+1;
  531. cut_down[i][j] = m[col][row];
  532. }
  533. }
  534. return matrix3_determinant(cut_down);
  535. }
  536. matrix4_cofactor :: proc(m: Matrix4, c, r: int) -> Float {
  537. sign, minor: Float;
  538. sign = 1 if (c + r) % 2 == 0 else -1;
  539. minor = matrix4_minor(m, c, r);
  540. return sign * minor;
  541. }
  542. matrix4_adjoint :: proc(m: Matrix4) -> Matrix4 {
  543. adjoint: Matrix4;
  544. for i in 0..<4 {
  545. for j in 0..<4 {
  546. adjoint[i][j] = matrix4_cofactor(m, i, j);
  547. }
  548. }
  549. return adjoint;
  550. }
  551. matrix4_determinant :: proc(m: Matrix4) -> Float {
  552. adjoint := matrix4_adjoint(m);
  553. determinant: Float = 0;
  554. for i in 0..<4 {
  555. determinant += m[i][0] * adjoint[i][0];
  556. }
  557. return determinant;
  558. }
  559. matrix4_inverse_transpose :: proc(m: Matrix4) -> Matrix4 {
  560. adjoint := matrix4_adjoint(m);
  561. determinant: Float = 0;
  562. for i in 0..<4 {
  563. determinant += m[i][0] * adjoint[i][0];
  564. }
  565. inv_determinant := 1.0 / determinant;
  566. inverse_transpose: Matrix4;
  567. for i in 0..<4 {
  568. for j in 0..<4 {
  569. inverse_transpose[i][j] = adjoint[i][j] * inv_determinant;
  570. }
  571. }
  572. return inverse_transpose;
  573. }
  574. matrix4_translate :: proc(v: Vector3) -> Matrix4 {
  575. m := identity(Matrix4);
  576. m[3][0] = v[0];
  577. m[3][1] = v[1];
  578. m[3][2] = v[2];
  579. return m;
  580. }
  581. matrix4_rotate :: proc(angle_radians: Float, v: Vector3) -> Matrix4 {
  582. c := math.cos(angle_radians);
  583. s := math.sin(angle_radians);
  584. a := normalize(v);
  585. t := a * (1-c);
  586. rot := identity(Matrix4);
  587. rot[0][0] = c + t[0]*a[0];
  588. rot[0][1] = 0 + t[0]*a[1] + s*a[2];
  589. rot[0][2] = 0 + t[0]*a[2] - s*a[1];
  590. rot[0][3] = 0;
  591. rot[1][0] = 0 + t[1]*a[0] - s*a[2];
  592. rot[1][1] = c + t[1]*a[1];
  593. rot[1][2] = 0 + t[1]*a[2] + s*a[0];
  594. rot[1][3] = 0;
  595. rot[2][0] = 0 + t[2]*a[0] + s*a[1];
  596. rot[2][1] = 0 + t[2]*a[1] - s*a[0];
  597. rot[2][2] = c + t[2]*a[2];
  598. rot[2][3] = 0;
  599. return rot;
  600. }
  601. matrix4_scale :: proc(v: Vector3) -> Matrix4 {
  602. m: Matrix4;
  603. m[0][0] = v[0];
  604. m[1][1] = v[1];
  605. m[2][2] = v[2];
  606. m[3][3] = 1;
  607. return m;
  608. }
  609. matrix4_look_at :: proc(eye, centre, up: Vector3) -> Matrix4 {
  610. f := normalize(centre - eye);
  611. s := normalize(cross(f, up));
  612. u := cross(s, f);
  613. return Matrix4{
  614. {+s.x, +u.x, -f.x, 0},
  615. {+s.y, +u.y, -f.y, 0},
  616. {+s.z, +u.z, -f.z, 0},
  617. {-dot(s, eye), -dot(u, eye), +dot(f, eye), 1},
  618. };
  619. }
  620. matrix4_perspective :: proc(fovy, aspect, near, far: Float, flip_z_axis := true) -> (m: Matrix4) {
  621. tan_half_fovy := math.tan(0.5 * fovy);
  622. m[0][0] = 1 / (aspect*tan_half_fovy);
  623. m[1][1] = 1 / (tan_half_fovy);
  624. m[2][2] = +(far + near) / (far - near);
  625. m[2][3] = +1;
  626. m[3][2] = -2*far*near / (far - near);
  627. if flip_z_axis {
  628. m[2] = -m[2];
  629. }
  630. return;
  631. }
  632. matrix_ortho3d :: proc(left, right, bottom, top, near, far: Float, flip_z_axis := true) -> (m: Matrix4) {
  633. m[0][0] = +2 / (right - left);
  634. m[1][1] = +2 / (top - bottom);
  635. m[2][2] = +2 / (far - near);
  636. m[3][0] = -(right + left) / (right - left);
  637. m[3][1] = -(top + bottom) / (top - bottom);
  638. m[3][2] = -(far + near) / (far- near);
  639. m[3][3] = 1;
  640. if flip_z_axis {
  641. m[2] = -m[2];
  642. }
  643. return;
  644. }
  645. matrix4_infinite_perspective :: proc(fovy, aspect, near: Float, flip_z_axis := true) -> (m: Matrix4) {
  646. tan_half_fovy := math.tan(0.5 * fovy);
  647. m[0][0] = 1 / (aspect*tan_half_fovy);
  648. m[1][1] = 1 / (tan_half_fovy);
  649. m[2][2] = +1;
  650. m[2][3] = +1;
  651. m[3][2] = -2*near;
  652. if flip_z_axis {
  653. m[2] = -m[2];
  654. }
  655. return;
  656. }