matrix4.h 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include <cstring> // memset, memcpy
  6. #include "irrMath.h"
  7. #include "vector3d.h"
  8. #include "vector2d.h"
  9. #include "plane3d.h"
  10. #include "aabbox3d.h"
  11. #include "rect.h"
  12. #include "IrrCompileConfig.h" // for IRRLICHT_API
  13. namespace irr
  14. {
  15. namespace core
  16. {
  17. //! 4x4 matrix. Mostly used as transformation matrix for 3d calculations.
  18. /** Conventions: Matrices are considered to be in row-major order.
  19. * Multiplication of a matrix A with a row vector v is the premultiplication vA.
  20. * Translations are thus in the 4th row.
  21. * The matrix product AB yields a matrix C such that vC = (vB)A:
  22. * B is applied first, then A.
  23. */
  24. template <class T>
  25. class CMatrix4
  26. {
  27. public:
  28. //! Constructor Flags
  29. enum eConstructor
  30. {
  31. EM4CONST_NOTHING = 0,
  32. EM4CONST_COPY,
  33. EM4CONST_IDENTITY,
  34. EM4CONST_TRANSPOSED,
  35. EM4CONST_INVERSE,
  36. EM4CONST_INVERSE_TRANSPOSED
  37. };
  38. //! Default constructor
  39. /** \param constructor Choose the initialization style */
  40. CMatrix4(eConstructor constructor = EM4CONST_IDENTITY);
  41. //! Constructor with value initialization
  42. constexpr CMatrix4(const T &r0c0, const T &r0c1, const T &r0c2, const T &r0c3,
  43. const T &r1c0, const T &r1c1, const T &r1c2, const T &r1c3,
  44. const T &r2c0, const T &r2c1, const T &r2c2, const T &r2c3,
  45. const T &r3c0, const T &r3c1, const T &r3c2, const T &r3c3)
  46. {
  47. M[0] = r0c0;
  48. M[1] = r0c1;
  49. M[2] = r0c2;
  50. M[3] = r0c3;
  51. M[4] = r1c0;
  52. M[5] = r1c1;
  53. M[6] = r1c2;
  54. M[7] = r1c3;
  55. M[8] = r2c0;
  56. M[9] = r2c1;
  57. M[10] = r2c2;
  58. M[11] = r2c3;
  59. M[12] = r3c0;
  60. M[13] = r3c1;
  61. M[14] = r3c2;
  62. M[15] = r3c3;
  63. }
  64. //! Copy constructor
  65. /** \param other Other matrix to copy from
  66. \param constructor Choose the initialization style */
  67. CMatrix4(const CMatrix4<T> &other, eConstructor constructor = EM4CONST_COPY);
  68. //! Simple operator for directly accessing every element of the matrix.
  69. T &operator()(const s32 row, const s32 col)
  70. {
  71. return M[row * 4 + col];
  72. }
  73. //! Simple operator for directly accessing every element of the matrix.
  74. const T &operator()(const s32 row, const s32 col) const { return M[row * 4 + col]; }
  75. //! Simple operator for linearly accessing every element of the matrix.
  76. T &operator[](u32 index)
  77. {
  78. return M[index];
  79. }
  80. //! Simple operator for linearly accessing every element of the matrix.
  81. const T &operator[](u32 index) const { return M[index]; }
  82. //! Sets this matrix equal to the other matrix.
  83. CMatrix4<T> &operator=(const CMatrix4<T> &other) = default;
  84. //! Sets all elements of this matrix to the value.
  85. inline CMatrix4<T> &operator=(const T &scalar);
  86. //! Returns pointer to internal array
  87. const T *pointer() const { return M; }
  88. T *pointer()
  89. {
  90. return M;
  91. }
  92. //! Returns true if other matrix is equal to this matrix.
  93. constexpr bool operator==(const CMatrix4<T> &other) const
  94. {
  95. for (s32 i = 0; i < 16; ++i)
  96. if (M[i] != other.M[i])
  97. return false;
  98. return true;
  99. }
  100. //! Returns true if other matrix is not equal to this matrix.
  101. constexpr bool operator!=(const CMatrix4<T> &other) const
  102. {
  103. return !(*this == other);
  104. }
  105. //! Add another matrix.
  106. CMatrix4<T> operator+(const CMatrix4<T> &other) const;
  107. //! Add another matrix.
  108. CMatrix4<T> &operator+=(const CMatrix4<T> &other);
  109. //! Subtract another matrix.
  110. CMatrix4<T> operator-(const CMatrix4<T> &other) const;
  111. //! Subtract another matrix.
  112. CMatrix4<T> &operator-=(const CMatrix4<T> &other);
  113. //! set this matrix to the product of two matrices
  114. /** Calculate b*a */
  115. inline CMatrix4<T> &setbyproduct(const CMatrix4<T> &other_a, const CMatrix4<T> &other_b);
  116. //! Set this matrix to the product of two matrices
  117. /** Calculate b*a, no optimization used,
  118. use it if you know you never have an identity matrix */
  119. CMatrix4<T> &setbyproduct_nocheck(const CMatrix4<T> &other_a, const CMatrix4<T> &other_b);
  120. //! Multiply by another matrix.
  121. /** Calculate other*this */
  122. CMatrix4<T> operator*(const CMatrix4<T> &other) const;
  123. //! Multiply by another matrix.
  124. /** Like calling: (*this) = (*this) * other
  125. */
  126. CMatrix4<T> &operator*=(const CMatrix4<T> &other);
  127. //! Multiply by scalar.
  128. CMatrix4<T> operator*(const T &scalar) const;
  129. //! Multiply by scalar.
  130. CMatrix4<T> &operator*=(const T &scalar);
  131. //! Set matrix to identity.
  132. inline CMatrix4<T> &makeIdentity();
  133. //! Returns true if the matrix is the identity matrix
  134. inline bool isIdentity() const;
  135. //! Returns true if the matrix is orthogonal
  136. inline bool isOrthogonal() const;
  137. //! Returns true if the matrix is the identity matrix
  138. bool isIdentity_integer_base() const;
  139. //! Set the translation of the current matrix. Will erase any previous values.
  140. CMatrix4<T> &setTranslation(const vector3d<T> &translation);
  141. //! Gets the current translation
  142. vector3d<T> getTranslation() const;
  143. //! Set the inverse translation of the current matrix. Will erase any previous values.
  144. CMatrix4<T> &setInverseTranslation(const vector3d<T> &translation);
  145. //! Make a rotation matrix from Euler angles. The 4th row and column are unmodified.
  146. //! NOTE: Rotation order is ZYX. This means that vectors are
  147. //! first rotated around the X, then the Y, and finally the Z axis.
  148. //! NOTE: The rotation is done as per the right-hand rule.
  149. //! See test_irr_matrix4.cpp if you're still unsure about the conventions used here.
  150. inline CMatrix4<T> &setRotationRadians(const vector3d<T> &rotation);
  151. //! Same as `setRotationRadians`, but uses degrees.
  152. CMatrix4<T> &setRotationDegrees(const vector3d<T> &rotation);
  153. //! Get the rotation, as set by setRotation() when you already know the scale used to create the matrix
  154. /** NOTE: The scale needs to be the correct one used to create this matrix.
  155. You can _not_ use the result of getScale(), but have to save your scale
  156. variable in another place (like ISceneNode does).
  157. NOTE: No scale value can be 0 or the result is undefined.
  158. NOTE: It does not necessarily return the *same* Euler angles as those set by setRotationDegrees(),
  159. but the rotation will be equivalent, i.e. will have the same result when used to rotate a vector or node.
  160. NOTE: It will (usually) give wrong results when further transformations have been added in the matrix (like shear).
  161. WARNING: There have been troubles with this function over the years and we may still have missed some corner cases.
  162. It's generally safer to keep the rotation and scale you used to create the matrix around and work with those.
  163. */
  164. core::vector3d<T> getRotationDegrees(const vector3d<T> &scale) const;
  165. //! Returns the rotation, as set by setRotation().
  166. /** NOTE: You will have the same end-rotation as used in setRotation, but it might not use the same axis values.
  167. NOTE: This only works correct if no other matrix operations have been done on the inner 3x3 matrix besides
  168. setting rotation (so no scale/shear). Thought it (probably) works as long as scale doesn't flip handedness.
  169. NOTE: It does not necessarily return the *same* Euler angles as those set by setRotationDegrees(),
  170. but the rotation will be equivalent, i.e. will have the same result when used to rotate a vector or node.
  171. */
  172. core::vector3d<T> getRotationDegrees() const;
  173. //! Make an inverted rotation matrix from Euler angles.
  174. /** The 4th row and column are unmodified. */
  175. inline CMatrix4<T> &setInverseRotationRadians(const vector3d<T> &rotation);
  176. //! Make an inverted rotation matrix from Euler angles.
  177. /** The 4th row and column are unmodified. */
  178. inline CMatrix4<T> &setInverseRotationDegrees(const vector3d<T> &rotation);
  179. //! Make a rotation matrix from angle and axis, assuming left handed rotation.
  180. /** The 4th row and column are unmodified. */
  181. inline CMatrix4<T> &setRotationAxisRadians(const T &angle, const vector3d<T> &axis);
  182. //! Set Scale
  183. CMatrix4<T> &setScale(const vector3d<T> &scale);
  184. //! Set Scale
  185. CMatrix4<T> &setScale(const T scale) { return setScale(core::vector3d<T>(scale, scale, scale)); }
  186. //! Get Scale
  187. core::vector3d<T> getScale() const;
  188. //! Translate a vector by the inverse of the translation part of this matrix.
  189. void inverseTranslateVect(vector3df &vect) const;
  190. //! Scale a vector, then rotate by the inverse of the rotation part of this matrix.
  191. [[nodiscard]] vector3d<T> scaleThenInvRotVect(const vector3d<T> &vect) const;
  192. //! Rotate and scale a vector. Applies both rotation & scale part of the matrix.
  193. [[nodiscard]] vector3d<T> rotateAndScaleVect(const vector3d<T> &vect) const;
  194. //! Transforms the vector by this matrix
  195. /** This operation is performed as if the vector was 4d with the 4th component = 1 */
  196. [[nodiscard]] vector3d<T> transformVect(const vector3d<T> &v) const;
  197. //! Transforms the vector by this matrix
  198. /** This operation is performed as if the vector was 4d with the 4th component = 1 */
  199. void transformVect(vector3d<T> &vect) const {
  200. const vector3d<T> &v = vect;
  201. vect = transformVect(v);
  202. }
  203. //! Transforms input vector by this matrix and stores result in output vector
  204. /** This operation is performed as if the vector was 4d with the 4th component = 1 */
  205. void transformVect(vector3d<T> &out, const vector3d<T> &in) const {
  206. out = transformVect(in);
  207. }
  208. //! An alternate transform vector method, writing into an array of 4 floats
  209. /** This operation is performed as if the vector was 4d with the 4th component =1.
  210. NOTE: out[3] will be written to (4th vector component)*/
  211. void transformVect(T *out, const core::vector3df &in) const;
  212. //! An alternate transform vector method, reading from and writing to an array of 3 floats
  213. /** This operation is performed as if the vector was 4d with the 4th component =1
  214. NOTE: out[3] will be written to (4th vector component)*/
  215. void transformVec3(T *out, const T *in) const;
  216. //! An alternate transform vector method, reading from and writing to an array of 4 floats
  217. void transformVec4(T *out, const T *in) const;
  218. //! Translate a vector by the translation part of this matrix.
  219. /** This operation is performed as if the vector was 4d with the 4th component =1 */
  220. void translateVect(vector3df &vect) const;
  221. //! Transforms a plane by this matrix
  222. void transformPlane(core::plane3d<f32> &plane) const;
  223. //! Transforms a plane by this matrix
  224. void transformPlane(const core::plane3d<f32> &in, core::plane3d<f32> &out) const;
  225. //! Transforms a axis aligned bounding box
  226. void transformBoxEx(core::aabbox3d<f32> &box) const;
  227. //! Multiplies this matrix by a 1x4 matrix
  228. void multiplyWith1x4Matrix(T *matrix) const;
  229. //! Calculates inverse of matrix. Slow.
  230. /** \return Returns false if there is no inverse matrix.*/
  231. bool makeInverse();
  232. //! Inverts a primitive matrix which only contains a translation and a rotation
  233. /** \param out: where result matrix is written to. */
  234. bool getInversePrimitive(CMatrix4<T> &out) const;
  235. //! Gets the inverse matrix of this one
  236. /** \param out: where result matrix is written to.
  237. \return Returns false if there is no inverse matrix. */
  238. bool getInverse(CMatrix4<T> &out) const;
  239. //! Builds a right-handed perspective projection matrix based on a field of view
  240. //\param zClipFromZero: Clipping of z can be projected from 0 to w when true (D3D style) and from -w to w when false (OGL style).
  241. CMatrix4<T> &buildProjectionMatrixPerspectiveFovRH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar, bool zClipFromZero = true);
  242. //! Builds a left-handed perspective projection matrix based on a field of view
  243. CMatrix4<T> &buildProjectionMatrixPerspectiveFovLH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar, bool zClipFromZero = true);
  244. //! Builds a left-handed perspective projection matrix based on a field of view, with far plane at infinity
  245. CMatrix4<T> &buildProjectionMatrixPerspectiveFovInfinityLH(f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 epsilon = 0);
  246. //! Builds a right-handed perspective projection matrix.
  247. CMatrix4<T> &buildProjectionMatrixPerspectiveRH(f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar, bool zClipFromZero = true);
  248. //! Builds a left-handed perspective projection matrix.
  249. CMatrix4<T> &buildProjectionMatrixPerspectiveLH(f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar, bool zClipFromZero = true);
  250. //! Builds a left-handed orthogonal projection matrix.
  251. //\param zClipFromZero: Clipping of z can be projected from 0 to 1 when true (D3D style) and from -1 to 1 when false (OGL style).
  252. CMatrix4<T> &buildProjectionMatrixOrthoLH(f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar, bool zClipFromZero = true);
  253. //! Builds a right-handed orthogonal projection matrix.
  254. CMatrix4<T> &buildProjectionMatrixOrthoRH(f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar, bool zClipFromZero = true);
  255. //! Builds a left-handed look-at matrix.
  256. CMatrix4<T> &buildCameraLookAtMatrixLH(
  257. const vector3df &position,
  258. const vector3df &target,
  259. const vector3df &upVector);
  260. //! Builds a right-handed look-at matrix.
  261. CMatrix4<T> &buildCameraLookAtMatrixRH(
  262. const vector3df &position,
  263. const vector3df &target,
  264. const vector3df &upVector);
  265. //! Builds a matrix that flattens geometry into a plane.
  266. /** \param light: light source
  267. \param plane: plane into which the geometry if flattened into
  268. \param point: value between 0 and 1, describing the light source.
  269. If this is 1, it is a point light, if it is 0, it is a directional light. */
  270. CMatrix4<T> &buildShadowMatrix(const core::vector3df &light, core::plane3df plane, f32 point = 1.0f);
  271. //! Builds a matrix which transforms a normalized Device Coordinate to Device Coordinates.
  272. /** Used to scale <-1,-1><1,1> to viewport, for example from <-1,-1> <1,1> to the viewport <0,0><0,640> */
  273. CMatrix4<T> &buildNDCToDCMatrix(const core::rect<s32> &area, f32 zScale);
  274. //! Creates a new matrix as interpolated matrix from two other ones.
  275. /** \param b: other matrix to interpolate with
  276. \param time: Must be a value between 0 and 1. */
  277. CMatrix4<T> interpolate(const core::CMatrix4<T> &b, f32 time) const;
  278. //! Gets transposed matrix
  279. CMatrix4<T> getTransposed() const;
  280. //! Gets transposed matrix
  281. inline void getTransposed(CMatrix4<T> &dest) const;
  282. //! Builds a matrix that rotates from one vector to another
  283. /** \param from: vector to rotate from
  284. \param to: vector to rotate to
  285. */
  286. CMatrix4<T> &buildRotateFromTo(const core::vector3df &from, const core::vector3df &to);
  287. //! Builds a combined matrix which translates to a center before rotation and translates from origin afterwards
  288. /** \param center Position to rotate around
  289. \param translate Translation applied after the rotation
  290. */
  291. void setRotationCenter(const core::vector3df &center, const core::vector3df &translate);
  292. //! Builds a matrix which rotates a source vector to a look vector over an arbitrary axis
  293. /** \param camPos: viewer position in world coo
  294. \param center: object position in world-coo and rotation pivot
  295. \param translation: object final translation from center
  296. \param axis: axis to rotate about
  297. \param from: source vector to rotate from
  298. */
  299. void buildAxisAlignedBillboard(const core::vector3df &camPos,
  300. const core::vector3df &center,
  301. const core::vector3df &translation,
  302. const core::vector3df &axis,
  303. const core::vector3df &from);
  304. /*
  305. construct 2D Texture transformations
  306. rotate about center, scale, and transform.
  307. */
  308. //! Set to a texture transformation matrix with the given parameters.
  309. CMatrix4<T> &buildTextureTransform(f32 rotateRad,
  310. const core::vector2df &rotatecenter,
  311. const core::vector2df &translate,
  312. const core::vector2df &scale);
  313. //! Set texture transformation rotation
  314. /** Rotate about z axis, recenter at (0.5,0.5).
  315. Doesn't clear other elements than those affected
  316. \param radAngle Angle in radians
  317. \return Altered matrix */
  318. CMatrix4<T> &setTextureRotationCenter(f32 radAngle);
  319. //! Set texture transformation translation
  320. /** Doesn't clear other elements than those affected.
  321. \param x Offset on x axis
  322. \param y Offset on y axis
  323. \return Altered matrix */
  324. CMatrix4<T> &setTextureTranslate(f32 x, f32 y);
  325. //! Get texture transformation translation
  326. /** \param x returns offset on x axis
  327. \param y returns offset on y axis */
  328. void getTextureTranslate(f32 &x, f32 &y) const;
  329. //! Set texture transformation translation, using a transposed representation
  330. /** Doesn't clear other elements than those affected.
  331. \param x Offset on x axis
  332. \param y Offset on y axis
  333. \return Altered matrix */
  334. CMatrix4<T> &setTextureTranslateTransposed(f32 x, f32 y);
  335. //! Set texture transformation scale
  336. /** Doesn't clear other elements than those affected.
  337. \param sx Scale factor on x axis
  338. \param sy Scale factor on y axis
  339. \return Altered matrix. */
  340. CMatrix4<T> &setTextureScale(f32 sx, f32 sy);
  341. //! Get texture transformation scale
  342. /** \param sx Returns x axis scale factor
  343. \param sy Returns y axis scale factor */
  344. void getTextureScale(f32 &sx, f32 &sy) const;
  345. //! Set texture transformation scale, and recenter at (0.5,0.5)
  346. /** Doesn't clear other elements than those affected.
  347. \param sx Scale factor on x axis
  348. \param sy Scale factor on y axis
  349. \return Altered matrix. */
  350. CMatrix4<T> &setTextureScaleCenter(f32 sx, f32 sy);
  351. //! Sets all matrix data members at once
  352. CMatrix4<T> &setM(const T *data);
  353. //! Compare two matrices using the equal method
  354. bool equals(const core::CMatrix4<T> &other, const T tolerance = (T)ROUNDING_ERROR_f64) const;
  355. private:
  356. //! Matrix data, stored in row-major order
  357. T M[16];
  358. };
  359. // Default constructor
  360. template <class T>
  361. inline CMatrix4<T>::CMatrix4(eConstructor constructor)
  362. {
  363. switch (constructor) {
  364. case EM4CONST_NOTHING:
  365. case EM4CONST_COPY:
  366. break;
  367. case EM4CONST_IDENTITY:
  368. case EM4CONST_INVERSE:
  369. default:
  370. makeIdentity();
  371. break;
  372. }
  373. }
  374. // Copy constructor
  375. template <class T>
  376. inline CMatrix4<T>::CMatrix4(const CMatrix4<T> &other, eConstructor constructor)
  377. {
  378. switch (constructor) {
  379. case EM4CONST_IDENTITY:
  380. makeIdentity();
  381. break;
  382. case EM4CONST_NOTHING:
  383. break;
  384. case EM4CONST_COPY:
  385. *this = other;
  386. break;
  387. case EM4CONST_TRANSPOSED:
  388. other.getTransposed(*this);
  389. break;
  390. case EM4CONST_INVERSE:
  391. if (!other.getInverse(*this))
  392. memset(M, 0, 16 * sizeof(T));
  393. break;
  394. case EM4CONST_INVERSE_TRANSPOSED:
  395. if (!other.getInverse(*this))
  396. memset(M, 0, 16 * sizeof(T));
  397. else
  398. *this = getTransposed();
  399. break;
  400. }
  401. }
  402. //! Add another matrix.
  403. template <class T>
  404. inline CMatrix4<T> CMatrix4<T>::operator+(const CMatrix4<T> &other) const
  405. {
  406. CMatrix4<T> temp(EM4CONST_NOTHING);
  407. temp[0] = M[0] + other[0];
  408. temp[1] = M[1] + other[1];
  409. temp[2] = M[2] + other[2];
  410. temp[3] = M[3] + other[3];
  411. temp[4] = M[4] + other[4];
  412. temp[5] = M[5] + other[5];
  413. temp[6] = M[6] + other[6];
  414. temp[7] = M[7] + other[7];
  415. temp[8] = M[8] + other[8];
  416. temp[9] = M[9] + other[9];
  417. temp[10] = M[10] + other[10];
  418. temp[11] = M[11] + other[11];
  419. temp[12] = M[12] + other[12];
  420. temp[13] = M[13] + other[13];
  421. temp[14] = M[14] + other[14];
  422. temp[15] = M[15] + other[15];
  423. return temp;
  424. }
  425. //! Add another matrix.
  426. template <class T>
  427. inline CMatrix4<T> &CMatrix4<T>::operator+=(const CMatrix4<T> &other)
  428. {
  429. M[0] += other[0];
  430. M[1] += other[1];
  431. M[2] += other[2];
  432. M[3] += other[3];
  433. M[4] += other[4];
  434. M[5] += other[5];
  435. M[6] += other[6];
  436. M[7] += other[7];
  437. M[8] += other[8];
  438. M[9] += other[9];
  439. M[10] += other[10];
  440. M[11] += other[11];
  441. M[12] += other[12];
  442. M[13] += other[13];
  443. M[14] += other[14];
  444. M[15] += other[15];
  445. return *this;
  446. }
  447. //! Subtract another matrix.
  448. template <class T>
  449. inline CMatrix4<T> CMatrix4<T>::operator-(const CMatrix4<T> &other) const
  450. {
  451. CMatrix4<T> temp(EM4CONST_NOTHING);
  452. temp[0] = M[0] - other[0];
  453. temp[1] = M[1] - other[1];
  454. temp[2] = M[2] - other[2];
  455. temp[3] = M[3] - other[3];
  456. temp[4] = M[4] - other[4];
  457. temp[5] = M[5] - other[5];
  458. temp[6] = M[6] - other[6];
  459. temp[7] = M[7] - other[7];
  460. temp[8] = M[8] - other[8];
  461. temp[9] = M[9] - other[9];
  462. temp[10] = M[10] - other[10];
  463. temp[11] = M[11] - other[11];
  464. temp[12] = M[12] - other[12];
  465. temp[13] = M[13] - other[13];
  466. temp[14] = M[14] - other[14];
  467. temp[15] = M[15] - other[15];
  468. return temp;
  469. }
  470. //! Subtract another matrix.
  471. template <class T>
  472. inline CMatrix4<T> &CMatrix4<T>::operator-=(const CMatrix4<T> &other)
  473. {
  474. M[0] -= other[0];
  475. M[1] -= other[1];
  476. M[2] -= other[2];
  477. M[3] -= other[3];
  478. M[4] -= other[4];
  479. M[5] -= other[5];
  480. M[6] -= other[6];
  481. M[7] -= other[7];
  482. M[8] -= other[8];
  483. M[9] -= other[9];
  484. M[10] -= other[10];
  485. M[11] -= other[11];
  486. M[12] -= other[12];
  487. M[13] -= other[13];
  488. M[14] -= other[14];
  489. M[15] -= other[15];
  490. return *this;
  491. }
  492. //! Multiply by scalar.
  493. template <class T>
  494. inline CMatrix4<T> CMatrix4<T>::operator*(const T &scalar) const
  495. {
  496. CMatrix4<T> temp(EM4CONST_NOTHING);
  497. temp[0] = M[0] * scalar;
  498. temp[1] = M[1] * scalar;
  499. temp[2] = M[2] * scalar;
  500. temp[3] = M[3] * scalar;
  501. temp[4] = M[4] * scalar;
  502. temp[5] = M[5] * scalar;
  503. temp[6] = M[6] * scalar;
  504. temp[7] = M[7] * scalar;
  505. temp[8] = M[8] * scalar;
  506. temp[9] = M[9] * scalar;
  507. temp[10] = M[10] * scalar;
  508. temp[11] = M[11] * scalar;
  509. temp[12] = M[12] * scalar;
  510. temp[13] = M[13] * scalar;
  511. temp[14] = M[14] * scalar;
  512. temp[15] = M[15] * scalar;
  513. return temp;
  514. }
  515. //! Multiply by scalar.
  516. template <class T>
  517. inline CMatrix4<T> &CMatrix4<T>::operator*=(const T &scalar)
  518. {
  519. M[0] *= scalar;
  520. M[1] *= scalar;
  521. M[2] *= scalar;
  522. M[3] *= scalar;
  523. M[4] *= scalar;
  524. M[5] *= scalar;
  525. M[6] *= scalar;
  526. M[7] *= scalar;
  527. M[8] *= scalar;
  528. M[9] *= scalar;
  529. M[10] *= scalar;
  530. M[11] *= scalar;
  531. M[12] *= scalar;
  532. M[13] *= scalar;
  533. M[14] *= scalar;
  534. M[15] *= scalar;
  535. return *this;
  536. }
  537. //! Multiply by another matrix.
  538. template <class T>
  539. inline CMatrix4<T> &CMatrix4<T>::operator*=(const CMatrix4<T> &other)
  540. {
  541. #if defined(USE_MATRIX_TEST)
  542. // do checks on your own in order to avoid copy creation
  543. if (!other.isIdentity()) {
  544. if (this->isIdentity()) {
  545. return (*this = other);
  546. } else {
  547. CMatrix4<T> temp(*this);
  548. return setbyproduct_nocheck(temp, other);
  549. }
  550. }
  551. return *this;
  552. #else
  553. CMatrix4<T> temp(*this);
  554. return setbyproduct_nocheck(temp, other);
  555. #endif
  556. }
  557. //! multiply by another matrix
  558. // set this matrix to the product of two other matrices
  559. // goal is to reduce stack use and copy
  560. template <class T>
  561. inline CMatrix4<T> &CMatrix4<T>::setbyproduct_nocheck(const CMatrix4<T> &other_a, const CMatrix4<T> &other_b)
  562. {
  563. const T *m1 = other_a.M;
  564. const T *m2 = other_b.M;
  565. M[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
  566. M[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
  567. M[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
  568. M[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
  569. M[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
  570. M[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
  571. M[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
  572. M[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
  573. M[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
  574. M[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
  575. M[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
  576. M[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
  577. M[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
  578. M[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
  579. M[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
  580. M[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
  581. return *this;
  582. }
  583. //! multiply by another matrix
  584. // set this matrix to the product of two other matrices
  585. // goal is to reduce stack use and copy
  586. template <class T>
  587. inline CMatrix4<T> &CMatrix4<T>::setbyproduct(const CMatrix4<T> &other_a, const CMatrix4<T> &other_b)
  588. {
  589. #if defined(USE_MATRIX_TEST)
  590. if (other_a.isIdentity())
  591. return (*this = other_b);
  592. else if (other_b.isIdentity())
  593. return (*this = other_a);
  594. else
  595. return setbyproduct_nocheck(other_a, other_b);
  596. #else
  597. return setbyproduct_nocheck(other_a, other_b);
  598. #endif
  599. }
  600. //! multiply by another matrix
  601. template <class T>
  602. inline CMatrix4<T> CMatrix4<T>::operator*(const CMatrix4<T> &m2) const
  603. {
  604. #if defined(USE_MATRIX_TEST)
  605. // Testing purpose..
  606. if (this->isIdentity())
  607. return m2;
  608. if (m2.isIdentity())
  609. return *this;
  610. #endif
  611. CMatrix4<T> m3(EM4CONST_NOTHING);
  612. const T *m1 = M;
  613. m3[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
  614. m3[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
  615. m3[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
  616. m3[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
  617. m3[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
  618. m3[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
  619. m3[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
  620. m3[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
  621. m3[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
  622. m3[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
  623. m3[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
  624. m3[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
  625. m3[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
  626. m3[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
  627. m3[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
  628. m3[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
  629. return m3;
  630. }
  631. template <class T>
  632. inline vector3d<T> CMatrix4<T>::getTranslation() const
  633. {
  634. return vector3d<T>(M[12], M[13], M[14]);
  635. }
  636. template <class T>
  637. inline CMatrix4<T> &CMatrix4<T>::setTranslation(const vector3d<T> &translation)
  638. {
  639. M[12] = translation.X;
  640. M[13] = translation.Y;
  641. M[14] = translation.Z;
  642. return *this;
  643. }
  644. template <class T>
  645. inline CMatrix4<T> &CMatrix4<T>::setInverseTranslation(const vector3d<T> &translation)
  646. {
  647. M[12] = -translation.X;
  648. M[13] = -translation.Y;
  649. M[14] = -translation.Z;
  650. return *this;
  651. }
  652. template <class T>
  653. inline CMatrix4<T> &CMatrix4<T>::setScale(const vector3d<T> &scale)
  654. {
  655. M[0] = scale.X;
  656. M[5] = scale.Y;
  657. M[10] = scale.Z;
  658. return *this;
  659. }
  660. //! Returns the absolute values of the scales of the matrix.
  661. /**
  662. Note: You only get back original values if the matrix only set the scale.
  663. Otherwise the result is a scale you can use to normalize the matrix axes,
  664. but it's usually no longer what you did set with setScale.
  665. */
  666. template <class T>
  667. inline vector3d<T> CMatrix4<T>::getScale() const
  668. {
  669. // See http://www.robertblum.com/articles/2005/02/14/decomposing-matrices
  670. // Deal with the 0 rotation case first
  671. // Prior to Irrlicht 1.6, we always returned this value.
  672. if (core::iszero(M[1]) && core::iszero(M[2]) &&
  673. core::iszero(M[4]) && core::iszero(M[6]) &&
  674. core::iszero(M[8]) && core::iszero(M[9]))
  675. return vector3d<T>(M[0], M[5], M[10]);
  676. // We have to do the full calculation.
  677. return vector3d<T>(sqrtf(M[0] * M[0] + M[1] * M[1] + M[2] * M[2]),
  678. sqrtf(M[4] * M[4] + M[5] * M[5] + M[6] * M[6]),
  679. sqrtf(M[8] * M[8] + M[9] * M[9] + M[10] * M[10]));
  680. }
  681. template <class T>
  682. inline CMatrix4<T> &CMatrix4<T>::setRotationDegrees(const vector3d<T> &rotation)
  683. {
  684. return setRotationRadians(rotation * core::DEGTORAD);
  685. }
  686. template <class T>
  687. inline CMatrix4<T> &CMatrix4<T>::setInverseRotationDegrees(const vector3d<T> &rotation)
  688. {
  689. return setInverseRotationRadians(rotation * core::DEGTORAD);
  690. }
  691. template <class T>
  692. inline CMatrix4<T> &CMatrix4<T>::setRotationRadians(const vector3d<T> &rotation)
  693. {
  694. const f64 cPitch = cos(rotation.X);
  695. const f64 sPitch = sin(rotation.X);
  696. const f64 cYaw = cos(rotation.Y);
  697. const f64 sYaw = sin(rotation.Y);
  698. const f64 cRoll = cos(rotation.Z);
  699. const f64 sRoll = sin(rotation.Z);
  700. M[0] = (T)(cYaw * cRoll);
  701. M[1] = (T)(cYaw * sRoll);
  702. M[2] = (T)(-sYaw);
  703. const f64 sPitch_sYaw = sPitch * sYaw;
  704. const f64 cPitch_sYaw = cPitch * sYaw;
  705. M[4] = (T)(sPitch_sYaw * cRoll - cPitch * sRoll);
  706. M[5] = (T)(sPitch_sYaw * sRoll + cPitch * cRoll);
  707. M[6] = (T)(sPitch * cYaw);
  708. M[8] = (T)(cPitch_sYaw * cRoll + sPitch * sRoll);
  709. M[9] = (T)(cPitch_sYaw * sRoll - sPitch * cRoll);
  710. M[10] = (T)(cPitch * cYaw);
  711. return *this;
  712. }
  713. //! Returns a rotation which (mostly) works in combination with the given scale
  714. /**
  715. This code was originally written by by Chev (assuming no scaling back then,
  716. we can be blamed for all problems added by regarding scale)
  717. */
  718. template <class T>
  719. inline core::vector3d<T> CMatrix4<T>::getRotationDegrees(const vector3d<T> &scale_) const
  720. {
  721. const CMatrix4<T> &mat = *this;
  722. const core::vector3d<f64> scale(core::iszero(scale_.X) ? FLT_MAX : scale_.X, core::iszero(scale_.Y) ? FLT_MAX : scale_.Y, core::iszero(scale_.Z) ? FLT_MAX : scale_.Z);
  723. const core::vector3d<f64> invScale(core::reciprocal(scale.X), core::reciprocal(scale.Y), core::reciprocal(scale.Z));
  724. f64 Y = -asin(core::clamp(mat[2] * invScale.X, -1.0, 1.0));
  725. const f64 C = cos(Y);
  726. Y *= RADTODEG64;
  727. f64 rotx, roty, X, Z;
  728. if (!core::iszero((T)C)) {
  729. const f64 invC = core::reciprocal(C);
  730. rotx = mat[10] * invC * invScale.Z;
  731. roty = mat[6] * invC * invScale.Y;
  732. X = atan2(roty, rotx) * RADTODEG64;
  733. rotx = mat[0] * invC * invScale.X;
  734. roty = mat[1] * invC * invScale.X;
  735. Z = atan2(roty, rotx) * RADTODEG64;
  736. } else {
  737. X = 0.0;
  738. rotx = mat[5] * invScale.Y;
  739. roty = -mat[4] * invScale.Y;
  740. Z = atan2(roty, rotx) * RADTODEG64;
  741. }
  742. // fix values that get below zero
  743. if (X < 0.0)
  744. X += 360.0;
  745. if (Y < 0.0)
  746. Y += 360.0;
  747. if (Z < 0.0)
  748. Z += 360.0;
  749. return vector3d<T>((T)X, (T)Y, (T)Z);
  750. }
  751. //! Returns a rotation that is equivalent to that set by setRotationDegrees().
  752. template <class T>
  753. inline core::vector3d<T> CMatrix4<T>::getRotationDegrees() const
  754. {
  755. // Note: Using getScale() here make it look like it could do matrix decomposition.
  756. // It can't! It works (or should work) as long as rotation doesn't flip the handedness
  757. // aka scale swapping 1 or 3 axes. (I think we could catch that as well by comparing
  758. // crossproduct of first 2 axes to direction of third axis, but TODO)
  759. // And maybe it should also offer the solution for the simple calculation
  760. // without regarding scaling as Irrlicht did before 1.7
  761. core::vector3d<T> scale(getScale());
  762. // We assume the matrix uses rotations instead of negative scaling 2 axes.
  763. // Otherwise it fails even for some simple cases, like rotating around
  764. // 2 axes by 180° which getScale thinks is a negative scaling.
  765. if (scale.Y < 0 && scale.Z < 0) {
  766. scale.Y = -scale.Y;
  767. scale.Z = -scale.Z;
  768. } else if (scale.X < 0 && scale.Z < 0) {
  769. scale.X = -scale.X;
  770. scale.Z = -scale.Z;
  771. } else if (scale.X < 0 && scale.Y < 0) {
  772. scale.X = -scale.X;
  773. scale.Y = -scale.Y;
  774. }
  775. return getRotationDegrees(scale);
  776. }
  777. //! Sets matrix to rotation matrix of inverse angles given as parameters
  778. template <class T>
  779. inline CMatrix4<T> &CMatrix4<T>::setInverseRotationRadians(const vector3d<T> &rotation)
  780. {
  781. f64 cPitch = cos(rotation.X);
  782. f64 sPitch = sin(rotation.X);
  783. f64 cYaw = cos(rotation.Y);
  784. f64 sYaw = sin(rotation.Y);
  785. f64 cRoll = cos(rotation.Z);
  786. f64 sRoll = sin(rotation.Z);
  787. M[0] = (T)(cYaw * cRoll);
  788. M[4] = (T)(cYaw * sRoll);
  789. M[8] = (T)(-sYaw);
  790. f64 sPitch_sYaw = sPitch * sYaw;
  791. f64 cPitch_sYaw = cPitch * sYaw;
  792. M[1] = (T)(sPitch_sYaw * cRoll - cPitch * sRoll);
  793. M[5] = (T)(sPitch_sYaw * sRoll + cPitch * cRoll);
  794. M[9] = (T)(sPitch * cYaw);
  795. M[2] = (T)(cPitch_sYaw * cRoll + sPitch * sRoll);
  796. M[6] = (T)(cPitch_sYaw * sRoll - sPitch * cRoll);
  797. M[10] = (T)(cPitch * cYaw);
  798. return *this;
  799. }
  800. //! Sets matrix to rotation matrix defined by axis and angle, assuming LH rotation
  801. template <class T>
  802. inline CMatrix4<T> &CMatrix4<T>::setRotationAxisRadians(const T &angle, const vector3d<T> &axis)
  803. {
  804. const f64 c = cos(angle);
  805. const f64 s = sin(angle);
  806. const f64 t = 1.0 - c;
  807. const f64 tx = t * axis.X;
  808. const f64 ty = t * axis.Y;
  809. const f64 tz = t * axis.Z;
  810. const f64 sx = s * axis.X;
  811. const f64 sy = s * axis.Y;
  812. const f64 sz = s * axis.Z;
  813. M[0] = (T)(tx * axis.X + c);
  814. M[1] = (T)(tx * axis.Y + sz);
  815. M[2] = (T)(tx * axis.Z - sy);
  816. M[4] = (T)(ty * axis.X - sz);
  817. M[5] = (T)(ty * axis.Y + c);
  818. M[6] = (T)(ty * axis.Z + sx);
  819. M[8] = (T)(tz * axis.X + sy);
  820. M[9] = (T)(tz * axis.Y - sx);
  821. M[10] = (T)(tz * axis.Z + c);
  822. return *this;
  823. }
  824. /*!
  825. */
  826. template <class T>
  827. inline CMatrix4<T> &CMatrix4<T>::makeIdentity()
  828. {
  829. memset(M, 0, 16 * sizeof(T));
  830. M[0] = M[5] = M[10] = M[15] = (T)1;
  831. return *this;
  832. }
  833. /*
  834. check identity with epsilon
  835. solve floating range problems..
  836. */
  837. template <class T>
  838. inline bool CMatrix4<T>::isIdentity() const
  839. {
  840. if (!core::equals(M[12], (T)0) || !core::equals(M[13], (T)0) || !core::equals(M[14], (T)0) || !core::equals(M[15], (T)1))
  841. return false;
  842. if (!core::equals(M[0], (T)1) || !core::equals(M[1], (T)0) || !core::equals(M[2], (T)0) || !core::equals(M[3], (T)0))
  843. return false;
  844. if (!core::equals(M[4], (T)0) || !core::equals(M[5], (T)1) || !core::equals(M[6], (T)0) || !core::equals(M[7], (T)0))
  845. return false;
  846. if (!core::equals(M[8], (T)0) || !core::equals(M[9], (T)0) || !core::equals(M[10], (T)1) || !core::equals(M[11], (T)0))
  847. return false;
  848. /*
  849. if (!core::equals( M[ 0], (T)1 ) ||
  850. !core::equals( M[ 5], (T)1 ) ||
  851. !core::equals( M[10], (T)1 ) ||
  852. !core::equals( M[15], (T)1 ))
  853. return false;
  854. for (s32 i=0; i<4; ++i)
  855. for (s32 j=0; j<4; ++j)
  856. if ((j != i) && (!iszero((*this)(i,j))))
  857. return false;
  858. */
  859. return true;
  860. }
  861. /* Check orthogonality of matrix. */
  862. template <class T>
  863. inline bool CMatrix4<T>::isOrthogonal() const
  864. {
  865. T dp = M[0] * M[4] + M[1] * M[5] + M[2] * M[6] + M[3] * M[7];
  866. if (!iszero(dp))
  867. return false;
  868. dp = M[0] * M[8] + M[1] * M[9] + M[2] * M[10] + M[3] * M[11];
  869. if (!iszero(dp))
  870. return false;
  871. dp = M[0] * M[12] + M[1] * M[13] + M[2] * M[14] + M[3] * M[15];
  872. if (!iszero(dp))
  873. return false;
  874. dp = M[4] * M[8] + M[5] * M[9] + M[6] * M[10] + M[7] * M[11];
  875. if (!iszero(dp))
  876. return false;
  877. dp = M[4] * M[12] + M[5] * M[13] + M[6] * M[14] + M[7] * M[15];
  878. if (!iszero(dp))
  879. return false;
  880. dp = M[8] * M[12] + M[9] * M[13] + M[10] * M[14] + M[11] * M[15];
  881. return (iszero(dp));
  882. }
  883. /*
  884. doesn't solve floating range problems..
  885. but takes care on +/- 0 on translation because we are changing it..
  886. reducing floating point branches
  887. but it needs the floats in memory..
  888. */
  889. template <class T>
  890. inline bool CMatrix4<T>::isIdentity_integer_base() const
  891. {
  892. if (IR(M[0]) != F32_VALUE_1)
  893. return false;
  894. if (IR(M[1]) != 0)
  895. return false;
  896. if (IR(M[2]) != 0)
  897. return false;
  898. if (IR(M[3]) != 0)
  899. return false;
  900. if (IR(M[4]) != 0)
  901. return false;
  902. if (IR(M[5]) != F32_VALUE_1)
  903. return false;
  904. if (IR(M[6]) != 0)
  905. return false;
  906. if (IR(M[7]) != 0)
  907. return false;
  908. if (IR(M[8]) != 0)
  909. return false;
  910. if (IR(M[9]) != 0)
  911. return false;
  912. if (IR(M[10]) != F32_VALUE_1)
  913. return false;
  914. if (IR(M[11]) != 0)
  915. return false;
  916. if (IR(M[12]) != 0)
  917. return false;
  918. if (IR(M[13]) != 0)
  919. return false;
  920. if (IR(M[13]) != 0)
  921. return false;
  922. if (IR(M[15]) != F32_VALUE_1)
  923. return false;
  924. return true;
  925. }
  926. template <class T>
  927. inline vector3d<T> CMatrix4<T>::rotateAndScaleVect(const vector3d<T> &v) const
  928. {
  929. return {
  930. v.X * M[0] + v.Y * M[4] + v.Z * M[8],
  931. v.X * M[1] + v.Y * M[5] + v.Z * M[9],
  932. v.X * M[2] + v.Y * M[6] + v.Z * M[10]
  933. };
  934. }
  935. template <class T>
  936. inline vector3d<T> CMatrix4<T>::scaleThenInvRotVect(const vector3d<T> &v) const
  937. {
  938. return {
  939. v.X * M[0] + v.Y * M[1] + v.Z * M[2],
  940. v.X * M[4] + v.Y * M[5] + v.Z * M[6],
  941. v.X * M[8] + v.Y * M[9] + v.Z * M[10]
  942. };
  943. }
  944. template <class T>
  945. inline vector3d<T> CMatrix4<T>::transformVect(const vector3d<T> &v) const
  946. {
  947. return {
  948. v.X * M[0] + v.Y * M[4] + v.Z * M[8] + M[12],
  949. v.X * M[1] + v.Y * M[5] + v.Z * M[9] + M[13],
  950. v.X * M[2] + v.Y * M[6] + v.Z * M[10] + M[14],
  951. };
  952. }
  953. template <class T>
  954. inline void CMatrix4<T>::transformVect(T *out, const core::vector3df &in) const
  955. {
  956. out[0] = in.X * M[0] + in.Y * M[4] + in.Z * M[8] + M[12];
  957. out[1] = in.X * M[1] + in.Y * M[5] + in.Z * M[9] + M[13];
  958. out[2] = in.X * M[2] + in.Y * M[6] + in.Z * M[10] + M[14];
  959. out[3] = in.X * M[3] + in.Y * M[7] + in.Z * M[11] + M[15];
  960. }
  961. template <class T>
  962. inline void CMatrix4<T>::transformVec3(T *out, const T *in) const
  963. {
  964. out[0] = in[0] * M[0] + in[1] * M[4] + in[2] * M[8] + M[12];
  965. out[1] = in[0] * M[1] + in[1] * M[5] + in[2] * M[9] + M[13];
  966. out[2] = in[0] * M[2] + in[1] * M[6] + in[2] * M[10] + M[14];
  967. }
  968. template <class T>
  969. inline void CMatrix4<T>::transformVec4(T *out, const T *in) const
  970. {
  971. out[0] = in[0] * M[0] + in[1] * M[4] + in[2] * M[8] + in[3] * M[12];
  972. out[1] = in[0] * M[1] + in[1] * M[5] + in[2] * M[9] + in[3] * M[13];
  973. out[2] = in[0] * M[2] + in[1] * M[6] + in[2] * M[10] + in[3] * M[14];
  974. out[3] = in[0] * M[3] + in[1] * M[7] + in[2] * M[11] + in[3] * M[15];
  975. }
  976. //! Transforms a plane by this matrix
  977. template <class T>
  978. inline void CMatrix4<T>::transformPlane(core::plane3d<f32> &plane) const
  979. {
  980. vector3df member;
  981. // Transform the plane member point, i.e. rotate, translate and scale it.
  982. transformVect(member, plane.getMemberPoint());
  983. // Transform the normal by the transposed inverse of the matrix
  984. CMatrix4<T> transposedInverse(*this, EM4CONST_INVERSE_TRANSPOSED);
  985. vector3df normal = transposedInverse.rotateAndScaleVect(plane.Normal);
  986. plane.setPlane(member, normal.normalize());
  987. }
  988. //! Transforms a plane by this matrix
  989. template <class T>
  990. inline void CMatrix4<T>::transformPlane(const core::plane3d<f32> &in, core::plane3d<f32> &out) const
  991. {
  992. out = in;
  993. transformPlane(out);
  994. }
  995. //! Transforms a axis aligned bounding box more accurately than transformBox()
  996. template <class T>
  997. inline void CMatrix4<T>::transformBoxEx(core::aabbox3d<f32> &box) const
  998. {
  999. #if defined(USE_MATRIX_TEST)
  1000. if (isIdentity())
  1001. return;
  1002. #endif
  1003. const f32 Amin[3] = {box.MinEdge.X, box.MinEdge.Y, box.MinEdge.Z};
  1004. const f32 Amax[3] = {box.MaxEdge.X, box.MaxEdge.Y, box.MaxEdge.Z};
  1005. f32 Bmin[3];
  1006. f32 Bmax[3];
  1007. Bmin[0] = Bmax[0] = M[12];
  1008. Bmin[1] = Bmax[1] = M[13];
  1009. Bmin[2] = Bmax[2] = M[14];
  1010. const CMatrix4<T> &m = *this;
  1011. for (u32 i = 0; i < 3; ++i) {
  1012. for (u32 j = 0; j < 3; ++j) {
  1013. const f32 a = m(j, i) * Amin[j];
  1014. const f32 b = m(j, i) * Amax[j];
  1015. if (a < b) {
  1016. Bmin[i] += a;
  1017. Bmax[i] += b;
  1018. } else {
  1019. Bmin[i] += b;
  1020. Bmax[i] += a;
  1021. }
  1022. }
  1023. }
  1024. box.MinEdge.X = Bmin[0];
  1025. box.MinEdge.Y = Bmin[1];
  1026. box.MinEdge.Z = Bmin[2];
  1027. box.MaxEdge.X = Bmax[0];
  1028. box.MaxEdge.Y = Bmax[1];
  1029. box.MaxEdge.Z = Bmax[2];
  1030. }
  1031. //! Multiplies this matrix by a 1x4 matrix
  1032. template <class T>
  1033. inline void CMatrix4<T>::multiplyWith1x4Matrix(T *matrix) const
  1034. {
  1035. /*
  1036. 0 1 2 3
  1037. 4 5 6 7
  1038. 8 9 10 11
  1039. 12 13 14 15
  1040. */
  1041. T mat[4];
  1042. mat[0] = matrix[0];
  1043. mat[1] = matrix[1];
  1044. mat[2] = matrix[2];
  1045. mat[3] = matrix[3];
  1046. matrix[0] = M[0] * mat[0] + M[4] * mat[1] + M[8] * mat[2] + M[12] * mat[3];
  1047. matrix[1] = M[1] * mat[0] + M[5] * mat[1] + M[9] * mat[2] + M[13] * mat[3];
  1048. matrix[2] = M[2] * mat[0] + M[6] * mat[1] + M[10] * mat[2] + M[14] * mat[3];
  1049. matrix[3] = M[3] * mat[0] + M[7] * mat[1] + M[11] * mat[2] + M[15] * mat[3];
  1050. }
  1051. template <class T>
  1052. inline void CMatrix4<T>::inverseTranslateVect(vector3df &vect) const
  1053. {
  1054. vect.X = vect.X - M[12];
  1055. vect.Y = vect.Y - M[13];
  1056. vect.Z = vect.Z - M[14];
  1057. }
  1058. template <class T>
  1059. inline void CMatrix4<T>::translateVect(vector3df &vect) const
  1060. {
  1061. vect.X = vect.X + M[12];
  1062. vect.Y = vect.Y + M[13];
  1063. vect.Z = vect.Z + M[14];
  1064. }
  1065. template <class T>
  1066. inline bool CMatrix4<T>::getInverse(CMatrix4<T> &out) const
  1067. {
  1068. /// Calculates the inverse of this Matrix
  1069. /// The inverse is calculated using Cramers rule.
  1070. /// If no inverse exists then 'false' is returned.
  1071. #if defined(USE_MATRIX_TEST)
  1072. if (this->isIdentity()) {
  1073. out = *this;
  1074. return true;
  1075. }
  1076. #endif
  1077. const CMatrix4<T> &m = *this;
  1078. f32 d = (m[0] * m[5] - m[1] * m[4]) * (m[10] * m[15] - m[11] * m[14]) -
  1079. (m[0] * m[6] - m[2] * m[4]) * (m[9] * m[15] - m[11] * m[13]) +
  1080. (m[0] * m[7] - m[3] * m[4]) * (m[9] * m[14] - m[10] * m[13]) +
  1081. (m[1] * m[6] - m[2] * m[5]) * (m[8] * m[15] - m[11] * m[12]) -
  1082. (m[1] * m[7] - m[3] * m[5]) * (m[8] * m[14] - m[10] * m[12]) +
  1083. (m[2] * m[7] - m[3] * m[6]) * (m[8] * m[13] - m[9] * m[12]);
  1084. if (core::iszero(d, FLT_MIN))
  1085. return false;
  1086. d = core::reciprocal(d);
  1087. out[0] = d * (m[5] * (m[10] * m[15] - m[11] * m[14]) +
  1088. m[6] * (m[11] * m[13] - m[9] * m[15]) +
  1089. m[7] * (m[9] * m[14] - m[10] * m[13]));
  1090. out[1] = d * (m[9] * (m[2] * m[15] - m[3] * m[14]) +
  1091. m[10] * (m[3] * m[13] - m[1] * m[15]) +
  1092. m[11] * (m[1] * m[14] - m[2] * m[13]));
  1093. out[2] = d * (m[13] * (m[2] * m[7] - m[3] * m[6]) +
  1094. m[14] * (m[3] * m[5] - m[1] * m[7]) +
  1095. m[15] * (m[1] * m[6] - m[2] * m[5]));
  1096. out[3] = d * (m[1] * (m[7] * m[10] - m[6] * m[11]) +
  1097. m[2] * (m[5] * m[11] - m[7] * m[9]) +
  1098. m[3] * (m[6] * m[9] - m[5] * m[10]));
  1099. out[4] = d * (m[6] * (m[8] * m[15] - m[11] * m[12]) +
  1100. m[7] * (m[10] * m[12] - m[8] * m[14]) +
  1101. m[4] * (m[11] * m[14] - m[10] * m[15]));
  1102. out[5] = d * (m[10] * (m[0] * m[15] - m[3] * m[12]) +
  1103. m[11] * (m[2] * m[12] - m[0] * m[14]) +
  1104. m[8] * (m[3] * m[14] - m[2] * m[15]));
  1105. out[6] = d * (m[14] * (m[0] * m[7] - m[3] * m[4]) +
  1106. m[15] * (m[2] * m[4] - m[0] * m[6]) +
  1107. m[12] * (m[3] * m[6] - m[2] * m[7]));
  1108. out[7] = d * (m[2] * (m[7] * m[8] - m[4] * m[11]) +
  1109. m[3] * (m[4] * m[10] - m[6] * m[8]) +
  1110. m[0] * (m[6] * m[11] - m[7] * m[10]));
  1111. out[8] = d * (m[7] * (m[8] * m[13] - m[9] * m[12]) +
  1112. m[4] * (m[9] * m[15] - m[11] * m[13]) +
  1113. m[5] * (m[11] * m[12] - m[8] * m[15]));
  1114. out[9] = d * (m[11] * (m[0] * m[13] - m[1] * m[12]) +
  1115. m[8] * (m[1] * m[15] - m[3] * m[13]) +
  1116. m[9] * (m[3] * m[12] - m[0] * m[15]));
  1117. out[10] = d * (m[15] * (m[0] * m[5] - m[1] * m[4]) +
  1118. m[12] * (m[1] * m[7] - m[3] * m[5]) +
  1119. m[13] * (m[3] * m[4] - m[0] * m[7]));
  1120. out[11] = d * (m[3] * (m[5] * m[8] - m[4] * m[9]) +
  1121. m[0] * (m[7] * m[9] - m[5] * m[11]) +
  1122. m[1] * (m[4] * m[11] - m[7] * m[8]));
  1123. out[12] = d * (m[4] * (m[10] * m[13] - m[9] * m[14]) +
  1124. m[5] * (m[8] * m[14] - m[10] * m[12]) +
  1125. m[6] * (m[9] * m[12] - m[8] * m[13]));
  1126. out[13] = d * (m[8] * (m[2] * m[13] - m[1] * m[14]) +
  1127. m[9] * (m[0] * m[14] - m[2] * m[12]) +
  1128. m[10] * (m[1] * m[12] - m[0] * m[13]));
  1129. out[14] = d * (m[12] * (m[2] * m[5] - m[1] * m[6]) +
  1130. m[13] * (m[0] * m[6] - m[2] * m[4]) +
  1131. m[14] * (m[1] * m[4] - m[0] * m[5]));
  1132. out[15] = d * (m[0] * (m[5] * m[10] - m[6] * m[9]) +
  1133. m[1] * (m[6] * m[8] - m[4] * m[10]) +
  1134. m[2] * (m[4] * m[9] - m[5] * m[8]));
  1135. return true;
  1136. }
  1137. //! Inverts a primitive matrix which only contains a translation and a rotation
  1138. //! \param out: where result matrix is written to.
  1139. template <class T>
  1140. inline bool CMatrix4<T>::getInversePrimitive(CMatrix4<T> &out) const
  1141. {
  1142. out.M[0] = M[0];
  1143. out.M[1] = M[4];
  1144. out.M[2] = M[8];
  1145. out.M[3] = 0;
  1146. out.M[4] = M[1];
  1147. out.M[5] = M[5];
  1148. out.M[6] = M[9];
  1149. out.M[7] = 0;
  1150. out.M[8] = M[2];
  1151. out.M[9] = M[6];
  1152. out.M[10] = M[10];
  1153. out.M[11] = 0;
  1154. out.M[12] = (T) - (M[12] * M[0] + M[13] * M[1] + M[14] * M[2]);
  1155. out.M[13] = (T) - (M[12] * M[4] + M[13] * M[5] + M[14] * M[6]);
  1156. out.M[14] = (T) - (M[12] * M[8] + M[13] * M[9] + M[14] * M[10]);
  1157. out.M[15] = 1;
  1158. return true;
  1159. }
  1160. /*!
  1161. */
  1162. template <class T>
  1163. inline bool CMatrix4<T>::makeInverse()
  1164. {
  1165. CMatrix4<T> temp(EM4CONST_NOTHING);
  1166. if (getInverse(temp)) {
  1167. *this = temp;
  1168. return true;
  1169. }
  1170. return false;
  1171. }
  1172. template <class T>
  1173. inline CMatrix4<T> &CMatrix4<T>::operator=(const T &scalar)
  1174. {
  1175. for (s32 i = 0; i < 16; ++i)
  1176. M[i] = scalar;
  1177. return *this;
  1178. }
  1179. // Builds a right-handed perspective projection matrix based on a field of view
  1180. template <class T>
  1181. inline CMatrix4<T> &CMatrix4<T>::buildProjectionMatrixPerspectiveFovRH(
  1182. f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar, bool zClipFromZero)
  1183. {
  1184. const f64 h = reciprocal(tan(fieldOfViewRadians * 0.5));
  1185. _IRR_DEBUG_BREAK_IF(aspectRatio == 0.f); // divide by zero
  1186. const T w = static_cast<T>(h / aspectRatio);
  1187. _IRR_DEBUG_BREAK_IF(zNear == zFar); // divide by zero
  1188. M[0] = w;
  1189. M[1] = 0;
  1190. M[2] = 0;
  1191. M[3] = 0;
  1192. M[4] = 0;
  1193. M[5] = (T)h;
  1194. M[6] = 0;
  1195. M[7] = 0;
  1196. M[8] = 0;
  1197. M[9] = 0;
  1198. // M[10]
  1199. M[11] = -1;
  1200. M[12] = 0;
  1201. M[13] = 0;
  1202. // M[14]
  1203. M[15] = 0;
  1204. if (zClipFromZero) { // DirectX version
  1205. M[10] = (T)(zFar / (zNear - zFar));
  1206. M[14] = (T)(zNear * zFar / (zNear - zFar));
  1207. } else // OpenGL version
  1208. {
  1209. M[10] = (T)((zFar + zNear) / (zNear - zFar));
  1210. M[14] = (T)(2.0f * zNear * zFar / (zNear - zFar));
  1211. }
  1212. return *this;
  1213. }
  1214. // Builds a left-handed perspective projection matrix based on a field of view
  1215. template <class T>
  1216. inline CMatrix4<T> &CMatrix4<T>::buildProjectionMatrixPerspectiveFovLH(
  1217. f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 zFar, bool zClipFromZero)
  1218. {
  1219. const f64 h = reciprocal(tan(fieldOfViewRadians * 0.5));
  1220. _IRR_DEBUG_BREAK_IF(aspectRatio == 0.f); // divide by zero
  1221. const T w = static_cast<T>(h / aspectRatio);
  1222. _IRR_DEBUG_BREAK_IF(zNear == zFar); // divide by zero
  1223. M[0] = w;
  1224. M[1] = 0;
  1225. M[2] = 0;
  1226. M[3] = 0;
  1227. M[4] = 0;
  1228. M[5] = (T)h;
  1229. M[6] = 0;
  1230. M[7] = 0;
  1231. M[8] = 0;
  1232. M[9] = 0;
  1233. // M[10]
  1234. M[11] = 1;
  1235. M[12] = 0;
  1236. M[13] = 0;
  1237. // M[14]
  1238. M[15] = 0;
  1239. if (zClipFromZero) { // DirectX version
  1240. M[10] = (T)(zFar / (zFar - zNear));
  1241. M[14] = (T)(-zNear * zFar / (zFar - zNear));
  1242. } else // OpenGL version
  1243. {
  1244. M[10] = (T)((zFar + zNear) / (zFar - zNear));
  1245. M[14] = (T)(2.0f * zNear * zFar / (zNear - zFar));
  1246. }
  1247. return *this;
  1248. }
  1249. // Builds a left-handed perspective projection matrix based on a field of view, with far plane culling at infinity
  1250. template <class T>
  1251. inline CMatrix4<T> &CMatrix4<T>::buildProjectionMatrixPerspectiveFovInfinityLH(
  1252. f32 fieldOfViewRadians, f32 aspectRatio, f32 zNear, f32 epsilon)
  1253. {
  1254. const f64 h = reciprocal(tan(fieldOfViewRadians * 0.5));
  1255. _IRR_DEBUG_BREAK_IF(aspectRatio == 0.f); // divide by zero
  1256. const T w = static_cast<T>(h / aspectRatio);
  1257. M[0] = w;
  1258. M[1] = 0;
  1259. M[2] = 0;
  1260. M[3] = 0;
  1261. M[4] = 0;
  1262. M[5] = (T)h;
  1263. M[6] = 0;
  1264. M[7] = 0;
  1265. M[8] = 0;
  1266. M[9] = 0;
  1267. M[10] = (T)(1.f - epsilon);
  1268. M[11] = 1;
  1269. M[12] = 0;
  1270. M[13] = 0;
  1271. M[14] = (T)(zNear * (epsilon - 1.f));
  1272. M[15] = 0;
  1273. return *this;
  1274. }
  1275. // Builds a left-handed orthogonal projection matrix.
  1276. template <class T>
  1277. inline CMatrix4<T> &CMatrix4<T>::buildProjectionMatrixOrthoLH(
  1278. f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar, bool zClipFromZero)
  1279. {
  1280. _IRR_DEBUG_BREAK_IF(widthOfViewVolume == 0.f); // divide by zero
  1281. _IRR_DEBUG_BREAK_IF(heightOfViewVolume == 0.f); // divide by zero
  1282. _IRR_DEBUG_BREAK_IF(zNear == zFar); // divide by zero
  1283. M[0] = (T)(2 / widthOfViewVolume);
  1284. M[1] = 0;
  1285. M[2] = 0;
  1286. M[3] = 0;
  1287. M[4] = 0;
  1288. M[5] = (T)(2 / heightOfViewVolume);
  1289. M[6] = 0;
  1290. M[7] = 0;
  1291. M[8] = 0;
  1292. M[9] = 0;
  1293. // M[10]
  1294. M[11] = 0;
  1295. M[12] = 0;
  1296. M[13] = 0;
  1297. // M[14]
  1298. M[15] = 1;
  1299. if (zClipFromZero) {
  1300. M[10] = (T)(1 / (zFar - zNear));
  1301. M[14] = (T)(zNear / (zNear - zFar));
  1302. } else {
  1303. M[10] = (T)(2 / (zFar - zNear));
  1304. M[14] = (T) - (zFar + zNear) / (zFar - zNear);
  1305. }
  1306. return *this;
  1307. }
  1308. // Builds a right-handed orthogonal projection matrix.
  1309. template <class T>
  1310. inline CMatrix4<T> &CMatrix4<T>::buildProjectionMatrixOrthoRH(
  1311. f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar, bool zClipFromZero)
  1312. {
  1313. _IRR_DEBUG_BREAK_IF(widthOfViewVolume == 0.f); // divide by zero
  1314. _IRR_DEBUG_BREAK_IF(heightOfViewVolume == 0.f); // divide by zero
  1315. _IRR_DEBUG_BREAK_IF(zNear == zFar); // divide by zero
  1316. M[0] = (T)(2 / widthOfViewVolume);
  1317. M[1] = 0;
  1318. M[2] = 0;
  1319. M[3] = 0;
  1320. M[4] = 0;
  1321. M[5] = (T)(2 / heightOfViewVolume);
  1322. M[6] = 0;
  1323. M[7] = 0;
  1324. M[8] = 0;
  1325. M[9] = 0;
  1326. // M[10]
  1327. M[11] = 0;
  1328. M[12] = 0;
  1329. M[13] = 0;
  1330. // M[14]
  1331. M[15] = 1;
  1332. if (zClipFromZero) {
  1333. M[10] = (T)(1 / (zNear - zFar));
  1334. M[14] = (T)(zNear / (zNear - zFar));
  1335. } else {
  1336. M[10] = (T)(2 / (zNear - zFar));
  1337. M[14] = (T) - (zFar + zNear) / (zFar - zNear);
  1338. }
  1339. return *this;
  1340. }
  1341. // Builds a right-handed perspective projection matrix.
  1342. template <class T>
  1343. inline CMatrix4<T> &CMatrix4<T>::buildProjectionMatrixPerspectiveRH(
  1344. f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar, bool zClipFromZero)
  1345. {
  1346. _IRR_DEBUG_BREAK_IF(widthOfViewVolume == 0.f); // divide by zero
  1347. _IRR_DEBUG_BREAK_IF(heightOfViewVolume == 0.f); // divide by zero
  1348. _IRR_DEBUG_BREAK_IF(zNear == zFar); // divide by zero
  1349. M[0] = (T)(2 * zNear / widthOfViewVolume);
  1350. M[1] = 0;
  1351. M[2] = 0;
  1352. M[3] = 0;
  1353. M[4] = 0;
  1354. M[5] = (T)(2 * zNear / heightOfViewVolume);
  1355. M[6] = 0;
  1356. M[7] = 0;
  1357. M[8] = 0;
  1358. M[9] = 0;
  1359. // M[10]
  1360. M[11] = -1;
  1361. M[12] = 0;
  1362. M[13] = 0;
  1363. // M[14]
  1364. M[15] = 0;
  1365. if (zClipFromZero) { // DirectX version
  1366. M[10] = (T)(zFar / (zNear - zFar));
  1367. M[14] = (T)(zNear * zFar / (zNear - zFar));
  1368. } else // OpenGL version
  1369. {
  1370. M[10] = (T)((zFar + zNear) / (zNear - zFar));
  1371. M[14] = (T)(2.0f * zNear * zFar / (zNear - zFar));
  1372. }
  1373. return *this;
  1374. }
  1375. // Builds a left-handed perspective projection matrix.
  1376. template <class T>
  1377. inline CMatrix4<T> &CMatrix4<T>::buildProjectionMatrixPerspectiveLH(
  1378. f32 widthOfViewVolume, f32 heightOfViewVolume, f32 zNear, f32 zFar, bool zClipFromZero)
  1379. {
  1380. _IRR_DEBUG_BREAK_IF(widthOfViewVolume == 0.f); // divide by zero
  1381. _IRR_DEBUG_BREAK_IF(heightOfViewVolume == 0.f); // divide by zero
  1382. _IRR_DEBUG_BREAK_IF(zNear == zFar); // divide by zero
  1383. M[0] = (T)(2 * zNear / widthOfViewVolume);
  1384. M[1] = 0;
  1385. M[2] = 0;
  1386. M[3] = 0;
  1387. M[4] = 0;
  1388. M[5] = (T)(2 * zNear / heightOfViewVolume);
  1389. M[6] = 0;
  1390. M[7] = 0;
  1391. M[8] = 0;
  1392. M[9] = 0;
  1393. // M[10]
  1394. M[11] = 1;
  1395. M[12] = 0;
  1396. M[13] = 0;
  1397. // M[14] = (T)(zNear*zFar/(zNear-zFar));
  1398. M[15] = 0;
  1399. if (zClipFromZero) { // DirectX version
  1400. M[10] = (T)(zFar / (zFar - zNear));
  1401. M[14] = (T)(zNear * zFar / (zNear - zFar));
  1402. } else // OpenGL version
  1403. {
  1404. M[10] = (T)((zFar + zNear) / (zFar - zNear));
  1405. M[14] = (T)(2.0f * zNear * zFar / (zNear - zFar));
  1406. }
  1407. return *this;
  1408. }
  1409. // Builds a matrix that flattens geometry into a plane.
  1410. template <class T>
  1411. inline CMatrix4<T> &CMatrix4<T>::buildShadowMatrix(const core::vector3df &light, core::plane3df plane, f32 point)
  1412. {
  1413. plane.Normal.normalize();
  1414. const f32 d = plane.Normal.dotProduct(light);
  1415. M[0] = (T)(-plane.Normal.X * light.X + d);
  1416. M[1] = (T)(-plane.Normal.X * light.Y);
  1417. M[2] = (T)(-plane.Normal.X * light.Z);
  1418. M[3] = (T)(-plane.Normal.X * point);
  1419. M[4] = (T)(-plane.Normal.Y * light.X);
  1420. M[5] = (T)(-plane.Normal.Y * light.Y + d);
  1421. M[6] = (T)(-plane.Normal.Y * light.Z);
  1422. M[7] = (T)(-plane.Normal.Y * point);
  1423. M[8] = (T)(-plane.Normal.Z * light.X);
  1424. M[9] = (T)(-plane.Normal.Z * light.Y);
  1425. M[10] = (T)(-plane.Normal.Z * light.Z + d);
  1426. M[11] = (T)(-plane.Normal.Z * point);
  1427. M[12] = (T)(-plane.D * light.X);
  1428. M[13] = (T)(-plane.D * light.Y);
  1429. M[14] = (T)(-plane.D * light.Z);
  1430. M[15] = (T)(-plane.D * point + d);
  1431. return *this;
  1432. }
  1433. // Builds a left-handed look-at matrix.
  1434. template <class T>
  1435. inline CMatrix4<T> &CMatrix4<T>::buildCameraLookAtMatrixLH(
  1436. const vector3df &position,
  1437. const vector3df &target,
  1438. const vector3df &upVector)
  1439. {
  1440. vector3df zaxis = target - position;
  1441. zaxis.normalize();
  1442. vector3df xaxis = upVector.crossProduct(zaxis);
  1443. xaxis.normalize();
  1444. vector3df yaxis = zaxis.crossProduct(xaxis);
  1445. M[0] = (T)xaxis.X;
  1446. M[1] = (T)yaxis.X;
  1447. M[2] = (T)zaxis.X;
  1448. M[3] = 0;
  1449. M[4] = (T)xaxis.Y;
  1450. M[5] = (T)yaxis.Y;
  1451. M[6] = (T)zaxis.Y;
  1452. M[7] = 0;
  1453. M[8] = (T)xaxis.Z;
  1454. M[9] = (T)yaxis.Z;
  1455. M[10] = (T)zaxis.Z;
  1456. M[11] = 0;
  1457. M[12] = (T)-xaxis.dotProduct(position);
  1458. M[13] = (T)-yaxis.dotProduct(position);
  1459. M[14] = (T)-zaxis.dotProduct(position);
  1460. M[15] = 1;
  1461. return *this;
  1462. }
  1463. // Builds a right-handed look-at matrix.
  1464. template <class T>
  1465. inline CMatrix4<T> &CMatrix4<T>::buildCameraLookAtMatrixRH(
  1466. const vector3df &position,
  1467. const vector3df &target,
  1468. const vector3df &upVector)
  1469. {
  1470. vector3df zaxis = position - target;
  1471. zaxis.normalize();
  1472. vector3df xaxis = upVector.crossProduct(zaxis);
  1473. xaxis.normalize();
  1474. vector3df yaxis = zaxis.crossProduct(xaxis);
  1475. M[0] = (T)xaxis.X;
  1476. M[1] = (T)yaxis.X;
  1477. M[2] = (T)zaxis.X;
  1478. M[3] = 0;
  1479. M[4] = (T)xaxis.Y;
  1480. M[5] = (T)yaxis.Y;
  1481. M[6] = (T)zaxis.Y;
  1482. M[7] = 0;
  1483. M[8] = (T)xaxis.Z;
  1484. M[9] = (T)yaxis.Z;
  1485. M[10] = (T)zaxis.Z;
  1486. M[11] = 0;
  1487. M[12] = (T)-xaxis.dotProduct(position);
  1488. M[13] = (T)-yaxis.dotProduct(position);
  1489. M[14] = (T)-zaxis.dotProduct(position);
  1490. M[15] = 1;
  1491. return *this;
  1492. }
  1493. // creates a new matrix as interpolated matrix from this and the passed one.
  1494. template <class T>
  1495. inline CMatrix4<T> CMatrix4<T>::interpolate(const core::CMatrix4<T> &b, f32 time) const
  1496. {
  1497. CMatrix4<T> mat(EM4CONST_NOTHING);
  1498. for (u32 i = 0; i < 16; i += 4) {
  1499. mat.M[i + 0] = (T)(M[i + 0] + (b.M[i + 0] - M[i + 0]) * time);
  1500. mat.M[i + 1] = (T)(M[i + 1] + (b.M[i + 1] - M[i + 1]) * time);
  1501. mat.M[i + 2] = (T)(M[i + 2] + (b.M[i + 2] - M[i + 2]) * time);
  1502. mat.M[i + 3] = (T)(M[i + 3] + (b.M[i + 3] - M[i + 3]) * time);
  1503. }
  1504. return mat;
  1505. }
  1506. // returns transposed matrix
  1507. template <class T>
  1508. inline CMatrix4<T> CMatrix4<T>::getTransposed() const
  1509. {
  1510. CMatrix4<T> t(EM4CONST_NOTHING);
  1511. getTransposed(t);
  1512. return t;
  1513. }
  1514. // returns transposed matrix
  1515. template <class T>
  1516. inline void CMatrix4<T>::getTransposed(CMatrix4<T> &o) const
  1517. {
  1518. o[0] = M[0];
  1519. o[1] = M[4];
  1520. o[2] = M[8];
  1521. o[3] = M[12];
  1522. o[4] = M[1];
  1523. o[5] = M[5];
  1524. o[6] = M[9];
  1525. o[7] = M[13];
  1526. o[8] = M[2];
  1527. o[9] = M[6];
  1528. o[10] = M[10];
  1529. o[11] = M[14];
  1530. o[12] = M[3];
  1531. o[13] = M[7];
  1532. o[14] = M[11];
  1533. o[15] = M[15];
  1534. }
  1535. // used to scale <-1,-1><1,1> to viewport
  1536. template <class T>
  1537. inline CMatrix4<T> &CMatrix4<T>::buildNDCToDCMatrix(const core::rect<s32> &viewport, f32 zScale)
  1538. {
  1539. const f32 scaleX = (viewport.getWidth() - 0.75f) * 0.5f;
  1540. const f32 scaleY = -(viewport.getHeight() - 0.75f) * 0.5f;
  1541. const f32 dx = -0.5f + ((viewport.UpperLeftCorner.X + viewport.LowerRightCorner.X) * 0.5f);
  1542. const f32 dy = -0.5f + ((viewport.UpperLeftCorner.Y + viewport.LowerRightCorner.Y) * 0.5f);
  1543. makeIdentity();
  1544. M[12] = (T)dx;
  1545. M[13] = (T)dy;
  1546. return setScale(core::vector3d<T>((T)scaleX, (T)scaleY, (T)zScale));
  1547. }
  1548. //! Builds a matrix that rotates from one vector to another
  1549. /** \param from: vector to rotate from
  1550. \param to: vector to rotate to
  1551. http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToMatrix/index.htm
  1552. */
  1553. template <class T>
  1554. inline CMatrix4<T> &CMatrix4<T>::buildRotateFromTo(const core::vector3df &from, const core::vector3df &to)
  1555. {
  1556. // unit vectors
  1557. core::vector3df f(from);
  1558. core::vector3df t(to);
  1559. f.normalize();
  1560. t.normalize();
  1561. // axis multiplication by sin
  1562. core::vector3df vs(t.crossProduct(f));
  1563. // axis of rotation
  1564. core::vector3df v(vs);
  1565. v.normalize();
  1566. // cosine angle
  1567. T ca = f.dotProduct(t);
  1568. core::vector3df vt(v * (1 - ca));
  1569. M[0] = vt.X * v.X + ca;
  1570. M[5] = vt.Y * v.Y + ca;
  1571. M[10] = vt.Z * v.Z + ca;
  1572. vt.X *= v.Y;
  1573. vt.Z *= v.X;
  1574. vt.Y *= v.Z;
  1575. M[1] = vt.X - vs.Z;
  1576. M[2] = vt.Z + vs.Y;
  1577. M[3] = 0;
  1578. M[4] = vt.X + vs.Z;
  1579. M[6] = vt.Y - vs.X;
  1580. M[7] = 0;
  1581. M[8] = vt.Z - vs.Y;
  1582. M[9] = vt.Y + vs.X;
  1583. M[11] = 0;
  1584. M[12] = 0;
  1585. M[13] = 0;
  1586. M[14] = 0;
  1587. M[15] = 1;
  1588. return *this;
  1589. }
  1590. //! Builds a matrix which rotates a source vector to a look vector over an arbitrary axis
  1591. /** \param camPos: viewer position in world coord
  1592. \param center: object position in world-coord, rotation pivot
  1593. \param translation: object final translation from center
  1594. \param axis: axis to rotate about
  1595. \param from: source vector to rotate from
  1596. */
  1597. template <class T>
  1598. inline void CMatrix4<T>::buildAxisAlignedBillboard(
  1599. const core::vector3df &camPos,
  1600. const core::vector3df &center,
  1601. const core::vector3df &translation,
  1602. const core::vector3df &axis,
  1603. const core::vector3df &from)
  1604. {
  1605. // axis of rotation
  1606. core::vector3df up = axis;
  1607. up.normalize();
  1608. const core::vector3df forward = (camPos - center).normalize();
  1609. const core::vector3df right = up.crossProduct(forward).normalize();
  1610. // correct look vector
  1611. const core::vector3df look = right.crossProduct(up);
  1612. // rotate from to
  1613. // axis multiplication by sin
  1614. const core::vector3df vs = look.crossProduct(from);
  1615. // cosine angle
  1616. const f32 ca = from.dotProduct(look);
  1617. core::vector3df vt(up * (1.f - ca));
  1618. M[0] = static_cast<T>(vt.X * up.X + ca);
  1619. M[5] = static_cast<T>(vt.Y * up.Y + ca);
  1620. M[10] = static_cast<T>(vt.Z * up.Z + ca);
  1621. vt.X *= up.Y;
  1622. vt.Z *= up.X;
  1623. vt.Y *= up.Z;
  1624. M[1] = static_cast<T>(vt.X - vs.Z);
  1625. M[2] = static_cast<T>(vt.Z + vs.Y);
  1626. M[3] = 0;
  1627. M[4] = static_cast<T>(vt.X + vs.Z);
  1628. M[6] = static_cast<T>(vt.Y - vs.X);
  1629. M[7] = 0;
  1630. M[8] = static_cast<T>(vt.Z - vs.Y);
  1631. M[9] = static_cast<T>(vt.Y + vs.X);
  1632. M[11] = 0;
  1633. setRotationCenter(center, translation);
  1634. }
  1635. //! Builds a combined matrix which translate to a center before rotation and translate afterward
  1636. template <class T>
  1637. inline void CMatrix4<T>::setRotationCenter(const core::vector3df &center, const core::vector3df &translation)
  1638. {
  1639. M[12] = -M[0] * center.X - M[4] * center.Y - M[8] * center.Z + (center.X - translation.X);
  1640. M[13] = -M[1] * center.X - M[5] * center.Y - M[9] * center.Z + (center.Y - translation.Y);
  1641. M[14] = -M[2] * center.X - M[6] * center.Y - M[10] * center.Z + (center.Z - translation.Z);
  1642. M[15] = (T)1.0;
  1643. }
  1644. /*!
  1645. Generate texture coordinates as linear functions so that:
  1646. u = Ux*x + Uy*y + Uz*z + Uw
  1647. v = Vx*x + Vy*y + Vz*z + Vw
  1648. The matrix M for this case is:
  1649. Ux Vx 0 0
  1650. Uy Vy 0 0
  1651. Uz Vz 0 0
  1652. Uw Vw 0 0
  1653. */
  1654. template <class T>
  1655. inline CMatrix4<T> &CMatrix4<T>::buildTextureTransform(f32 rotateRad,
  1656. const core::vector2df &rotatecenter,
  1657. const core::vector2df &translate,
  1658. const core::vector2df &scale)
  1659. {
  1660. const f32 c = cosf(rotateRad);
  1661. const f32 s = sinf(rotateRad);
  1662. M[0] = (T)(c * scale.X);
  1663. M[1] = (T)(s * scale.Y);
  1664. M[2] = 0;
  1665. M[3] = 0;
  1666. M[4] = (T)(-s * scale.X);
  1667. M[5] = (T)(c * scale.Y);
  1668. M[6] = 0;
  1669. M[7] = 0;
  1670. M[8] = (T)(c * scale.X * rotatecenter.X + -s * rotatecenter.Y + translate.X);
  1671. M[9] = (T)(s * scale.Y * rotatecenter.X + c * rotatecenter.Y + translate.Y);
  1672. M[10] = 1;
  1673. M[11] = 0;
  1674. M[12] = 0;
  1675. M[13] = 0;
  1676. M[14] = 0;
  1677. M[15] = 1;
  1678. return *this;
  1679. }
  1680. // rotate about z axis, center ( 0.5, 0.5 )
  1681. template <class T>
  1682. inline CMatrix4<T> &CMatrix4<T>::setTextureRotationCenter(f32 rotateRad)
  1683. {
  1684. const f32 c = cosf(rotateRad);
  1685. const f32 s = sinf(rotateRad);
  1686. M[0] = (T)c;
  1687. M[1] = (T)s;
  1688. M[4] = (T)-s;
  1689. M[5] = (T)c;
  1690. M[8] = (T)(0.5f * (s - c) + 0.5f);
  1691. M[9] = (T)(-0.5f * (s + c) + 0.5f);
  1692. return *this;
  1693. }
  1694. template <class T>
  1695. inline CMatrix4<T> &CMatrix4<T>::setTextureTranslate(f32 x, f32 y)
  1696. {
  1697. M[8] = (T)x;
  1698. M[9] = (T)y;
  1699. return *this;
  1700. }
  1701. template <class T>
  1702. inline void CMatrix4<T>::getTextureTranslate(f32 &x, f32 &y) const
  1703. {
  1704. x = (f32)M[8];
  1705. y = (f32)M[9];
  1706. }
  1707. template <class T>
  1708. inline CMatrix4<T> &CMatrix4<T>::setTextureTranslateTransposed(f32 x, f32 y)
  1709. {
  1710. M[2] = (T)x;
  1711. M[6] = (T)y;
  1712. return *this;
  1713. }
  1714. template <class T>
  1715. inline CMatrix4<T> &CMatrix4<T>::setTextureScale(f32 sx, f32 sy)
  1716. {
  1717. M[0] = (T)sx;
  1718. M[5] = (T)sy;
  1719. return *this;
  1720. }
  1721. template <class T>
  1722. inline void CMatrix4<T>::getTextureScale(f32 &sx, f32 &sy) const
  1723. {
  1724. sx = (f32)M[0];
  1725. sy = (f32)M[5];
  1726. }
  1727. template <class T>
  1728. inline CMatrix4<T> &CMatrix4<T>::setTextureScaleCenter(f32 sx, f32 sy)
  1729. {
  1730. M[0] = (T)sx;
  1731. M[5] = (T)sy;
  1732. M[8] = (T)(0.5f - 0.5f * sx);
  1733. M[9] = (T)(0.5f - 0.5f * sy);
  1734. return *this;
  1735. }
  1736. // sets all matrix data members at once
  1737. template <class T>
  1738. inline CMatrix4<T> &CMatrix4<T>::setM(const T *data)
  1739. {
  1740. memcpy(M, data, 16 * sizeof(T));
  1741. return *this;
  1742. }
  1743. //! Compare two matrices using the equal method
  1744. template <class T>
  1745. inline bool CMatrix4<T>::equals(const core::CMatrix4<T> &other, const T tolerance) const
  1746. {
  1747. for (s32 i = 0; i < 16; ++i)
  1748. if (!core::equals(M[i], other.M[i], tolerance))
  1749. return false;
  1750. return true;
  1751. }
  1752. // Multiply by scalar.
  1753. template <class T>
  1754. inline CMatrix4<T> operator*(const T scalar, const CMatrix4<T> &mat)
  1755. {
  1756. return mat * scalar;
  1757. }
  1758. //! Typedef for f32 matrix
  1759. typedef CMatrix4<f32> matrix4;
  1760. //! global const identity matrix
  1761. IRRLICHT_API extern const matrix4 IdentityMatrix;
  1762. } // end namespace core
  1763. } // end namespace irr