matrix4.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math32
  5. // Matrix4 is 4x4 matrix organized internally as column matrix.
  6. type Matrix4 [16]float32
  7. // NewMatrix4 creates and returns a pointer to a new Matrix4
  8. // initialized as the identity matrix.
  9. func NewMatrix4() *Matrix4 {
  10. var mat Matrix4
  11. mat.Identity()
  12. return &mat
  13. }
  14. // Set sets all the elements of this matrix row by row starting at row1, column1,
  15. // row1, column2, row1, column3 and so forth.
  16. // Returns pointer to this updated Matrix.
  17. func (m *Matrix4) Set(n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 float32) *Matrix4 {
  18. m[0] = n11
  19. m[4] = n12
  20. m[8] = n13
  21. m[12] = n14
  22. m[1] = n21
  23. m[5] = n22
  24. m[9] = n23
  25. m[13] = n24
  26. m[2] = n31
  27. m[6] = n32
  28. m[10] = n33
  29. m[14] = n34
  30. m[3] = n41
  31. m[7] = n42
  32. m[11] = n43
  33. m[15] = n44
  34. return m
  35. }
  36. // Identity sets this matrix as the identity matrix.
  37. // Returns pointer to this updated matrix.
  38. func (m *Matrix4) Identity() *Matrix4 {
  39. *m = Matrix4{
  40. 1, 0, 0, 0,
  41. 0, 1, 0, 0,
  42. 0, 0, 1, 0,
  43. 0, 0, 0, 1,
  44. }
  45. return m
  46. }
  47. // Copy copies src matrix into this one.
  48. // Returns pointer to this updated matrix.
  49. func (m *Matrix4) Copy(src *Matrix4) *Matrix4 {
  50. *m = *src
  51. return m
  52. }
  53. // CopyPosition copies the position elements of the src matrix into this one.
  54. // Returns pointer to this updated matrix.
  55. func (m *Matrix4) CopyPosition(src *Matrix4) *Matrix4 {
  56. m[12] = src[12]
  57. m[13] = src[13]
  58. m[14] = src[14]
  59. return m
  60. }
  61. // ExtractBasis updates the specified vectors with the basis vectors of this matrix.
  62. // Returns pointer to this unchanged matrix.
  63. func (m *Matrix4) ExtractBasis(xAxis, yAxis, zAxis *Vector3) *Matrix4 {
  64. xAxis.Set(m[0], m[1], m[2])
  65. yAxis.Set(m[4], m[5], m[6])
  66. zAxis.Set(m[8], m[9], m[10])
  67. return m
  68. }
  69. // MakeBasis sets this matrix basis vectors from the specified vectors.
  70. // Returns pointer to this updated matrix.
  71. func (m *Matrix4) MakeBasis(xAxis, yAxis, zAxis *Vector3) *Matrix4 {
  72. m.Set(
  73. xAxis.X, yAxis.X, zAxis.X, 0,
  74. xAxis.Y, yAxis.Y, zAxis.Y, 0,
  75. xAxis.Z, yAxis.Z, zAxis.Z, 0,
  76. 0, 0, 0, 1,
  77. )
  78. return m
  79. }
  80. // ExtractRotation set this matrix as rotation matrix from the src transformation matrix.
  81. // Returns pointer to this updated matrix.
  82. func (m *Matrix4) ExtractRotation(src *Matrix4) *Matrix4 {
  83. var v1 Vector3
  84. scaleX := 1 / v1.Set(src[0], src[1], src[2]).Length()
  85. scaleY := 1 / v1.Set(src[4], src[5], src[6]).Length()
  86. scaleZ := 1 / v1.Set(src[8], src[9], src[10]).Length()
  87. m[0] = src[0] * scaleX
  88. m[1] = src[1] * scaleX
  89. m[2] = src[2] * scaleX
  90. m[4] = src[4] * scaleY
  91. m[5] = src[5] * scaleY
  92. m[6] = src[6] * scaleY
  93. m[8] = src[8] * scaleZ
  94. m[9] = src[9] * scaleZ
  95. m[10] = src[10] * scaleZ
  96. return m
  97. }
  98. // MakeRotationFromEuler set this a matrix as a rotation matrix from the specified euler angles.
  99. // Returns pointer to this updated matrix.
  100. func (m *Matrix4) MakeRotationFromEuler(euler *Vector3) *Matrix4 {
  101. x := euler.X
  102. y := euler.Y
  103. z := euler.Z
  104. a := Cos(x)
  105. b := Sin(x)
  106. c := Cos(y)
  107. d := Sin(y)
  108. e := Cos(z)
  109. f := Sin(z)
  110. ae := a * e
  111. af := a * f
  112. be := b * e
  113. bf := b * f
  114. m[0] = c * e
  115. m[4] = -c * f
  116. m[8] = d
  117. m[1] = af + be*d
  118. m[5] = ae - bf*d
  119. m[9] = -b * c
  120. m[2] = bf - ae*d
  121. m[6] = be + af*d
  122. m[10] = a * c
  123. // Last column
  124. m[3] = 0
  125. m[7] = 0
  126. m[11] = 0
  127. // Bottom row
  128. m[12] = 0
  129. m[13] = 0
  130. m[14] = 0
  131. m[15] = 1
  132. return m
  133. }
  134. // MakeRotationFromQuaternion sets this matrix as a rotation matrix from the specified quaternion.
  135. // Returns pointer to this updated matrix.
  136. func (m *Matrix4) MakeRotationFromQuaternion(q *Quaternion) *Matrix4 {
  137. x := q.x
  138. y := q.y
  139. z := q.z
  140. w := q.w
  141. x2 := x + x
  142. y2 := y + y
  143. z2 := z + z
  144. xx := x * x2
  145. xy := x * y2
  146. xz := x * z2
  147. yy := y * y2
  148. yz := y * z2
  149. zz := z * z2
  150. wx := w * x2
  151. wy := w * y2
  152. wz := w * z2
  153. m[0] = 1 - (yy + zz)
  154. m[4] = xy - wz
  155. m[8] = xz + wy
  156. m[1] = xy + wz
  157. m[5] = 1 - (xx + zz)
  158. m[9] = yz - wx
  159. m[2] = xz - wy
  160. m[6] = yz + wx
  161. m[10] = 1 - (xx + yy)
  162. // last column
  163. m[3] = 0
  164. m[7] = 0
  165. m[11] = 0
  166. // bottom row
  167. m[12] = 0
  168. m[13] = 0
  169. m[14] = 0
  170. m[15] = 1
  171. return m
  172. }
  173. // LookAt sets this matrix as view transform matrix with origin at eye,
  174. // looking at target and using the up vector.
  175. // Returns pointer to this updated matrix.
  176. func (m *Matrix4) LookAt(eye, target, up *Vector3) *Matrix4 {
  177. var f Vector3
  178. var s Vector3
  179. var u Vector3
  180. f.SubVectors(target, eye).Normalize()
  181. s.CrossVectors(&f, up).Normalize()
  182. u.CrossVectors(&s, &f)
  183. m[0] = s.X
  184. m[1] = u.X
  185. m[2] = -f.X
  186. m[3] = 0.0
  187. m[4] = s.Y
  188. m[5] = u.Y
  189. m[6] = -f.Y
  190. m[7] = 0.0
  191. m[8] = s.Z
  192. m[9] = u.Z
  193. m[10] = -f.Z
  194. m[11] = 0.0
  195. m[12] = -s.Dot(eye)
  196. m[13] = -u.Dot(eye)
  197. m[14] = f.Dot(eye)
  198. m[15] = 1.0
  199. return m
  200. }
  201. // Multiply multiply this matrix by the other matrix
  202. // Returns pointer to this updated matrix.
  203. func (m *Matrix4) Multiply(other *Matrix4) *Matrix4 {
  204. return m.MultiplyMatrices(m, other)
  205. }
  206. // MultiplyMatrices multiply matrix a by b storing the result in this matrix.
  207. // Returns pointer to this updated matrix.
  208. func (m *Matrix4) MultiplyMatrices(a, b *Matrix4) *Matrix4 {
  209. a11 := a[0]
  210. a12 := a[4]
  211. a13 := a[8]
  212. a14 := a[12]
  213. a21 := a[1]
  214. a22 := a[5]
  215. a23 := a[9]
  216. a24 := a[13]
  217. a31 := a[2]
  218. a32 := a[6]
  219. a33 := a[10]
  220. a34 := a[14]
  221. a41 := a[3]
  222. a42 := a[7]
  223. a43 := a[11]
  224. a44 := a[15]
  225. b11 := b[0]
  226. b12 := b[4]
  227. b13 := b[8]
  228. b14 := b[12]
  229. b21 := b[1]
  230. b22 := b[5]
  231. b23 := b[9]
  232. b24 := b[13]
  233. b31 := b[2]
  234. b32 := b[6]
  235. b33 := b[10]
  236. b34 := b[14]
  237. b41 := b[3]
  238. b42 := b[7]
  239. b43 := b[11]
  240. b44 := b[15]
  241. m[0] = a11*b11 + a12*b21 + a13*b31 + a14*b41
  242. m[4] = a11*b12 + a12*b22 + a13*b32 + a14*b42
  243. m[8] = a11*b13 + a12*b23 + a13*b33 + a14*b43
  244. m[12] = a11*b14 + a12*b24 + a13*b34 + a14*b44
  245. m[1] = a21*b11 + a22*b21 + a23*b31 + a24*b41
  246. m[5] = a21*b12 + a22*b22 + a23*b32 + a24*b42
  247. m[9] = a21*b13 + a22*b23 + a23*b33 + a24*b43
  248. m[13] = a21*b14 + a22*b24 + a23*b34 + a24*b44
  249. m[2] = a31*b11 + a32*b21 + a33*b31 + a34*b41
  250. m[6] = a31*b12 + a32*b22 + a33*b32 + a34*b42
  251. m[10] = a31*b13 + a32*b23 + a33*b33 + a34*b43
  252. m[14] = a31*b14 + a32*b24 + a33*b34 + a34*b44
  253. m[3] = a41*b11 + a42*b21 + a43*b31 + a44*b41
  254. m[7] = a41*b12 + a42*b22 + a43*b32 + a44*b42
  255. m[11] = a41*b13 + a42*b23 + a43*b33 + a44*b43
  256. m[15] = a41*b14 + a42*b24 + a43*b34 + a44*b44
  257. return m
  258. }
  259. // MultiplyScalar multiplies each element of this matrix by the specified scalar.
  260. // Returns pointer to this updated matrix.
  261. func (m *Matrix4) MultiplyScalar(s float32) *Matrix4 {
  262. m[0] *= s
  263. m[4] *= s
  264. m[8] *= s
  265. m[12] *= s
  266. m[1] *= s
  267. m[5] *= s
  268. m[9] *= s
  269. m[13] *= s
  270. m[2] *= s
  271. m[6] *= s
  272. m[10] *= s
  273. m[14] *= s
  274. m[3] *= s
  275. m[7] *= s
  276. m[11] *= s
  277. m[15] *= s
  278. return m
  279. }
  280. // ApplyToVector3Array multiplies length vectors in the array starting at offset by this matrix.
  281. // Returns pointer to the updated array.
  282. // This matrix is unchanged.
  283. func (m *Matrix4) ApplyToVector3Array(array []float32, offset int, length int) []float32 {
  284. var v1 Vector3
  285. j := offset
  286. for i := 0; i < length; i += 3 {
  287. v1.X = array[j]
  288. v1.Y = array[j+1]
  289. v1.Z = array[j+2]
  290. v1.ApplyMatrix4(m)
  291. array[j] = v1.X
  292. array[j+1] = v1.Y
  293. array[j+2] = v1.Z
  294. j += 3
  295. }
  296. return array
  297. }
  298. // Determinant calculates and returns the determinat of this matrix.
  299. func (m *Matrix4) Determinant() float32 {
  300. n11 := m[0]
  301. n12 := m[4]
  302. n13 := m[8]
  303. n14 := m[12]
  304. n21 := m[1]
  305. n22 := m[5]
  306. n23 := m[9]
  307. n24 := m[13]
  308. n31 := m[2]
  309. n32 := m[6]
  310. n33 := m[10]
  311. n34 := m[14]
  312. n41 := m[3]
  313. n42 := m[7]
  314. n43 := m[11]
  315. n44 := m[15]
  316. return n41*(+n14*n23*n32-n13*n24*n32-n14*n22*n33+n12*n24*n33+n13*n22*n34-n12*n23*n34) +
  317. n42*(+n11*n23*n34-n11*n24*n33+n14*n21*n33-n13*n21*n34+n13*n24*n31-n14*n23*n31) +
  318. n43*(+n11*n24*n32-n11*n22*n34-n14*n21*n32+n12*n21*n34+n14*n22*n31-n12*n24*n31) +
  319. n44*(-n13*n22*n31-n11*n23*n32+n11*n22*n33+n13*n21*n32-n12*n21*n33+n12*n23*n31)
  320. }
  321. // Transpose transposes this matrix.
  322. // Returns pointer to this updated matrix.
  323. func (m *Matrix4) Transpose() *Matrix4 {
  324. var tmp float32
  325. tmp = m[1]
  326. m[1] = m[4]
  327. m[4] = tmp
  328. tmp = m[2]
  329. m[2] = m[8]
  330. m[8] = tmp
  331. tmp = m[6]
  332. m[6] = m[9]
  333. m[9] = tmp
  334. tmp = m[3]
  335. m[3] = m[12]
  336. m[12] = tmp
  337. tmp = m[7]
  338. m[7] = m[13]
  339. m[13] = tmp
  340. tmp = m[11]
  341. m[11] = m[14]
  342. m[14] = tmp
  343. return m
  344. }
  345. // SetPosition sets this transformation matrix position fields from the specified vector v.
  346. // Returns pointer to this updated matrix.
  347. func (m *Matrix4) SetPosition(v *Vector3) *Matrix4 {
  348. m[12] = v.X
  349. m[13] = v.Y
  350. m[14] = v.Z
  351. return m
  352. }
  353. // GetInverse sets this matrix to the inverse of the src matrix and
  354. // returns pointer to this updated matrix.
  355. // If the src matrix cannot be inverted returns nil and
  356. // sets this matrix to the identity matrix.
  357. func (m *Matrix4) GetInverse(src *Matrix4) *Matrix4 {
  358. n11 := src[0]
  359. n12 := src[4]
  360. n13 := src[8]
  361. n14 := src[12]
  362. n21 := src[1]
  363. n22 := src[5]
  364. n23 := src[9]
  365. n24 := src[13]
  366. n31 := src[2]
  367. n32 := src[6]
  368. n33 := src[10]
  369. n34 := src[14]
  370. n41 := src[3]
  371. n42 := src[7]
  372. n43 := src[11]
  373. n44 := src[15]
  374. m[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44
  375. m[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44
  376. m[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44
  377. m[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34
  378. m[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44
  379. m[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44
  380. m[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44
  381. m[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34
  382. m[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44
  383. m[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44
  384. m[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44
  385. m[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34
  386. m[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43
  387. m[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43
  388. m[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43
  389. m[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33
  390. det := n11*m[0] + n21*m[4] + n31*m[8] + n41*m[12]
  391. if det == 0 {
  392. m.Identity()
  393. return nil
  394. }
  395. m.MultiplyScalar(1.0 / det)
  396. return m
  397. }
  398. // Scale multiply the first column of this matrix by the vector X component,
  399. // the second column by the vector Y component and the third column by
  400. // the vector Z component. The matrix fourth column is unchanged.
  401. // Returns pointer to this updated matrix.
  402. func (m *Matrix4) Scale(v *Vector3) *Matrix4 {
  403. m[0] *= v.X
  404. m[4] *= v.Y
  405. m[8] *= v.Z
  406. m[1] *= v.X
  407. m[5] *= v.Y
  408. m[9] *= v.Z
  409. m[2] *= v.X
  410. m[6] *= v.Y
  411. m[10] *= v.Z
  412. m[3] *= v.X
  413. m[7] *= v.Y
  414. m[11] *= v.Z
  415. return m
  416. }
  417. // GetMaxScaleOnAxis returns the maximum scale value of the 3 axes.
  418. func (m *Matrix4) GetMaxScaleOnAxis() float32 {
  419. scaleXSq := m[0]*m[0] + m[1]*m[1] + m[2]*m[2]
  420. scaleYSq := m[4]*m[4] + m[5]*m[5] + m[6]*m[6]
  421. scaleZSq := m[8]*m[8] + m[9]*m[9] + m[10]*m[10]
  422. return Sqrt(Max(scaleXSq, Max(scaleYSq, scaleZSq)))
  423. }
  424. // MakeTranslation sets this matrix to a translation matrix from the specified x, y and z values.
  425. // Returns pointer to this updated matrix.
  426. func (m *Matrix4) MakeTranslation(x, y, z float32) *Matrix4 {
  427. m.Set(
  428. 1, 0, 0, x,
  429. 0, 1, 0, y,
  430. 0, 0, 1, z,
  431. 0, 0, 0, 1,
  432. )
  433. return m
  434. }
  435. // MakeRotationX sets this matrix to a rotation matrix of angle theta around the X axis.
  436. // Returns pointer to this updated matrix.
  437. func (m *Matrix4) MakeRotationX(theta float32) *Matrix4 {
  438. c := Cos(theta)
  439. s := Sin(theta)
  440. m.Set(
  441. 1, 0, 0, 0,
  442. 0, c, -s, 0,
  443. 0, s, c, 0,
  444. 0, 0, 0, 1,
  445. )
  446. return m
  447. }
  448. // MakeRotationY sets this matrix to a rotation matrix of angle theta around the Y axis.
  449. // Returns pointer to this updated matrix.
  450. func (m *Matrix4) MakeRotationY(theta float32) *Matrix4 {
  451. c := Cos(theta)
  452. s := Sin(theta)
  453. m.Set(
  454. c, 0, s, 0,
  455. 0, 1, 0, 0,
  456. -s, 0, c, 0,
  457. 0, 0, 0, 1,
  458. )
  459. return m
  460. }
  461. // MakeRotationZ sets this matrix to a rotation matrix of angle theta around the Z axis.
  462. // Returns pointer to this updated matrix.
  463. func (m *Matrix4) MakeRotationZ(theta float32) *Matrix4 {
  464. c := Cos(theta)
  465. s := Sin(theta)
  466. m.Set(
  467. c, -s, 0, 0,
  468. s, c, 0, 0,
  469. 0, 0, 1, 0,
  470. 0, 0, 0, 1,
  471. )
  472. return m
  473. }
  474. // MakeRotationAxis sets this matrix to a rotation matrix of the specified angle around the specified axis.
  475. // Returns pointer to this updated matrix.
  476. func (m *Matrix4) MakeRotationAxis(axis *Vector3, angle float32) *Matrix4 {
  477. c := Cos(angle)
  478. s := Sin(angle)
  479. t := 1 - c
  480. x := axis.X
  481. y := axis.Y
  482. z := axis.Z
  483. tx := t * x
  484. ty := t * y
  485. m.Set(
  486. tx*x+c, tx*y-s*z, tx*z+s*y, 0,
  487. tx*y+s*z, ty*y+c, ty*z-s*x, 0,
  488. tx*z-s*y, ty*z+s*x, t*z*z+c, 0,
  489. 0, 0, 0, 1,
  490. )
  491. return m
  492. }
  493. // MakeScale sets this matrix to a scale transformation matrix using the specified x, y and z values.
  494. // Returns pointer to this updated matrix.
  495. func (m *Matrix4) MakeScale(x, y, z float32) *Matrix4 {
  496. m.Set(
  497. x, 0, 0, 0,
  498. 0, y, 0, 0,
  499. 0, 0, z, 0,
  500. 0, 0, 0, 1,
  501. )
  502. return m
  503. }
  504. // Compose sets this matrix to a transformation matrix for the specified position,
  505. // rotation specified by the quaternion and scale.
  506. // Returns pointer to this updated matrix.
  507. func (m *Matrix4) Compose(position *Vector3, quaternion *Quaternion, scale *Vector3) *Matrix4 {
  508. m.MakeRotationFromQuaternion(quaternion)
  509. m.Scale(scale)
  510. m.SetPosition(position)
  511. return m
  512. }
  513. // Decompose updates the position vector, quaternion and scale from this transformation matrix.
  514. // Returns pointer to this unchanged matrix.
  515. func (m *Matrix4) Decompose(position *Vector3, quaternion *Quaternion, scale *Vector3) *Matrix4 {
  516. var vector Vector3
  517. var matrix = *m
  518. sx := vector.Set(m[0], m[1], m[2]).Length()
  519. sy := vector.Set(m[4], m[5], m[6]).Length()
  520. sz := vector.Set(m[8], m[9], m[10]).Length()
  521. // If determinant is negative, we need to invert one scale
  522. det := m.Determinant()
  523. if det < 0 {
  524. sx = -sx
  525. }
  526. position.X = m[12]
  527. position.Y = m[13]
  528. position.Z = m[14]
  529. // Scale the rotation part
  530. invSX := 1 / sx
  531. invSY := 1 / sy
  532. invSZ := 1 / sz
  533. matrix[0] *= invSX
  534. matrix[1] *= invSX
  535. matrix[2] *= invSX
  536. matrix[4] *= invSY
  537. matrix[5] *= invSY
  538. matrix[6] *= invSY
  539. matrix[8] *= invSZ
  540. matrix[9] *= invSZ
  541. matrix[10] *= invSZ
  542. quaternion.SetFromRotationMatrix(&matrix)
  543. scale.X = sx
  544. scale.Y = sy
  545. scale.Z = sz
  546. return m
  547. }
  548. // MakeFrustum sets this matrix to a projection frustum matrix bounded by the specified planes.
  549. // Returns pointer to this updated matrix.
  550. func (m *Matrix4) MakeFrustum(left, right, bottom, top, near, far float32) *Matrix4 {
  551. m[0] = 2 * near / (right - left)
  552. m[1] = 0
  553. m[2] = 0
  554. m[3] = 0
  555. m[4] = 0
  556. m[5] = 2 * near / (top - bottom)
  557. m[6] = 0
  558. m[7] = 0
  559. m[8] = (right + left) / (right - left)
  560. m[9] = (top + bottom) / (top - bottom)
  561. m[10] = -(far + near) / (far - near)
  562. m[11] = -1
  563. m[12] = 0
  564. m[13] = 0
  565. m[14] = -(2 * far * near) / (far - near)
  566. m[15] = 0
  567. return m
  568. }
  569. // MakePerspective sets this matrix to a perspective projection matrix
  570. // with the specified field of view in degrees,
  571. // aspect ratio (width/height) and near and far planes.
  572. // Returns pointer to this updated matrix.
  573. func (m *Matrix4) MakePerspective(fov, aspect, near, far float32) *Matrix4 {
  574. ymax := near * Tan(DegToRad(fov*0.5))
  575. ymin := -ymax
  576. xmin := ymin * aspect
  577. xmax := ymax * aspect
  578. return m.MakeFrustum(xmin, xmax, ymin, ymax, near, far)
  579. }
  580. // MakeOrthographic sets this matrix to an orthographic projection matrix
  581. // bounded by the specified planes.
  582. // Returns pointer to this updated matrix.
  583. func (m *Matrix4) MakeOrthographic(left, right, top, bottom, near, far float32) *Matrix4 {
  584. w := right - left
  585. h := top - bottom
  586. p := far - near
  587. x := (right + left) / w
  588. y := (top + bottom) / h
  589. z := (far + near) / p
  590. m[0] = 2 / w
  591. m[4] = 0
  592. m[8] = 0
  593. m[12] = -x
  594. m[1] = 0
  595. m[5] = 2 / h
  596. m[9] = 0
  597. m[13] = -y
  598. m[2] = 0
  599. m[6] = 0
  600. m[10] = -2 / p
  601. m[14] = -z
  602. m[3] = 0
  603. m[7] = 0
  604. m[11] = 0
  605. m[15] = 1
  606. return m
  607. }
  608. // FromArray set this matrix elements from the array starting at offset.
  609. // Returns pointer to this updated matrix.
  610. func (m *Matrix4) FromArray(array []float32, offset int) *Matrix4 {
  611. copy(m[:], array[offset:offset+16])
  612. return m
  613. }
  614. // ToArray copies this matrix elements to array starting at offset.
  615. // Returns pointer to the updated array.
  616. func (m *Matrix4) ToArray(array []float32, offset int) []float32 {
  617. copy(array[offset:], m[:])
  618. return array
  619. }
  620. // Clone creates and returns a pointer to a copy of this matrix.
  621. func (m *Matrix4) Clone() *Matrix4 {
  622. var cloned Matrix4
  623. cloned = *m
  624. return &cloned
  625. }