Matrix4.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. #region --- License ---
  2. /*
  3. Copyright (c) 2006 - 2008 The Open Toolkit library.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #endregion
  21. using System;
  22. using System.Runtime.InteropServices;
  23. namespace AtomicEngine
  24. {
  25. /// <summary>
  26. /// Represents a 4x4 Matrix
  27. /// </summary>
  28. [StructLayout(LayoutKind.Sequential)]
  29. public struct Matrix4 : IEquatable<Matrix4>
  30. {
  31. #region Fields
  32. /// <summary>
  33. /// Top row of the matrix
  34. /// </summary>
  35. public Vector4 Row0;
  36. /// <summary>
  37. /// 2nd row of the matrix
  38. /// </summary>
  39. public Vector4 Row1;
  40. /// <summary>
  41. /// 3rd row of the matrix
  42. /// </summary>
  43. public Vector4 Row2;
  44. /// <summary>
  45. /// Bottom row of the matrix
  46. /// </summary>
  47. public Vector4 Row3;
  48. /// <summary>
  49. /// The identity matrix
  50. /// </summary>
  51. public static Matrix4 Identity = new Matrix4(Vector4.UnitX, Vector4.UnitY, Vector4.UnitZ, Vector4.UnitW);
  52. #endregion
  53. #region Constructors
  54. /// <summary>
  55. /// Constructs a new instance.
  56. /// </summary>
  57. /// <param name="row0">Top row of the matrix</param>
  58. /// <param name="row1">Second row of the matrix</param>
  59. /// <param name="row2">Third row of the matrix</param>
  60. /// <param name="row3">Bottom row of the matrix</param>
  61. public Matrix4(Vector4 row0, Vector4 row1, Vector4 row2, Vector4 row3)
  62. {
  63. Row0 = row0;
  64. Row1 = row1;
  65. Row2 = row2;
  66. Row3 = row3;
  67. }
  68. /// <summary>
  69. /// Constructs a new instance.
  70. /// </summary>
  71. /// <param name="m00">First item of the first row of the matrix.</param>
  72. /// <param name="m01">Second item of the first row of the matrix.</param>
  73. /// <param name="m02">Third item of the first row of the matrix.</param>
  74. /// <param name="m03">Fourth item of the first row of the matrix.</param>
  75. /// <param name="m10">First item of the second row of the matrix.</param>
  76. /// <param name="m11">Second item of the second row of the matrix.</param>
  77. /// <param name="m12">Third item of the second row of the matrix.</param>
  78. /// <param name="m13">Fourth item of the second row of the matrix.</param>
  79. /// <param name="m20">First item of the third row of the matrix.</param>
  80. /// <param name="m21">Second item of the third row of the matrix.</param>
  81. /// <param name="m22">Third item of the third row of the matrix.</param>
  82. /// <param name="m23">First item of the third row of the matrix.</param>
  83. /// <param name="m30">Fourth item of the fourth row of the matrix.</param>
  84. /// <param name="m31">Second item of the fourth row of the matrix.</param>
  85. /// <param name="m32">Third item of the fourth row of the matrix.</param>
  86. /// <param name="m33">Fourth item of the fourth row of the matrix.</param>
  87. public Matrix4(
  88. float m00, float m01, float m02, float m03,
  89. float m10, float m11, float m12, float m13,
  90. float m20, float m21, float m22, float m23,
  91. float m30, float m31, float m32, float m33)
  92. {
  93. Row0 = new Vector4(m00, m01, m02, m03);
  94. Row1 = new Vector4(m10, m11, m12, m13);
  95. Row2 = new Vector4(m20, m21, m22, m23);
  96. Row3 = new Vector4(m30, m31, m32, m33);
  97. }
  98. #endregion
  99. #region Public Members
  100. #region Properties
  101. /// <summary>
  102. /// The determinant of this matrix
  103. /// </summary>
  104. public float Determinant
  105. {
  106. get
  107. {
  108. return
  109. Row0.X * Row1.Y * Row2.Z * Row3.W - Row0.X * Row1.Y * Row2.W * Row3.Z + Row0.X * Row1.Z * Row2.W * Row3.Y - Row0.X * Row1.Z * Row2.Y * Row3.W
  110. + Row0.X * Row1.W * Row2.Y * Row3.Z - Row0.X * Row1.W * Row2.Z * Row3.Y - Row0.Y * Row1.Z * Row2.W * Row3.X + Row0.Y * Row1.Z * Row2.X * Row3.W
  111. - Row0.Y * Row1.W * Row2.X * Row3.Z + Row0.Y * Row1.W * Row2.Z * Row3.X - Row0.Y * Row1.X * Row2.Z * Row3.W + Row0.Y * Row1.X * Row2.W * Row3.Z
  112. + Row0.Z * Row1.W * Row2.X * Row3.Y - Row0.Z * Row1.W * Row2.Y * Row3.X + Row0.Z * Row1.X * Row2.Y * Row3.W - Row0.Z * Row1.X * Row2.W * Row3.Y
  113. + Row0.Z * Row1.Y * Row2.W * Row3.X - Row0.Z * Row1.Y * Row2.X * Row3.W - Row0.W * Row1.X * Row2.Y * Row3.Z + Row0.W * Row1.X * Row2.Z * Row3.Y
  114. - Row0.W * Row1.Y * Row2.Z * Row3.X + Row0.W * Row1.Y * Row2.X * Row3.Z - Row0.W * Row1.Z * Row2.X * Row3.Y + Row0.W * Row1.Z * Row2.Y * Row3.X;
  115. }
  116. }
  117. /// <summary>
  118. /// The first column of this matrix
  119. /// </summary>
  120. public Vector4 Column0
  121. {
  122. get { return new Vector4(Row0.X, Row1.X, Row2.X, Row3.X); }
  123. }
  124. /// <summary>
  125. /// The second column of this matrix
  126. /// </summary>
  127. public Vector4 Column1
  128. {
  129. get { return new Vector4(Row0.Y, Row1.Y, Row2.Y, Row3.Y); }
  130. }
  131. /// <summary>
  132. /// The third column of this matrix
  133. /// </summary>
  134. public Vector4 Column2
  135. {
  136. get { return new Vector4(Row0.Z, Row1.Z, Row2.Z, Row3.Z); }
  137. }
  138. /// <summary>
  139. /// The fourth column of this matrix
  140. /// </summary>
  141. public Vector4 Column3
  142. {
  143. get { return new Vector4(Row0.W, Row1.W, Row2.W, Row3.W); }
  144. }
  145. /// <summary>
  146. /// Gets or sets the value at row 1, column 1 of this instance.
  147. /// </summary>
  148. public float M11 { get { return Row0.X; } set { Row0.X = value; } }
  149. /// <summary>
  150. /// Gets or sets the value at row 1, column 2 of this instance.
  151. /// </summary>
  152. public float M12 { get { return Row0.Y; } set { Row0.Y = value; } }
  153. /// <summary>
  154. /// Gets or sets the value at row 1, column 3 of this instance.
  155. /// </summary>
  156. public float M13 { get { return Row0.Z; } set { Row0.Z = value; } }
  157. /// <summary>
  158. /// Gets or sets the value at row 1, column 4 of this instance.
  159. /// </summary>
  160. public float M14 { get { return Row0.W; } set { Row0.W = value; } }
  161. /// <summary>
  162. /// Gets or sets the value at row 2, column 1 of this instance.
  163. /// </summary>
  164. public float M21 { get { return Row1.X; } set { Row1.X = value; } }
  165. /// <summary>
  166. /// Gets or sets the value at row 2, column 2 of this instance.
  167. /// </summary>
  168. public float M22 { get { return Row1.Y; } set { Row1.Y = value; } }
  169. /// <summary>
  170. /// Gets or sets the value at row 2, column 3 of this instance.
  171. /// </summary>
  172. public float M23 { get { return Row1.Z; } set { Row1.Z = value; } }
  173. /// <summary>
  174. /// Gets or sets the value at row 2, column 4 of this instance.
  175. /// </summary>
  176. public float M24 { get { return Row1.W; } set { Row1.W = value; } }
  177. /// <summary>
  178. /// Gets or sets the value at row 3, column 1 of this instance.
  179. /// </summary>
  180. public float M31 { get { return Row2.X; } set { Row2.X = value; } }
  181. /// <summary>
  182. /// Gets or sets the value at row 3, column 2 of this instance.
  183. /// </summary>
  184. public float M32 { get { return Row2.Y; } set { Row2.Y = value; } }
  185. /// <summary>
  186. /// Gets or sets the value at row 3, column 3 of this instance.
  187. /// </summary>
  188. public float M33 { get { return Row2.Z; } set { Row2.Z = value; } }
  189. /// <summary>
  190. /// Gets or sets the value at row 3, column 4 of this instance.
  191. /// </summary>
  192. public float M34 { get { return Row2.W; } set { Row2.W = value; } }
  193. /// <summary>
  194. /// Gets or sets the value at row 4, column 1 of this instance.
  195. /// </summary>
  196. public float M41 { get { return Row3.X; } set { Row3.X = value; } }
  197. /// <summary>
  198. /// Gets or sets the value at row 4, column 2 of this instance.
  199. /// </summary>
  200. public float M42 { get { return Row3.Y; } set { Row3.Y = value; } }
  201. /// <summary>
  202. /// Gets or sets the value at row 4, column 3 of this instance.
  203. /// </summary>
  204. public float M43 { get { return Row3.Z; } set { Row3.Z = value; } }
  205. /// <summary>
  206. /// Gets or sets the value at row 4, column 4 of this instance.
  207. /// </summary>
  208. public float M44 { get { return Row3.W; } set { Row3.W = value; } }
  209. #endregion
  210. #region Instance
  211. #region public void Invert()
  212. /// <summary>
  213. /// Converts this instance into its inverse.
  214. /// </summary>
  215. public void Invert()
  216. {
  217. this = Matrix4.Invert(this);
  218. }
  219. #endregion
  220. #region public void Transpose()
  221. /// <summary>
  222. /// Converts this instance into its transpose.
  223. /// </summary>
  224. public void Transpose()
  225. {
  226. this = Matrix4.Transpose(this);
  227. }
  228. #endregion
  229. #endregion
  230. #region Static
  231. #region CreateFromAxisAngle
  232. /// <summary>
  233. /// Build a rotation matrix from the specified axis/angle rotation.
  234. /// </summary>
  235. /// <param name="axis">The axis to rotate about.</param>
  236. /// <param name="angle">Angle in radians to rotate counter-clockwise (looking in the direction of the given axis).</param>
  237. /// <param name="result">A matrix instance.</param>
  238. public static void CreateFromAxisAngle(Vector3 axis, float angle, out Matrix4 result)
  239. {
  240. float cos = (float)System.Math.Cos(-angle);
  241. float sin = (float)System.Math.Sin(-angle);
  242. float t = 1.0f - cos;
  243. axis.Normalize();
  244. result = new Matrix4(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0f,
  245. t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0f,
  246. t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0f,
  247. 0, 0, 0, 1);
  248. }
  249. /// <summary>
  250. /// Build a rotation matrix from the specified axis/angle rotation.
  251. /// </summary>
  252. /// <param name="axis">The axis to rotate about.</param>
  253. /// <param name="angle">Angle in radians to rotate counter-clockwise (looking in the direction of the given axis).</param>
  254. /// <returns>A matrix instance.</returns>
  255. public static Matrix4 CreateFromAxisAngle(Vector3 axis, float angle)
  256. {
  257. Matrix4 result;
  258. CreateFromAxisAngle(axis, angle, out result);
  259. return result;
  260. }
  261. #endregion
  262. #region CreateRotation[XYZ]
  263. /// <summary>
  264. /// Builds a rotation matrix for a rotation around the x-axis.
  265. /// </summary>
  266. /// <param name="angle">The counter-clockwise angle in radians.</param>
  267. /// <param name="result">The resulting Matrix4 instance.</param>
  268. public static void CreateRotationX(float angle, out Matrix4 result)
  269. {
  270. float cos = (float)System.Math.Cos(angle);
  271. float sin = (float)System.Math.Sin(angle);
  272. result.Row0 = Vector4.UnitX;
  273. result.Row1 = new Vector4(0.0f, cos, sin, 0.0f);
  274. result.Row2 = new Vector4(0.0f, -sin, cos, 0.0f);
  275. result.Row3 = Vector4.UnitW;
  276. }
  277. /// <summary>
  278. /// Builds a rotation matrix for a rotation around the x-axis.
  279. /// </summary>
  280. /// <param name="angle">The counter-clockwise angle in radians.</param>
  281. /// <returns>The resulting Matrix4 instance.</returns>
  282. public static Matrix4 CreateRotationX(float angle)
  283. {
  284. Matrix4 result;
  285. CreateRotationX(angle, out result);
  286. return result;
  287. }
  288. /// <summary>
  289. /// Builds a rotation matrix for a rotation around the y-axis.
  290. /// </summary>
  291. /// <param name="angle">The counter-clockwise angle in radians.</param>
  292. /// <param name="result">The resulting Matrix4 instance.</param>
  293. public static void CreateRotationY(float angle, out Matrix4 result)
  294. {
  295. float cos = (float)System.Math.Cos(angle);
  296. float sin = (float)System.Math.Sin(angle);
  297. result.Row0 = new Vector4(cos, 0.0f, -sin, 0.0f);
  298. result.Row1 = Vector4.UnitY;
  299. result.Row2 = new Vector4(sin, 0.0f, cos, 0.0f);
  300. result.Row3 = Vector4.UnitW;
  301. }
  302. /// <summary>
  303. /// Builds a rotation matrix for a rotation around the y-axis.
  304. /// </summary>
  305. /// <param name="angle">The counter-clockwise angle in radians.</param>
  306. /// <returns>The resulting Matrix4 instance.</returns>
  307. public static Matrix4 CreateRotationY(float angle)
  308. {
  309. Matrix4 result;
  310. CreateRotationY(angle, out result);
  311. return result;
  312. }
  313. /// <summary>
  314. /// Builds a rotation matrix for a rotation around the z-axis.
  315. /// </summary>
  316. /// <param name="angle">The counter-clockwise angle in radians.</param>
  317. /// <param name="result">The resulting Matrix4 instance.</param>
  318. public static void CreateRotationZ(float angle, out Matrix4 result)
  319. {
  320. float cos = (float)System.Math.Cos(angle);
  321. float sin = (float)System.Math.Sin(angle);
  322. result.Row0 = new Vector4(cos, sin, 0.0f, 0.0f);
  323. result.Row1 = new Vector4(-sin, cos, 0.0f, 0.0f);
  324. result.Row2 = Vector4.UnitZ;
  325. result.Row3 = Vector4.UnitW;
  326. }
  327. /// <summary>
  328. /// Builds a rotation matrix for a rotation around the z-axis.
  329. /// </summary>
  330. /// <param name="angle">The counter-clockwise angle in radians.</param>
  331. /// <returns>The resulting Matrix4 instance.</returns>
  332. public static Matrix4 CreateRotationZ(float angle)
  333. {
  334. Matrix4 result;
  335. CreateRotationZ(angle, out result);
  336. return result;
  337. }
  338. #endregion
  339. #region CreateTranslation
  340. /// <summary>
  341. /// Creates a translation matrix.
  342. /// </summary>
  343. /// <param name="x">X translation.</param>
  344. /// <param name="y">Y translation.</param>
  345. /// <param name="z">Z translation.</param>
  346. /// <param name="result">The resulting Matrix4 instance.</param>
  347. public static void CreateTranslation(float x, float y, float z, out Matrix4 result)
  348. {
  349. result = Identity;
  350. result.Row3 = new Vector4(x, y, z, 1);
  351. }
  352. /// <summary>
  353. /// Creates a translation matrix.
  354. /// </summary>
  355. /// <param name="vector">The translation vector.</param>
  356. /// <param name="result">The resulting Matrix4 instance.</param>
  357. public static void CreateTranslation(ref Vector3 vector, out Matrix4 result)
  358. {
  359. result = Identity;
  360. result.Row3 = new Vector4(vector.X, vector.Y, vector.Z, 1);
  361. }
  362. /// <summary>
  363. /// Creates a translation matrix.
  364. /// </summary>
  365. /// <param name="x">X translation.</param>
  366. /// <param name="y">Y translation.</param>
  367. /// <param name="z">Z translation.</param>
  368. /// <returns>The resulting Matrix4 instance.</returns>
  369. public static Matrix4 CreateTranslation(float x, float y, float z)
  370. {
  371. Matrix4 result;
  372. CreateTranslation(x, y, z, out result);
  373. return result;
  374. }
  375. /// <summary>
  376. /// Creates a translation matrix.
  377. /// </summary>
  378. /// <param name="vector">The translation vector.</param>
  379. /// <returns>The resulting Matrix4 instance.</returns>
  380. public static Matrix4 CreateTranslation(Vector3 vector)
  381. {
  382. Matrix4 result;
  383. CreateTranslation(vector.X, vector.Y, vector.Z, out result);
  384. return result;
  385. }
  386. #endregion
  387. #region CreateOrthographic
  388. /// <summary>
  389. /// Creates an orthographic projection matrix.
  390. /// </summary>
  391. /// <param name="width">The width of the projection volume.</param>
  392. /// <param name="height">The height of the projection volume.</param>
  393. /// <param name="zNear">The near edge of the projection volume.</param>
  394. /// <param name="zFar">The far edge of the projection volume.</param>
  395. /// <param name="result">The resulting Matrix4 instance.</param>
  396. public static void CreateOrthographic(float width, float height, float zNear, float zFar, out Matrix4 result)
  397. {
  398. CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result);
  399. }
  400. /// <summary>
  401. /// Creates an orthographic projection matrix.
  402. /// </summary>
  403. /// <param name="width">The width of the projection volume.</param>
  404. /// <param name="height">The height of the projection volume.</param>
  405. /// <param name="zNear">The near edge of the projection volume.</param>
  406. /// <param name="zFar">The far edge of the projection volume.</param>
  407. /// <rereturns>The resulting Matrix4 instance.</rereturns>
  408. public static Matrix4 CreateOrthographic(float width, float height, float zNear, float zFar)
  409. {
  410. Matrix4 result;
  411. CreateOrthographicOffCenter(-width / 2, width / 2, -height / 2, height / 2, zNear, zFar, out result);
  412. return result;
  413. }
  414. #endregion
  415. #region CreateOrthographicOffCenter
  416. /// <summary>
  417. /// Creates an orthographic projection matrix.
  418. /// </summary>
  419. /// <param name="left">The left edge of the projection volume.</param>
  420. /// <param name="right">The right edge of the projection volume.</param>
  421. /// <param name="bottom">The bottom edge of the projection volume.</param>
  422. /// <param name="top">The top edge of the projection volume.</param>
  423. /// <param name="zNear">The near edge of the projection volume.</param>
  424. /// <param name="zFar">The far edge of the projection volume.</param>
  425. /// <param name="result">The resulting Matrix4 instance.</param>
  426. public static void CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, out Matrix4 result)
  427. {
  428. result = new Matrix4();
  429. float invRL = 1 / (right - left);
  430. float invTB = 1 / (top - bottom);
  431. float invFN = 1 / (zFar - zNear);
  432. result.M11 = 2 * invRL;
  433. result.M22 = 2 * invTB;
  434. result.M33 = -2 * invFN;
  435. result.M41 = -(right + left) * invRL;
  436. result.M42 = -(top + bottom) * invTB;
  437. result.M43 = -(zFar + zNear) * invFN;
  438. result.M44 = 1;
  439. }
  440. /// <summary>
  441. /// Creates an orthographic projection matrix.
  442. /// </summary>
  443. /// <param name="left">The left edge of the projection volume.</param>
  444. /// <param name="right">The right edge of the projection volume.</param>
  445. /// <param name="bottom">The bottom edge of the projection volume.</param>
  446. /// <param name="top">The top edge of the projection volume.</param>
  447. /// <param name="zNear">The near edge of the projection volume.</param>
  448. /// <param name="zFar">The far edge of the projection volume.</param>
  449. /// <returns>The resulting Matrix4 instance.</returns>
  450. public static Matrix4 CreateOrthographicOffCenter(float left, float right, float bottom, float top, float zNear, float zFar)
  451. {
  452. Matrix4 result;
  453. CreateOrthographicOffCenter(left, right, bottom, top, zNear, zFar, out result);
  454. return result;
  455. }
  456. #endregion
  457. #region CreatePerspectiveFieldOfView
  458. /// <summary>
  459. /// Creates a perspective projection matrix.
  460. /// </summary>
  461. /// <param name="fovy">Angle of the field of view in the y direction (in radians)</param>
  462. /// <param name="aspect">Aspect ratio of the view (width / height)</param>
  463. /// <param name="zNear">Distance to the near clip plane</param>
  464. /// <param name="zFar">Distance to the far clip plane</param>
  465. /// <param name="result">A projection matrix that transforms camera space to raster space</param>
  466. /// <exception cref="System.ArgumentOutOfRangeException">
  467. /// Thrown under the following conditions:
  468. /// <list type="bullet">
  469. /// <item>fovy is zero, less than zero or larger than Math.PI</item>
  470. /// <item>aspect is negative or zero</item>
  471. /// <item>zNear is negative or zero</item>
  472. /// <item>zFar is negative or zero</item>
  473. /// <item>zNear is larger than zFar</item>
  474. /// </list>
  475. /// </exception>
  476. public static void CreatePerspectiveFieldOfView(float fovy, float aspect, float zNear, float zFar, out Matrix4 result)
  477. {
  478. if (fovy <= 0 || fovy > Math.PI)
  479. throw new ArgumentOutOfRangeException("fovy");
  480. if (aspect <= 0)
  481. throw new ArgumentOutOfRangeException("aspect");
  482. if (zNear <= 0)
  483. throw new ArgumentOutOfRangeException("zNear");
  484. if (zFar <= 0)
  485. throw new ArgumentOutOfRangeException("zFar");
  486. if (zNear >= zFar)
  487. throw new ArgumentOutOfRangeException("zNear");
  488. float yMax = zNear * (float)System.Math.Tan(0.5f * fovy);
  489. float yMin = -yMax;
  490. float xMin = yMin * aspect;
  491. float xMax = yMax * aspect;
  492. CreatePerspectiveOffCenter(xMin, xMax, yMin, yMax, zNear, zFar, out result);
  493. }
  494. /// <summary>
  495. /// Creates a perspective projection matrix.
  496. /// </summary>
  497. /// <param name="fovy">Angle of the field of view in the y direction (in radians)</param>
  498. /// <param name="aspect">Aspect ratio of the view (width / height)</param>
  499. /// <param name="zNear">Distance to the near clip plane</param>
  500. /// <param name="zFar">Distance to the far clip plane</param>
  501. /// <returns>A projection matrix that transforms camera space to raster space</returns>
  502. /// <exception cref="System.ArgumentOutOfRangeException">
  503. /// Thrown under the following conditions:
  504. /// <list type="bullet">
  505. /// <item>fovy is zero, less than zero or larger than Math.PI</item>
  506. /// <item>aspect is negative or zero</item>
  507. /// <item>zNear is negative or zero</item>
  508. /// <item>zFar is negative or zero</item>
  509. /// <item>zNear is larger than zFar</item>
  510. /// </list>
  511. /// </exception>
  512. public static Matrix4 CreatePerspectiveFieldOfView(float fovy, float aspect, float zNear, float zFar)
  513. {
  514. Matrix4 result;
  515. CreatePerspectiveFieldOfView(fovy, aspect, zNear, zFar, out result);
  516. return result;
  517. }
  518. #endregion
  519. #region CreatePerspectiveOffCenter
  520. /// <summary>
  521. /// Creates an perspective projection matrix.
  522. /// </summary>
  523. /// <param name="left">Left edge of the view frustum</param>
  524. /// <param name="right">Right edge of the view frustum</param>
  525. /// <param name="bottom">Bottom edge of the view frustum</param>
  526. /// <param name="top">Top edge of the view frustum</param>
  527. /// <param name="zNear">Distance to the near clip plane</param>
  528. /// <param name="zFar">Distance to the far clip plane</param>
  529. /// <param name="result">A projection matrix that transforms camera space to raster space</param>
  530. /// <exception cref="System.ArgumentOutOfRangeException">
  531. /// Thrown under the following conditions:
  532. /// <list type="bullet">
  533. /// <item>zNear is negative or zero</item>
  534. /// <item>zFar is negative or zero</item>
  535. /// <item>zNear is larger than zFar</item>
  536. /// </list>
  537. /// </exception>
  538. public static void CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar, out Matrix4 result)
  539. {
  540. if (zNear <= 0)
  541. throw new ArgumentOutOfRangeException("zNear");
  542. if (zFar <= 0)
  543. throw new ArgumentOutOfRangeException("zFar");
  544. if (zNear >= zFar)
  545. throw new ArgumentOutOfRangeException("zNear");
  546. float x = (2.0f * zNear) / (right - left);
  547. float y = (2.0f * zNear) / (top - bottom);
  548. float a = (right + left) / (right - left);
  549. float b = (top + bottom) / (top - bottom);
  550. float c = -(zFar + zNear) / (zFar - zNear);
  551. float d = -(2.0f * zFar * zNear) / (zFar - zNear);
  552. result = new Matrix4(x, 0, 0, 0,
  553. 0, y, 0, 0,
  554. a, b, c, -1,
  555. 0, 0, d, 0);
  556. }
  557. /// <summary>
  558. /// Creates an perspective projection matrix.
  559. /// </summary>
  560. /// <param name="left">Left edge of the view frustum</param>
  561. /// <param name="right">Right edge of the view frustum</param>
  562. /// <param name="bottom">Bottom edge of the view frustum</param>
  563. /// <param name="top">Top edge of the view frustum</param>
  564. /// <param name="zNear">Distance to the near clip plane</param>
  565. /// <param name="zFar">Distance to the far clip plane</param>
  566. /// <returns>A projection matrix that transforms camera space to raster space</returns>
  567. /// <exception cref="System.ArgumentOutOfRangeException">
  568. /// Thrown under the following conditions:
  569. /// <list type="bullet">
  570. /// <item>zNear is negative or zero</item>
  571. /// <item>zFar is negative or zero</item>
  572. /// <item>zNear is larger than zFar</item>
  573. /// </list>
  574. /// </exception>
  575. public static Matrix4 CreatePerspectiveOffCenter(float left, float right, float bottom, float top, float zNear, float zFar)
  576. {
  577. Matrix4 result;
  578. CreatePerspectiveOffCenter(left, right, bottom, top, zNear, zFar, out result);
  579. return result;
  580. }
  581. #endregion
  582. #region Obsolete Functions
  583. #region Translation Functions
  584. /// <summary>
  585. /// Builds a translation matrix.
  586. /// </summary>
  587. /// <param name="trans">The translation vector.</param>
  588. /// <returns>A new Matrix4 instance.</returns>
  589. [Obsolete("Use CreateTranslation instead.")]
  590. public static Matrix4 Translation(Vector3 trans)
  591. {
  592. return Translation(trans.X, trans.Y, trans.Z);
  593. }
  594. /// <summary>
  595. /// Build a translation matrix with the given translation
  596. /// </summary>
  597. /// <param name="x">X translation</param>
  598. /// <param name="y">Y translation</param>
  599. /// <param name="z">Z translation</param>
  600. /// <returns>A Translation matrix</returns>
  601. [Obsolete("Use CreateTranslation instead.")]
  602. public static Matrix4 Translation(float x, float y, float z)
  603. {
  604. Matrix4 result = Identity;
  605. result.Row3 = new Vector4(x, y, z, 1.0f);
  606. return result;
  607. }
  608. #endregion
  609. #endregion
  610. #region Scale Functions
  611. /// <summary>
  612. /// Build a scaling matrix
  613. /// </summary>
  614. /// <param name="scale">Single scale factor for x,y and z axes</param>
  615. /// <returns>A scaling matrix</returns>
  616. public static Matrix4 Scale(float scale)
  617. {
  618. return Scale(scale, scale, scale);
  619. }
  620. /// <summary>
  621. /// Build a scaling matrix
  622. /// </summary>
  623. /// <param name="scale">Scale factors for x,y and z axes</param>
  624. /// <returns>A scaling matrix</returns>
  625. public static Matrix4 Scale(Vector3 scale)
  626. {
  627. return Scale(scale.X, scale.Y, scale.Z);
  628. }
  629. /// <summary>
  630. /// Build a scaling matrix
  631. /// </summary>
  632. /// <param name="x">Scale factor for x-axis</param>
  633. /// <param name="y">Scale factor for y-axis</param>
  634. /// <param name="z">Scale factor for z-axis</param>
  635. /// <returns>A scaling matrix</returns>
  636. public static Matrix4 Scale(float x, float y, float z)
  637. {
  638. Matrix4 result;
  639. result.Row0 = Vector4.UnitX * x;
  640. result.Row1 = Vector4.UnitY * y;
  641. result.Row2 = Vector4.UnitZ * z;
  642. result.Row3 = Vector4.UnitW;
  643. return result;
  644. }
  645. #endregion
  646. #region Rotation Functions
  647. /// <summary>
  648. /// Build a rotation matrix that rotates about the x-axis
  649. /// </summary>
  650. /// <param name="angle">angle in radians to rotate counter-clockwise around the x-axis</param>
  651. /// <returns>A rotation matrix</returns>
  652. [Obsolete("Use CreateRotationX instead.")]
  653. public static Matrix4 RotateX(float angle)
  654. {
  655. float cos = (float)System.Math.Cos(angle);
  656. float sin = (float)System.Math.Sin(angle);
  657. Matrix4 result;
  658. result.Row0 = Vector4.UnitX;
  659. result.Row1 = new Vector4(0.0f, cos, sin, 0.0f);
  660. result.Row2 = new Vector4(0.0f, -sin, cos, 0.0f);
  661. result.Row3 = Vector4.UnitW;
  662. return result;
  663. }
  664. /// <summary>
  665. /// Build a rotation matrix that rotates about the y-axis
  666. /// </summary>
  667. /// <param name="angle">angle in radians to rotate counter-clockwise around the y-axis</param>
  668. /// <returns>A rotation matrix</returns>
  669. [Obsolete("Use CreateRotationY instead.")]
  670. public static Matrix4 RotateY(float angle)
  671. {
  672. float cos = (float)System.Math.Cos(angle);
  673. float sin = (float)System.Math.Sin(angle);
  674. Matrix4 result;
  675. result.Row0 = new Vector4(cos, 0.0f, -sin, 0.0f);
  676. result.Row1 = Vector4.UnitY;
  677. result.Row2 = new Vector4(sin, 0.0f, cos, 0.0f);
  678. result.Row3 = Vector4.UnitW;
  679. return result;
  680. }
  681. /// <summary>
  682. /// Build a rotation matrix that rotates about the z-axis
  683. /// </summary>
  684. /// <param name="angle">angle in radians to rotate counter-clockwise around the z-axis</param>
  685. /// <returns>A rotation matrix</returns>
  686. [Obsolete("Use CreateRotationZ instead.")]
  687. public static Matrix4 RotateZ(float angle)
  688. {
  689. float cos = (float)System.Math.Cos(angle);
  690. float sin = (float)System.Math.Sin(angle);
  691. Matrix4 result;
  692. result.Row0 = new Vector4(cos, sin, 0.0f, 0.0f);
  693. result.Row1 = new Vector4(-sin, cos, 0.0f, 0.0f);
  694. result.Row2 = Vector4.UnitZ;
  695. result.Row3 = Vector4.UnitW;
  696. return result;
  697. }
  698. /// <summary>
  699. /// Build a rotation matrix to rotate about the given axis
  700. /// </summary>
  701. /// <param name="axis">the axis to rotate about</param>
  702. /// <param name="angle">angle in radians to rotate counter-clockwise (looking in the direction of the given axis)</param>
  703. /// <returns>A rotation matrix</returns>
  704. [Obsolete("Use CreateFromAxisAngle instead.")]
  705. public static Matrix4 Rotate(Vector3 axis, float angle)
  706. {
  707. float cos = (float)System.Math.Cos(-angle);
  708. float sin = (float)System.Math.Sin(-angle);
  709. float t = 1.0f - cos;
  710. axis.Normalize();
  711. Matrix4 result;
  712. result.Row0 = new Vector4(t * axis.X * axis.X + cos, t * axis.X * axis.Y - sin * axis.Z, t * axis.X * axis.Z + sin * axis.Y, 0.0f);
  713. result.Row1 = new Vector4(t * axis.X * axis.Y + sin * axis.Z, t * axis.Y * axis.Y + cos, t * axis.Y * axis.Z - sin * axis.X, 0.0f);
  714. result.Row2 = new Vector4(t * axis.X * axis.Z - sin * axis.Y, t * axis.Y * axis.Z + sin * axis.X, t * axis.Z * axis.Z + cos, 0.0f);
  715. result.Row3 = Vector4.UnitW;
  716. return result;
  717. }
  718. /// <summary>
  719. /// Build a rotation matrix from a quaternion
  720. /// </summary>
  721. /// <param name="q">the quaternion</param>
  722. /// <returns>A rotation matrix</returns>
  723. public static Matrix4 Rotate(Quaternion q)
  724. {
  725. Vector3 axis;
  726. float angle;
  727. q.ToAxisAngle(out axis, out angle);
  728. return CreateFromAxisAngle(axis, angle);
  729. }
  730. #endregion
  731. #region Camera Helper Functions
  732. /// <summary>
  733. /// Build a world space to camera space matrix
  734. /// </summary>
  735. /// <param name="eye">Eye (camera) position in world space</param>
  736. /// <param name="target">Target position in world space</param>
  737. /// <param name="up">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
  738. /// <returns>A Matrix4 that transforms world space to camera space</returns>
  739. public static Matrix4 LookAt(Vector3 eye, Vector3 target, Vector3 up)
  740. {
  741. Vector3 z = Vector3.Normalize(eye - target);
  742. Vector3 x = Vector3.Normalize(Vector3.Cross(up, z));
  743. Vector3 y = Vector3.Normalize(Vector3.Cross(z, x));
  744. Matrix4 rot = new Matrix4(new Vector4(x.X, y.X, z.X, 0.0f),
  745. new Vector4(x.Y, y.Y, z.Y, 0.0f),
  746. new Vector4(x.Z, y.Z, z.Z, 0.0f),
  747. Vector4.UnitW);
  748. Matrix4 trans = Matrix4.CreateTranslation(-eye);
  749. return trans * rot;
  750. }
  751. /// <summary>
  752. /// Build a world space to camera space matrix
  753. /// </summary>
  754. /// <param name="eyeX">Eye (camera) position in world space</param>
  755. /// <param name="eyeY">Eye (camera) position in world space</param>
  756. /// <param name="eyeZ">Eye (camera) position in world space</param>
  757. /// <param name="targetX">Target position in world space</param>
  758. /// <param name="targetY">Target position in world space</param>
  759. /// <param name="targetZ">Target position in world space</param>
  760. /// <param name="upX">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
  761. /// <param name="upY">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
  762. /// <param name="upZ">Up vector in world space (should not be parallel to the camera direction, that is target - eye)</param>
  763. /// <returns>A Matrix4 that transforms world space to camera space</returns>
  764. public static Matrix4 LookAt(float eyeX, float eyeY, float eyeZ, float targetX, float targetY, float targetZ, float upX, float upY, float upZ)
  765. {
  766. return LookAt(new Vector3(eyeX, eyeY, eyeZ), new Vector3(targetX, targetY, targetZ), new Vector3(upX, upY, upZ));
  767. }
  768. /// <summary>
  769. /// Build a projection matrix
  770. /// </summary>
  771. /// <param name="left">Left edge of the view frustum</param>
  772. /// <param name="right">Right edge of the view frustum</param>
  773. /// <param name="bottom">Bottom edge of the view frustum</param>
  774. /// <param name="top">Top edge of the view frustum</param>
  775. /// <param name="near">Distance to the near clip plane</param>
  776. /// <param name="far">Distance to the far clip plane</param>
  777. /// <returns>A projection matrix that transforms camera space to raster space</returns>
  778. [Obsolete("Use CreatePerspectiveOffCenter instead.")]
  779. public static Matrix4 Frustum(float left, float right, float bottom, float top, float near, float far)
  780. {
  781. float invRL = 1.0f / (right - left);
  782. float invTB = 1.0f / (top - bottom);
  783. float invFN = 1.0f / (far - near);
  784. return new Matrix4(new Vector4(2.0f * near * invRL, 0.0f, 0.0f, 0.0f),
  785. new Vector4(0.0f, 2.0f * near * invTB, 0.0f, 0.0f),
  786. new Vector4((right + left) * invRL, (top + bottom) * invTB, -(far + near) * invFN, -1.0f),
  787. new Vector4(0.0f, 0.0f, -2.0f * far * near * invFN, 0.0f));
  788. }
  789. /// <summary>
  790. /// Build a projection matrix
  791. /// </summary>
  792. /// <param name="fovy">Angle of the field of view in the y direction (in radians)</param>
  793. /// <param name="aspect">Aspect ratio of the view (width / height)</param>
  794. /// <param name="near">Distance to the near clip plane</param>
  795. /// <param name="far">Distance to the far clip plane</param>
  796. /// <returns>A projection matrix that transforms camera space to raster space</returns>
  797. [Obsolete("Use CreatePerspectiveFieldOfView instead.")]
  798. public static Matrix4 Perspective(float fovy, float aspect, float near, float far)
  799. {
  800. float yMax = near * (float)System.Math.Tan(0.5f * fovy);
  801. float yMin = -yMax;
  802. float xMin = yMin * aspect;
  803. float xMax = yMax * aspect;
  804. return Frustum(xMin, xMax, yMin, yMax, near, far);
  805. }
  806. #endregion
  807. #region Multiply Functions
  808. /// <summary>
  809. /// Multiplies two instances.
  810. /// </summary>
  811. /// <param name="left">The left operand of the multiplication.</param>
  812. /// <param name="right">The right operand of the multiplication.</param>
  813. /// <returns>A new instance that is the result of the multiplication</returns>
  814. public static Matrix4 Mult(Matrix4 left, Matrix4 right)
  815. {
  816. Matrix4 result;
  817. Mult(ref left, ref right, out result);
  818. return result;
  819. }
  820. /// <summary>
  821. /// Multiplies two instances.
  822. /// </summary>
  823. /// <param name="left">The left operand of the multiplication.</param>
  824. /// <param name="right">The right operand of the multiplication.</param>
  825. /// <param name="result">A new instance that is the result of the multiplication</param>
  826. public static void Mult(ref Matrix4 left, ref Matrix4 right, out Matrix4 result)
  827. {
  828. result = new Matrix4(
  829. left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31 + left.M14 * right.M41,
  830. left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32 + left.M14 * right.M42,
  831. left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33 + left.M14 * right.M43,
  832. left.M11 * right.M14 + left.M12 * right.M24 + left.M13 * right.M34 + left.M14 * right.M44,
  833. left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31 + left.M24 * right.M41,
  834. left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32 + left.M24 * right.M42,
  835. left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33 + left.M24 * right.M43,
  836. left.M21 * right.M14 + left.M22 * right.M24 + left.M23 * right.M34 + left.M24 * right.M44,
  837. left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31 + left.M34 * right.M41,
  838. left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32 + left.M34 * right.M42,
  839. left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33 + left.M34 * right.M43,
  840. left.M31 * right.M14 + left.M32 * right.M24 + left.M33 * right.M34 + left.M34 * right.M44,
  841. left.M41 * right.M11 + left.M42 * right.M21 + left.M43 * right.M31 + left.M44 * right.M41,
  842. left.M41 * right.M12 + left.M42 * right.M22 + left.M43 * right.M32 + left.M44 * right.M42,
  843. left.M41 * right.M13 + left.M42 * right.M23 + left.M43 * right.M33 + left.M44 * right.M43,
  844. left.M41 * right.M14 + left.M42 * right.M24 + left.M43 * right.M34 + left.M44 * right.M44);
  845. }
  846. #endregion
  847. #region Invert Functions
  848. /// <summary>
  849. /// Calculate the inverse of the given matrix
  850. /// </summary>
  851. /// <param name="mat">The matrix to invert</param>
  852. /// <returns>The inverse of the given matrix if it has one, or the input if it is singular</returns>
  853. /// <exception cref="InvalidOperationException">Thrown if the Matrix4 is singular.</exception>
  854. public static Matrix4 Invert(Matrix4 mat)
  855. {
  856. int[] colIdx = { 0, 0, 0, 0 };
  857. int[] rowIdx = { 0, 0, 0, 0 };
  858. int[] pivotIdx = { -1, -1, -1, -1 };
  859. // convert the matrix to an array for easy looping
  860. float[,] inverse = {{mat.Row0.X, mat.Row0.Y, mat.Row0.Z, mat.Row0.W},
  861. {mat.Row1.X, mat.Row1.Y, mat.Row1.Z, mat.Row1.W},
  862. {mat.Row2.X, mat.Row2.Y, mat.Row2.Z, mat.Row2.W},
  863. {mat.Row3.X, mat.Row3.Y, mat.Row3.Z, mat.Row3.W} };
  864. int icol = 0;
  865. int irow = 0;
  866. for (int i = 0; i < 4; i++)
  867. {
  868. // Find the largest pivot value
  869. float maxPivot = 0.0f;
  870. for (int j = 0; j < 4; j++)
  871. {
  872. if (pivotIdx[j] != 0)
  873. {
  874. for (int k = 0; k < 4; ++k)
  875. {
  876. if (pivotIdx[k] == -1)
  877. {
  878. float absVal = System.Math.Abs(inverse[j, k]);
  879. if (absVal > maxPivot)
  880. {
  881. maxPivot = absVal;
  882. irow = j;
  883. icol = k;
  884. }
  885. }
  886. else if (pivotIdx[k] > 0)
  887. {
  888. return mat;
  889. }
  890. }
  891. }
  892. }
  893. ++(pivotIdx[icol]);
  894. // Swap rows over so pivot is on diagonal
  895. if (irow != icol)
  896. {
  897. for (int k = 0; k < 4; ++k)
  898. {
  899. float f = inverse[irow, k];
  900. inverse[irow, k] = inverse[icol, k];
  901. inverse[icol, k] = f;
  902. }
  903. }
  904. rowIdx[i] = irow;
  905. colIdx[i] = icol;
  906. float pivot = inverse[icol, icol];
  907. // check for singular matrix
  908. if (pivot == 0.0f)
  909. {
  910. throw new InvalidOperationException("Matrix is singular and cannot be inverted.");
  911. //return mat;
  912. }
  913. // Scale row so it has a unit diagonal
  914. float oneOverPivot = 1.0f / pivot;
  915. inverse[icol, icol] = 1.0f;
  916. for (int k = 0; k < 4; ++k)
  917. inverse[icol, k] *= oneOverPivot;
  918. // Do elimination of non-diagonal elements
  919. for (int j = 0; j < 4; ++j)
  920. {
  921. // check this isn't on the diagonal
  922. if (icol != j)
  923. {
  924. float f = inverse[j, icol];
  925. inverse[j, icol] = 0.0f;
  926. for (int k = 0; k < 4; ++k)
  927. inverse[j, k] -= inverse[icol, k] * f;
  928. }
  929. }
  930. }
  931. for (int j = 3; j >= 0; --j)
  932. {
  933. int ir = rowIdx[j];
  934. int ic = colIdx[j];
  935. for (int k = 0; k < 4; ++k)
  936. {
  937. float f = inverse[k, ir];
  938. inverse[k, ir] = inverse[k, ic];
  939. inverse[k, ic] = f;
  940. }
  941. }
  942. mat.Row0 = new Vector4(inverse[0, 0], inverse[0, 1], inverse[0, 2], inverse[0, 3]);
  943. mat.Row1 = new Vector4(inverse[1, 0], inverse[1, 1], inverse[1, 2], inverse[1, 3]);
  944. mat.Row2 = new Vector4(inverse[2, 0], inverse[2, 1], inverse[2, 2], inverse[2, 3]);
  945. mat.Row3 = new Vector4(inverse[3, 0], inverse[3, 1], inverse[3, 2], inverse[3, 3]);
  946. return mat;
  947. }
  948. #endregion
  949. #region Transpose
  950. /// <summary>
  951. /// Calculate the transpose of the given matrix
  952. /// </summary>
  953. /// <param name="mat">The matrix to transpose</param>
  954. /// <returns>The transpose of the given matrix</returns>
  955. public static Matrix4 Transpose(Matrix4 mat)
  956. {
  957. return new Matrix4(mat.Column0, mat.Column1, mat.Column2, mat.Column3);
  958. }
  959. /// <summary>
  960. /// Calculate the transpose of the given matrix
  961. /// </summary>
  962. /// <param name="mat">The matrix to transpose</param>
  963. /// <param name="result">The result of the calculation</param>
  964. public static void Transpose(ref Matrix4 mat, out Matrix4 result)
  965. {
  966. result.Row0 = mat.Column0;
  967. result.Row1 = mat.Column1;
  968. result.Row2 = mat.Column2;
  969. result.Row3 = mat.Column3;
  970. }
  971. #endregion
  972. #endregion
  973. #region Operators
  974. /// <summary>
  975. /// Matrix multiplication
  976. /// </summary>
  977. /// <param name="left">left-hand operand</param>
  978. /// <param name="right">right-hand operand</param>
  979. /// <returns>A new Matrix44 which holds the result of the multiplication</returns>
  980. public static Matrix4 operator *(Matrix4 left, Matrix4 right)
  981. {
  982. return Matrix4.Mult(left, right);
  983. }
  984. /// <summary>
  985. /// Compares two instances for equality.
  986. /// </summary>
  987. /// <param name="left">The first instance.</param>
  988. /// <param name="right">The second instance.</param>
  989. /// <returns>True, if left equals right; false otherwise.</returns>
  990. public static bool operator ==(Matrix4 left, Matrix4 right)
  991. {
  992. return left.Equals(right);
  993. }
  994. /// <summary>
  995. /// Compares two instances for inequality.
  996. /// </summary>
  997. /// <param name="left">The first instance.</param>
  998. /// <param name="right">The second instance.</param>
  999. /// <returns>True, if left does not equal right; false otherwise.</returns>
  1000. public static bool operator !=(Matrix4 left, Matrix4 right)
  1001. {
  1002. return !left.Equals(right);
  1003. }
  1004. #endregion
  1005. #region Overrides
  1006. #region public override string ToString()
  1007. /// <summary>
  1008. /// Returns a System.String that represents the current Matrix44.
  1009. /// </summary>
  1010. /// <returns></returns>
  1011. public override string ToString()
  1012. {
  1013. return String.Format("{0}\n{1}\n{2}\n{3}", Row0, Row1, Row2, Row3);
  1014. }
  1015. #endregion
  1016. #region public override int GetHashCode()
  1017. /// <summary>
  1018. /// Returns the hashcode for this instance.
  1019. /// </summary>
  1020. /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
  1021. public override int GetHashCode()
  1022. {
  1023. return Row0.GetHashCode() ^ Row1.GetHashCode() ^ Row2.GetHashCode() ^ Row3.GetHashCode();
  1024. }
  1025. #endregion
  1026. #region public override bool Equals(object obj)
  1027. /// <summary>
  1028. /// Indicates whether this instance and a specified object are equal.
  1029. /// </summary>
  1030. /// <param name="obj">The object to compare tresult.</param>
  1031. /// <returns>True if the instances are equal; false otherwise.</returns>
  1032. public override bool Equals(object obj)
  1033. {
  1034. if (!(obj is Matrix4))
  1035. return false;
  1036. return this.Equals((Matrix4)obj);
  1037. }
  1038. #endregion
  1039. #endregion
  1040. #endregion
  1041. #region IEquatable<Matrix4> Members
  1042. /// <summary>Indicates whether the current matrix is equal to another matrix.</summary>
  1043. /// <param name="other">An matrix to compare with this matrix.</param>
  1044. /// <returns>true if the current matrix is equal to the matrix parameter; otherwise, false.</returns>
  1045. public bool Equals(Matrix4 other)
  1046. {
  1047. return
  1048. Row0 == other.Row0 &&
  1049. Row1 == other.Row1 &&
  1050. Row2 == other.Row2 &&
  1051. Row3 == other.Row3;
  1052. }
  1053. #endregion
  1054. }
  1055. }