Matrix4.cs 42 KB

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