matrix4.go 17 KB

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