specific.odin 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. package linalg
  2. import "core:builtin"
  3. import "core:math"
  4. F16_EPSILON :: 1e-3
  5. F32_EPSILON :: 1e-7
  6. F64_EPSILON :: 1e-15
  7. Vector2f16 :: distinct [2]f16
  8. Vector3f16 :: distinct [3]f16
  9. Vector4f16 :: distinct [4]f16
  10. Matrix1x1f16 :: distinct matrix[1, 1]f16
  11. Matrix1x2f16 :: distinct matrix[1, 2]f16
  12. Matrix1x3f16 :: distinct matrix[1, 3]f16
  13. Matrix1x4f16 :: distinct matrix[1, 4]f16
  14. Matrix2x1f16 :: distinct matrix[2, 1]f16
  15. Matrix2x2f16 :: distinct matrix[2, 2]f16
  16. Matrix2x3f16 :: distinct matrix[2, 3]f16
  17. Matrix2x4f16 :: distinct matrix[2, 4]f16
  18. Matrix3x1f16 :: distinct matrix[3, 1]f16
  19. Matrix3x2f16 :: distinct matrix[3, 2]f16
  20. Matrix3x3f16 :: distinct matrix[3, 3]f16
  21. Matrix3x4f16 :: distinct matrix[3, 4]f16
  22. Matrix4x1f16 :: distinct matrix[4, 1]f16
  23. Matrix4x2f16 :: distinct matrix[4, 2]f16
  24. Matrix4x3f16 :: distinct matrix[4, 3]f16
  25. Matrix4x4f16 :: distinct matrix[4, 4]f16
  26. Matrix1f16 :: Matrix1x1f16
  27. Matrix2f16 :: Matrix2x2f16
  28. Matrix3f16 :: Matrix3x3f16
  29. Matrix4f16 :: Matrix4x4f16
  30. Vector2f32 :: distinct [2]f32
  31. Vector3f32 :: distinct [3]f32
  32. Vector4f32 :: distinct [4]f32
  33. Matrix1x1f32 :: distinct matrix[1, 1]f32
  34. Matrix1x2f32 :: distinct matrix[1, 2]f32
  35. Matrix1x3f32 :: distinct matrix[1, 3]f32
  36. Matrix1x4f32 :: distinct matrix[1, 4]f32
  37. Matrix2x1f32 :: distinct matrix[2, 1]f32
  38. Matrix2x2f32 :: distinct matrix[2, 2]f32
  39. Matrix2x3f32 :: distinct matrix[2, 3]f32
  40. Matrix2x4f32 :: distinct matrix[2, 4]f32
  41. Matrix3x1f32 :: distinct matrix[3, 1]f32
  42. Matrix3x2f32 :: distinct matrix[3, 2]f32
  43. Matrix3x3f32 :: distinct matrix[3, 3]f32
  44. Matrix3x4f32 :: distinct matrix[3, 4]f32
  45. Matrix4x1f32 :: distinct matrix[4, 1]f32
  46. Matrix4x2f32 :: distinct matrix[4, 2]f32
  47. Matrix4x3f32 :: distinct matrix[4, 3]f32
  48. Matrix4x4f32 :: distinct matrix[4, 4]f32
  49. Matrix1f32 :: Matrix1x1f32
  50. Matrix2f32 :: Matrix2x2f32
  51. Matrix3f32 :: Matrix3x3f32
  52. Matrix4f32 :: Matrix4x4f32
  53. Vector2f64 :: distinct [2]f64
  54. Vector3f64 :: distinct [3]f64
  55. Vector4f64 :: distinct [4]f64
  56. Matrix1x1f64 :: distinct matrix[1, 1]f64
  57. Matrix1x2f64 :: distinct matrix[1, 2]f64
  58. Matrix1x3f64 :: distinct matrix[1, 3]f64
  59. Matrix1x4f64 :: distinct matrix[1, 4]f64
  60. Matrix2x1f64 :: distinct matrix[2, 1]f64
  61. Matrix2x2f64 :: distinct matrix[2, 2]f64
  62. Matrix2x3f64 :: distinct matrix[2, 3]f64
  63. Matrix2x4f64 :: distinct matrix[2, 4]f64
  64. Matrix3x1f64 :: distinct matrix[3, 1]f64
  65. Matrix3x2f64 :: distinct matrix[3, 2]f64
  66. Matrix3x3f64 :: distinct matrix[3, 3]f64
  67. Matrix3x4f64 :: distinct matrix[3, 4]f64
  68. Matrix4x1f64 :: distinct matrix[4, 1]f64
  69. Matrix4x2f64 :: distinct matrix[4, 2]f64
  70. Matrix4x3f64 :: distinct matrix[4, 3]f64
  71. Matrix4x4f64 :: distinct matrix[4, 4]f64
  72. Matrix1f64 :: Matrix1x1f64
  73. Matrix2f64 :: Matrix2x2f64
  74. Matrix3f64 :: Matrix3x3f64
  75. Matrix4f64 :: Matrix4x4f64
  76. Quaternionf16 :: distinct quaternion64
  77. Quaternionf32 :: distinct quaternion128
  78. Quaternionf64 :: distinct quaternion256
  79. MATRIX1F16_IDENTITY :: Matrix1f16(1)
  80. MATRIX2F16_IDENTITY :: Matrix2f16(1)
  81. MATRIX3F16_IDENTITY :: Matrix3f16(1)
  82. MATRIX4F16_IDENTITY :: Matrix4f16(1)
  83. MATRIX1F32_IDENTITY :: Matrix1f32(1)
  84. MATRIX2F32_IDENTITY :: Matrix2f32(1)
  85. MATRIX3F32_IDENTITY :: Matrix3f32(1)
  86. MATRIX4F32_IDENTITY :: Matrix4f32(1)
  87. MATRIX1F64_IDENTITY :: Matrix1f64(1)
  88. MATRIX2F64_IDENTITY :: Matrix2f64(1)
  89. MATRIX3F64_IDENTITY :: Matrix3f64(1)
  90. MATRIX4F64_IDENTITY :: Matrix4f64(1)
  91. QUATERNIONF16_IDENTITY :: Quaternionf16(1)
  92. QUATERNIONF32_IDENTITY :: Quaternionf32(1)
  93. QUATERNIONF64_IDENTITY :: Quaternionf64(1)
  94. VECTOR3F16_X_AXIS :: Vector3f16{1, 0, 0}
  95. VECTOR3F16_Y_AXIS :: Vector3f16{0, 1, 0}
  96. VECTOR3F16_Z_AXIS :: Vector3f16{0, 0, 1}
  97. VECTOR3F32_X_AXIS :: Vector3f32{1, 0, 0}
  98. VECTOR3F32_Y_AXIS :: Vector3f32{0, 1, 0}
  99. VECTOR3F32_Z_AXIS :: Vector3f32{0, 0, 1}
  100. VECTOR3F64_X_AXIS :: Vector3f64{1, 0, 0}
  101. VECTOR3F64_Y_AXIS :: Vector3f64{0, 1, 0}
  102. VECTOR3F64_Z_AXIS :: Vector3f64{0, 0, 1}
  103. @(require_results)
  104. vector2_orthogonal :: proc "contextless" (v: $V/[2]$E) -> V where !IS_ARRAY(E), IS_FLOAT(E) {
  105. return {-v.y, v.x}
  106. }
  107. @(require_results)
  108. vector3_orthogonal :: proc "contextless" (v: $V/[3]$E) -> V where !IS_ARRAY(E), IS_FLOAT(E) {
  109. x := abs(v.x)
  110. y := abs(v.y)
  111. z := abs(v.z)
  112. other: V
  113. if x < y {
  114. if x < z {
  115. other = {1, 0, 0}
  116. } else {
  117. other = {0, 0, 1}
  118. }
  119. } else {
  120. if y < z {
  121. other = {0, 1, 0}
  122. } else {
  123. other = {0, 0, 1}
  124. }
  125. }
  126. return normalize(cross(v, other))
  127. }
  128. orthogonal :: proc{vector2_orthogonal, vector3_orthogonal}
  129. @(require_results)
  130. vector4_srgb_to_linear_f16 :: proc "contextless" (col: Vector4f16) -> Vector4f16 {
  131. r := math.pow(col.x, 2.2)
  132. g := math.pow(col.y, 2.2)
  133. b := math.pow(col.z, 2.2)
  134. a := col.w
  135. return {r, g, b, a}
  136. }
  137. @(require_results)
  138. vector4_srgb_to_linear_f32 :: proc "contextless" (col: Vector4f32) -> Vector4f32 {
  139. r := math.pow(col.x, 2.2)
  140. g := math.pow(col.y, 2.2)
  141. b := math.pow(col.z, 2.2)
  142. a := col.w
  143. return {r, g, b, a}
  144. }
  145. @(require_results)
  146. vector4_srgb_to_linear_f64 :: proc "contextless" (col: Vector4f64) -> Vector4f64 {
  147. r := math.pow(col.x, 2.2)
  148. g := math.pow(col.y, 2.2)
  149. b := math.pow(col.z, 2.2)
  150. a := col.w
  151. return {r, g, b, a}
  152. }
  153. vector4_srgb_to_linear :: proc{
  154. vector4_srgb_to_linear_f16,
  155. vector4_srgb_to_linear_f32,
  156. vector4_srgb_to_linear_f64,
  157. }
  158. @(require_results)
  159. vector4_linear_to_srgb_f16 :: proc "contextless" (col: Vector4f16) -> Vector4f16 {
  160. a :: 2.51
  161. b :: 0.03
  162. c :: 2.43
  163. d :: 0.59
  164. e :: 0.14
  165. x := col.x
  166. y := col.y
  167. z := col.z
  168. x = (x * (a * x + b)) / (x * (c * x + d) + e)
  169. y = (y * (a * y + b)) / (y * (c * y + d) + e)
  170. z = (z * (a * z + b)) / (z * (c * z + d) + e)
  171. x = math.pow(clamp(x, 0, 1), 1.0 / 2.2)
  172. y = math.pow(clamp(y, 0, 1), 1.0 / 2.2)
  173. z = math.pow(clamp(z, 0, 1), 1.0 / 2.2)
  174. return {x, y, z, col.w}
  175. }
  176. @(require_results)
  177. vector4_linear_to_srgb_f32 :: proc "contextless" (col: Vector4f32) -> Vector4f32 {
  178. a :: 2.51
  179. b :: 0.03
  180. c :: 2.43
  181. d :: 0.59
  182. e :: 0.14
  183. x := col.x
  184. y := col.y
  185. z := col.z
  186. x = (x * (a * x + b)) / (x * (c * x + d) + e)
  187. y = (y * (a * y + b)) / (y * (c * y + d) + e)
  188. z = (z * (a * z + b)) / (z * (c * z + d) + e)
  189. x = math.pow(clamp(x, 0, 1), 1.0 / 2.2)
  190. y = math.pow(clamp(y, 0, 1), 1.0 / 2.2)
  191. z = math.pow(clamp(z, 0, 1), 1.0 / 2.2)
  192. return {x, y, z, col.w}
  193. }
  194. @(require_results)
  195. vector4_linear_to_srgb_f64 :: proc "contextless" (col: Vector4f64) -> Vector4f64 {
  196. a :: 2.51
  197. b :: 0.03
  198. c :: 2.43
  199. d :: 0.59
  200. e :: 0.14
  201. x := col.x
  202. y := col.y
  203. z := col.z
  204. x = (x * (a * x + b)) / (x * (c * x + d) + e)
  205. y = (y * (a * y + b)) / (y * (c * y + d) + e)
  206. z = (z * (a * z + b)) / (z * (c * z + d) + e)
  207. x = math.pow(clamp(x, 0, 1), 1.0 / 2.2)
  208. y = math.pow(clamp(y, 0, 1), 1.0 / 2.2)
  209. z = math.pow(clamp(z, 0, 1), 1.0 / 2.2)
  210. return {x, y, z, col.w}
  211. }
  212. vector4_linear_to_srgb :: proc{
  213. vector4_linear_to_srgb_f16,
  214. vector4_linear_to_srgb_f32,
  215. vector4_linear_to_srgb_f64,
  216. }
  217. @(require_results)
  218. vector4_hsl_to_rgb_f16 :: proc "contextless" (h, s, l: f16, a: f16 = 1) -> Vector4f16 {
  219. @(require_results)
  220. hue_to_rgb :: proc "contextless" (p, q, t: f16) -> f16 {
  221. t := t
  222. if t < 0 { t += 1 }
  223. if t > 1 { t -= 1 }
  224. switch {
  225. case t < 1.0/6.0: return p + (q - p) * 6.0 * t
  226. case t < 1.0/2.0: return q
  227. case t < 2.0/3.0: return p + (q - p) * 6.0 * (2.0/3.0 - t)
  228. }
  229. return p
  230. }
  231. r, g, b: f16
  232. if s == 0 {
  233. r = l
  234. g = l
  235. b = l
  236. } else {
  237. q := l * (1+s) if l < 0.5 else l+s - l*s
  238. p := 2*l - q
  239. r = hue_to_rgb(p, q, h + 1.0/3.0)
  240. g = hue_to_rgb(p, q, h)
  241. b = hue_to_rgb(p, q, h - 1.0/3.0)
  242. }
  243. return {r, g, b, a}
  244. }
  245. @(require_results)
  246. vector4_hsl_to_rgb_f32 :: proc "contextless" (h, s, l: f32, a: f32 = 1) -> Vector4f32 {
  247. @(require_results)
  248. hue_to_rgb :: proc "contextless" (p, q, t: f32) -> f32 {
  249. t := t
  250. if t < 0 { t += 1 }
  251. if t > 1 { t -= 1 }
  252. switch {
  253. case t < 1.0/6.0: return p + (q - p) * 6.0 * t
  254. case t < 1.0/2.0: return q
  255. case t < 2.0/3.0: return p + (q - p) * 6.0 * (2.0/3.0 - t)
  256. }
  257. return p
  258. }
  259. r, g, b: f32
  260. if s == 0 {
  261. r = l
  262. g = l
  263. b = l
  264. } else {
  265. q := l * (1+s) if l < 0.5 else l+s - l*s
  266. p := 2*l - q
  267. r = hue_to_rgb(p, q, h + 1.0/3.0)
  268. g = hue_to_rgb(p, q, h)
  269. b = hue_to_rgb(p, q, h - 1.0/3.0)
  270. }
  271. return {r, g, b, a}
  272. }
  273. @(require_results)
  274. vector4_hsl_to_rgb_f64 :: proc "contextless" (h, s, l: f64, a: f64 = 1) -> Vector4f64 {
  275. @(require_results)
  276. hue_to_rgb :: proc "contextless" (p, q, t: f64) -> f64 {
  277. t := t
  278. if t < 0 { t += 1 }
  279. if t > 1 { t -= 1 }
  280. switch {
  281. case t < 1.0/6.0: return p + (q - p) * 6.0 * t
  282. case t < 1.0/2.0: return q
  283. case t < 2.0/3.0: return p + (q - p) * 6.0 * (2.0/3.0 - t)
  284. }
  285. return p
  286. }
  287. r, g, b: f64
  288. if s == 0 {
  289. r = l
  290. g = l
  291. b = l
  292. } else {
  293. q := l * (1+s) if l < 0.5 else l+s - l*s
  294. p := 2*l - q
  295. r = hue_to_rgb(p, q, h + 1.0/3.0)
  296. g = hue_to_rgb(p, q, h)
  297. b = hue_to_rgb(p, q, h - 1.0/3.0)
  298. }
  299. return {r, g, b, a}
  300. }
  301. vector4_hsl_to_rgb :: proc{
  302. vector4_hsl_to_rgb_f16,
  303. vector4_hsl_to_rgb_f32,
  304. vector4_hsl_to_rgb_f64,
  305. }
  306. @(require_results)
  307. vector4_rgb_to_hsl_f16 :: proc "contextless" (col: Vector4f16) -> Vector4f16 {
  308. r := col.x
  309. g := col.y
  310. b := col.z
  311. a := col.w
  312. v_min := min(r, g, b)
  313. v_max := max(r, g, b)
  314. h, s, l: f16
  315. h = 0.0
  316. s = 0.0
  317. l = (v_min + v_max) * 0.5
  318. if v_max != v_min {
  319. d: = v_max - v_min
  320. s = d / (2.0 - v_max - v_min) if l > 0.5 else d / (v_max + v_min)
  321. switch {
  322. case v_max == r:
  323. h = (g - b) / d + (6.0 if g < b else 0.0)
  324. case v_max == g:
  325. h = (b - r) / d + 2.0
  326. case v_max == b:
  327. h = (r - g) / d + 4.0
  328. }
  329. h *= 1.0/6.0
  330. }
  331. return {h, s, l, a}
  332. }
  333. @(require_results)
  334. vector4_rgb_to_hsl_f32 :: proc "contextless" (col: Vector4f32) -> Vector4f32 {
  335. r := col.x
  336. g := col.y
  337. b := col.z
  338. a := col.w
  339. v_min := min(r, g, b)
  340. v_max := max(r, g, b)
  341. h, s, l: f32
  342. h = 0.0
  343. s = 0.0
  344. l = (v_min + v_max) * 0.5
  345. if v_max != v_min {
  346. d: = v_max - v_min
  347. s = d / (2.0 - v_max - v_min) if l > 0.5 else d / (v_max + v_min)
  348. switch {
  349. case v_max == r:
  350. h = (g - b) / d + (6.0 if g < b else 0.0)
  351. case v_max == g:
  352. h = (b - r) / d + 2.0
  353. case v_max == b:
  354. h = (r - g) / d + 4.0
  355. }
  356. h *= 1.0/6.0
  357. }
  358. return {h, s, l, a}
  359. }
  360. @(require_results)
  361. vector4_rgb_to_hsl_f64 :: proc "contextless" (col: Vector4f64) -> Vector4f64 {
  362. r := col.x
  363. g := col.y
  364. b := col.z
  365. a := col.w
  366. v_min := min(r, g, b)
  367. v_max := max(r, g, b)
  368. h, s, l: f64
  369. h = 0.0
  370. s = 0.0
  371. l = (v_min + v_max) * 0.5
  372. if v_max != v_min {
  373. d: = v_max - v_min
  374. s = d / (2.0 - v_max - v_min) if l > 0.5 else d / (v_max + v_min)
  375. switch {
  376. case v_max == r:
  377. h = (g - b) / d + (6.0 if g < b else 0.0)
  378. case v_max == g:
  379. h = (b - r) / d + 2.0
  380. case v_max == b:
  381. h = (r - g) / d + 4.0
  382. }
  383. h *= 1.0/6.0
  384. }
  385. return {h, s, l, a}
  386. }
  387. vector4_rgb_to_hsl :: proc{
  388. vector4_rgb_to_hsl_f16,
  389. vector4_rgb_to_hsl_f32,
  390. vector4_rgb_to_hsl_f64,
  391. }
  392. @(require_results)
  393. quaternion_angle_axis_f16 :: proc "contextless" (angle_radians: f16, axis: Vector3f16) -> (q: Quaternionf16) {
  394. t := angle_radians*0.5
  395. v := normalize(axis) * math.sin(t)
  396. q.x = v.x
  397. q.y = v.y
  398. q.z = v.z
  399. q.w = math.cos(t)
  400. return
  401. }
  402. @(require_results)
  403. quaternion_angle_axis_f32 :: proc "contextless" (angle_radians: f32, axis: Vector3f32) -> (q: Quaternionf32) {
  404. t := angle_radians*0.5
  405. v := normalize(axis) * math.sin(t)
  406. q.x = v.x
  407. q.y = v.y
  408. q.z = v.z
  409. q.w = math.cos(t)
  410. return
  411. }
  412. @(require_results)
  413. quaternion_angle_axis_f64 :: proc "contextless" (angle_radians: f64, axis: Vector3f64) -> (q: Quaternionf64) {
  414. t := angle_radians*0.5
  415. v := normalize(axis) * math.sin(t)
  416. q.x = v.x
  417. q.y = v.y
  418. q.z = v.z
  419. q.w = math.cos(t)
  420. return
  421. }
  422. quaternion_angle_axis :: proc{
  423. quaternion_angle_axis_f16,
  424. quaternion_angle_axis_f32,
  425. quaternion_angle_axis_f64,
  426. }
  427. @(require_results)
  428. angle_from_quaternion_f16 :: proc "contextless" (q: Quaternionf16) -> f16 {
  429. if abs(q.w) > math.SQRT_THREE*0.5 {
  430. return math.asin(math.sqrt(q.x*q.x + q.y*q.y + q.z*q.z)) * 2
  431. }
  432. return math.acos(q.w) * 2
  433. }
  434. @(require_results)
  435. angle_from_quaternion_f32 :: proc "contextless" (q: Quaternionf32) -> f32 {
  436. if abs(q.w) > math.SQRT_THREE*0.5 {
  437. return math.asin(math.sqrt(q.x*q.x + q.y*q.y + q.z*q.z)) * 2
  438. }
  439. return math.acos(q.w) * 2
  440. }
  441. @(require_results)
  442. angle_from_quaternion_f64 :: proc "contextless" (q: Quaternionf64) -> f64 {
  443. if abs(q.w) > math.SQRT_THREE*0.5 {
  444. return math.asin(math.sqrt(q.x*q.x + q.y*q.y + q.z*q.z)) * 2
  445. }
  446. return math.acos(q.w) * 2
  447. }
  448. angle_from_quaternion :: proc{
  449. angle_from_quaternion_f16,
  450. angle_from_quaternion_f32,
  451. angle_from_quaternion_f64,
  452. }
  453. @(require_results)
  454. axis_from_quaternion_f16 :: proc "contextless" (q: Quaternionf16) -> Vector3f16 {
  455. t1 := 1 - q.w*q.w
  456. if t1 < 0 {
  457. return {0, 0, 1}
  458. }
  459. t2 := 1.0 / math.sqrt(t1)
  460. return {q.x*t2, q.y*t2, q.z*t2}
  461. }
  462. @(require_results)
  463. axis_from_quaternion_f32 :: proc "contextless" (q: Quaternionf32) -> Vector3f32 {
  464. t1 := 1 - q.w*q.w
  465. if t1 < 0 {
  466. return {0, 0, 1}
  467. }
  468. t2 := 1.0 / math.sqrt(t1)
  469. return {q.x*t2, q.y*t2, q.z*t2}
  470. }
  471. @(require_results)
  472. axis_from_quaternion_f64 :: proc "contextless" (q: Quaternionf64) -> Vector3f64 {
  473. t1 := 1 - q.w*q.w
  474. if t1 < 0 {
  475. return {0, 0, 1}
  476. }
  477. t2 := 1.0 / math.sqrt(t1)
  478. return {q.x*t2, q.y*t2, q.z*t2}
  479. }
  480. axis_from_quaternion :: proc{
  481. axis_from_quaternion_f16,
  482. axis_from_quaternion_f32,
  483. axis_from_quaternion_f64,
  484. }
  485. @(require_results)
  486. angle_axis_from_quaternion_f16 :: proc "contextless" (q: Quaternionf16) -> (angle: f16, axis: Vector3f16) {
  487. angle = angle_from_quaternion(q)
  488. axis = axis_from_quaternion(q)
  489. return
  490. }
  491. @(require_results)
  492. angle_axis_from_quaternion_f32 :: proc "contextless" (q: Quaternionf32) -> (angle: f32, axis: Vector3f32) {
  493. angle = angle_from_quaternion(q)
  494. axis = axis_from_quaternion(q)
  495. return
  496. }
  497. @(require_results)
  498. angle_axis_from_quaternion_f64 :: proc "contextless" (q: Quaternionf64) -> (angle: f64, axis: Vector3f64) {
  499. angle = angle_from_quaternion(q)
  500. axis = axis_from_quaternion(q)
  501. return
  502. }
  503. angle_axis_from_quaternion :: proc {
  504. angle_axis_from_quaternion_f16,
  505. angle_axis_from_quaternion_f32,
  506. angle_axis_from_quaternion_f64,
  507. }
  508. @(require_results)
  509. quaternion_from_forward_and_up_f16 :: proc "contextless" (forward, up: Vector3f16) -> Quaternionf16 {
  510. f := normalize(forward)
  511. s := normalize(cross(f, up))
  512. u := cross(s, f)
  513. m := Matrix3f16{
  514. +s.x, +s.y, +s.z,
  515. +u.x, +u.y, +u.z,
  516. -f.x, -f.y, -f.z,
  517. }
  518. tr := trace(m)
  519. q: Quaternionf16
  520. switch {
  521. case tr > 0:
  522. S := 2 * math.sqrt(1 + tr)
  523. q.w = 0.25 * S
  524. q.x = (m[1, 2] - m[2, 1]) / S
  525. q.y = (m[2, 0] - m[0, 2]) / S
  526. q.z = (m[0, 1] - m[1, 0]) / S
  527. case (m[0, 0] > m[1, 1]) && (m[0, 0] > m[2, 2]):
  528. S := 2 * math.sqrt(1 + m[0, 0] - m[1, 1] - m[2, 2])
  529. q.w = (m[1, 2] - m[2, 1]) / S
  530. q.x = 0.25 * S
  531. q.y = (m[1, 0] + m[0, 1]) / S
  532. q.z = (m[2, 0] + m[0, 2]) / S
  533. case m[1, 1] > m[2, 2]:
  534. S := 2 * math.sqrt(1 + m[1, 1] - m[0, 0] - m[2, 2])
  535. q.w = (m[2, 0] - m[0, 2]) / S
  536. q.x = (m[1, 0] + m[0, 1]) / S
  537. q.y = 0.25 * S
  538. q.z = (m[2, 1] + m[1, 2]) / S
  539. case:
  540. S := 2 * math.sqrt(1 + m[2, 2] - m[0, 0] - m[1, 1])
  541. q.w = (m[0, 1] - m[1, 0]) / S
  542. q.x = (m[2, 0] - m[0, 2]) / S
  543. q.y = (m[2, 1] + m[1, 2]) / S
  544. q.z = 0.25 * S
  545. }
  546. return normalize(q)
  547. }
  548. @(require_results)
  549. quaternion_from_forward_and_up_f32 :: proc "contextless" (forward, up: Vector3f32) -> Quaternionf32 {
  550. f := normalize(forward)
  551. s := normalize(cross(f, up))
  552. u := cross(s, f)
  553. m := Matrix3f32{
  554. +s.x, +s.y, +s.z,
  555. +u.x, +u.y, +u.z,
  556. -f.x, -f.y, -f.z,
  557. }
  558. tr := trace(m)
  559. q: Quaternionf32
  560. switch {
  561. case tr > 0:
  562. S := 2 * math.sqrt(1 + tr)
  563. q.w = 0.25 * S
  564. q.x = (m[1, 2] - m[2, 1]) / S
  565. q.y = (m[2, 0] - m[0, 2]) / S
  566. q.z = (m[0, 1] - m[1, 0]) / S
  567. case (m[0, 0] > m[1, 1]) && (m[0, 0] > m[2, 2]):
  568. S := 2 * math.sqrt(1 + m[0, 0] - m[1, 1] - m[2, 2])
  569. q.w = (m[1, 2] - m[2, 1]) / S
  570. q.x = 0.25 * S
  571. q.y = (m[1, 0] + m[0, 1]) / S
  572. q.z = (m[2, 0] + m[0, 2]) / S
  573. case m[1, 1] > m[2, 2]:
  574. S := 2 * math.sqrt(1 + m[1, 1] - m[0, 0] - m[2, 2])
  575. q.w = (m[2, 0] - m[0, 2]) / S
  576. q.x = (m[1, 0] + m[0, 1]) / S
  577. q.y = 0.25 * S
  578. q.z = (m[2, 1] + m[1, 2]) / S
  579. case:
  580. S := 2 * math.sqrt(1 + m[2, 2] - m[0, 0] - m[1, 1])
  581. q.w = (m[0, 1] - m[1, 0]) / S
  582. q.x = (m[2, 0] - m[0, 2]) / S
  583. q.y = (m[2, 1] + m[1, 2]) / S
  584. q.z = 0.25 * S
  585. }
  586. return normalize(q)
  587. }
  588. @(require_results)
  589. quaternion_from_forward_and_up_f64 :: proc "contextless" (forward, up: Vector3f64) -> Quaternionf64 {
  590. f := normalize(forward)
  591. s := normalize(cross(f, up))
  592. u := cross(s, f)
  593. m := Matrix3f64{
  594. +s.x, +s.y, +s.z,
  595. +u.x, +u.y, +u.z,
  596. -f.x, -f.y, -f.z,
  597. }
  598. tr := trace(m)
  599. q: Quaternionf64
  600. switch {
  601. case tr > 0:
  602. S := 2 * math.sqrt(1 + tr)
  603. q.w = 0.25 * S
  604. q.x = (m[1, 2] - m[2, 1]) / S
  605. q.y = (m[2, 0] - m[0, 2]) / S
  606. q.z = (m[0, 1] - m[1, 0]) / S
  607. case (m[0, 0] > m[1, 1]) && (m[0, 0] > m[2, 2]):
  608. S := 2 * math.sqrt(1 + m[0, 0] - m[1, 1] - m[2, 2])
  609. q.w = (m[1, 2] - m[2, 1]) / S
  610. q.x = 0.25 * S
  611. q.y = (m[1, 0] + m[0, 1]) / S
  612. q.z = (m[2, 0] + m[0, 2]) / S
  613. case m[1, 1] > m[2, 2]:
  614. S := 2 * math.sqrt(1 + m[1, 1] - m[0, 0] - m[2, 2])
  615. q.w = (m[2, 0] - m[0, 2]) / S
  616. q.x = (m[1, 0] + m[0, 1]) / S
  617. q.y = 0.25 * S
  618. q.z = (m[2, 1] + m[1, 2]) / S
  619. case:
  620. S := 2 * math.sqrt(1 + m[2, 2] - m[0, 0] - m[1, 1])
  621. q.w = (m[0, 1] - m[1, 0]) / S
  622. q.x = (m[2, 0] - m[0, 2]) / S
  623. q.y = (m[2, 1] + m[1, 2]) / S
  624. q.z = 0.25 * S
  625. }
  626. return normalize(q)
  627. }
  628. quaternion_from_forward_and_up :: proc{
  629. quaternion_from_forward_and_up_f16,
  630. quaternion_from_forward_and_up_f32,
  631. quaternion_from_forward_and_up_f64,
  632. }
  633. @(require_results)
  634. quaternion_look_at_f16 :: proc "contextless" (eye, centre: Vector3f16, up: Vector3f16) -> Quaternionf16 {
  635. return quaternion_from_matrix3(matrix3_look_at(eye, centre, up))
  636. }
  637. @(require_results)
  638. quaternion_look_at_f32 :: proc "contextless" (eye, centre: Vector3f32, up: Vector3f32) -> Quaternionf32 {
  639. return quaternion_from_matrix3(matrix3_look_at(eye, centre, up))
  640. }
  641. @(require_results)
  642. quaternion_look_at_f64 :: proc "contextless" (eye, centre: Vector3f64, up: Vector3f64) -> Quaternionf64 {
  643. return quaternion_from_matrix3(matrix3_look_at(eye, centre, up))
  644. }
  645. quaternion_look_at :: proc{
  646. quaternion_look_at_f16,
  647. quaternion_look_at_f32,
  648. quaternion_look_at_f64,
  649. }
  650. @(require_results)
  651. quaternion_nlerp_f16 :: proc "contextless" (a, b: Quaternionf16, t: f16) -> (c: Quaternionf16) {
  652. c.x = a.x + (b.x-a.x)*t
  653. c.y = a.y + (b.y-a.y)*t
  654. c.z = a.z + (b.z-a.z)*t
  655. c.w = a.w + (b.w-a.w)*t
  656. return normalize(c)
  657. }
  658. @(require_results)
  659. quaternion_nlerp_f32 :: proc "contextless" (a, b: Quaternionf32, t: f32) -> (c: Quaternionf32) {
  660. c.x = a.x + (b.x-a.x)*t
  661. c.y = a.y + (b.y-a.y)*t
  662. c.z = a.z + (b.z-a.z)*t
  663. c.w = a.w + (b.w-a.w)*t
  664. return normalize(c)
  665. }
  666. @(require_results)
  667. quaternion_nlerp_f64 :: proc "contextless" (a, b: Quaternionf64, t: f64) -> (c: Quaternionf64) {
  668. c.x = a.x + (b.x-a.x)*t
  669. c.y = a.y + (b.y-a.y)*t
  670. c.z = a.z + (b.z-a.z)*t
  671. c.w = a.w + (b.w-a.w)*t
  672. return normalize(c)
  673. }
  674. quaternion_nlerp :: proc{
  675. quaternion_nlerp_f16,
  676. quaternion_nlerp_f32,
  677. quaternion_nlerp_f64,
  678. }
  679. @(require_results)
  680. quaternion_slerp_f16 :: proc "contextless" (x, y: Quaternionf16, t: f16) -> (q: Quaternionf16) {
  681. a, b := x, y
  682. cos_angle := dot(a, b)
  683. if cos_angle < 0 {
  684. b = -b
  685. cos_angle = -cos_angle
  686. }
  687. if cos_angle > 1 - F32_EPSILON {
  688. q.x = a.x + (b.x-a.x)*t
  689. q.y = a.y + (b.y-a.y)*t
  690. q.z = a.z + (b.z-a.z)*t
  691. q.w = a.w + (b.w-a.w)*t
  692. return
  693. }
  694. angle := math.acos(cos_angle)
  695. sin_angle := math.sin(angle)
  696. factor_a := math.sin((1-t) * angle) / sin_angle
  697. factor_b := math.sin(t * angle) / sin_angle
  698. q.x = factor_a * a.x + factor_b * b.x
  699. q.y = factor_a * a.y + factor_b * b.y
  700. q.z = factor_a * a.z + factor_b * b.z
  701. q.w = factor_a * a.w + factor_b * b.w
  702. return
  703. }
  704. @(require_results)
  705. quaternion_slerp_f32 :: proc "contextless" (x, y: Quaternionf32, t: f32) -> (q: Quaternionf32) {
  706. a, b := x, y
  707. cos_angle := dot(a, b)
  708. if cos_angle < 0 {
  709. b = -b
  710. cos_angle = -cos_angle
  711. }
  712. if cos_angle > 1 - F32_EPSILON {
  713. q.x = a.x + (b.x-a.x)*t
  714. q.y = a.y + (b.y-a.y)*t
  715. q.z = a.z + (b.z-a.z)*t
  716. q.w = a.w + (b.w-a.w)*t
  717. return
  718. }
  719. angle := math.acos(cos_angle)
  720. sin_angle := math.sin(angle)
  721. factor_a := math.sin((1-t) * angle) / sin_angle
  722. factor_b := math.sin(t * angle) / sin_angle
  723. q.x = factor_a * a.x + factor_b * b.x
  724. q.y = factor_a * a.y + factor_b * b.y
  725. q.z = factor_a * a.z + factor_b * b.z
  726. q.w = factor_a * a.w + factor_b * b.w
  727. return
  728. }
  729. @(require_results)
  730. quaternion_slerp_f64 :: proc "contextless" (x, y: Quaternionf64, t: f64) -> (q: Quaternionf64) {
  731. a, b := x, y
  732. cos_angle := dot(a, b)
  733. if cos_angle < 0 {
  734. b = -b
  735. cos_angle = -cos_angle
  736. }
  737. if cos_angle > 1 - F64_EPSILON {
  738. q.x = a.x + (b.x-a.x)*t
  739. q.y = a.y + (b.y-a.y)*t
  740. q.z = a.z + (b.z-a.z)*t
  741. q.w = a.w + (b.w-a.w)*t
  742. return
  743. }
  744. angle := math.acos(cos_angle)
  745. sin_angle := math.sin(angle)
  746. factor_a := math.sin((1-t) * angle) / sin_angle
  747. factor_b := math.sin(t * angle) / sin_angle
  748. q.x = factor_a * a.x + factor_b * b.x
  749. q.y = factor_a * a.y + factor_b * b.y
  750. q.z = factor_a * a.z + factor_b * b.z
  751. q.w = factor_a * a.w + factor_b * b.w
  752. return
  753. }
  754. quaternion_slerp :: proc{
  755. quaternion_slerp_f16,
  756. quaternion_slerp_f32,
  757. quaternion_slerp_f64,
  758. }
  759. @(require_results)
  760. quaternion_squad_f16 :: proc "contextless" (q1, q2, s1, s2: Quaternionf16, h: f16) -> Quaternionf16 {
  761. slerp :: quaternion_slerp
  762. return slerp(slerp(q1, q2, h), slerp(s1, s2, h), 2 * (1 - h) * h)
  763. }
  764. @(require_results)
  765. quaternion_squad_f32 :: proc "contextless" (q1, q2, s1, s2: Quaternionf32, h: f32) -> Quaternionf32 {
  766. slerp :: quaternion_slerp
  767. return slerp(slerp(q1, q2, h), slerp(s1, s2, h), 2 * (1 - h) * h)
  768. }
  769. @(require_results)
  770. quaternion_squad_f64 :: proc "contextless" (q1, q2, s1, s2: Quaternionf64, h: f64) -> Quaternionf64 {
  771. slerp :: quaternion_slerp
  772. return slerp(slerp(q1, q2, h), slerp(s1, s2, h), 2 * (1 - h) * h)
  773. }
  774. quaternion_squad :: proc{
  775. quaternion_squad_f16,
  776. quaternion_squad_f32,
  777. quaternion_squad_f64,
  778. }
  779. @(require_results)
  780. quaternion_from_matrix4_f16 :: proc "contextless" (m: Matrix4f16) -> (q: Quaternionf16) {
  781. m3: Matrix3f16 = ---
  782. m3[0, 0], m3[1, 0], m3[2, 0] = m[0, 0], m[1, 0], m[2, 0]
  783. m3[0, 1], m3[1, 1], m3[2, 1] = m[0, 1], m[1, 1], m[2, 1]
  784. m3[0, 2], m3[1, 2], m3[2, 2] = m[0, 2], m[1, 2], m[2, 2]
  785. return quaternion_from_matrix3(m3)
  786. }
  787. @(require_results)
  788. quaternion_from_matrix4_f32 :: proc "contextless" (m: Matrix4f32) -> (q: Quaternionf32) {
  789. m3: Matrix3f32 = ---
  790. m3[0, 0], m3[1, 0], m3[2, 0] = m[0, 0], m[1, 0], m[2, 0]
  791. m3[0, 1], m3[1, 1], m3[2, 1] = m[0, 1], m[1, 1], m[2, 1]
  792. m3[0, 2], m3[1, 2], m3[2, 2] = m[0, 2], m[1, 2], m[2, 2]
  793. return quaternion_from_matrix3(m3)
  794. }
  795. @(require_results)
  796. quaternion_from_matrix4_f64 :: proc "contextless" (m: Matrix4f64) -> (q: Quaternionf64) {
  797. m3: Matrix3f64 = ---
  798. m3[0, 0], m3[1, 0], m3[2, 0] = m[0, 0], m[1, 0], m[2, 0]
  799. m3[0, 1], m3[1, 1], m3[2, 1] = m[0, 1], m[1, 1], m[2, 1]
  800. m3[0, 2], m3[1, 2], m3[2, 2] = m[0, 2], m[1, 2], m[2, 2]
  801. return quaternion_from_matrix3(m3)
  802. }
  803. quaternion_from_matrix4 :: proc{
  804. quaternion_from_matrix4_f16,
  805. quaternion_from_matrix4_f32,
  806. quaternion_from_matrix4_f64,
  807. }
  808. @(require_results)
  809. quaternion_from_matrix3_f16 :: proc "contextless" (m: Matrix3f16) -> (q: Quaternionf16) {
  810. four_x_squared_minus_1 := m[0, 0] - m[1, 1] - m[2, 2]
  811. four_y_squared_minus_1 := m[1, 1] - m[0, 0] - m[2, 2]
  812. four_z_squared_minus_1 := m[2, 2] - m[0, 0] - m[1, 1]
  813. four_w_squared_minus_1 := m[0, 0] + m[1, 1] + m[2, 2]
  814. biggest_index := 0
  815. four_biggest_squared_minus_1 := four_w_squared_minus_1
  816. if four_x_squared_minus_1 > four_biggest_squared_minus_1 {
  817. four_biggest_squared_minus_1 = four_x_squared_minus_1
  818. biggest_index = 1
  819. }
  820. if four_y_squared_minus_1 > four_biggest_squared_minus_1 {
  821. four_biggest_squared_minus_1 = four_y_squared_minus_1
  822. biggest_index = 2
  823. }
  824. if four_z_squared_minus_1 > four_biggest_squared_minus_1 {
  825. four_biggest_squared_minus_1 = four_z_squared_minus_1
  826. biggest_index = 3
  827. }
  828. biggest_val := math.sqrt(four_biggest_squared_minus_1 + 1) * 0.5
  829. mult := 0.25 / biggest_val
  830. q = 1
  831. switch biggest_index {
  832. case 0:
  833. q.w = biggest_val
  834. q.x = (m[2, 1] - m[1, 2]) * mult
  835. q.y = (m[0, 2] - m[2, 0]) * mult
  836. q.z = (m[1, 0] - m[0, 1]) * mult
  837. case 1:
  838. q.w = (m[2, 1] - m[1, 2]) * mult
  839. q.x = biggest_val
  840. q.y = (m[1, 0] + m[0, 1]) * mult
  841. q.z = (m[0, 2] + m[2, 0]) * mult
  842. case 2:
  843. q.w = (m[0, 2] - m[2, 0]) * mult
  844. q.x = (m[1, 0] + m[0, 1]) * mult
  845. q.y = biggest_val
  846. q.z = (m[2, 1] + m[1, 2]) * mult
  847. case 3:
  848. q.w = (m[1, 0] - m[0, 1]) * mult
  849. q.x = (m[0, 2] + m[2, 0]) * mult
  850. q.y = (m[2, 1] + m[1, 2]) * mult
  851. q.z = biggest_val
  852. }
  853. return
  854. }
  855. @(require_results)
  856. quaternion_from_matrix3_f32 :: proc "contextless" (m: Matrix3f32) -> (q: Quaternionf32) {
  857. four_x_squared_minus_1 := m[0, 0] - m[1, 1] - m[2, 2]
  858. four_y_squared_minus_1 := m[1, 1] - m[0, 0] - m[2, 2]
  859. four_z_squared_minus_1 := m[2, 2] - m[0, 0] - m[1, 1]
  860. four_w_squared_minus_1 := m[0, 0] + m[1, 1] + m[2, 2]
  861. biggest_index := 0
  862. four_biggest_squared_minus_1 := four_w_squared_minus_1
  863. if four_x_squared_minus_1 > four_biggest_squared_minus_1 {
  864. four_biggest_squared_minus_1 = four_x_squared_minus_1
  865. biggest_index = 1
  866. }
  867. if four_y_squared_minus_1 > four_biggest_squared_minus_1 {
  868. four_biggest_squared_minus_1 = four_y_squared_minus_1
  869. biggest_index = 2
  870. }
  871. if four_z_squared_minus_1 > four_biggest_squared_minus_1 {
  872. four_biggest_squared_minus_1 = four_z_squared_minus_1
  873. biggest_index = 3
  874. }
  875. biggest_val := math.sqrt(four_biggest_squared_minus_1 + 1) * 0.5
  876. mult := 0.25 / biggest_val
  877. q = 1
  878. switch biggest_index {
  879. case 0:
  880. q.w = biggest_val
  881. q.x = (m[2, 1] - m[1, 2]) * mult
  882. q.y = (m[0, 2] - m[2, 0]) * mult
  883. q.z = (m[1, 0] - m[0, 1]) * mult
  884. case 1:
  885. q.w = (m[2, 1] - m[1, 2]) * mult
  886. q.x = biggest_val
  887. q.y = (m[1, 0] + m[0, 1]) * mult
  888. q.z = (m[0, 2] + m[2, 0]) * mult
  889. case 2:
  890. q.w = (m[0, 2] - m[2, 0]) * mult
  891. q.x = (m[1, 0] + m[0, 1]) * mult
  892. q.y = biggest_val
  893. q.z = (m[2, 1] + m[1, 2]) * mult
  894. case 3:
  895. q.w = (m[1, 0] - m[0, 1]) * mult
  896. q.x = (m[0, 2] + m[2, 0]) * mult
  897. q.y = (m[2, 1] + m[1, 2]) * mult
  898. q.z = biggest_val
  899. }
  900. return
  901. }
  902. @(require_results)
  903. quaternion_from_matrix3_f64 :: proc "contextless" (m: Matrix3f64) -> (q: Quaternionf64) {
  904. four_x_squared_minus_1 := m[0, 0] - m[1, 1] - m[2, 2]
  905. four_y_squared_minus_1 := m[1, 1] - m[0, 0] - m[2, 2]
  906. four_z_squared_minus_1 := m[2, 2] - m[0, 0] - m[1, 1]
  907. four_w_squared_minus_1 := m[0, 0] + m[1, 1] + m[2, 2]
  908. biggest_index := 0
  909. four_biggest_squared_minus_1 := four_w_squared_minus_1
  910. if four_x_squared_minus_1 > four_biggest_squared_minus_1 {
  911. four_biggest_squared_minus_1 = four_x_squared_minus_1
  912. biggest_index = 1
  913. }
  914. if four_y_squared_minus_1 > four_biggest_squared_minus_1 {
  915. four_biggest_squared_minus_1 = four_y_squared_minus_1
  916. biggest_index = 2
  917. }
  918. if four_z_squared_minus_1 > four_biggest_squared_minus_1 {
  919. four_biggest_squared_minus_1 = four_z_squared_minus_1
  920. biggest_index = 3
  921. }
  922. biggest_val := math.sqrt(four_biggest_squared_minus_1 + 1) * 0.5
  923. mult := 0.25 / biggest_val
  924. q = 1
  925. switch biggest_index {
  926. case 0:
  927. q.w = biggest_val
  928. q.x = (m[2, 1] - m[1, 2]) * mult
  929. q.y = (m[0, 2] - m[2, 0]) * mult
  930. q.z = (m[1, 0] - m[0, 1]) * mult
  931. case 1:
  932. q.w = (m[2, 1] - m[1, 2]) * mult
  933. q.x = biggest_val
  934. q.y = (m[1, 0] + m[0, 1]) * mult
  935. q.z = (m[0, 2] + m[2, 0]) * mult
  936. case 2:
  937. q.w = (m[0, 2] - m[2, 0]) * mult
  938. q.x = (m[1, 0] + m[0, 1]) * mult
  939. q.y = biggest_val
  940. q.z = (m[2, 1] + m[1, 2]) * mult
  941. case 3:
  942. q.w = (m[1, 0] - m[0, 1]) * mult
  943. q.x = (m[0, 2] + m[2, 0]) * mult
  944. q.y = (m[2, 1] + m[1, 2]) * mult
  945. q.z = biggest_val
  946. }
  947. return
  948. }
  949. quaternion_from_matrix3 :: proc{
  950. quaternion_from_matrix3_f16,
  951. quaternion_from_matrix3_f32,
  952. quaternion_from_matrix3_f64,
  953. }
  954. @(require_results)
  955. quaternion_between_two_vector3_f16 :: proc "contextless" (from, to: Vector3f16) -> (q: Quaternionf16) {
  956. x := normalize(from)
  957. y := normalize(to)
  958. cos_theta := dot(x, y)
  959. if abs(cos_theta + 1) < 2*F32_EPSILON {
  960. v := vector3_orthogonal(x)
  961. q.x = v.x
  962. q.y = v.y
  963. q.z = v.z
  964. q.w = 0
  965. return
  966. }
  967. v := cross(x, y)
  968. w := cos_theta + 1
  969. q.w = w
  970. q.x = v.x
  971. q.y = v.y
  972. q.z = v.z
  973. return normalize(q)
  974. }
  975. @(require_results)
  976. quaternion_between_two_vector3_f32 :: proc "contextless" (from, to: Vector3f32) -> (q: Quaternionf32) {
  977. x := normalize(from)
  978. y := normalize(to)
  979. cos_theta := dot(x, y)
  980. if abs(cos_theta + 1) < 2*F32_EPSILON {
  981. v := vector3_orthogonal(x)
  982. q.x = v.x
  983. q.y = v.y
  984. q.z = v.z
  985. q.w = 0
  986. return
  987. }
  988. v := cross(x, y)
  989. w := cos_theta + 1
  990. q.w = w
  991. q.x = v.x
  992. q.y = v.y
  993. q.z = v.z
  994. return normalize(q)
  995. }
  996. @(require_results)
  997. quaternion_between_two_vector3_f64 :: proc "contextless" (from, to: Vector3f64) -> (q: Quaternionf64) {
  998. x := normalize(from)
  999. y := normalize(to)
  1000. cos_theta := dot(x, y)
  1001. if abs(cos_theta + 1) < 2*F64_EPSILON {
  1002. v := vector3_orthogonal(x)
  1003. q.x = v.x
  1004. q.y = v.y
  1005. q.z = v.z
  1006. q.w = 0
  1007. return
  1008. }
  1009. v := cross(x, y)
  1010. w := cos_theta + 1
  1011. q.w = w
  1012. q.x = v.x
  1013. q.y = v.y
  1014. q.z = v.z
  1015. return normalize(q)
  1016. }
  1017. quaternion_between_two_vector3 :: proc{
  1018. quaternion_between_two_vector3_f16,
  1019. quaternion_between_two_vector3_f32,
  1020. quaternion_between_two_vector3_f64,
  1021. }
  1022. @(require_results)
  1023. matrix2_inverse_transpose_f16 :: proc "contextless" (m: Matrix2f16) -> (c: Matrix2f16) {
  1024. d := m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1025. id := 1.0/d
  1026. c[0, 0] = +m[1, 1] * id
  1027. c[1, 0] = -m[1, 0] * id
  1028. c[0, 1] = -m[0, 1] * id
  1029. c[1, 1] = +m[0, 0] * id
  1030. return c
  1031. }
  1032. @(require_results)
  1033. matrix2_inverse_transpose_f32 :: proc "contextless" (m: Matrix2f32) -> (c: Matrix2f32) {
  1034. d := m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1035. id := 1.0/d
  1036. c[0, 0] = +m[1, 1] * id
  1037. c[1, 0] = -m[1, 0] * id
  1038. c[0, 1] = -m[0, 1] * id
  1039. c[1, 1] = +m[0, 0] * id
  1040. return c
  1041. }
  1042. @(require_results)
  1043. matrix2_inverse_transpose_f64 :: proc "contextless" (m: Matrix2f64) -> (c: Matrix2f64) {
  1044. d := m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1045. id := 1.0/d
  1046. c[0, 0] = +m[1, 1] * id
  1047. c[1, 0] = -m[1, 0] * id
  1048. c[0, 1] = -m[0, 1] * id
  1049. c[1, 1] = +m[0, 0] * id
  1050. return c
  1051. }
  1052. matrix2_inverse_transpose :: proc{
  1053. matrix2_inverse_transpose_f16,
  1054. matrix2_inverse_transpose_f32,
  1055. matrix2_inverse_transpose_f64,
  1056. }
  1057. @(require_results)
  1058. matrix2_determinant_f16 :: proc "contextless" (m: Matrix2f16) -> f16 {
  1059. return m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1060. }
  1061. @(require_results)
  1062. matrix2_determinant_f32 :: proc "contextless" (m: Matrix2f32) -> f32 {
  1063. return m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1064. }
  1065. @(require_results)
  1066. matrix2_determinant_f64 :: proc "contextless" (m: Matrix2f64) -> f64 {
  1067. return m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1068. }
  1069. matrix2_determinant :: proc{
  1070. matrix2_determinant_f16,
  1071. matrix2_determinant_f32,
  1072. matrix2_determinant_f64,
  1073. }
  1074. @(require_results)
  1075. matrix2_inverse_f16 :: proc "contextless" (m: Matrix2f16) -> (c: Matrix2f16) {
  1076. d := m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1077. id := 1.0/d
  1078. c[0, 0] = +m[1, 1] * id
  1079. c[0, 1] = -m[1, 0] * id
  1080. c[1, 0] = -m[0, 1] * id
  1081. c[1, 1] = +m[0, 0] * id
  1082. return c
  1083. }
  1084. @(require_results)
  1085. matrix2_inverse_f32 :: proc "contextless" (m: Matrix2f32) -> (c: Matrix2f32) {
  1086. d := m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1087. id := 1.0/d
  1088. c[0, 0] = +m[1, 1] * id
  1089. c[0, 1] = -m[1, 0] * id
  1090. c[1, 0] = -m[0, 1] * id
  1091. c[1, 1] = +m[0, 0] * id
  1092. return c
  1093. }
  1094. @(require_results)
  1095. matrix2_inverse_f64 :: proc "contextless" (m: Matrix2f64) -> (c: Matrix2f64) {
  1096. d := m[0, 0]*m[1, 1] - m[0, 1]*m[1, 0]
  1097. id := 1.0/d
  1098. c[0, 0] = +m[1, 1] * id
  1099. c[0, 1] = -m[1, 0] * id
  1100. c[1, 0] = -m[0, 1] * id
  1101. c[1, 1] = +m[0, 0] * id
  1102. return c
  1103. }
  1104. matrix2_inverse :: proc{
  1105. matrix2_inverse_f16,
  1106. matrix2_inverse_f32,
  1107. matrix2_inverse_f64,
  1108. }
  1109. @(require_results)
  1110. matrix2_adjoint_f16 :: proc "contextless" (m: Matrix2f16) -> (c: Matrix2f16) {
  1111. c[0, 0] = +m[1, 1]
  1112. c[1, 0] = -m[0, 1]
  1113. c[0, 1] = -m[1, 0]
  1114. c[1, 1] = +m[0, 0]
  1115. return c
  1116. }
  1117. @(require_results)
  1118. matrix2_adjoint_f32 :: proc "contextless" (m: Matrix2f32) -> (c: Matrix2f32) {
  1119. c[0, 0] = +m[1, 1]
  1120. c[1, 0] = -m[0, 1]
  1121. c[0, 1] = -m[1, 0]
  1122. c[1, 1] = +m[0, 0]
  1123. return c
  1124. }
  1125. @(require_results)
  1126. matrix2_adjoint_f64 :: proc "contextless" (m: Matrix2f64) -> (c: Matrix2f64) {
  1127. c[0, 0] = +m[1, 1]
  1128. c[1, 0] = -m[0, 1]
  1129. c[0, 1] = -m[1, 0]
  1130. c[1, 1] = +m[0, 0]
  1131. return c
  1132. }
  1133. matrix2_adjoint :: proc{
  1134. matrix2_adjoint_f16,
  1135. matrix2_adjoint_f32,
  1136. matrix2_adjoint_f64,
  1137. }
  1138. @(require_results)
  1139. matrix3_from_quaternion_f16 :: proc "contextless" (q: Quaternionf16) -> (m: Matrix3f16) {
  1140. qxx := q.x * q.x
  1141. qyy := q.y * q.y
  1142. qzz := q.z * q.z
  1143. qxz := q.x * q.z
  1144. qxy := q.x * q.y
  1145. qyz := q.y * q.z
  1146. qwx := q.w * q.x
  1147. qwy := q.w * q.y
  1148. qwz := q.w * q.z
  1149. m[0, 0] = 1 - 2 * (qyy + qzz)
  1150. m[1, 0] = 2 * (qxy + qwz)
  1151. m[2, 0] = 2 * (qxz - qwy)
  1152. m[0, 1] = 2 * (qxy - qwz)
  1153. m[1, 1] = 1 - 2 * (qxx + qzz)
  1154. m[2, 1] = 2 * (qyz + qwx)
  1155. m[0, 2] = 2 * (qxz + qwy)
  1156. m[1, 2] = 2 * (qyz - qwx)
  1157. m[2, 2] = 1 - 2 * (qxx + qyy)
  1158. return m
  1159. }
  1160. @(require_results)
  1161. matrix3_from_quaternion_f32 :: proc "contextless" (q: Quaternionf32) -> (m: Matrix3f32) {
  1162. qxx := q.x * q.x
  1163. qyy := q.y * q.y
  1164. qzz := q.z * q.z
  1165. qxz := q.x * q.z
  1166. qxy := q.x * q.y
  1167. qyz := q.y * q.z
  1168. qwx := q.w * q.x
  1169. qwy := q.w * q.y
  1170. qwz := q.w * q.z
  1171. m[0, 0] = 1 - 2 * (qyy + qzz)
  1172. m[1, 0] = 2 * (qxy + qwz)
  1173. m[2, 0] = 2 * (qxz - qwy)
  1174. m[0, 1] = 2 * (qxy - qwz)
  1175. m[1, 1] = 1 - 2 * (qxx + qzz)
  1176. m[2, 1] = 2 * (qyz + qwx)
  1177. m[0, 2] = 2 * (qxz + qwy)
  1178. m[1, 2] = 2 * (qyz - qwx)
  1179. m[2, 2] = 1 - 2 * (qxx + qyy)
  1180. return m
  1181. }
  1182. @(require_results)
  1183. matrix3_from_quaternion_f64 :: proc "contextless" (q: Quaternionf64) -> (m: Matrix3f64) {
  1184. qxx := q.x * q.x
  1185. qyy := q.y * q.y
  1186. qzz := q.z * q.z
  1187. qxz := q.x * q.z
  1188. qxy := q.x * q.y
  1189. qyz := q.y * q.z
  1190. qwx := q.w * q.x
  1191. qwy := q.w * q.y
  1192. qwz := q.w * q.z
  1193. m[0, 0] = 1 - 2 * (qyy + qzz)
  1194. m[1, 0] = 2 * (qxy + qwz)
  1195. m[2, 0] = 2 * (qxz - qwy)
  1196. m[0, 1] = 2 * (qxy - qwz)
  1197. m[1, 1] = 1 - 2 * (qxx + qzz)
  1198. m[2, 1] = 2 * (qyz + qwx)
  1199. m[0, 2] = 2 * (qxz + qwy)
  1200. m[1, 2] = 2 * (qyz - qwx)
  1201. m[2, 2] = 1 - 2 * (qxx + qyy)
  1202. return m
  1203. }
  1204. matrix3_from_quaternion :: proc{
  1205. matrix3_from_quaternion_f16,
  1206. matrix3_from_quaternion_f32,
  1207. matrix3_from_quaternion_f64,
  1208. }
  1209. @(require_results)
  1210. matrix3_inverse_f16 :: proc "contextless" (m: Matrix3f16) -> Matrix3f16 {
  1211. return transpose(matrix3_inverse_transpose(m))
  1212. }
  1213. @(require_results)
  1214. matrix3_inverse_f32 :: proc "contextless" (m: Matrix3f32) -> Matrix3f32 {
  1215. return transpose(matrix3_inverse_transpose(m))
  1216. }
  1217. @(require_results)
  1218. matrix3_inverse_f64 :: proc "contextless" (m: Matrix3f64) -> Matrix3f64 {
  1219. return transpose(matrix3_inverse_transpose(m))
  1220. }
  1221. matrix3_inverse :: proc{
  1222. matrix3_inverse_f16,
  1223. matrix3_inverse_f32,
  1224. matrix3_inverse_f64,
  1225. }
  1226. @(require_results)
  1227. matrix3_determinant_f16 :: proc "contextless" (m: Matrix3f16) -> f16 {
  1228. a := +m[0, 0] * (m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1])
  1229. b := -m[0, 1] * (m[1, 0] * m[2, 2] - m[1, 2] * m[2, 0])
  1230. c := +m[0, 2] * (m[1, 0] * m[2, 1] - m[1, 1] * m[2, 0])
  1231. return a + b + c
  1232. }
  1233. @(require_results)
  1234. matrix3_determinant_f32 :: proc "contextless" (m: Matrix3f32) -> f32 {
  1235. a := +m[0, 0] * (m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1])
  1236. b := -m[0, 1] * (m[1, 0] * m[2, 2] - m[1, 2] * m[2, 0])
  1237. c := +m[0, 2] * (m[1, 0] * m[2, 1] - m[1, 1] * m[2, 0])
  1238. return a + b + c
  1239. }
  1240. @(require_results)
  1241. matrix3_determinant_f64 :: proc "contextless" (m: Matrix3f64) -> f64 {
  1242. a := +m[0, 0] * (m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1])
  1243. b := -m[0, 1] * (m[1, 0] * m[2, 2] - m[1, 2] * m[2, 0])
  1244. c := +m[0, 2] * (m[1, 0] * m[2, 1] - m[1, 1] * m[2, 0])
  1245. return a + b + c
  1246. }
  1247. matrix3_determinant :: proc{
  1248. matrix3_determinant_f16,
  1249. matrix3_determinant_f32,
  1250. matrix3_determinant_f64,
  1251. }
  1252. @(require_results)
  1253. matrix3_adjoint_f16 :: proc "contextless" (m: Matrix3f16) -> (adjoint: Matrix3f16) {
  1254. adjoint[0, 0] = +(m[1, 1] * m[2, 2] - m[2, 1] * m[1, 2])
  1255. adjoint[0, 1] = -(m[1, 0] * m[2, 2] - m[2, 0] * m[1, 2])
  1256. adjoint[0, 2] = +(m[1, 0] * m[2, 1] - m[2, 0] * m[1, 1])
  1257. adjoint[1, 0] = -(m[0, 1] * m[2, 2] - m[2, 1] * m[0, 2])
  1258. adjoint[1, 1] = +(m[0, 0] * m[2, 2] - m[2, 0] * m[0, 2])
  1259. adjoint[1, 2] = -(m[0, 0] * m[2, 1] - m[2, 0] * m[0, 1])
  1260. adjoint[2, 0] = +(m[0, 1] * m[1, 2] - m[1, 1] * m[0, 2])
  1261. adjoint[2, 1] = -(m[0, 0] * m[1, 2] - m[1, 0] * m[0, 2])
  1262. adjoint[2, 2] = +(m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1])
  1263. return adjoint
  1264. }
  1265. @(require_results)
  1266. matrix3_adjoint_f32 :: proc "contextless" (m: Matrix3f32) -> (adjoint: Matrix3f32) {
  1267. adjoint[0, 0] = +(m[1, 1] * m[2, 2] - m[2, 1] * m[1, 2])
  1268. adjoint[0, 1] = -(m[1, 0] * m[2, 2] - m[2, 0] * m[1, 2])
  1269. adjoint[0, 2] = +(m[1, 0] * m[2, 1] - m[2, 0] * m[1, 1])
  1270. adjoint[1, 0] = -(m[0, 1] * m[2, 2] - m[2, 1] * m[0, 2])
  1271. adjoint[1, 1] = +(m[0, 0] * m[2, 2] - m[2, 0] * m[0, 2])
  1272. adjoint[1, 2] = -(m[0, 0] * m[2, 1] - m[2, 0] * m[0, 1])
  1273. adjoint[2, 0] = +(m[0, 1] * m[1, 2] - m[1, 1] * m[0, 2])
  1274. adjoint[2, 1] = -(m[0, 0] * m[1, 2] - m[1, 0] * m[0, 2])
  1275. adjoint[2, 2] = +(m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1])
  1276. return adjoint
  1277. }
  1278. @(require_results)
  1279. matrix3_adjoint_f64 :: proc "contextless" (m: Matrix3f64) -> (adjoint: Matrix3f64) {
  1280. adjoint[0, 0] = +(m[1, 1] * m[2, 2] - m[2, 1] * m[1, 2])
  1281. adjoint[0, 1] = -(m[1, 0] * m[2, 2] - m[2, 0] * m[1, 2])
  1282. adjoint[0, 2] = +(m[1, 0] * m[2, 1] - m[2, 0] * m[1, 1])
  1283. adjoint[1, 0] = -(m[0, 1] * m[2, 2] - m[2, 1] * m[0, 2])
  1284. adjoint[1, 1] = +(m[0, 0] * m[2, 2] - m[2, 0] * m[0, 2])
  1285. adjoint[1, 2] = -(m[0, 0] * m[2, 1] - m[2, 0] * m[0, 1])
  1286. adjoint[2, 0] = +(m[0, 1] * m[1, 2] - m[1, 1] * m[0, 2])
  1287. adjoint[2, 1] = -(m[0, 0] * m[1, 2] - m[1, 0] * m[0, 2])
  1288. adjoint[2, 2] = +(m[0, 0] * m[1, 1] - m[1, 0] * m[0, 1])
  1289. return adjoint
  1290. }
  1291. matrix3_adjoint :: proc{
  1292. matrix3_adjoint_f16,
  1293. matrix3_adjoint_f32,
  1294. matrix3_adjoint_f64,
  1295. }
  1296. @(require_results)
  1297. matrix3_inverse_transpose_f16 :: proc "contextless" (m: Matrix3f16) -> (inverse_transpose: Matrix3f16) {
  1298. return builtin.inverse_transpose(m)
  1299. }
  1300. @(require_results)
  1301. matrix3_inverse_transpose_f32 :: proc "contextless" (m: Matrix3f32) -> (inverse_transpose: Matrix3f32) {
  1302. return builtin.inverse_transpose(m)
  1303. }
  1304. @(require_results)
  1305. matrix3_inverse_transpose_f64 :: proc "contextless" (m: Matrix3f64) -> (inverse_transpose: Matrix3f64) {
  1306. return builtin.inverse_transpose(m)
  1307. }
  1308. matrix3_inverse_transpose :: proc{
  1309. matrix3_inverse_transpose_f16,
  1310. matrix3_inverse_transpose_f32,
  1311. matrix3_inverse_transpose_f64,
  1312. }
  1313. @(require_results)
  1314. matrix3_scale_f16 :: proc "contextless" (s: Vector3f16) -> (m: Matrix3f16) {
  1315. m[0, 0] = s[0]
  1316. m[1, 1] = s[1]
  1317. m[2, 2] = s[2]
  1318. return m
  1319. }
  1320. @(require_results)
  1321. matrix3_scale_f32 :: proc "contextless" (s: Vector3f32) -> (m: Matrix3f32) {
  1322. m[0, 0] = s[0]
  1323. m[1, 1] = s[1]
  1324. m[2, 2] = s[2]
  1325. return m
  1326. }
  1327. @(require_results)
  1328. matrix3_scale_f64 :: proc "contextless" (s: Vector3f64) -> (m: Matrix3f64) {
  1329. m[0, 0] = s[0]
  1330. m[1, 1] = s[1]
  1331. m[2, 2] = s[2]
  1332. return m
  1333. }
  1334. matrix3_scale :: proc{
  1335. matrix3_scale_f16,
  1336. matrix3_scale_f32,
  1337. matrix3_scale_f64,
  1338. }
  1339. @(require_results)
  1340. matrix3_rotate_f16 :: proc "contextless" (angle_radians: f16, v: Vector3f16) -> (rot: Matrix3f16) {
  1341. c := math.cos(angle_radians)
  1342. s := math.sin(angle_radians)
  1343. a := normalize(v)
  1344. t := a * (1-c)
  1345. rot[0, 0] = c + t[0]*a[0]
  1346. rot[1, 0] = 0 + t[0]*a[1] + s*a[2]
  1347. rot[2, 0] = 0 + t[0]*a[2] - s*a[1]
  1348. rot[0, 1] = 0 + t[1]*a[0] - s*a[2]
  1349. rot[1, 1] = c + t[1]*a[1]
  1350. rot[2, 1] = 0 + t[1]*a[2] + s*a[0]
  1351. rot[0, 2] = 0 + t[2]*a[0] + s*a[1]
  1352. rot[1, 2] = 0 + t[2]*a[1] - s*a[0]
  1353. rot[2, 2] = c + t[2]*a[2]
  1354. return rot
  1355. }
  1356. @(require_results)
  1357. matrix3_rotate_f32 :: proc "contextless" (angle_radians: f32, v: Vector3f32) -> (rot: Matrix3f32) {
  1358. c := math.cos(angle_radians)
  1359. s := math.sin(angle_radians)
  1360. a := normalize(v)
  1361. t := a * (1-c)
  1362. rot[0, 0] = c + t[0]*a[0]
  1363. rot[1, 0] = 0 + t[0]*a[1] + s*a[2]
  1364. rot[2, 0] = 0 + t[0]*a[2] - s*a[1]
  1365. rot[0, 1] = 0 + t[1]*a[0] - s*a[2]
  1366. rot[1, 1] = c + t[1]*a[1]
  1367. rot[2, 1] = 0 + t[1]*a[2] + s*a[0]
  1368. rot[0, 2] = 0 + t[2]*a[0] + s*a[1]
  1369. rot[1, 2] = 0 + t[2]*a[1] - s*a[0]
  1370. rot[2, 2] = c + t[2]*a[2]
  1371. return rot
  1372. }
  1373. @(require_results)
  1374. matrix3_rotate_f64 :: proc "contextless" (angle_radians: f64, v: Vector3f64) -> (rot: Matrix3f64) {
  1375. c := math.cos(angle_radians)
  1376. s := math.sin(angle_radians)
  1377. a := normalize(v)
  1378. t := a * (1-c)
  1379. rot[0, 0] = c + t[0]*a[0]
  1380. rot[1, 0] = 0 + t[0]*a[1] + s*a[2]
  1381. rot[2, 0] = 0 + t[0]*a[2] - s*a[1]
  1382. rot[0, 1] = 0 + t[1]*a[0] - s*a[2]
  1383. rot[1, 1] = c + t[1]*a[1]
  1384. rot[2, 1] = 0 + t[1]*a[2] + s*a[0]
  1385. rot[0, 2] = 0 + t[2]*a[0] + s*a[1]
  1386. rot[1, 2] = 0 + t[2]*a[1] - s*a[0]
  1387. rot[2, 2] = c + t[2]*a[2]
  1388. return rot
  1389. }
  1390. matrix3_rotate :: proc{
  1391. matrix3_rotate_f16,
  1392. matrix3_rotate_f32,
  1393. matrix3_rotate_f64,
  1394. }
  1395. @(require_results)
  1396. matrix3_look_at_f16 :: proc "contextless" (eye, centre, up: Vector3f16) -> Matrix3f16 {
  1397. f := normalize(centre - eye)
  1398. s := normalize(cross(f, up))
  1399. u := cross(s, f)
  1400. return Matrix3f16{
  1401. +s.x, +s.y, +s.z,
  1402. +u.x, +u.y, +u.z,
  1403. -f.x, -f.y, -f.z,
  1404. }
  1405. }
  1406. @(require_results)
  1407. matrix3_look_at_f32 :: proc "contextless" (eye, centre, up: Vector3f32) -> Matrix3f32 {
  1408. f := normalize(centre - eye)
  1409. s := normalize(cross(f, up))
  1410. u := cross(s, f)
  1411. return Matrix3f32{
  1412. +s.x, +s.y, +s.z,
  1413. +u.x, +u.y, +u.z,
  1414. -f.x, -f.y, -f.z,
  1415. }
  1416. }
  1417. @(require_results)
  1418. matrix3_look_at_f64 :: proc "contextless" (eye, centre, up: Vector3f64) -> Matrix3f64 {
  1419. f := normalize(centre - eye)
  1420. s := normalize(cross(f, up))
  1421. u := cross(s, f)
  1422. return Matrix3f64{
  1423. +s.x, +s.y, +s.z,
  1424. +u.x, +u.y, +u.z,
  1425. -f.x, -f.y, -f.z,
  1426. }
  1427. }
  1428. matrix3_look_at :: proc{
  1429. matrix3_look_at_f16,
  1430. matrix3_look_at_f32,
  1431. matrix3_look_at_f64,
  1432. }
  1433. @(require_results)
  1434. matrix4_from_quaternion_f16 :: proc "contextless" (q: Quaternionf16) -> (m: Matrix4f16) {
  1435. qxx := q.x * q.x
  1436. qyy := q.y * q.y
  1437. qzz := q.z * q.z
  1438. qxz := q.x * q.z
  1439. qxy := q.x * q.y
  1440. qyz := q.y * q.z
  1441. qwx := q.w * q.x
  1442. qwy := q.w * q.y
  1443. qwz := q.w * q.z
  1444. m[0, 0] = 1 - 2 * (qyy + qzz)
  1445. m[1, 0] = 2 * (qxy + qwz)
  1446. m[2, 0] = 2 * (qxz - qwy)
  1447. m[0, 1] = 2 * (qxy - qwz)
  1448. m[1, 1] = 1 - 2 * (qxx + qzz)
  1449. m[2, 1] = 2 * (qyz + qwx)
  1450. m[0, 2] = 2 * (qxz + qwy)
  1451. m[1, 2] = 2 * (qyz - qwx)
  1452. m[2, 2] = 1 - 2 * (qxx + qyy)
  1453. m[3, 3] = 1
  1454. return m
  1455. }
  1456. @(require_results)
  1457. matrix4_from_quaternion_f32 :: proc "contextless" (q: Quaternionf32) -> (m: Matrix4f32) {
  1458. qxx := q.x * q.x
  1459. qyy := q.y * q.y
  1460. qzz := q.z * q.z
  1461. qxz := q.x * q.z
  1462. qxy := q.x * q.y
  1463. qyz := q.y * q.z
  1464. qwx := q.w * q.x
  1465. qwy := q.w * q.y
  1466. qwz := q.w * q.z
  1467. m[0, 0] = 1 - 2 * (qyy + qzz)
  1468. m[1, 0] = 2 * (qxy + qwz)
  1469. m[2, 0] = 2 * (qxz - qwy)
  1470. m[0, 1] = 2 * (qxy - qwz)
  1471. m[1, 1] = 1 - 2 * (qxx + qzz)
  1472. m[2, 1] = 2 * (qyz + qwx)
  1473. m[0, 2] = 2 * (qxz + qwy)
  1474. m[1, 2] = 2 * (qyz - qwx)
  1475. m[2, 2] = 1 - 2 * (qxx + qyy)
  1476. m[3, 3] = 1
  1477. return m
  1478. }
  1479. @(require_results)
  1480. matrix4_from_quaternion_f64 :: proc "contextless" (q: Quaternionf64) -> (m: Matrix4f64) {
  1481. qxx := q.x * q.x
  1482. qyy := q.y * q.y
  1483. qzz := q.z * q.z
  1484. qxz := q.x * q.z
  1485. qxy := q.x * q.y
  1486. qyz := q.y * q.z
  1487. qwx := q.w * q.x
  1488. qwy := q.w * q.y
  1489. qwz := q.w * q.z
  1490. m[0, 0] = 1 - 2 * (qyy + qzz)
  1491. m[1, 0] = 2 * (qxy + qwz)
  1492. m[2, 0] = 2 * (qxz - qwy)
  1493. m[0, 1] = 2 * (qxy - qwz)
  1494. m[1, 1] = 1 - 2 * (qxx + qzz)
  1495. m[2, 1] = 2 * (qyz + qwx)
  1496. m[0, 2] = 2 * (qxz + qwy)
  1497. m[1, 2] = 2 * (qyz - qwx)
  1498. m[2, 2] = 1 - 2 * (qxx + qyy)
  1499. m[3, 3] = 1
  1500. return m
  1501. }
  1502. matrix4_from_quaternion :: proc{
  1503. matrix4_from_quaternion_f16,
  1504. matrix4_from_quaternion_f32,
  1505. matrix4_from_quaternion_f64,
  1506. }
  1507. @(require_results)
  1508. matrix4_from_trs_f16 :: proc "contextless" (t: Vector3f16, r: Quaternionf16, s: Vector3f16) -> Matrix4f16 {
  1509. translation := matrix4_translate(t)
  1510. rotation := matrix4_from_quaternion(r)
  1511. scale := matrix4_scale(s)
  1512. return mul(translation, mul(rotation, scale))
  1513. }
  1514. @(require_results)
  1515. matrix4_from_trs_f32 :: proc "contextless" (t: Vector3f32, r: Quaternionf32, s: Vector3f32) -> Matrix4f32 {
  1516. translation := matrix4_translate(t)
  1517. rotation := matrix4_from_quaternion(r)
  1518. scale := matrix4_scale(s)
  1519. return mul(translation, mul(rotation, scale))
  1520. }
  1521. @(require_results)
  1522. matrix4_from_trs_f64 :: proc "contextless" (t: Vector3f64, r: Quaternionf64, s: Vector3f64) -> Matrix4f64 {
  1523. translation := matrix4_translate(t)
  1524. rotation := matrix4_from_quaternion(r)
  1525. scale := matrix4_scale(s)
  1526. return mul(translation, mul(rotation, scale))
  1527. }
  1528. matrix4_from_trs :: proc{
  1529. matrix4_from_trs_f16,
  1530. matrix4_from_trs_f32,
  1531. matrix4_from_trs_f64,
  1532. }
  1533. @(require_results)
  1534. matrix4_inverse_f16 :: proc "contextless" (m: Matrix4f16) -> Matrix4f16 {
  1535. return transpose(matrix4_inverse_transpose(m))
  1536. }
  1537. @(require_results)
  1538. matrix4_inverse_f32 :: proc "contextless" (m: Matrix4f32) -> Matrix4f32 {
  1539. return transpose(matrix4_inverse_transpose(m))
  1540. }
  1541. @(require_results)
  1542. matrix4_inverse_f64 :: proc "contextless" (m: Matrix4f64) -> Matrix4f64 {
  1543. return transpose(matrix4_inverse_transpose(m))
  1544. }
  1545. matrix4_inverse :: proc{
  1546. matrix4_inverse_f16,
  1547. matrix4_inverse_f32,
  1548. matrix4_inverse_f64,
  1549. }
  1550. @(require_results)
  1551. matrix4_minor_f16 :: proc "contextless" (m: Matrix4f16, c, r: int) -> f16 {
  1552. cut_down: Matrix3f16
  1553. for i in 0..<3 {
  1554. col := i if i < c else i+1
  1555. for j in 0..<3 {
  1556. row := j if j < r else j+1
  1557. cut_down[i][j] = m[col][row]
  1558. }
  1559. }
  1560. return matrix3_determinant(cut_down)
  1561. }
  1562. @(require_results)
  1563. matrix4_minor_f32 :: proc "contextless" (m: Matrix4f32, c, r: int) -> f32 {
  1564. cut_down: Matrix3f32
  1565. for i in 0..<3 {
  1566. col := i if i < c else i+1
  1567. for j in 0..<3 {
  1568. row := j if j < r else j+1
  1569. cut_down[i][j] = m[col][row]
  1570. }
  1571. }
  1572. return matrix3_determinant(cut_down)
  1573. }
  1574. @(require_results)
  1575. matrix4_minor_f64 :: proc "contextless" (m: Matrix4f64, c, r: int) -> f64 {
  1576. cut_down: Matrix3f64
  1577. for i in 0..<3 {
  1578. col := i if i < c else i+1
  1579. for j in 0..<3 {
  1580. row := j if j < r else j+1
  1581. cut_down[i][j] = m[col][row]
  1582. }
  1583. }
  1584. return matrix3_determinant(cut_down)
  1585. }
  1586. matrix4_minor :: proc{
  1587. matrix4_minor_f16,
  1588. matrix4_minor_f32,
  1589. matrix4_minor_f64,
  1590. }
  1591. @(require_results)
  1592. matrix4_cofactor_f16 :: proc "contextless" (m: Matrix4f16, c, r: int) -> f16 {
  1593. sign, minor: f16
  1594. sign = 1 if (c + r) % 2 == 0 else -1
  1595. minor = matrix4_minor(m, c, r)
  1596. return sign * minor
  1597. }
  1598. @(require_results)
  1599. matrix4_cofactor_f32 :: proc "contextless" (m: Matrix4f32, c, r: int) -> f32 {
  1600. sign, minor: f32
  1601. sign = 1 if (c + r) % 2 == 0 else -1
  1602. minor = matrix4_minor(m, c, r)
  1603. return sign * minor
  1604. }
  1605. @(require_results)
  1606. matrix4_cofactor_f64 :: proc "contextless" (m: Matrix4f64, c, r: int) -> f64 {
  1607. sign, minor: f64
  1608. sign = 1 if (c + r) % 2 == 0 else -1
  1609. minor = matrix4_minor(m, c, r)
  1610. return sign * minor
  1611. }
  1612. matrix4_cofactor :: proc{
  1613. matrix4_cofactor_f16,
  1614. matrix4_cofactor_f32,
  1615. matrix4_cofactor_f64,
  1616. }
  1617. @(require_results)
  1618. matrix4_adjoint_f16 :: proc "contextless" (m: Matrix4f16) -> (adjoint: Matrix4f16) {
  1619. for i in 0..<4 {
  1620. for j in 0..<4 {
  1621. adjoint[i][j] = matrix4_cofactor(m, i, j)
  1622. }
  1623. }
  1624. return
  1625. }
  1626. @(require_results)
  1627. matrix4_adjoint_f32 :: proc "contextless" (m: Matrix4f32) -> (adjoint: Matrix4f32) {
  1628. for i in 0..<4 {
  1629. for j in 0..<4 {
  1630. adjoint[i][j] = matrix4_cofactor(m, i, j)
  1631. }
  1632. }
  1633. return
  1634. }
  1635. @(require_results)
  1636. matrix4_adjoint_f64 :: proc "contextless" (m: Matrix4f64) -> (adjoint: Matrix4f64) {
  1637. for i in 0..<4 {
  1638. for j in 0..<4 {
  1639. adjoint[i][j] = matrix4_cofactor(m, i, j)
  1640. }
  1641. }
  1642. return
  1643. }
  1644. matrix4_adjoint :: proc{
  1645. matrix4_adjoint_f16,
  1646. matrix4_adjoint_f32,
  1647. matrix4_adjoint_f64,
  1648. }
  1649. @(require_results)
  1650. matrix4_determinant_f16 :: proc "contextless" (m: Matrix4f16) -> (determinant: f16) {
  1651. adjoint := matrix4_adjoint(m)
  1652. for i in 0..<4 {
  1653. determinant += m[i][0] * adjoint[i][0]
  1654. }
  1655. return
  1656. }
  1657. @(require_results)
  1658. matrix4_determinant_f32 :: proc "contextless" (m: Matrix4f32) -> (determinant: f32) {
  1659. adjoint := matrix4_adjoint(m)
  1660. for i in 0..<4 {
  1661. determinant += m[i][0] * adjoint[i][0]
  1662. }
  1663. return
  1664. }
  1665. @(require_results)
  1666. matrix4_determinant_f64 :: proc "contextless" (m: Matrix4f64) -> (determinant: f64) {
  1667. adjoint := matrix4_adjoint(m)
  1668. for i in 0..<4 {
  1669. determinant += m[i][0] * adjoint[i][0]
  1670. }
  1671. return
  1672. }
  1673. matrix4_determinant :: proc{
  1674. matrix4_determinant_f16,
  1675. matrix4_determinant_f32,
  1676. matrix4_determinant_f64,
  1677. }
  1678. @(require_results)
  1679. matrix4_inverse_transpose_f16 :: proc "contextless" (m: Matrix4f16) -> (inverse_transpose: Matrix4f16) {
  1680. adjoint := matrix4_adjoint(m)
  1681. determinant: f16 = 0
  1682. for i in 0..<4 {
  1683. determinant += m[i][0] * adjoint[i][0]
  1684. }
  1685. inv_determinant := 1.0 / determinant
  1686. for i in 0..<4 {
  1687. for j in 0..<4 {
  1688. inverse_transpose[i][j] = adjoint[i][j] * inv_determinant
  1689. }
  1690. }
  1691. return
  1692. }
  1693. @(require_results)
  1694. matrix4_inverse_transpose_f32 :: proc "contextless" (m: Matrix4f32) -> (inverse_transpose: Matrix4f32) {
  1695. adjoint := matrix4_adjoint(m)
  1696. determinant: f32 = 0
  1697. for i in 0..<4 {
  1698. determinant += m[i][0] * adjoint[i][0]
  1699. }
  1700. inv_determinant := 1.0 / determinant
  1701. for i in 0..<4 {
  1702. for j in 0..<4 {
  1703. inverse_transpose[i][j] = adjoint[i][j] * inv_determinant
  1704. }
  1705. }
  1706. return
  1707. }
  1708. @(require_results)
  1709. matrix4_inverse_transpose_f64 :: proc "contextless" (m: Matrix4f64) -> (inverse_transpose: Matrix4f64) {
  1710. adjoint := matrix4_adjoint(m)
  1711. determinant: f64 = 0
  1712. for i in 0..<4 {
  1713. determinant += m[i][0] * adjoint[i][0]
  1714. }
  1715. inv_determinant := 1.0 / determinant
  1716. for i in 0..<4 {
  1717. for j in 0..<4 {
  1718. inverse_transpose[i][j] = adjoint[i][j] * inv_determinant
  1719. }
  1720. }
  1721. return
  1722. }
  1723. matrix4_inverse_transpose :: proc{
  1724. matrix4_inverse_transpose_f16,
  1725. matrix4_inverse_transpose_f32,
  1726. matrix4_inverse_transpose_f64,
  1727. }
  1728. @(require_results)
  1729. matrix4_translate_f16 :: proc "contextless" (v: Vector3f16) -> Matrix4f16 {
  1730. m := MATRIX4F16_IDENTITY
  1731. m[3][0] = v[0]
  1732. m[3][1] = v[1]
  1733. m[3][2] = v[2]
  1734. return m
  1735. }
  1736. @(require_results)
  1737. matrix4_translate_f32 :: proc "contextless" (v: Vector3f32) -> Matrix4f32 {
  1738. m := MATRIX4F32_IDENTITY
  1739. m[3][0] = v[0]
  1740. m[3][1] = v[1]
  1741. m[3][2] = v[2]
  1742. return m
  1743. }
  1744. @(require_results)
  1745. matrix4_translate_f64 :: proc "contextless" (v: Vector3f64) -> Matrix4f64 {
  1746. m := MATRIX4F64_IDENTITY
  1747. m[3][0] = v[0]
  1748. m[3][1] = v[1]
  1749. m[3][2] = v[2]
  1750. return m
  1751. }
  1752. matrix4_translate :: proc{
  1753. matrix4_translate_f16,
  1754. matrix4_translate_f32,
  1755. matrix4_translate_f64,
  1756. }
  1757. @(require_results)
  1758. matrix4_rotate_f16 :: proc "contextless" (angle_radians: f16, v: Vector3f16) -> Matrix4f16 {
  1759. c := math.cos(angle_radians)
  1760. s := math.sin(angle_radians)
  1761. a := normalize(v)
  1762. t := a * (1-c)
  1763. rot := MATRIX4F16_IDENTITY
  1764. rot[0][0] = c + t[0]*a[0]
  1765. rot[0][1] = 0 + t[0]*a[1] + s*a[2]
  1766. rot[0][2] = 0 + t[0]*a[2] - s*a[1]
  1767. rot[0][3] = 0
  1768. rot[1][0] = 0 + t[1]*a[0] - s*a[2]
  1769. rot[1][1] = c + t[1]*a[1]
  1770. rot[1][2] = 0 + t[1]*a[2] + s*a[0]
  1771. rot[1][3] = 0
  1772. rot[2][0] = 0 + t[2]*a[0] + s*a[1]
  1773. rot[2][1] = 0 + t[2]*a[1] - s*a[0]
  1774. rot[2][2] = c + t[2]*a[2]
  1775. rot[2][3] = 0
  1776. return rot
  1777. }
  1778. @(require_results)
  1779. matrix4_rotate_f32 :: proc "contextless" (angle_radians: f32, v: Vector3f32) -> Matrix4f32 {
  1780. c := math.cos(angle_radians)
  1781. s := math.sin(angle_radians)
  1782. a := normalize(v)
  1783. t := a * (1-c)
  1784. rot := MATRIX4F32_IDENTITY
  1785. rot[0][0] = c + t[0]*a[0]
  1786. rot[0][1] = 0 + t[0]*a[1] + s*a[2]
  1787. rot[0][2] = 0 + t[0]*a[2] - s*a[1]
  1788. rot[0][3] = 0
  1789. rot[1][0] = 0 + t[1]*a[0] - s*a[2]
  1790. rot[1][1] = c + t[1]*a[1]
  1791. rot[1][2] = 0 + t[1]*a[2] + s*a[0]
  1792. rot[1][3] = 0
  1793. rot[2][0] = 0 + t[2]*a[0] + s*a[1]
  1794. rot[2][1] = 0 + t[2]*a[1] - s*a[0]
  1795. rot[2][2] = c + t[2]*a[2]
  1796. rot[2][3] = 0
  1797. return rot
  1798. }
  1799. @(require_results)
  1800. matrix4_rotate_f64 :: proc "contextless" (angle_radians: f64, v: Vector3f64) -> Matrix4f64 {
  1801. c := math.cos(angle_radians)
  1802. s := math.sin(angle_radians)
  1803. a := normalize(v)
  1804. t := a * (1-c)
  1805. rot := MATRIX4F64_IDENTITY
  1806. rot[0][0] = c + t[0]*a[0]
  1807. rot[0][1] = 0 + t[0]*a[1] + s*a[2]
  1808. rot[0][2] = 0 + t[0]*a[2] - s*a[1]
  1809. rot[0][3] = 0
  1810. rot[1][0] = 0 + t[1]*a[0] - s*a[2]
  1811. rot[1][1] = c + t[1]*a[1]
  1812. rot[1][2] = 0 + t[1]*a[2] + s*a[0]
  1813. rot[1][3] = 0
  1814. rot[2][0] = 0 + t[2]*a[0] + s*a[1]
  1815. rot[2][1] = 0 + t[2]*a[1] - s*a[0]
  1816. rot[2][2] = c + t[2]*a[2]
  1817. rot[2][3] = 0
  1818. return rot
  1819. }
  1820. matrix4_rotate :: proc{
  1821. matrix4_rotate_f16,
  1822. matrix4_rotate_f32,
  1823. matrix4_rotate_f64,
  1824. }
  1825. @(require_results)
  1826. matrix4_scale_f16 :: proc "contextless" (v: Vector3f16) -> (m: Matrix4f16) {
  1827. m[0][0] = v[0]
  1828. m[1][1] = v[1]
  1829. m[2][2] = v[2]
  1830. m[3][3] = 1
  1831. return
  1832. }
  1833. @(require_results)
  1834. matrix4_scale_f32 :: proc "contextless" (v: Vector3f32) -> (m: Matrix4f32) {
  1835. m[0][0] = v[0]
  1836. m[1][1] = v[1]
  1837. m[2][2] = v[2]
  1838. m[3][3] = 1
  1839. return
  1840. }
  1841. @(require_results)
  1842. matrix4_scale_f64 :: proc "contextless" (v: Vector3f64) -> (m: Matrix4f64) {
  1843. m[0][0] = v[0]
  1844. m[1][1] = v[1]
  1845. m[2][2] = v[2]
  1846. m[3][3] = 1
  1847. return
  1848. }
  1849. matrix4_scale :: proc{
  1850. matrix4_scale_f16,
  1851. matrix4_scale_f32,
  1852. matrix4_scale_f64,
  1853. }
  1854. @(require_results)
  1855. matrix4_look_at_f16 :: proc "contextless" (eye, centre, up: Vector3f16, flip_z_axis := true) -> (m: Matrix4f16) {
  1856. f := normalize(centre - eye)
  1857. s := normalize(cross(f, up))
  1858. u := cross(s, f)
  1859. fe := dot(f, eye)
  1860. return {
  1861. +s.x, +s.y, +s.z, -dot(s, eye),
  1862. +u.x, +u.y, +u.z, -dot(u, eye),
  1863. -f.x, -f.y, -f.z, +fe if flip_z_axis else -fe,
  1864. 0, 0, 0, 1,
  1865. }
  1866. }
  1867. @(require_results)
  1868. matrix4_look_at_f32 :: proc "contextless" (eye, centre, up: Vector3f32, flip_z_axis := true) -> (m: Matrix4f32) {
  1869. f := normalize(centre - eye)
  1870. s := normalize(cross(f, up))
  1871. u := cross(s, f)
  1872. fe := dot(f, eye)
  1873. return {
  1874. +s.x, +s.y, +s.z, -dot(s, eye),
  1875. +u.x, +u.y, +u.z, -dot(u, eye),
  1876. -f.x, -f.y, -f.z, +fe if flip_z_axis else -fe,
  1877. 0, 0, 0, 1,
  1878. }
  1879. }
  1880. @(require_results)
  1881. matrix4_look_at_f64 :: proc "contextless" (eye, centre, up: Vector3f64, flip_z_axis := true) -> (m: Matrix4f64) {
  1882. f := normalize(centre - eye)
  1883. s := normalize(cross(f, up))
  1884. u := cross(s, f)
  1885. fe := dot(f, eye)
  1886. return {
  1887. +s.x, +s.y, +s.z, -dot(s, eye),
  1888. +u.x, +u.y, +u.z, -dot(u, eye),
  1889. -f.x, -f.y, -f.z, +fe if flip_z_axis else -fe,
  1890. 0, 0, 0, 1,
  1891. }
  1892. }
  1893. matrix4_look_at :: proc{
  1894. matrix4_look_at_f16,
  1895. matrix4_look_at_f32,
  1896. matrix4_look_at_f64,
  1897. }
  1898. @(require_results)
  1899. matrix4_look_at_from_fru_f16 :: proc "contextless" (eye, f, r, u: Vector3f16, flip_z_axis := true) -> (m: Matrix4f16) {
  1900. f, s, u := f, r, u
  1901. f = normalize(f)
  1902. s = normalize(s)
  1903. u = normalize(u)
  1904. fe := dot(f, eye)
  1905. return {
  1906. +s.x, +s.y, +s.z, -dot(s, eye),
  1907. +u.x, +u.y, +u.z, -dot(u, eye),
  1908. -f.x, -f.y, -f.z, +fe if flip_z_axis else -fe,
  1909. 0, 0, 0, 1,
  1910. }
  1911. }
  1912. @(require_results)
  1913. matrix4_look_at_from_fru_f32 :: proc "contextless" (eye, f, r, u: Vector3f32, flip_z_axis := true) -> (m: Matrix4f32) {
  1914. f, s, u := f, r, u
  1915. f = normalize(f)
  1916. s = normalize(s)
  1917. u = normalize(u)
  1918. fe := dot(f, eye)
  1919. return {
  1920. +s.x, +s.y, +s.z, -dot(s, eye),
  1921. +u.x, +u.y, +u.z, -dot(u, eye),
  1922. -f.x, -f.y, -f.z, +fe if flip_z_axis else -fe,
  1923. 0, 0, 0, 1,
  1924. }
  1925. }
  1926. @(require_results)
  1927. matrix4_look_at_from_fru_f64 :: proc "contextless" (eye, f, r, u: Vector3f64, flip_z_axis := true) -> (m: Matrix4f64) {
  1928. f, s, u := f, r, u
  1929. f = normalize(f)
  1930. s = normalize(s)
  1931. u = normalize(u)
  1932. fe := dot(f, eye)
  1933. return {
  1934. +s.x, +s.y, +s.z, -dot(s, eye),
  1935. +u.x, +u.y, +u.z, -dot(u, eye),
  1936. -f.x, -f.y, -f.z, +fe if flip_z_axis else -fe,
  1937. 0, 0, 0, 1,
  1938. }
  1939. }
  1940. matrix4_look_at_from_fru :: proc{
  1941. matrix4_look_at_from_fru_f16,
  1942. matrix4_look_at_from_fru_f32,
  1943. matrix4_look_at_from_fru_f64,
  1944. }
  1945. @(require_results)
  1946. matrix4_perspective_f16 :: proc "contextless" (fovy, aspect, near, far: f16, flip_z_axis := true) -> (m: Matrix4f16) {
  1947. tan_half_fovy := math.tan(0.5 * fovy)
  1948. m[0, 0] = 1 / (aspect*tan_half_fovy)
  1949. m[1, 1] = 1 / (tan_half_fovy)
  1950. m[2, 2] = +(far + near) / (far - near)
  1951. m[3, 2] = +1
  1952. m[2, 3] = -2*far*near / (far - near)
  1953. if flip_z_axis {
  1954. m[2] = -m[2]
  1955. }
  1956. return
  1957. }
  1958. @(require_results)
  1959. matrix4_perspective_f32 :: proc "contextless" (fovy, aspect, near, far: f32, flip_z_axis := true) -> (m: Matrix4f32) {
  1960. tan_half_fovy := math.tan(0.5 * fovy)
  1961. m[0, 0] = 1 / (aspect*tan_half_fovy)
  1962. m[1, 1] = 1 / (tan_half_fovy)
  1963. m[2, 2] = +(far + near) / (far - near)
  1964. m[3, 2] = +1
  1965. m[2, 3] = -2*far*near / (far - near)
  1966. if flip_z_axis {
  1967. m[2] = -m[2]
  1968. }
  1969. return
  1970. }
  1971. @(require_results)
  1972. matrix4_perspective_f64 :: proc "contextless" (fovy, aspect, near, far: f64, flip_z_axis := true) -> (m: Matrix4f64) {
  1973. tan_half_fovy := math.tan(0.5 * fovy)
  1974. m[0, 0] = 1 / (aspect*tan_half_fovy)
  1975. m[1, 1] = 1 / (tan_half_fovy)
  1976. m[2, 2] = +(far + near) / (far - near)
  1977. m[3, 2] = +1
  1978. m[2, 3] = -2*far*near / (far - near)
  1979. if flip_z_axis {
  1980. m[2] = -m[2]
  1981. }
  1982. return
  1983. }
  1984. matrix4_perspective :: proc{
  1985. matrix4_perspective_f16,
  1986. matrix4_perspective_f32,
  1987. matrix4_perspective_f64,
  1988. }
  1989. @(require_results)
  1990. matrix_ortho3d_f16 :: proc "contextless" (left, right, bottom, top, near, far: f16, flip_z_axis := true) -> (m: Matrix4f16) {
  1991. m[0, 0] = +2 / (right - left)
  1992. m[1, 1] = +2 / (top - bottom)
  1993. m[2, 2] = +2 / (far - near)
  1994. m[0, 3] = -(right + left) / (right - left)
  1995. m[1, 3] = -(top + bottom) / (top - bottom)
  1996. m[2, 3] = -(far + near) / (far- near)
  1997. m[3, 3] = 1
  1998. if flip_z_axis {
  1999. m[2] = -m[2]
  2000. }
  2001. return
  2002. }
  2003. @(require_results)
  2004. matrix_ortho3d_f32 :: proc "contextless" (left, right, bottom, top, near, far: f32, flip_z_axis := true) -> (m: Matrix4f32) {
  2005. m[0, 0] = +2 / (right - left)
  2006. m[1, 1] = +2 / (top - bottom)
  2007. m[2, 2] = +2 / (far - near)
  2008. m[0, 3] = -(right + left) / (right - left)
  2009. m[1, 3] = -(top + bottom) / (top - bottom)
  2010. m[2, 3] = -(far + near) / (far- near)
  2011. m[3, 3] = 1
  2012. if flip_z_axis {
  2013. m[2] = -m[2]
  2014. }
  2015. return
  2016. }
  2017. @(require_results)
  2018. matrix_ortho3d_f64 :: proc "contextless" (left, right, bottom, top, near, far: f64, flip_z_axis := true) -> (m: Matrix4f64) {
  2019. m[0, 0] = +2 / (right - left)
  2020. m[1, 1] = +2 / (top - bottom)
  2021. m[2, 2] = +2 / (far - near)
  2022. m[0, 3] = -(right + left) / (right - left)
  2023. m[1, 3] = -(top + bottom) / (top - bottom)
  2024. m[2, 3] = -(far + near) / (far- near)
  2025. m[3, 3] = 1
  2026. if flip_z_axis {
  2027. m[2] = -m[2]
  2028. }
  2029. return
  2030. }
  2031. matrix_ortho3d :: proc{
  2032. matrix_ortho3d_f16,
  2033. matrix_ortho3d_f32,
  2034. matrix_ortho3d_f64,
  2035. }
  2036. @(require_results)
  2037. matrix4_infinite_perspective_f16 :: proc "contextless" (fovy, aspect, near: f16, flip_z_axis := true) -> (m: Matrix4f16) {
  2038. tan_half_fovy := math.tan(0.5 * fovy)
  2039. m[0, 0] = 1 / (aspect*tan_half_fovy)
  2040. m[1, 1] = 1 / (tan_half_fovy)
  2041. m[2, 2] = +1
  2042. m[3, 2] = +1
  2043. m[2, 3] = -2*near
  2044. if flip_z_axis {
  2045. m[2] = -m[2]
  2046. }
  2047. return
  2048. }
  2049. @(require_results)
  2050. matrix4_infinite_perspective_f32 :: proc "contextless" (fovy, aspect, near: f32, flip_z_axis := true) -> (m: Matrix4f32) {
  2051. tan_half_fovy := math.tan(0.5 * fovy)
  2052. m[0, 0] = 1 / (aspect*tan_half_fovy)
  2053. m[1, 1] = 1 / (tan_half_fovy)
  2054. m[2, 2] = +1
  2055. m[3, 2] = +1
  2056. m[2, 3] = -2*near
  2057. if flip_z_axis {
  2058. m[2] = -m[2]
  2059. }
  2060. return
  2061. }
  2062. @(require_results)
  2063. matrix4_infinite_perspective_f64 :: proc "contextless" (fovy, aspect, near: f64, flip_z_axis := true) -> (m: Matrix4f64) {
  2064. tan_half_fovy := math.tan(0.5 * fovy)
  2065. m[0, 0] = 1 / (aspect*tan_half_fovy)
  2066. m[1, 1] = 1 / (tan_half_fovy)
  2067. m[2, 2] = +1
  2068. m[3, 2] = +1
  2069. m[2, 3] = -2*near
  2070. if flip_z_axis {
  2071. m[2] = -m[2]
  2072. }
  2073. return
  2074. }
  2075. matrix4_infinite_perspective :: proc{
  2076. matrix4_infinite_perspective_f16,
  2077. matrix4_infinite_perspective_f32,
  2078. matrix4_infinite_perspective_f64,
  2079. }
  2080. @(require_results)
  2081. matrix2_from_scalar_f16 :: proc "contextless" (f: f16) -> (m: Matrix2f16) {
  2082. m[0, 0], m[1, 0] = f, 0
  2083. m[0, 1], m[1, 1] = 0, f
  2084. return
  2085. }
  2086. @(require_results)
  2087. matrix2_from_scalar_f32 :: proc "contextless" (f: f32) -> (m: Matrix2f32) {
  2088. m[0, 0], m[1, 0] = f, 0
  2089. m[0, 1], m[1, 1] = 0, f
  2090. return
  2091. }
  2092. @(require_results)
  2093. matrix2_from_scalar_f64 :: proc "contextless" (f: f64) -> (m: Matrix2f64) {
  2094. m[0, 0], m[1, 0] = f, 0
  2095. m[0, 1], m[1, 1] = 0, f
  2096. return
  2097. }
  2098. matrix2_from_scalar :: proc{
  2099. matrix2_from_scalar_f16,
  2100. matrix2_from_scalar_f32,
  2101. matrix2_from_scalar_f64,
  2102. }
  2103. @(require_results)
  2104. matrix3_from_scalar_f16 :: proc "contextless" (f: f16) -> (m: Matrix3f16) {
  2105. m[0, 0], m[1, 0], m[2, 0] = f, 0, 0
  2106. m[0, 1], m[1, 1], m[2, 1] = 0, f, 0
  2107. m[0, 2], m[1, 2], m[2, 2] = 0, 0, f
  2108. return
  2109. }
  2110. @(require_results)
  2111. matrix3_from_scalar_f32 :: proc "contextless" (f: f32) -> (m: Matrix3f32) {
  2112. m[0, 0], m[1, 0], m[2, 0] = f, 0, 0
  2113. m[0, 1], m[1, 1], m[2, 1] = 0, f, 0
  2114. m[0, 2], m[1, 2], m[2, 2] = 0, 0, f
  2115. return
  2116. }
  2117. @(require_results)
  2118. matrix3_from_scalar_f64 :: proc "contextless" (f: f64) -> (m: Matrix3f64) {
  2119. m[0, 0], m[1, 0], m[2, 0] = f, 0, 0
  2120. m[0, 1], m[1, 1], m[2, 1] = 0, f, 0
  2121. m[0, 2], m[1, 2], m[2, 2] = 0, 0, f
  2122. return
  2123. }
  2124. matrix3_from_scalar :: proc{
  2125. matrix3_from_scalar_f16,
  2126. matrix3_from_scalar_f32,
  2127. matrix3_from_scalar_f64,
  2128. }
  2129. @(require_results)
  2130. matrix4_from_scalar_f16 :: proc "contextless" (f: f16) -> (m: Matrix4f16) {
  2131. m[0, 0], m[1, 0], m[2, 0], m[3, 0] = f, 0, 0, 0
  2132. m[0, 1], m[1, 1], m[2, 1], m[3, 1] = 0, f, 0, 0
  2133. m[0, 2], m[1, 2], m[2, 2], m[3, 2] = 0, 0, f, 0
  2134. m[0, 3], m[1, 3], m[2, 3], m[3, 3] = 0, 0, 0, f
  2135. return
  2136. }
  2137. @(require_results)
  2138. matrix4_from_scalar_f32 :: proc "contextless" (f: f32) -> (m: Matrix4f32) {
  2139. m[0, 0], m[1, 0], m[2, 0], m[3, 0] = f, 0, 0, 0
  2140. m[0, 1], m[1, 1], m[2, 1], m[3, 1] = 0, f, 0, 0
  2141. m[0, 2], m[1, 2], m[2, 2], m[3, 2] = 0, 0, f, 0
  2142. m[0, 3], m[1, 3], m[2, 3], m[3, 3] = 0, 0, 0, f
  2143. return
  2144. }
  2145. @(require_results)
  2146. matrix4_from_scalar_f64 :: proc "contextless" (f: f64) -> (m: Matrix4f64) {
  2147. m[0, 0], m[1, 0], m[2, 0], m[3, 0] = f, 0, 0, 0
  2148. m[0, 1], m[1, 1], m[2, 1], m[3, 1] = 0, f, 0, 0
  2149. m[0, 2], m[1, 2], m[2, 2], m[3, 2] = 0, 0, f, 0
  2150. m[0, 3], m[1, 3], m[2, 3], m[3, 3] = 0, 0, 0, f
  2151. return
  2152. }
  2153. matrix4_from_scalar :: proc{
  2154. matrix4_from_scalar_f16,
  2155. matrix4_from_scalar_f32,
  2156. matrix4_from_scalar_f64,
  2157. }
  2158. @(require_results)
  2159. matrix2_from_matrix3_f16 :: proc "contextless" (m: Matrix3f16) -> (r: Matrix2f16) {
  2160. r[0, 0], r[1, 0] = m[0, 0], m[1, 0]
  2161. r[0, 1], r[1, 1] = m[0, 1], m[1, 1]
  2162. return
  2163. }
  2164. @(require_results)
  2165. matrix2_from_matrix3_f32 :: proc "contextless" (m: Matrix3f32) -> (r: Matrix2f32) {
  2166. r[0, 0], r[1, 0] = m[0, 0], m[1, 0]
  2167. r[0, 1], r[1, 1] = m[0, 1], m[1, 1]
  2168. return
  2169. }
  2170. @(require_results)
  2171. matrix2_from_matrix3_f64 :: proc "contextless" (m: Matrix3f64) -> (r: Matrix2f64) {
  2172. r[0, 0], r[1, 0] = m[0, 0], m[1, 0]
  2173. r[0, 1], r[1, 1] = m[0, 1], m[1, 1]
  2174. return
  2175. }
  2176. matrix2_from_matrix3 :: proc{
  2177. matrix2_from_matrix3_f16,
  2178. matrix2_from_matrix3_f32,
  2179. matrix2_from_matrix3_f64,
  2180. }
  2181. @(require_results)
  2182. matrix2_from_matrix4_f16 :: proc "contextless" (m: Matrix4f16) -> (r: Matrix2f16) {
  2183. r[0, 0], r[1, 0] = m[0, 0], m[1, 0]
  2184. r[0, 1], r[1, 1] = m[0, 1], m[1, 1]
  2185. return
  2186. }
  2187. @(require_results)
  2188. matrix2_from_matrix4_f32 :: proc "contextless" (m: Matrix4f32) -> (r: Matrix2f32) {
  2189. r[0, 0], r[1, 0] = m[0, 0], m[1, 0]
  2190. r[0, 1], r[1, 1] = m[0, 1], m[1, 1]
  2191. return
  2192. }
  2193. @(require_results)
  2194. matrix2_from_matrix4_f64 :: proc "contextless" (m: Matrix4f64) -> (r: Matrix2f64) {
  2195. r[0, 0], r[1, 0] = m[0, 0], m[1, 0]
  2196. r[0, 1], r[1, 1] = m[0, 1], m[1, 1]
  2197. return
  2198. }
  2199. matrix2_from_matrix4 :: proc{
  2200. matrix2_from_matrix4_f16,
  2201. matrix2_from_matrix4_f32,
  2202. matrix2_from_matrix4_f64,
  2203. }
  2204. @(require_results)
  2205. matrix3_from_matrix2_f16 :: proc "contextless" (m: Matrix2f16) -> (r: Matrix3f16) {
  2206. r[0, 0], r[1, 0], r[2, 0] = m[0, 0], m[1, 0], 0
  2207. r[0, 1], r[1, 1], r[2, 1] = m[0, 1], m[1, 1], 0
  2208. r[0, 2], r[1, 2], r[2, 2] = 0, 0, 1
  2209. return
  2210. }
  2211. @(require_results)
  2212. matrix3_from_matrix2_f32 :: proc "contextless" (m: Matrix2f32) -> (r: Matrix3f32) {
  2213. r[0, 0], r[1, 0], r[2, 0] = m[0, 0], m[1, 0], 0
  2214. r[0, 1], r[1, 1], r[2, 1] = m[0, 1], m[1, 1], 0
  2215. r[0, 2], r[1, 2], r[2, 2] = 0, 0, 1
  2216. return
  2217. }
  2218. @(require_results)
  2219. matrix3_from_matrix2_f64 :: proc "contextless" (m: Matrix2f64) -> (r: Matrix3f64) {
  2220. r[0, 0], r[1, 0], r[2, 0] = m[0, 0], m[1, 0], 0
  2221. r[0, 1], r[1, 1], r[2, 1] = m[0, 1], m[1, 1], 0
  2222. r[0, 2], r[1, 2], r[2, 2] = 0, 0, 1
  2223. return
  2224. }
  2225. matrix3_from_matrix2 :: proc{
  2226. matrix3_from_matrix2_f16,
  2227. matrix3_from_matrix2_f32,
  2228. matrix3_from_matrix2_f64,
  2229. }
  2230. @(require_results)
  2231. matrix3_from_matrix4_f16 :: proc "contextless" (m: Matrix4f16) -> (r: Matrix3f16) {
  2232. r[0, 0], r[1, 0], r[2, 0] = m[0, 0], m[1, 0], m[2, 0]
  2233. r[0, 1], r[1, 1], r[2, 1] = m[0, 1], m[1, 1], m[2, 1]
  2234. r[0, 2], r[1, 2], r[2, 2] = m[0, 2], m[1, 2], m[2, 2]
  2235. return
  2236. }
  2237. @(require_results)
  2238. matrix3_from_matrix4_f32 :: proc "contextless" (m: Matrix4f32) -> (r: Matrix3f32) {
  2239. r[0, 0], r[1, 0], r[2, 0] = m[0, 0], m[1, 0], m[2, 0]
  2240. r[0, 1], r[1, 1], r[2, 1] = m[0, 1], m[1, 1], m[2, 1]
  2241. r[0, 2], r[1, 2], r[2, 2] = m[0, 2], m[1, 2], m[2, 2]
  2242. return
  2243. }
  2244. @(require_results)
  2245. matrix3_from_matrix4_f64 :: proc "contextless" (m: Matrix4f64) -> (r: Matrix3f64) {
  2246. r[0, 0], r[1, 0], r[2, 0] = m[0, 0], m[1, 0], m[2, 0]
  2247. r[0, 1], r[1, 1], r[2, 1] = m[0, 1], m[1, 1], m[2, 1]
  2248. r[0, 2], r[1, 2], r[2, 2] = m[0, 2], m[1, 2], m[2, 2]
  2249. return
  2250. }
  2251. matrix3_from_matrix4 :: proc{
  2252. matrix3_from_matrix4_f16,
  2253. matrix3_from_matrix4_f32,
  2254. matrix3_from_matrix4_f64,
  2255. }
  2256. @(require_results)
  2257. matrix4_from_matrix2_f16 :: proc "contextless" (m: Matrix2f16) -> (r: Matrix4f16) {
  2258. r[0, 0], r[1, 0], r[2, 0], r[3, 0] = m[0, 0], m[1, 0], 0, 0
  2259. r[0, 1], r[1, 1], r[2, 1], r[3, 1] = m[0, 1], m[1, 1], 0, 0
  2260. r[0, 2], r[1, 2], r[2, 2], r[3, 2] = 0, 0, 1, 0
  2261. r[0, 3], r[1, 3], r[2, 3], r[3, 3] = 0, 0, 0, 1
  2262. return
  2263. }
  2264. @(require_results)
  2265. matrix4_from_matrix2_f32 :: proc "contextless" (m: Matrix2f32) -> (r: Matrix4f32) {
  2266. r[0, 0], r[1, 0], r[2, 0], r[3, 0] = m[0, 0], m[1, 0], 0, 0
  2267. r[0, 1], r[1, 1], r[2, 1], r[3, 1] = m[0, 1], m[1, 1], 0, 0
  2268. r[0, 2], r[1, 2], r[2, 2], r[3, 2] = 0, 0, 1, 0
  2269. r[0, 3], r[1, 3], r[2, 3], r[3, 3] = 0, 0, 0, 1
  2270. return
  2271. }
  2272. @(require_results)
  2273. matrix4_from_matrix2_f64 :: proc "contextless" (m: Matrix2f64) -> (r: Matrix4f64) {
  2274. r[0, 0], r[1, 0], r[2, 0], r[3, 0] = m[0, 0], m[1, 0], 0, 0
  2275. r[0, 1], r[1, 1], r[2, 1], r[3, 1] = m[0, 1], m[1, 1], 0, 0
  2276. r[0, 2], r[1, 2], r[2, 2], r[3, 2] = 0, 0, 1, 0
  2277. r[0, 3], r[1, 3], r[2, 3], r[3, 3] = 0, 0, 0, 1
  2278. return
  2279. }
  2280. matrix4_from_matrix2 :: proc{
  2281. matrix4_from_matrix2_f16,
  2282. matrix4_from_matrix2_f32,
  2283. matrix4_from_matrix2_f64,
  2284. }
  2285. @(require_results)
  2286. matrix4_from_matrix3_f16 :: proc "contextless" (m: Matrix3f16) -> (r: Matrix4f16) {
  2287. r[0, 0], r[1, 0], r[2, 0], r[3, 0] = m[0, 0], m[1, 0], m[2, 0], 0
  2288. r[0, 1], r[1, 1], r[2, 1], r[3, 1] = m[0, 1], m[1, 1], m[2, 1], 0
  2289. r[0, 2], r[1, 2], r[2, 2], r[3, 2] = m[0, 2], m[1, 2], m[2, 2], 0
  2290. r[0, 3], r[1, 3], r[2, 3], r[3, 3] = 0, 0, 0, 1
  2291. return
  2292. }
  2293. @(require_results)
  2294. matrix4_from_matrix3_f32 :: proc "contextless" (m: Matrix3f32) -> (r: Matrix4f32) {
  2295. r[0, 0], r[1, 0], r[2, 0], r[3, 0] = m[0, 0], m[1, 0], m[2, 0], 0
  2296. r[0, 1], r[1, 1], r[2, 1], r[3, 1] = m[0, 1], m[1, 1], m[2, 1], 0
  2297. r[0, 2], r[1, 2], r[2, 2], r[3, 2] = m[0, 2], m[1, 2], m[2, 2], 0
  2298. r[0, 3], r[1, 3], r[2, 3], r[3, 3] = 0, 0, 0, 1
  2299. return
  2300. }
  2301. @(require_results)
  2302. matrix4_from_matrix3_f64 :: proc "contextless" (m: Matrix3f64) -> (r: Matrix4f64) {
  2303. r[0, 0], r[1, 0], r[2, 0], r[3, 0] = m[0, 0], m[1, 0], m[2, 0], 0
  2304. r[0, 1], r[1, 1], r[2, 1], r[3, 1] = m[0, 1], m[1, 1], m[2, 1], 0
  2305. r[0, 2], r[1, 2], r[2, 2], r[3, 2] = m[0, 2], m[1, 2], m[2, 2], 0
  2306. r[0, 3], r[1, 3], r[2, 3], r[3, 3] = 0, 0, 0, 1
  2307. return
  2308. }
  2309. matrix4_from_matrix3 :: proc{
  2310. matrix4_from_matrix3_f16,
  2311. matrix4_from_matrix3_f32,
  2312. matrix4_from_matrix3_f64,
  2313. }
  2314. @(require_results)
  2315. quaternion_from_scalar_f16 :: proc "contextless" (f: f16) -> (q: Quaternionf16) {
  2316. q.w = f
  2317. return
  2318. }
  2319. @(require_results)
  2320. quaternion_from_scalar_f32 :: proc "contextless" (f: f32) -> (q: Quaternionf32) {
  2321. q.w = f
  2322. return
  2323. }
  2324. @(require_results)
  2325. quaternion_from_scalar_f64 :: proc "contextless" (f: f64) -> (q: Quaternionf64) {
  2326. q.w = f
  2327. return
  2328. }
  2329. quaternion_from_scalar :: proc{
  2330. quaternion_from_scalar_f16,
  2331. quaternion_from_scalar_f32,
  2332. quaternion_from_scalar_f64,
  2333. }
  2334. to_matrix2f16 :: proc{matrix2_from_scalar_f16, matrix2_from_matrix3_f16, matrix2_from_matrix4_f16}
  2335. to_matrix3f16 :: proc{matrix3_from_scalar_f16, matrix3_from_matrix2_f16, matrix3_from_matrix4_f16, matrix3_from_quaternion_f16}
  2336. to_matrix4f16 :: proc{matrix4_from_scalar_f16, matrix4_from_matrix2_f16, matrix4_from_matrix3_f16, matrix4_from_quaternion_f16}
  2337. to_quaternionf16 :: proc{quaternion_from_scalar_f16, quaternion_from_matrix3_f16, quaternion_from_matrix4_f16}
  2338. to_matrix2f32 :: proc{matrix2_from_scalar_f32, matrix2_from_matrix3_f32, matrix2_from_matrix4_f32}
  2339. to_matrix3f32 :: proc{matrix3_from_scalar_f32, matrix3_from_matrix2_f32, matrix3_from_matrix4_f32, matrix3_from_quaternion_f32}
  2340. to_matrix4f32 :: proc{matrix4_from_scalar_f32, matrix4_from_matrix2_f32, matrix4_from_matrix3_f32, matrix4_from_quaternion_f32}
  2341. to_quaternionf32 :: proc{quaternion_from_scalar_f32, quaternion_from_matrix3_f32, quaternion_from_matrix4_f32}
  2342. to_matrix2f64 :: proc{matrix2_from_scalar_f64, matrix2_from_matrix3_f64, matrix2_from_matrix4_f64}
  2343. to_matrix3f64 :: proc{matrix3_from_scalar_f64, matrix3_from_matrix2_f64, matrix3_from_matrix4_f64, matrix3_from_quaternion_f64}
  2344. to_matrix4f64 :: proc{matrix4_from_scalar_f64, matrix4_from_matrix2_f64, matrix4_from_matrix3_f64, matrix4_from_quaternion_f64}
  2345. to_quaternionf64 :: proc{quaternion_from_scalar_f64, quaternion_from_matrix3_f64, quaternion_from_matrix4_f64}
  2346. to_matrix2f :: proc{
  2347. matrix2_from_scalar_f16, matrix2_from_matrix3_f16, matrix2_from_matrix4_f16,
  2348. matrix2_from_scalar_f32, matrix2_from_matrix3_f32, matrix2_from_matrix4_f32,
  2349. matrix2_from_scalar_f64, matrix2_from_matrix3_f64, matrix2_from_matrix4_f64,
  2350. }
  2351. to_matrix3 :: proc{
  2352. matrix3_from_scalar_f16, matrix3_from_matrix2_f16, matrix3_from_matrix4_f16, matrix3_from_quaternion_f16,
  2353. matrix3_from_scalar_f32, matrix3_from_matrix2_f32, matrix3_from_matrix4_f32, matrix3_from_quaternion_f32,
  2354. matrix3_from_scalar_f64, matrix3_from_matrix2_f64, matrix3_from_matrix4_f64, matrix3_from_quaternion_f64,
  2355. }
  2356. to_matrix4 :: proc{
  2357. matrix4_from_scalar_f16, matrix4_from_matrix2_f16, matrix4_from_matrix3_f16, matrix4_from_quaternion_f16,
  2358. matrix4_from_scalar_f32, matrix4_from_matrix2_f32, matrix4_from_matrix3_f32, matrix4_from_quaternion_f32,
  2359. matrix4_from_scalar_f64, matrix4_from_matrix2_f64, matrix4_from_matrix3_f64, matrix4_from_quaternion_f64,
  2360. }
  2361. to_quaternion :: proc{
  2362. quaternion_from_scalar_f16, quaternion_from_matrix3_f16, quaternion_from_matrix4_f16,
  2363. quaternion_from_scalar_f32, quaternion_from_matrix3_f32, quaternion_from_matrix4_f32,
  2364. quaternion_from_scalar_f64, quaternion_from_matrix3_f64, quaternion_from_matrix4_f64,
  2365. }
  2366. @(require_results)
  2367. matrix2_orthonormalize_f16 :: proc "contextless" (m: Matrix2f16) -> (r: Matrix2f16) {
  2368. r[0] = normalize(m[0])
  2369. d0 := dot(r[0], r[1])
  2370. r[1] -= r[0] * d0
  2371. r[1] = normalize(r[1])
  2372. return
  2373. }
  2374. @(require_results)
  2375. matrix2_orthonormalize_f32 :: proc "contextless" (m: Matrix2f32) -> (r: Matrix2f32) {
  2376. r[0] = normalize(m[0])
  2377. d0 := dot(r[0], r[1])
  2378. r[1] -= r[0] * d0
  2379. r[1] = normalize(r[1])
  2380. return
  2381. }
  2382. @(require_results)
  2383. matrix2_orthonormalize_f64 :: proc "contextless" (m: Matrix2f64) -> (r: Matrix2f64) {
  2384. r[0] = normalize(m[0])
  2385. d0 := dot(r[0], r[1])
  2386. r[1] -= r[0] * d0
  2387. r[1] = normalize(r[1])
  2388. return
  2389. }
  2390. matrix2_orthonormalize :: proc{
  2391. matrix2_orthonormalize_f16,
  2392. matrix2_orthonormalize_f32,
  2393. matrix2_orthonormalize_f64,
  2394. }
  2395. @(require_results)
  2396. matrix3_orthonormalize_f16 :: proc "contextless" (m: Matrix3f16) -> (r: Matrix3f16) {
  2397. r[0] = normalize(m[0])
  2398. d0 := dot(r[0], r[1])
  2399. r[1] -= r[0] * d0
  2400. r[1] = normalize(r[1])
  2401. d1 := dot(r[1], r[2])
  2402. d0 = dot(r[0], r[2])
  2403. r[2] -= r[0]*d0 + r[1]*d1
  2404. r[2] = normalize(r[2])
  2405. return
  2406. }
  2407. @(require_results)
  2408. matrix3_orthonormalize_f32 :: proc "contextless" (m: Matrix3f32) -> (r: Matrix3f32) {
  2409. r[0] = normalize(m[0])
  2410. d0 := dot(r[0], r[1])
  2411. r[1] -= r[0] * d0
  2412. r[1] = normalize(r[1])
  2413. d1 := dot(r[1], r[2])
  2414. d0 = dot(r[0], r[2])
  2415. r[2] -= r[0]*d0 + r[1]*d1
  2416. r[2] = normalize(r[2])
  2417. return
  2418. }
  2419. @(require_results)
  2420. matrix3_orthonormalize_f64 :: proc "contextless" (m: Matrix3f64) -> (r: Matrix3f64) {
  2421. r[0] = normalize(m[0])
  2422. d0 := dot(r[0], r[1])
  2423. r[1] -= r[0] * d0
  2424. r[1] = normalize(r[1])
  2425. d1 := dot(r[1], r[2])
  2426. d0 = dot(r[0], r[2])
  2427. r[2] -= r[0]*d0 + r[1]*d1
  2428. r[2] = normalize(r[2])
  2429. return
  2430. }
  2431. matrix3_orthonormalize :: proc{
  2432. matrix3_orthonormalize_f16,
  2433. matrix3_orthonormalize_f32,
  2434. matrix3_orthonormalize_f64,
  2435. }
  2436. @(require_results)
  2437. vector3_orthonormalize_f16 :: proc "contextless" (x, y: Vector3f16) -> (z: Vector3f16) {
  2438. return normalize(x - y * dot(y, x))
  2439. }
  2440. @(require_results)
  2441. vector3_orthonormalize_f32 :: proc "contextless" (x, y: Vector3f32) -> (z: Vector3f32) {
  2442. return normalize(x - y * dot(y, x))
  2443. }
  2444. @(require_results)
  2445. vector3_orthonormalize_f64 :: proc "contextless" (x, y: Vector3f64) -> (z: Vector3f64) {
  2446. return normalize(x - y * dot(y, x))
  2447. }
  2448. vector3_orthonormalize :: proc{
  2449. vector3_orthonormalize_f16,
  2450. vector3_orthonormalize_f32,
  2451. vector3_orthonormalize_f64,
  2452. }
  2453. orthonormalize :: proc{
  2454. matrix2_orthonormalize_f16, matrix3_orthonormalize_f16, vector3_orthonormalize_f16,
  2455. matrix2_orthonormalize_f32, matrix3_orthonormalize_f32, vector3_orthonormalize_f32,
  2456. matrix2_orthonormalize_f64, matrix3_orthonormalize_f64, vector3_orthonormalize_f64,
  2457. }
  2458. @(require_results)
  2459. matrix4_orientation_f16 :: proc "contextless" (normal, up: Vector3f16) -> Matrix4f16 {
  2460. if all(equal(normal, up)) {
  2461. return MATRIX4F16_IDENTITY
  2462. }
  2463. rotation_axis := cross(up, normal)
  2464. angle := math.acos(dot(normal, up))
  2465. return matrix4_rotate(angle, rotation_axis)
  2466. }
  2467. @(require_results)
  2468. matrix4_orientation_f32 :: proc "contextless" (normal, up: Vector3f32) -> Matrix4f32 {
  2469. if all(equal(normal, up)) {
  2470. return MATRIX4F32_IDENTITY
  2471. }
  2472. rotation_axis := cross(up, normal)
  2473. angle := math.acos(dot(normal, up))
  2474. return matrix4_rotate(angle, rotation_axis)
  2475. }
  2476. @(require_results)
  2477. matrix4_orientation_f64 :: proc "contextless" (normal, up: Vector3f64) -> Matrix4f64 {
  2478. if all(equal(normal, up)) {
  2479. return MATRIX4F64_IDENTITY
  2480. }
  2481. rotation_axis := cross(up, normal)
  2482. angle := math.acos(dot(normal, up))
  2483. return matrix4_rotate(angle, rotation_axis)
  2484. }
  2485. matrix4_orientation :: proc{
  2486. matrix4_orientation_f16,
  2487. matrix4_orientation_f32,
  2488. matrix4_orientation_f64,
  2489. }
  2490. @(require_results)
  2491. euclidean_from_polar_f16 :: proc "contextless" (polar: Vector2f16) -> Vector3f16 {
  2492. latitude, longitude := polar.x, polar.y
  2493. cx, sx := math.cos(latitude), math.sin(latitude)
  2494. cy, sy := math.cos(longitude), math.sin(longitude)
  2495. return {
  2496. cx*sy,
  2497. sx,
  2498. cx*cy,
  2499. }
  2500. }
  2501. @(require_results)
  2502. euclidean_from_polar_f32 :: proc "contextless" (polar: Vector2f32) -> Vector3f32 {
  2503. latitude, longitude := polar.x, polar.y
  2504. cx, sx := math.cos(latitude), math.sin(latitude)
  2505. cy, sy := math.cos(longitude), math.sin(longitude)
  2506. return {
  2507. cx*sy,
  2508. sx,
  2509. cx*cy,
  2510. }
  2511. }
  2512. @(require_results)
  2513. euclidean_from_polar_f64 :: proc "contextless" (polar: Vector2f64) -> Vector3f64 {
  2514. latitude, longitude := polar.x, polar.y
  2515. cx, sx := math.cos(latitude), math.sin(latitude)
  2516. cy, sy := math.cos(longitude), math.sin(longitude)
  2517. return {
  2518. cx*sy,
  2519. sx,
  2520. cx*cy,
  2521. }
  2522. }
  2523. euclidean_from_polar :: proc{
  2524. euclidean_from_polar_f16,
  2525. euclidean_from_polar_f32,
  2526. euclidean_from_polar_f64,
  2527. }
  2528. @(require_results)
  2529. polar_from_euclidean_f16 :: proc "contextless" (euclidean: Vector3f16) -> Vector3f16 {
  2530. n := length(euclidean)
  2531. tmp := euclidean / n
  2532. xz_dist := math.sqrt(tmp.x*tmp.x + tmp.z*tmp.z)
  2533. return {
  2534. math.asin(tmp.y),
  2535. math.atan2(tmp.x, tmp.z),
  2536. xz_dist,
  2537. }
  2538. }
  2539. @(require_results)
  2540. polar_from_euclidean_f32 :: proc "contextless" (euclidean: Vector3f32) -> Vector3f32 {
  2541. n := length(euclidean)
  2542. tmp := euclidean / n
  2543. xz_dist := math.sqrt(tmp.x*tmp.x + tmp.z*tmp.z)
  2544. return {
  2545. math.asin(tmp.y),
  2546. math.atan2(tmp.x, tmp.z),
  2547. xz_dist,
  2548. }
  2549. }
  2550. @(require_results)
  2551. polar_from_euclidean_f64 :: proc "contextless" (euclidean: Vector3f64) -> Vector3f64 {
  2552. n := length(euclidean)
  2553. tmp := euclidean / n
  2554. xz_dist := math.sqrt(tmp.x*tmp.x + tmp.z*tmp.z)
  2555. return {
  2556. math.asin(tmp.y),
  2557. math.atan2(tmp.x, tmp.z),
  2558. xz_dist,
  2559. }
  2560. }
  2561. polar_from_euclidean :: proc{
  2562. polar_from_euclidean_f16,
  2563. polar_from_euclidean_f32,
  2564. polar_from_euclidean_f64,
  2565. }