matrix4.go 17 KB

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