1
0

matrix4.h 63 KB

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