Matrix.h 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /******************************************************************************
  2. Use 'Matrix3' to represent objects orientation and scale.
  3. Use 'Matrix' to represent objects orientation, scale and position.
  4. Use 'GetVel' to calculate objects velocities according to its previous and current matrix.
  5. Use 'SetMatrix' to set mesh rendering matrix (use before manual drawing only).
  6. /******************************************************************************/
  7. struct Matrix3 // Matrix 3x3 (orientation + scale)
  8. {
  9. Vec x, // right vector
  10. y, // up vector
  11. z; // forward vector
  12. // transform
  13. Matrix3& operator*=( Flt f);
  14. Matrix3& operator/=( Flt f);
  15. Matrix3& operator*=(C Vec &v);
  16. Matrix3& operator/=(C Vec &v);
  17. Matrix3& operator+=(C Matrix3 &m);
  18. Matrix3& operator-=(C Matrix3 &m);
  19. Matrix3& operator*=(C Matrix3 &m) {return mul(m);}
  20. Matrix3& operator/=(C Matrix3 &m) {return div(m);}
  21. Matrix3& operator*=(C RevMatrix3 &m) {return mul(m);}
  22. Bool operator==(C Matrix3 &m)C;
  23. Bool operator!=(C Matrix3 &m)C;
  24. friend Matrix3 operator* (C Matrix3 &a, C Matrix3 &b) {Matrix3 temp; a.mul (b, temp); return temp;} // get a*b
  25. friend Matrix3 operator* (C Matrix3 &a, C RevMatrix3 &b) {Matrix3 temp; a.mul (b, temp); return temp;} // get a*b
  26. friend Matrix3 operator/ (C Matrix3 &a, C Matrix3 &b) {Matrix3 temp; a.div (b, temp); return temp;} // get a/b
  27. friend Matrix3 operator~ (C Matrix3 &m ) {Matrix3 temp; m.inverse( temp); return temp;} // get inversed 'm'
  28. void mul(C Matrix3 &matrix, Matrix3 &dest)C; // multiply self by 'matrix' and store result in 'dest'
  29. void mul(C RevMatrix3 &matrix, Matrix3 &dest)C; // multiply self by 'matrix' and store result in 'dest'
  30. Matrix3& mul(C Matrix3 &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  31. Matrix3& mul(C RevMatrix3 &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  32. void div(C Matrix3 &matrix, Matrix3 &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  33. Matrix3& div(C Matrix3 &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  34. void divNormalized(C Matrix3 &matrix, Matrix3 &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  35. Matrix3& divNormalized(C Matrix3 &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  36. void inverse(Matrix3 &dest, Bool normalized=false)C; // inverse self to 'dest', if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  37. Matrix3& inverse( Bool normalized=false) {inverse(T, normalized); return T;} // inverse self , if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  38. void inverseNonOrthogonal(Matrix3 &dest)C; // inverse self to 'dest', this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  39. Matrix3& inverseNonOrthogonal( ) {inverseNonOrthogonal(T); return T;} // inverse self , this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  40. Matrix3& inverseScale(); // inverse scale
  41. Matrix3& normalize( ); // normalize scale , this sets the length of 'x' 'y' 'z' vectors to 1
  42. Matrix3& normalize( Flt scale); // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale'
  43. Matrix3& normalize(C Vec &scale); // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale.x' 'scale.y' 'scale.z'
  44. Matrix3& scale ( Flt scale) {T*=scale; return T;} // scale
  45. Matrix3& scale ( C Vec &scale) {T*=scale; return T;} // scale
  46. Matrix3& scaleL ( C Vec &scale); // scale in local space
  47. Matrix3& scale (C Vec &dir, Flt scale); // scale along 'dir' direction by 'scale' value, 'dir' must be normalized
  48. Matrix3& scalePlane(C Vec &nrm, Flt scale); // scale along plane of 'nrm' normal by 'scale' value, 'nrm' must be normalized
  49. Matrix3& rotateX ( Flt angle); // rotate by x axis
  50. Matrix3& rotateY ( Flt angle); // rotate by y axis
  51. Matrix3& rotateZ ( Flt angle); // rotate by z axis
  52. Matrix3& rotateXY( Flt x , Flt y ); // rotate by x axis and then by y axis, works the same as "rotateX(x).rotateY(y)" but faster
  53. Matrix3& rotate (C Vec &axis, Flt angle); // rotate by vector, 'axis' must be normalized
  54. Matrix3& rotateXL( Flt angle); // rotate matrix by its x vector (x-axis rotation in local space)
  55. Matrix3& rotateYL( Flt angle); // rotate matrix by its y vector (y-axis rotation in local space)
  56. Matrix3& rotateZL( Flt angle); // rotate matrix by its z vector (z-axis rotation in local space)
  57. Matrix3& rotateXLOrthoNormalized(Flt angle); // rotate matrix by its x vector (x-axis rotation in local space), this method is faster than 'rotateXL' however matrix must be orthogonal and normalized
  58. Matrix3& rotateYLOrthoNormalized(Flt angle); // rotate matrix by its y vector (y-axis rotation in local space), this method is faster than 'rotateYL' however matrix must be orthogonal and normalized
  59. Matrix3& rotateZLOrthoNormalized(Flt angle); // rotate matrix by its z vector (z-axis rotation in local space), this method is faster than 'rotateZL' however matrix must be orthogonal and normalized
  60. Matrix3& rotateXLOrthoNormalized(Flt cos, Flt sin); // rotate matrix by its x vector (x-axis rotation in local space), this method works like 'rotateXLOrthoNormalized(Flt angle)' however it accepts 'Cos' and 'Sin' of 'angle'
  61. Matrix3& mirrorX( ); // mirror matrix in X axis
  62. Matrix3& mirrorY( ); // mirror matrix in Y axis
  63. Matrix3& mirrorZ( ); // mirror matrix in Z axis
  64. Matrix3& mirror (C Vec &normal); // mirror matrix by plane normal
  65. Matrix3& swapYZ(); // swap Y and Z components of every vector
  66. // set (set methods reset the full matrix)
  67. Matrix3& identity( ); // set as identity
  68. Matrix3& identity(Flt blend); // set as identity, this method is similar to 'identity()' however it does not perform full reset of the matrix. Instead, smooth reset is applied depending on 'blend' value (0=no reset, 1=full reset)
  69. Matrix3& zero ( ); // set all vectors to zero
  70. Matrix3& setScale( Flt scale); // set as scaled identity
  71. Matrix3& setScale(C Vec &scale); // set as scaled identity
  72. Matrix3& setRotateX ( Flt angle); // set as x-rotated identity
  73. Matrix3& setRotateY ( Flt angle); // set as y-rotated identity
  74. Matrix3& setRotateZ ( Flt angle); // set as z-rotated identity
  75. Matrix3& setRotateXY( Flt x , Flt y ); // set as x-y-rotated identity, works the same as 'setRotateX(x).rotateY(y)' but faster
  76. Matrix3& setRotate (C Vec &axis, Flt angle); // set as rotated by vector identity, 'axis' must be normalized
  77. #if EE_PRIVATE
  78. Matrix3& setRotateCosSin(C Vec &axis, Flt cos, Flt sin); // set as rotated by vector identity, 'axis' must be normalized
  79. #endif
  80. Matrix3& setOrient (DIR_ENUM dir ); // set as identity orientation from DIR_ENUM
  81. Matrix3& setRight (C Vec &right ); // set as x='right' and calculate correct y,z, 'right' must be normalized
  82. Matrix3& setUp (C Vec &up ); // set as y='up' and calculate correct x,z, 'up' must be normalized
  83. Matrix3& setDir (C Vec &dir ); // set as z='dir' and calculate correct x,y, 'dir' must be normalized
  84. Matrix3& setDir (C Vec &dir, C Vec &up ); // set as z='dir', y='up' and calculate correct x , 'dir up' must be normalized
  85. Matrix3& setDir (C Vec &dir, C Vec &up, C Vec &right); // set as z='dir', y='up', x='right' , 'dir up right' must be normalized
  86. Matrix3& setRotation(C Vec &dir_from, C Vec &dir_to ); // set as matrix which rotates 'dir_from' to 'dir_to', 'dir_from dir_to' must be normalized
  87. Matrix3& setRotation(C Vec &dir_from, C Vec &dir_to, Flt blend ); // set as matrix which rotates 'dir_from' to 'dir_to', using blend value , 'dir_from dir_to' must be normalized
  88. Matrix3& setRotation(C Vec &dir_from, C Vec &dir_to, Flt blend, Flt roll); // set as matrix which rotates 'dir_from' to 'dir_to', using blend value and additional roll angle, 'dir_from dir_to' must be normalized
  89. // get
  90. Flt determinant()C;
  91. Bool mirrored ()C {return determinant()<0;} // if matrix is mirrored ('x' axis is on the other/left side)
  92. Vec scale ()C; // get each axis scale
  93. Vec scale2()C; // get each axis scale squared
  94. Flt avgScale ()C; // get average axis scale
  95. Flt maxScale ()C; // get maximum axis scale
  96. Vec angles ( )C; // get rotation angles, this allows to reconstruct the matrix using "setRotateZ(angle.z).rotateX(angle.x).rotateY(angle.y)" or faster by using "setRotateZ(angle.z).rotateXY(angle.x, angle.y)"
  97. Vec axis ( Bool normalized=false)C; // get rotation axis , if you know that the matrix is normalized then set 'normalized=true' for more performance
  98. Flt angle ( Bool normalized=false)C; // get rotation angle , if you know that the matrix is normalized then set 'normalized=true' for more performance
  99. Flt axisAngle(Vec &axis, Bool normalized=false)C; // get rotation axis and angle, if you know that the matrix is normalized then set 'normalized=true' for more performance
  100. Vec axisAngle( Bool normalized=false)C; // get rotation axis scaled by angle, if you know that the matrix is normalized then set 'normalized=true' for more performance
  101. Flt angleY ( Bool normalized=false)C; // get rotation angle along Y axis , if you know that the matrix is normalized then set 'normalized=true' for more performance, this is the same as "axisAngle(normalized).y" but faster
  102. Str asText(Int precision=INT_MAX)C {return S+"X: "+x.asText(precision)+", Y: "+y.asText(precision)+", Z:"+z.asText(precision);} // get text description
  103. #if EE_PRIVATE
  104. // operations
  105. Vec2 convert(C Vec &src, Bool normalized=false)C; // return converted 3D 'src' to 2D vector according to matrix x,y axes
  106. Vec convert(C Vec2 &src )C; // return converted 2D 'src' to 3D vector according to matrix x,y axes
  107. Edge2 convert(C Edge &src, Bool normalized=false)C; // return converted 3D 'src' to 2D edge according to matrix x,y axes
  108. Edge convert(C Edge2 &src )C; // return converted 2D 'src' to 3D edge according to matrix x,y axes
  109. #endif
  110. // draw
  111. void draw(C Vec &pos, C Color &x_color=RED, C Color &y_color=GREEN, C Color &z_color=BLUE, Bool arrow=true)C; // draw axes, this can be optionally called outside of Render function, this relies on active object matrix which can be set using 'SetMatrix' function
  112. Matrix3() {}
  113. Matrix3( Flt scale) {setScale(scale);}
  114. Matrix3(C Vec &scale) {setScale(scale);}
  115. Matrix3(C Vec &x, C Vec &y, C Vec &z) {T.x=x; T.y=y; T.z=z;}
  116. Matrix3(C MatrixD3 &m);
  117. Matrix3(C Matrix &m);
  118. Matrix3(C Matrix4 &m);
  119. Matrix3(C Orient &o);
  120. Matrix3(C Quaternion &q);
  121. };
  122. /******************************************************************************/
  123. struct MatrixD3 // Matrix 3x3 (orientation + scale, double precision)
  124. {
  125. VecD x, // right vector
  126. y, // up vector
  127. z; // forward vector
  128. // transform
  129. MatrixD3& operator*=( Dbl f);
  130. MatrixD3& operator/=( Dbl f);
  131. MatrixD3& operator*=(C VecD &v);
  132. MatrixD3& operator/=(C VecD &v);
  133. MatrixD3& operator+=(C MatrixD3 &m);
  134. MatrixD3& operator-=(C MatrixD3 &m);
  135. MatrixD3& operator*=(C Matrix3 &m) {return mul(m);}
  136. MatrixD3& operator*=(C MatrixD3 &m) {return mul(m);}
  137. MatrixD3& operator/=(C Matrix3 &m) {return div(m);}
  138. MatrixD3& operator/=(C MatrixD3 &m) {return div(m);}
  139. Bool operator==(C MatrixD3 &m)C;
  140. Bool operator!=(C MatrixD3 &m)C;
  141. friend MatrixD3 operator* (C MatrixD3 &a, C MatrixD3 &b) {MatrixD3 temp; a.mul (b, temp); return temp;} // get a*b
  142. friend MatrixD3 operator/ (C MatrixD3 &a, C MatrixD3 &b) {MatrixD3 temp; a.div (b, temp); return temp;} // get a/b
  143. friend MatrixD3 operator~ (C MatrixD3 &m ) {MatrixD3 temp; m.inverse( temp); return temp;} // get inversed 'm'
  144. void mul(C Matrix3 &matrix, MatrixD3 &dest)C; // multiply self by 'matrix' and store result in 'dest'
  145. void mul(C MatrixD3 &matrix, MatrixD3 &dest)C; // multiply self by 'matrix' and store result in 'dest'
  146. MatrixD3& mul(C Matrix3 &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  147. MatrixD3& mul(C MatrixD3 &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  148. void div(C Matrix3 &matrix, MatrixD3 &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  149. void div(C MatrixD3 &matrix, MatrixD3 &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  150. MatrixD3& div(C Matrix3 &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  151. MatrixD3& div(C MatrixD3 &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  152. void divNormalized(C Matrix3 &matrix, MatrixD3 &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  153. void divNormalized(C MatrixD3 &matrix, MatrixD3 &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  154. MatrixD3& divNormalized(C Matrix3 &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  155. MatrixD3& divNormalized(C MatrixD3 &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  156. void inverse(MatrixD3 &dest, Bool normalized=false)C; // inverse self to 'dest', if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  157. MatrixD3& inverse( Bool normalized=false) {inverse(T, normalized); return T;} // inverse self , if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  158. void inverseNonOrthogonal(MatrixD3 &dest)C; // inverse self to 'dest', this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  159. MatrixD3& inverseNonOrthogonal( ) {inverseNonOrthogonal(T); return T;} // inverse self , this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  160. MatrixD3& inverseScale(); // inverse scale
  161. MatrixD3& normalize( ); // normalize scale , this sets the length of 'x' 'y' 'z' vectors to 1
  162. MatrixD3& normalize( Dbl scale); // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale'
  163. MatrixD3& normalize(C VecD &scale); // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale.x' 'scale.y' 'scale.z'
  164. MatrixD3& scale ( Dbl scale) {T*=scale; return T;} // scale
  165. MatrixD3& scale ( C VecD &scale) {T*=scale; return T;} // scale
  166. MatrixD3& scale (C VecD &dir, Dbl scale); // scale along 'dir' direction by 'scale' value, 'dir' must be normalized
  167. MatrixD3& scalePlane(C VecD &nrm, Dbl scale); // scale along plane of 'nrm' normal by 'scale' value, 'nrm' must be normalized
  168. MatrixD3& rotateX ( Dbl angle); // rotate by x axis
  169. MatrixD3& rotateY ( Dbl angle); // rotate by y axis
  170. MatrixD3& rotateZ ( Dbl angle); // rotate by z axis
  171. MatrixD3& rotateXY( Dbl x , Dbl y ); // rotate by x axis and then by y axis, works the same as "rotateX(x).rotateY(y)" but faster
  172. MatrixD3& rotate (C VecD &axis, Dbl angle); // rotate by vector, 'axis' must be normalized
  173. MatrixD3& rotateXL( Dbl angle); // rotate matrix by its x vector (x-axis rotation in local space)
  174. MatrixD3& rotateYL( Dbl angle); // rotate matrix by its y vector (y-axis rotation in local space)
  175. MatrixD3& rotateZL( Dbl angle); // rotate matrix by its z vector (z-axis rotation in local space)
  176. MatrixD3& rotateXLOrthoNormalized(Dbl angle); // rotate matrix by its x vector (x-axis rotation in local space), this method is faster than 'rotateXL' however matrix must be orthogonal and normalized
  177. MatrixD3& rotateYLOrthoNormalized(Dbl angle); // rotate matrix by its y vector (y-axis rotation in local space), this method is faster than 'rotateYL' however matrix must be orthogonal and normalized
  178. MatrixD3& rotateZLOrthoNormalized(Dbl angle); // rotate matrix by its z vector (z-axis rotation in local space), this method is faster than 'rotateZL' however matrix must be orthogonal and normalized
  179. MatrixD3& mirrorX( ); // mirror matrix in X axis
  180. MatrixD3& mirrorY( ); // mirror matrix in Y axis
  181. MatrixD3& mirrorZ( ); // mirror matrix in Z axis
  182. MatrixD3& mirror (C VecD &normal); // mirror matrix by plane normal
  183. // set (set methods reset the full matrix)
  184. MatrixD3& identity(); // set as identity
  185. MatrixD3& zero (); // set all vectors to zero
  186. MatrixD3& setScale( Dbl scale); // set as scaled identity
  187. MatrixD3& setScale(C VecD &scale); // set as scaled identity
  188. MatrixD3& setRotateX ( Dbl angle); // set as x-rotated identity
  189. MatrixD3& setRotateY ( Dbl angle); // set as y-rotated identity
  190. MatrixD3& setRotateZ ( Dbl angle); // set as z-rotated identity
  191. MatrixD3& setRotateXY( Dbl x , Dbl y ); // set as x-y-rotated identity, works the same as 'setRotateX(x).rotateY(y)' but faster
  192. MatrixD3& setRotate (C VecD &axis, Dbl angle); // set as rotated by vector identity, 'axis' must be normalized
  193. #if EE_PRIVATE
  194. MatrixD3& setRotateCosSin(C VecD &axis, Dbl cos, Dbl sin); // set as rotated by vector identity, 'axis' must be normalized
  195. #endif
  196. MatrixD3& setOrient (DIR_ENUM dir ); // set as identity orientation from DIR_ENUM
  197. MatrixD3& setRight (C VecD &right ); // set as x='right' and calculate correct y,z, 'right' must be normalized
  198. MatrixD3& setUp (C VecD &up ); // set as y='up' and calculate correct x,z, 'up' must be normalized
  199. MatrixD3& setDir (C VecD &dir ); // set as z='dir' and calculate correct x,y, 'dir' must be normalized
  200. MatrixD3& setDir (C VecD &dir, C VecD &up ); // set as z='dir', y='up' and calculate correct x , 'dir up' must be normalized
  201. MatrixD3& setDir (C VecD &dir, C VecD &up, C VecD &right); // set as z='dir', y='up', x='right' , 'dir up right' must be normalized
  202. MatrixD3& setRotation(C VecD &dir_from, C VecD &dir_to ); // set as matrix which rotates 'dir_from' to 'dir_to', 'dir_from dir_to' must be normalized
  203. MatrixD3& setRotation(C VecD &dir_from, C VecD &dir_to, Dbl blend ); // set as matrix which rotates 'dir_from' to 'dir_to', using blend value , 'dir_from dir_to' must be normalized
  204. MatrixD3& setRotation(C VecD &dir_from, C VecD &dir_to, Dbl blend, Dbl roll); // set as matrix which rotates 'dir_from' to 'dir_to', using blend value and additional roll angle, 'dir_from dir_to' must be normalized
  205. // get
  206. Dbl determinant()C;
  207. Bool mirrored ()C {return determinant()<0;} // if matrix is mirrored ('x' axis is on the other/left side)
  208. VecD scale ()C; // get each axis scale
  209. VecD scale2()C; // get each axis scale squared
  210. Dbl avgScale ()C; // get average axis scale
  211. Dbl maxScale ()C; // get maximum axis scale
  212. VecD angles ( )C; // get rotation angles, this allows to reconstruct the matrix using "setRotateZ(angle.z).rotateX(angle.x).rotateY(angle.y)" or faster by using "setRotateZ(angle.z).rotateXY(angle.x, angle.y)"
  213. VecD axis ( Bool normalized=false)C; // get rotation axis , if you know that the matrix is normalized then set 'normalized=true' for more performance
  214. Dbl angle ( Bool normalized=false)C; // get rotation angle , if you know that the matrix is normalized then set 'normalized=true' for more performance
  215. Dbl axisAngle(VecD &axis, Bool normalized=false)C; // get rotation axis and angle , if you know that the matrix is normalized then set 'normalized=true' for more performance
  216. VecD axisAngle( Bool normalized=false)C; // get rotation axis scaled by angle, if you know that the matrix is normalized then set 'normalized=true' for more performance
  217. Str asText(Int precision=INT_MAX)C {return S+"X: "+x.asText(precision)+", Y: "+y.asText(precision)+", Z:"+z.asText(precision);} // get text description
  218. #if EE_PRIVATE
  219. // operations
  220. VecD2 convert(C VecD &src, Bool normalized=false)C; // return converted 3D 'src' to 2D vector according to matrix x,y axes
  221. VecD convert(C VecD2 &src )C; // retrun converted 2D 'src' to 3D vector according to matrix x,y axes
  222. EdgeD2 convert(C EdgeD &src, Bool normalized=false)C; // return converted 3D 'src' to 2D edge according to matrix x,y axes
  223. EdgeD convert(C EdgeD2 &src )C; // retrun converted 2D 'src' to 3D edge according to matrix x,y axes
  224. #endif
  225. // draw
  226. void draw(C VecD &pos, C Color &x_color=RED, C Color &y_color=GREEN, C Color &z_color=BLUE, Bool arrow=true)C; // draw axes, this can be optionally called outside of Render function, this relies on active object matrix which can be set using 'SetMatrix' function
  227. MatrixD3() {}
  228. MatrixD3( Dbl scale) {setScale(scale);}
  229. MatrixD3(C VecD &scale) {setScale(scale);}
  230. MatrixD3(C VecD &x, C VecD &y, C VecD &z) {T.x=x; T.y=y; T.z=z;}
  231. MatrixD3(C Matrix3 &m);
  232. MatrixD3(C MatrixD &m);
  233. MatrixD3(C Orient &o);
  234. MatrixD3(C OrientD &o);
  235. };
  236. /******************************************************************************/
  237. STRUCT(Matrix , Matrix3) // Matrix 4x3 (orientation + scale + position)
  238. //{
  239. Vec pos; // position
  240. Matrix3& orn() {return T;} // get reference to self as 'Matrix3'
  241. C Matrix3& orn()C {return T;} // get reference to self as const 'Matrix3'
  242. // transform
  243. Matrix& operator*=( Flt f);
  244. Matrix& operator/=( Flt f);
  245. Matrix& operator*=(C Vec &v);
  246. Matrix& operator/=(C Vec &v);
  247. Matrix& operator+=(C Vec &v) {pos+=v; return T;}
  248. Matrix& operator-=(C Vec &v) {pos-=v; return T;}
  249. Matrix& operator+=(C Matrix &m);
  250. Matrix& operator-=(C Matrix &m);
  251. Matrix& operator*=(C Matrix3 &m) {return mul(m);}
  252. Matrix& operator*=(C Matrix &m) {return mul(m);}
  253. Matrix& operator*=(C MatrixM &m) {return mul(m);}
  254. Matrix& operator/=(C Matrix3 &m) {return div(m);}
  255. Matrix& operator/=(C Matrix &m) {return div(m);}
  256. Matrix& operator/=(C MatrixM &m) {return div(m);}
  257. Matrix& operator*=(C RevMatrix &m) {return mul(m);}
  258. Bool operator==(C Matrix &m)C;
  259. Bool operator!=(C Matrix &m)C;
  260. friend Matrix operator+ (C Matrix &m, C Vec &v) {return Matrix(m)+=v; } // get m+v
  261. friend Matrix operator- (C Matrix &m, C Vec &v) {return Matrix(m)-=v; } // get m-v
  262. friend Matrix operator* (C Matrix &a, C Matrix3 &b) {Matrix temp; a.mul (b, temp); return temp;} // get a*b
  263. friend Matrix operator* (C Matrix &a, C Matrix &b) {Matrix temp; a.mul (b, temp); return temp;} // get a*b
  264. friend Matrix operator* (C Matrix &a, C MatrixM &b) {Matrix temp; a.mul (b, temp); return temp;} // get a*b
  265. friend Matrix operator* (C Matrix &a, C RevMatrix &b) {Matrix temp; a.mul (b, temp); return temp;} // get a*b
  266. friend Matrix operator/ (C Matrix &a, C Matrix3 &b) {Matrix temp; a.div (b, temp); return temp;} // get a/b
  267. friend Matrix operator/ (C Matrix &a, C Matrix &b) {Matrix temp; a.div (b, temp); return temp;} // get a/b
  268. friend Matrix operator/ (C Matrix &a, C MatrixM &b) {Matrix temp; a.div (b, temp); return temp;} // get a/b
  269. friend Matrix operator~ (C Matrix &m ) {Matrix temp; m.inverse( temp); return temp;} // get inversed 'm'
  270. friend Matrix operator* (C Matrix3 &a, C Matrix &b) {return Matrix(a)*=b;} // get a*b
  271. friend Matrix operator/ (C Matrix3 &a, C Matrix &b) {return Matrix(a)/=b;} // get a/b
  272. void mul(C Matrix3 &matrix, Matrix &dest)C; // multiply self by 'matrix' and store result in 'dest'
  273. void mul(C Matrix &matrix, Matrix &dest)C; // multiply self by 'matrix' and store result in 'dest'
  274. void mul(C MatrixM &matrix, Matrix &dest)C; // multiply self by 'matrix' and store result in 'dest'
  275. void mul(C Matrix &matrix, Matrix4 &dest)C; // multiply self by 'matrix' and store result in 'dest'
  276. void mul(C Matrix4 &matrix, Matrix4 &dest)C; // multiply self by 'matrix' and store result in 'dest'
  277. void mul(C RevMatrix &matrix, Matrix &dest)C; // multiply self by 'matrix' and store result in 'dest'
  278. Matrix& mul(C Matrix3 &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  279. Matrix& mul(C Matrix &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  280. Matrix& mul(C MatrixM &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  281. Matrix& mul(C RevMatrix &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  282. void mulTimes(Int n, C Matrix &matrix, Matrix &dest)C; // multiply self by 'matrix' 'n'-times and store result in 'dest'
  283. void mulTimes(Int n, C RevMatrix &matrix, Matrix &dest)C; // multiply self by 'matrix' 'n'-times and store result in 'dest'
  284. Matrix& mulTimes(Int n, C Matrix &matrix ) {mulTimes(n, matrix, T); return T;} // multiply self by 'matrix' 'n'-times
  285. Matrix& mulTimes(Int n, C RevMatrix &matrix ) {mulTimes(n, matrix, T); return T;} // multiply self by 'matrix' 'n'-times
  286. void div(C Matrix3 &matrix, Matrix &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  287. void div(C Matrix &matrix, Matrix &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  288. void div(C MatrixM &matrix, Matrix &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  289. Matrix& div(C Matrix3 &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  290. Matrix& div(C Matrix &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  291. Matrix& div(C MatrixM &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  292. void divNormalized(C Matrix3 &matrix, Matrix &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  293. void divNormalized(C Matrix &matrix, Matrix &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  294. void divNormalized(C MatrixM &matrix, Matrix &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  295. Matrix& divNormalized(C Matrix3 &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  296. Matrix& divNormalized(C Matrix &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  297. Matrix& divNormalized(C MatrixM &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  298. void inverse(Matrix &dest, Bool normalized=false)C; // inverse self to 'dest', if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  299. Matrix& inverse( Bool normalized=false) {inverse(T, normalized); return T;} // inverse self , if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  300. void inverseNonOrthogonal(Matrix &dest)C; // inverse self to 'dest', this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  301. Matrix& inverseNonOrthogonal( ) {inverseNonOrthogonal(T); return T;} // inverse self , this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  302. Matrix& normalize( ) {super::normalize( ); return T;} // normalize scale , this sets the length of 'x' 'y' 'z' vectors to 'scale'
  303. Matrix& normalize( Flt scale) {super::normalize(scale); return T;} // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale'
  304. Matrix& normalize(C Vec &scale) {super::normalize(scale); return T;} // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale.x' 'scale.y' 'scale.z'
  305. Matrix& move (Flt x, Flt y, Flt z) {pos +=Vec(x, y, z); return T;} // move
  306. Matrix& move (C Vec2 &move ) {pos.xy+=move ; return T;} // move
  307. Matrix& move (C Vec &move ) {pos +=move ; return T;} // move
  308. Matrix& moveBack(C Vec &move ) {pos -=move ; return T;} // move back
  309. Matrix& anchor(C Vec &anchor); // set 'pos' of this matrix so that 'anchor' transformed by this matrix will remain the same - "anchor*T==anchor"
  310. Matrix& scale ( Flt scale) { T*=scale ; return T;} // scale
  311. Matrix& scale ( C Vec &scale) { T*=scale ; return T;} // scale
  312. Matrix& scaleL ( C Vec &scale); // scale in local space
  313. Matrix& scaleOrn ( Flt scale) {super::scale (scale); return T;} // scale orientation only
  314. Matrix& scaleOrn ( C Vec &scale) {super::scale (scale); return T;} // scale orientation only
  315. Matrix& scaleOrnL ( C Vec &scale) {super::scaleL(scale); return T;} // scale orientation only in local space
  316. Matrix& scale (C Vec &dir, Flt scale); // scale along 'dir' direction by 'scale' value, 'dir' must be normalized
  317. Matrix& scalePlane(C Vec &nrm, Flt scale); // scale along plane of 'nrm' normal by 'scale' value, 'nrm' must be normalized
  318. Matrix& rotateX ( Flt angle); // rotate by x axis
  319. Matrix& rotateY ( Flt angle); // rotate by y axis
  320. Matrix& rotateZ ( Flt angle); // rotate by z axis
  321. Matrix& rotateXY( Flt x , Flt y ); // rotate by x axis and then by y axis, works the same as "rotateX(x).rotateY(y)" but faster
  322. Matrix& rotate (C Vec &axis, Flt angle); // rotate by vector, 'axis' must be normalized
  323. Matrix& rotateXL( Flt angle) {super::rotateXL(angle); return T;} // rotate matrix by its x vector (x-axis rotation in local space)
  324. Matrix& rotateYL( Flt angle) {super::rotateYL(angle); return T;} // rotate matrix by its y vector (y-axis rotation in local space)
  325. Matrix& rotateZL( Flt angle) {super::rotateZL(angle); return T;} // rotate matrix by its z vector (z-axis rotation in local space)
  326. Matrix& mirrorX( ); // mirror matrix in X axis
  327. Matrix& mirrorY( ); // mirror matrix in Y axis
  328. Matrix& mirrorZ( ); // mirror matrix in Z axis
  329. Matrix& mirror (C Plane &plane); // mirror matrix by plane
  330. Matrix& swapYZ(); // swap Y and Z components of every vector
  331. // set (set methods reset the full matrix)
  332. Matrix& identity( ); // set as identity
  333. Matrix& identity(Flt blend); // set as identity, this method is similar to 'identity()' however it does not perform full reset of the matrix. Instead, smooth reset is applied depending on 'blend' value (0=no reset, 1=full reset)
  334. Matrix& zero ( ); // set all vectors to zero
  335. Matrix& setPos (Flt x, Flt y, Flt z ); // set as positioned identity
  336. Matrix& setPos (C Vec2 &pos ); // set as positioned identity
  337. Matrix& setPos (C Vec &pos ); // set as positioned identity
  338. Matrix& setScale ( Flt scale ); // set as scaled identity
  339. Matrix& setScale (C Vec &scale ); // set as scaled identity
  340. Matrix& setPosScale(C Vec &pos , Flt scale); // set as positioned & scaled identity
  341. Matrix& setPosScale(C Vec &pos , C Vec &scale); // set as positioned & scaled identity
  342. Matrix& setScalePos( Flt scale, C Vec &pos ); // set as scaled & positioned identity
  343. Matrix& setScalePos(C Vec &scale, C Vec &pos ); // set as scaled & positioned identity
  344. Matrix& setRotateX ( Flt angle); // set as x-rotated identity
  345. Matrix& setRotateY ( Flt angle); // set as y-rotated identity
  346. Matrix& setRotateZ ( Flt angle); // set as z-rotated identity
  347. Matrix& setRotateXY( Flt x , Flt y ); // set as x-y-rotated identity, works the same as setRotateX(x).rotateY(y) but faster
  348. Matrix& setRotate (C Vec &axis, Flt angle); // set as rotated by vector identity, 'axis' must be normalized
  349. Matrix& setRotation(C Vec &pos, C Vec &dir_from, C Vec &dir_to, Flt blend=1); // set as matrix which rotates 'dir_from' to 'dir_to', using blend value, 'dir_from dir_to' must be normalized
  350. Matrix& setPosOrient(C Vec &pos, DIR_ENUM dir ); // set as positioned orientation from DIR_ENUM
  351. Matrix& setPosRight (C Vec &pos, C Vec &right ); // set as pos='pos', x='right' and calculate correct y,z, 'right' must be normalized
  352. Matrix& setPosUp (C Vec &pos, C Vec &up ); // set as pos='pos', y='up' and calculate correct x,z, 'up' must be normalized
  353. Matrix& setPosDir (C Vec &pos, C Vec &dir ); // set as pos='pos', z='dir' and calculate correct x,y, 'dir' must be normalized
  354. Matrix& setPosDir (C Vec &pos, C Vec &dir, C Vec &up ); // set as pos='pos', z='dir', y='up' and calculate correct x , 'dir up' must be normalized
  355. Matrix& setPosDir (C Vec &pos, C Vec &dir, C Vec &up, C Vec &right); // set as pos='pos', z='dir', y='up', x='right' , 'dir up right' must be normalized
  356. Matrix& set (C Box &src, C Box &dest); // set as matrix that transforms 'src' to 'dest' (src*m=dest)
  357. Matrix& setNormalizeX(C Box &box ); // set as matrix that (box*m).w() =1
  358. Matrix& setNormalizeY(C Box &box ); // set as matrix that (box*m).h() =1
  359. Matrix& setNormalizeZ(C Box &box ); // set as matrix that (box*m).d() =1
  360. Matrix& setNormalize (C Box &box ); // set as matrix that (box*m).size().max()=1
  361. // get
  362. Vec scale()C {return super::scale();} // get each axis scale
  363. Str asText(Int precision=INT_MAX)C {return super::asText(precision)+", Pos: "+pos.asText(precision);} // get text description
  364. // operations
  365. #if EE_PRIVATE
  366. Vec2 convert(C Vec &src, Bool normalized=false)C; // return converted 3D 'src' to 2D vector according to matrix x,y axes and position
  367. Vec convert(C Vec2 &src )C; // return converted 2D 'src' to 3D vector according to matrix x,y axes and position
  368. Edge2 convert(C Edge &src, Bool normalized=false)C; // return converted 3D 'src' to 2D edge according to matrix x,y axes and position
  369. Edge convert(C Edge2 &src )C; // return converted 2D 'src' to 3D edge according to matrix x,y axes and position
  370. #endif
  371. Matrix& setTransformAtPos(C Vec &pos, C Matrix3 &matrix); // set as transformation at position
  372. Matrix& setTransformAtPos(C Vec &pos, C Matrix &matrix); // set as transformation at position
  373. Matrix& transformAtPos(C Vec &pos, C Matrix3 &matrix); // transform at position
  374. Matrix& transformAtPos(C Vec &pos, C Matrix &matrix); // transform at position
  375. // draw
  376. void draw(C Color &x_color=RED, C Color &y_color=GREEN, C Color &z_color=BLUE, Bool arrow=true)C {super::draw(pos, x_color, y_color, z_color, arrow);} // draw axes, this can be optionally called outside of Render function, this relies on active object matrix which can be set using 'SetMatrix' function
  377. Matrix() {}
  378. Matrix( Flt scale ) {setScale (scale );}
  379. Matrix(C Vec &pos ) {setPos (pos );}
  380. Matrix(C Vec &pos , Flt scale) {setPosScale(pos , scale);}
  381. Matrix( Flt scale, C Vec &pos ) {setScalePos(scale, pos );}
  382. Matrix(C Vec &scale, C Vec &pos ) {setScalePos(scale, pos );}
  383. Matrix(C Matrix3 &orn , C Vec &pos ) {T.orn()=orn; T.pos=pos;}
  384. Matrix(C Vec &pos , C Matrix3 &orn ) {T.orn()=orn; T.pos=pos;}
  385. Matrix(C Matrix3 &m);
  386. Matrix(C MatrixD3 &m);
  387. Matrix(C MatrixM &m);
  388. Matrix(C MatrixD &m);
  389. Matrix(C Matrix4 &m);
  390. Matrix(C OrientP &o);
  391. };extern Matrix
  392. const MatrixIdentity; // identity
  393. #if EE_PRIVATE
  394. #define MAX_MATRIX_DX9 60 // max available Object Matrixes in DirectX 9 Hardware GPU ( 60 Matrix * 3 Vec4 = 180 Vec4's)
  395. #define MAX_MATRIX_DX10 256 // max available Object Matrixes in DirectX 10+ Hardware GPU (256 Matrix * 3 Vec4 = 768 Vec4's)
  396. #define MAX_MATRIX_SW 256 // max available Object Matrixes in Software CPU (256 Matrix * 3 Vec4 = 768 Vec4's)
  397. #define MAX_MATRIX_HWMIN 60 // max available Object Matrixes in worst Hardware GPU ( 60 Matrix * 3 Vec4 = 180 Vec4's)
  398. #define MAY_NEED_BONE_SPLITS (DX9 || GL)
  399. extern MatrixM ObjMatrix , // object matrix
  400. CamMatrix , // camera, this is always set, even when drawing shadows
  401. CamMatrixInv , // camera inversed = ~CamMatrix
  402. EyeMatrix[2] ; // 'ActiveCam.matrix' adjusted for both eyes 0=left, 1=right
  403. extern Matrix3 CamMatrixInvMotionScale; // 'ActiveCam.matrix' inversed and scaled by 'D.motionScale'
  404. extern GpuMatrix *ViewMatrix ;
  405. #endif
  406. /******************************************************************************/
  407. STRUCT(MatrixM , Matrix3) // Matrix 4x3 (orientation + scale + position, mixed precision, uses Flt for orientation+scale and Dbl for position)
  408. //{
  409. VecD pos; // position
  410. Matrix3& orn() {return T;} // get reference to self as 'Matrix3'
  411. C Matrix3& orn()C {return T;} // get reference to self as const 'Matrix3'
  412. // transform
  413. MatrixM& operator*=( Flt f);
  414. MatrixM& operator/=( Flt f);
  415. MatrixM& operator*=(C Vec &v);
  416. MatrixM& operator/=(C Vec &v);
  417. MatrixM& operator+=(C Vec &v) {pos+=v; return T;}
  418. MatrixM& operator-=(C Vec &v) {pos-=v; return T;}
  419. MatrixM& operator+=(C VecD &v) {pos+=v; return T;}
  420. MatrixM& operator-=(C VecD &v) {pos-=v; return T;}
  421. MatrixM& operator+=(C MatrixM &m);
  422. MatrixM& operator-=(C MatrixM &m);
  423. MatrixM& operator*=(C Matrix3 &m) {return mul(m);}
  424. MatrixM& operator*=(C Matrix &m) {return mul(m);}
  425. MatrixM& operator*=(C MatrixM &m) {return mul(m);}
  426. MatrixM& operator/=(C Matrix3 &m) {return div(m);}
  427. MatrixM& operator/=(C Matrix &m) {return div(m);}
  428. MatrixM& operator/=(C MatrixM &m) {return div(m);}
  429. Bool operator==(C MatrixM &m)C;
  430. Bool operator!=(C MatrixM &m)C;
  431. friend MatrixM operator+ (C MatrixM &m, C Vec &v) {return MatrixM(m)+=v; } // get m+v
  432. friend MatrixM operator- (C MatrixM &m, C Vec &v) {return MatrixM(m)-=v; } // get m-v
  433. friend MatrixM operator+ (C MatrixM &m, C VecD &v) {return MatrixM(m)+=v; } // get m+v
  434. friend MatrixM operator- (C MatrixM &m, C VecD &v) {return MatrixM(m)-=v; } // get m-v
  435. friend MatrixM operator* (C MatrixM &a, C Matrix3 &b) {MatrixM temp; a.mul (b, temp); return temp;} // get a*b
  436. friend MatrixM operator* (C MatrixM &a, C Matrix &b) {MatrixM temp; a.mul (b, temp); return temp;} // get a*b
  437. friend MatrixM operator* (C MatrixM &a, C MatrixM &b) {MatrixM temp; a.mul (b, temp); return temp;} // get a*b
  438. friend MatrixM operator/ (C MatrixM &a, C Matrix3 &b) {MatrixM temp; a.div (b, temp); return temp;} // get a/b
  439. friend MatrixM operator/ (C MatrixM &a, C Matrix &b) {MatrixM temp; a.div (b, temp); return temp;} // get a/b
  440. friend MatrixM operator/ (C MatrixM &a, C MatrixM &b) {MatrixM temp; a.div (b, temp); return temp;} // get a/b
  441. friend MatrixM operator~ (C MatrixM &m ) {MatrixM temp; m.inverse( temp); return temp;} // get inversed 'm'
  442. friend MatrixM operator* (C Matrix3 &a, C MatrixM &b) {return MatrixM(a)*=b;} // get a*b
  443. friend MatrixM operator/ (C Matrix3 &a, C MatrixM &b) {return MatrixM(a)/=b;} // get a/b
  444. void mul(C MatrixM &matrix, Matrix &dest)C; // multiply self by 'matrix' and store result in 'dest'
  445. void mul(C Matrix3 &matrix, MatrixM &dest)C; // multiply self by 'matrix' and store result in 'dest'
  446. void mul(C Matrix &matrix, MatrixM &dest)C; // multiply self by 'matrix' and store result in 'dest'
  447. void mul(C MatrixM &matrix, MatrixM &dest)C; // multiply self by 'matrix' and store result in 'dest'
  448. MatrixM& mul(C Matrix3 &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  449. MatrixM& mul(C Matrix &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  450. MatrixM& mul(C MatrixM &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  451. void div(C MatrixM &matrix, Matrix &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  452. void div(C Matrix3 &matrix, MatrixM &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  453. void div(C Matrix &matrix, MatrixM &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  454. void div(C MatrixM &matrix, MatrixM &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  455. MatrixM& div(C Matrix3 &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  456. MatrixM& div(C Matrix &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  457. MatrixM& div(C MatrixM &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  458. void divNormalized(C Matrix3 &matrix, MatrixM &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  459. void divNormalized(C MatrixM &matrix, Matrix &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  460. void divNormalized(C Matrix &matrix, MatrixM &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  461. void divNormalized(C MatrixM &matrix, MatrixM &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  462. MatrixM& divNormalized(C Matrix3 &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  463. MatrixM& divNormalized(C Matrix &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  464. MatrixM& divNormalized(C MatrixM &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  465. void inverse(MatrixM &dest, Bool normalized=false)C; // inverse self to 'dest', if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  466. MatrixM& inverse( Bool normalized=false) {inverse(T, normalized); return T;} // inverse self , if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  467. void inverseNonOrthogonal(MatrixM &dest)C; // inverse self to 'dest', this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  468. MatrixM& inverseNonOrthogonal( ) {inverseNonOrthogonal(T); return T;} // inverse self , this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  469. MatrixM& normalize( ) {super::normalize( ); return T;} // normalize scale , this sets the length of 'x' 'y' 'z' vectors to 'scale'
  470. MatrixM& normalize( Flt scale) {super::normalize(scale); return T;} // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale'
  471. MatrixM& normalize(C Vec &scale) {super::normalize(scale); return T;} // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale.x' 'scale.y' 'scale.z'
  472. MatrixM& move (Dbl x, Dbl y, Dbl z) {pos +=VecD(x, y, z); return T;} // move
  473. MatrixM& move (C VecD2 &move ) {pos.xy+=move ; return T;} // move
  474. MatrixM& move (C VecD &move ) {pos +=move ; return T;} // move
  475. MatrixM& moveBack(C VecD &move ) {pos -=move ; return T;} // move back
  476. MatrixM& anchor(C VecD &anchor); // set 'pos' of this matrix so that 'anchor' transformed by this matrix will remain the same - "anchor*T==anchor"
  477. MatrixM& scale ( Flt scale) { T*=scale ; return T;} // scale
  478. MatrixM& scale (C Vec &scale) { T*=scale ; return T;} // scale
  479. MatrixM& scaleOrn( Flt scale) {super::scale(scale); return T;} // scale orientation only
  480. MatrixM& scaleOrn(C Vec &scale) {super::scale(scale); return T;} // scale orientation only
  481. MatrixM& rotateX ( Flt angle); // rotate by x axis
  482. MatrixM& rotateY ( Flt angle); // rotate by y axis
  483. MatrixM& rotateZ ( Flt angle); // rotate by z axis
  484. MatrixM& rotateXY( Flt x , Flt y ); // rotate by x axis and then by y axis, works the same as "rotateX(x).rotateY(y)" but faster
  485. MatrixM& rotate (C Vec &axis, Flt angle); // rotate by vector, 'axis' must be normalized
  486. MatrixM& rotateXL( Flt angle) {super::rotateXL(angle); return T;} // rotate matrix by its x vector (x-axis rotation in local space)
  487. MatrixM& rotateYL( Flt angle) {super::rotateYL(angle); return T;} // rotate matrix by its y vector (y-axis rotation in local space)
  488. MatrixM& rotateZL( Flt angle) {super::rotateZL(angle); return T;} // rotate matrix by its z vector (z-axis rotation in local space)
  489. MatrixM& mirror(C PlaneM &plane); // mirror matrix by plane
  490. // get
  491. Vec scale()C {return super::scale();} // get each axis scale
  492. Str asText(Int precision=INT_MAX)C {return super::asText(precision)+", Pos: "+pos.asText(precision);} // get text description
  493. // set (set methods reset the full matrix)
  494. MatrixM& identity(); // set as identity
  495. MatrixM& zero (); // set all vectors to zero
  496. MatrixM& setPos (Dbl x, Dbl y, Dbl z ); // set as positioned identity
  497. MatrixM& setPos (C VecD2 &pos ); // set as positioned identity
  498. MatrixM& setPos (C VecD &pos ); // set as positioned identity
  499. MatrixM& setScale ( Flt scale ); // set as scaled identity
  500. MatrixM& setScale (C Vec &scale ); // set as scaled identity
  501. MatrixM& setPosScale(C VecD &pos , Flt scale); // set as positioned & scaled identity
  502. MatrixM& setPosScale(C VecD &pos , C Vec &scale); // set as positioned & scaled identity
  503. MatrixM& setScalePos( Flt scale, C VecD &pos ); // set as scaled & positioned identity
  504. MatrixM& setScalePos(C Vec &scale, C VecD &pos ); // set as scaled & positioned identity
  505. MatrixM& setRotateX ( Flt angle); // set as x-rotated identity
  506. MatrixM& setRotateY ( Flt angle); // set as y-rotated identity
  507. MatrixM& setRotateZ ( Flt angle); // set as z-rotated identity
  508. MatrixM& setRotateXY( Flt x , Flt y ); // set as x-y-rotated identity, works the same as setRotateX(x).rotateY(y) but faster
  509. MatrixM& setRotate (C Vec &axis, Flt angle); // set as rotated by vector identity, 'axis' must be normalized
  510. MatrixM& setPosOrient(C VecD &pos, DIR_ENUM dir ); // set as positioned orientation from DIR_ENUM
  511. MatrixM& setPosRight (C VecD &pos, C Vec &right ); // set as pos='pos', x='right' and calculate correct y,z, 'right' must be normalized
  512. MatrixM& setPosUp (C VecD &pos, C Vec &up ); // set as pos='pos', y='up' and calculate correct x,z, 'up' must be normalized
  513. MatrixM& setPosDir (C VecD &pos, C Vec &dir ); // set as pos='pos', z='dir' and calculate correct x,y, 'dir' must be normalized
  514. MatrixM& setPosDir (C VecD &pos, C Vec &dir, C Vec &up ); // set as pos='pos', z='dir', y='up' and calculate correct x , 'dir up' must be normalized
  515. MatrixM& setPosDir (C VecD &pos, C Vec &dir, C Vec &up, C Vec &right); // set as pos='pos', z='dir', y='up', x='right' , 'dir up right' must be normalized
  516. MatrixM() {}
  517. MatrixM( Flt scale ) {setScale (scale );}
  518. MatrixM(C Vec &pos ) {setPos (pos );}
  519. MatrixM(C VecD &pos ) {setPos (pos );}
  520. MatrixM(C VecD &pos , Flt scale) {setPosScale(pos , scale);}
  521. MatrixM( Flt scale, C VecD &pos ) {setScalePos(scale, pos );}
  522. MatrixM(C Vec &scale, C VecD &pos ) {setScalePos(scale, pos );}
  523. MatrixM(C Matrix3 &orn , C VecD &pos ) {T.orn()=orn; T.pos=pos;}
  524. MatrixM(C VecD &pos , C Matrix3 &orn ) {T.orn()=orn; T.pos=pos;}
  525. MatrixM(C Matrix3 &m);
  526. MatrixM(C Matrix &m);
  527. MatrixM(C OrientM &o);
  528. };extern MatrixM
  529. const MatrixMIdentity; // identity
  530. /******************************************************************************/
  531. STRUCT(MatrixD , MatrixD3) // Matrix 4x3 (orientation + scale + position, double precision)
  532. //{
  533. VecD pos; // position
  534. MatrixD3& orn() {return T;} // get reference to self as 'MatrixD3'
  535. C MatrixD3& orn()C {return T;} // get reference to self as const 'MatrixD3'
  536. // transform
  537. MatrixD& operator*=( Dbl f);
  538. MatrixD& operator/=( Dbl f);
  539. MatrixD& operator*=(C VecD &v);
  540. MatrixD& operator/=(C VecD &v);
  541. MatrixD& operator+=(C VecD &v) {pos+=v; return T;}
  542. MatrixD& operator-=(C VecD &v) {pos-=v; return T;}
  543. MatrixD& operator+=(C MatrixD &m);
  544. MatrixD& operator-=(C MatrixD &m);
  545. MatrixD& operator*=(C MatrixD3 &m) {return mul(m);}
  546. MatrixD& operator*=(C MatrixD &m) {return mul(m);}
  547. MatrixD& operator/=(C MatrixD3 &m) {return div(m);}
  548. MatrixD& operator/=(C MatrixD &m) {return div(m);}
  549. Bool operator==(C MatrixD &m)C;
  550. Bool operator!=(C MatrixD &m)C;
  551. friend MatrixD operator* (C MatrixD &a, C MatrixD3 &b) {MatrixD temp; a.mul (b, temp); return temp;} // get a*b
  552. friend MatrixD operator* (C MatrixD &a, C MatrixD &b) {MatrixD temp; a.mul (b, temp); return temp;} // get a*b
  553. friend MatrixD operator/ (C MatrixD &a, C MatrixD3 &b) {MatrixD temp; a.div (b, temp); return temp;} // get a/b
  554. friend MatrixD operator/ (C MatrixD &a, C MatrixD &b) {MatrixD temp; a.div (b, temp); return temp;} // get a/b
  555. friend MatrixD operator~ (C MatrixD &m ) {MatrixD temp; m.inverse( temp); return temp;} // get inversed 'm'
  556. friend MatrixD operator* (C MatrixD3 &a, C MatrixD &b) {return MatrixD(a)*=b;} // get a*b
  557. friend MatrixD operator/ (C MatrixD3 &a, C MatrixD &b) {return MatrixD(a)/=b;} // get a/b
  558. void mul(C MatrixD3 &matrix, MatrixD &dest)C; // multiply self by 'matrix' and store result in 'dest'
  559. void mul(C MatrixD &matrix, MatrixD &dest)C; // multiply self by 'matrix' and store result in 'dest'
  560. MatrixD& mul(C MatrixD &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  561. MatrixD& mul(C MatrixD3 &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  562. void div(C MatrixD3 &matrix, MatrixD &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  563. void div(C MatrixD &matrix, MatrixD &dest)C; // divide self by 'matrix' and store result in 'dest', this method assumes that matrixes are orthogonal
  564. MatrixD& div(C MatrixD3 &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  565. MatrixD& div(C MatrixD &matrix ) {div(matrix, T); return T;} // divide self by 'matrix' , this method assumes that matrixes are orthogonal
  566. void divNormalized(C MatrixD3 &matrix, MatrixD &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  567. void divNormalized(C MatrixD &matrix, MatrixD &dest)C; // divide self by 'matrix' and store result in 'dest', this method is faster than 'div' however 'matrix' must be normalized
  568. MatrixD& divNormalized(C MatrixD3 &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  569. MatrixD& divNormalized(C MatrixD &matrix ) {divNormalized(matrix, T); return T;} // divide self by 'matrix' , this method is faster than 'div' however 'matrix' must be normalized
  570. void inverse(MatrixD &dest, Bool normalized=false)C; // inverse self to 'dest', if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  571. MatrixD& inverse( Bool normalized=false) {inverse(T, normalized); return T;} // inverse self , if you know that the matrix is normalized then set 'normalized=true' for more performance, this method assumes that matrix is orthogonal
  572. void inverseNonOrthogonal(MatrixD &dest)C; // inverse self to 'dest', this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  573. MatrixD& inverseNonOrthogonal( ) {inverseNonOrthogonal(T); return T;} // inverse self , this method is slower than 'inverse' however it properly handles non-orthogonal matrixes
  574. MatrixD& normalize( ) {super::normalize( ); return T;} // normalize scale , this sets the length of 'x' 'y' 'z' vectors to 'scale'
  575. MatrixD& normalize( Dbl scale) {super::normalize(scale); return T;} // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale'
  576. MatrixD& normalize(C VecD &scale) {super::normalize(scale); return T;} // normalize scale to 'scale', this sets the length of 'x' 'y' 'z' vectors to 'scale.x' 'scale.y' 'scale.z'
  577. MatrixD& move (Dbl x, Dbl y, Dbl z) {pos +=VecD(x, y, z); return T;} // move
  578. MatrixD& move (C VecD2 &move ) {pos.xy+=move ; return T;} // move
  579. MatrixD& move (C VecD &move ) {pos +=move ; return T;} // move
  580. MatrixD& moveBack(C VecD &move ) {pos -=move ; return T;} // move back
  581. MatrixD& anchor(C VecD &anchor); // set 'pos' of this matrix so that 'anchor' transformed by this matrix will remain the same - "anchor*T==anchor"
  582. MatrixD& scale ( Dbl scale) { T*=scale ; return T;} // scale
  583. MatrixD& scale ( C VecD &scale) { T*=scale ; return T;} // scale
  584. MatrixD& scaleOrn ( Dbl scale) {super::scale(scale); return T;} // scale orientation only
  585. MatrixD& scaleOrn ( C VecD &scale) {super::scale(scale); return T;} // scale orientation only
  586. MatrixD& scale (C VecD &dir, Dbl scale); // scale along 'dir' direction by 'scale' value, 'dir' must be normalized
  587. MatrixD& scalePlane(C VecD &nrm, Dbl scale); // scale along plane of 'nrm' normal by 'scale' value, 'nrm' must be normalized
  588. MatrixD& rotateX ( Dbl angle); // rotate by x axis
  589. MatrixD& rotateY ( Dbl angle); // rotate by y axis
  590. MatrixD& rotateZ ( Dbl angle); // rotate by z axis
  591. MatrixD& rotateXY( Dbl x , Dbl y ); // rotate by x axis and then by y axis, works the same as "rotateX(x).rotateY(y)" but faster
  592. MatrixD& rotate (C VecD &axis, Dbl angle); // rotate by vector, 'axis' must be normalized
  593. MatrixD& rotateXL( Dbl angle) {super::rotateXL(angle); return T;} // rotate matrix by its x vector (x-axis rotation in local space)
  594. MatrixD& rotateYL( Dbl angle) {super::rotateYL(angle); return T;} // rotate matrix by its y vector (y-axis rotation in local space)
  595. MatrixD& rotateZL( Dbl angle) {super::rotateZL(angle); return T;} // rotate matrix by its z vector (z-axis rotation in local space)
  596. MatrixD& mirrorX( ); // mirror matrix in X axis
  597. MatrixD& mirrorY( ); // mirror matrix in Y axis
  598. MatrixD& mirrorZ( ); // mirror matrix in Z axis
  599. MatrixD& mirror (C PlaneD &plane); // mirror matrix by plane
  600. // set (set methods reset the full matrix)
  601. MatrixD& identity(); // set as identity
  602. MatrixD& zero (); // set all vectors to zero
  603. MatrixD& setPos (Dbl x, Dbl y, Dbl z ); // set as positioned identity
  604. MatrixD& setPos (C VecD2 &pos ); // set as positioned identity
  605. MatrixD& setPos (C VecD &pos ); // set as positioned identity
  606. MatrixD& setScale ( Dbl scale ); // set as scaled identity
  607. MatrixD& setScale (C VecD &scale ); // set as scaled identity
  608. MatrixD& setPosScale(C VecD &pos , Dbl scale); // set as positioned & scaled identity
  609. MatrixD& setPosScale(C VecD &pos , C VecD &scale); // set as positioned & scaled identity
  610. MatrixD& setScalePos( Dbl scale, C VecD &pos ); // set as scaled & positioned identity
  611. MatrixD& setScalePos(C VecD &scale, C VecD &pos ); // set as scaled & positioned identity
  612. MatrixD& setRotateX ( Dbl angle); // set as x-rotated identity
  613. MatrixD& setRotateY ( Dbl angle); // set as y-rotated identity
  614. MatrixD& setRotateZ ( Dbl angle); // set as z-rotated identity
  615. MatrixD& setRotateXY( Dbl x , Dbl y ); // set as x-y-rotated identity, works the same as setRotateX(x).rotateY(y) but faster
  616. MatrixD& setRotate (C VecD &axis, Dbl angle); // set as rotated by vector identity, 'axis' must be normalized
  617. MatrixD& setPosOrient(C VecD &pos, DIR_ENUM dir ); // set as positioned orientation from DIR_ENUM
  618. MatrixD& setPosRight (C VecD &pos, C VecD &right ); // set as pos='pos', x='right' and calculate correct y,z, 'right' must be normalized
  619. MatrixD& setPosUp (C VecD &pos, C VecD &up ); // set as pos='pos', y='up' and calculate correct x,z, 'up' must be normalized
  620. MatrixD& setPosDir (C VecD &pos, C VecD &dir ); // set as pos='pos', z='dir' and calculate correct x,y, 'dir' must be normalized
  621. MatrixD& setPosDir (C VecD &pos, C VecD &dir, C VecD &up ); // set as pos='pos', z='dir', y='up' and calculate correct x , 'dir up' must be normalized
  622. MatrixD& setPosDir (C VecD &pos, C VecD &dir, C VecD &up, C VecD &right); // set as pos='pos', z='dir', y='up', x='right' , 'dir up right' must be normalized
  623. // get
  624. VecD scale()C {return super::scale();} // get each axis scale
  625. Str asText(Int precision=INT_MAX)C {return super::asText(precision)+", Pos: "+pos.asText(precision);} // get text description
  626. // operations
  627. #if EE_PRIVATE
  628. VecD2 convert(C VecD &src, Bool normalized=false)C; // return converted 3D 'src' to 2D vector according to matrix x,y axes and position
  629. VecD convert(C VecD2 &src )C; // return converted 2D 'src' to 3D vector according to matrix x,y axes and position
  630. EdgeD2 convert(C EdgeD &src, Bool normalized=false)C; // return converted 3D 'src' to 2D edge according to matrix x,y axes and position
  631. EdgeD convert(C EdgeD2 &src )C; // return converted 2D 'src' to 3D edge according to matrix x,y axes and position
  632. #endif
  633. MatrixD& setTransformAtPos(C VecD &pos, C MatrixD3 &matrix); // set as transformation at position
  634. MatrixD& setTransformAtPos(C VecD &pos, C MatrixD &matrix); // set as transformation at position
  635. MatrixD& transformAtPos(C VecD &pos, C MatrixD3 &matrix); // transform at position
  636. MatrixD& transformAtPos(C VecD &pos, C MatrixD &matrix); // transform at position
  637. // draw
  638. void draw(C Color &x_color=RED, C Color &y_color=GREEN, C Color &z_color=BLUE, Bool arrow=true)C {super::draw(pos, x_color, y_color, z_color, arrow);} // draw axes, this can be optionally called outside of Render function, this relies on active object matrix which can be set using 'SetMatrix' function
  639. MatrixD() {}
  640. MatrixD( Dbl scale ) {setScale (scale );}
  641. MatrixD(C VecD &pos ) {setPos (pos );}
  642. MatrixD(C VecD &pos , Dbl scale) {setPosScale(pos , scale);}
  643. MatrixD( Dbl scale, C VecD &pos ) {setScalePos(scale, pos );}
  644. MatrixD(C VecD &scale, C VecD &pos ) {setScalePos(scale, pos );}
  645. MatrixD(C MatrixD3 &orn , C VecD &pos ) {T.orn()=orn; T.pos=pos;}
  646. MatrixD(C VecD &pos , C MatrixD3 &orn ) {T.orn()=orn; T.pos=pos;}
  647. MatrixD(C MatrixD3 &m);
  648. MatrixD(C Matrix &m);
  649. MatrixD(C OrientP &o);
  650. };
  651. /******************************************************************************/
  652. struct Matrix4 // Matrix 4x4
  653. {
  654. Vec4 x, y, z, pos;
  655. #if EE_PRIVATE
  656. Flt determinant()C;
  657. #endif
  658. // transform
  659. Matrix4& operator*=(C Matrix4 &m) {return mul(m);}
  660. friend Matrix4 operator* (C Matrix4 &a, C Matrix4 &b) {Matrix4 temp; a.mul(b, temp); return temp;} // get a*b
  661. friend Matrix4 operator* (C Matrix &a, C Matrix4 &b) {Matrix4 temp; a.mul(b, temp); return temp;} // get a*b
  662. Matrix4& inverse (); // inverse self
  663. Matrix4& transpose(); // transpose self
  664. void mul(C Matrix4 &matrix, Matrix4 &dest)C; // multiply self by 'matrix' and store result in 'dest'
  665. Matrix4& mul(C Matrix4 &matrix ) {mul(matrix, T); return T;} // multiply self by 'matrix'
  666. // set (set methods reset the full matrix)
  667. Matrix4& identity(); // set as identity
  668. Matrix4& zero (); // set all vectors to zero
  669. Matrix4() {}
  670. Matrix4(C Matrix3 &m);
  671. Matrix4(C Matrix &m);
  672. };
  673. #if EE_PRIVATE
  674. extern Matrix4 ProjMatrix; // Projection Matrix
  675. extern Flt ProjMatrixEyeOffset[2];
  676. extern Vec2 PixelOffset;
  677. #endif
  678. /******************************************************************************/
  679. #if EE_PRIVATE
  680. struct GpuMatrix // GPU Matrix (transposed Matrix)
  681. {
  682. Flt xx, yx, zx, _x,
  683. xy, yy, zy, _y,
  684. xz, yz, zz, _z;
  685. GpuMatrix& fromMul(C Matrix &a, C Matrix &b); // set from "a*b"
  686. GpuMatrix& fromMul(C Matrix &a, C MatrixM &b); // set from "a*b"
  687. GpuMatrix& fromMul(C MatrixM &a, C MatrixM &b); // set from "a*b"
  688. GpuMatrix() {}
  689. GpuMatrix(C Matrix &m);
  690. GpuMatrix(C MatrixM &m);
  691. };
  692. #endif
  693. struct RevMatrix3 : Matrix3 // Reverse Matrix
  694. {
  695. };
  696. struct RevMatrix : Matrix // Reverse Matrix
  697. {
  698. RevMatrix3& orn() {return (RevMatrix3&)T;} // get reference to self as 'RevMatrix3'
  699. C RevMatrix3& orn()C {return (RevMatrix3&)T;} // get reference to self as const 'RevMatrix3'
  700. };
  701. /******************************************************************************/
  702. #if EE_PRIVATE
  703. extern Int Matrixes; // number of active matrixes
  704. #endif
  705. /******************************************************************************/
  706. Bool Equal(C Matrix3 &a, C Matrix3 &b, Flt eps=EPS );
  707. Bool Equal(C MatrixD3 &a, C MatrixD3 &b, Dbl eps=EPSD );
  708. Bool Equal(C Matrix &a, C Matrix &b, Flt eps=EPS , Flt pos_eps=EPS );
  709. Bool Equal(C MatrixM &a, C MatrixM &b, Flt eps=EPS , Dbl pos_eps=EPSD);
  710. Bool Equal(C MatrixD &a, C MatrixD &b, Dbl eps=EPSD, Dbl pos_eps=EPSD);
  711. Bool Equal(C Matrix4 &a, C Matrix4 &b, Flt eps=EPS );
  712. void GetTransform(Matrix3 &transform, C Orient &start, C Orient &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  713. void GetTransform(Matrix3 &transform, C Matrix3 &start, C Matrix3 &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  714. void GetTransform(MatrixD3 &transform, C MatrixD3 &start, C MatrixD3 &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  715. void GetTransform(Matrix &transform, C Matrix &start, C Matrix &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  716. void GetTransform(MatrixD &transform, C MatrixD &start, C MatrixD &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  717. Matrix3 GetTransform( C Orient &start, C Orient &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  718. Matrix3 GetTransform( C Matrix3 &start, C Matrix3 &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  719. MatrixD3 GetTransform( C MatrixD3 &start, C MatrixD3 &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  720. Matrix GetTransform( C Matrix &start, C Matrix &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  721. MatrixD GetTransform( C MatrixD &start, C MatrixD &result); // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  722. inline void GetTransform (RevMatrix &transform, C Matrix &start, C Matrix &result) {result.div (start, transform);} // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result"
  723. inline void GetTransformNormalized(RevMatrix &transform, C Matrix &start, C Matrix &result) {result.divNormalized(start, transform);} // get 'transform' matrix that transforms 'start' to 'result' according to following formula "start*transform=result", this function assumes that 'start' and 'result' are normalized
  724. void GetDelta(Vec &pos, Vec &angle, C Matrix &from, C Matrix &to); // get position and angle axis delta from 'from' and 'to' matrixes !! matrixes must be normalized !!
  725. void GetDelta(Vec &pos, Vec &angle, C MatrixM &from, C MatrixM &to); // get position and angle axis delta from 'from' and 'to' matrixes !! matrixes must be normalized !!
  726. void GetVel(Vec &vel, Vec &ang_vel, C Matrix &from, C Matrix &to, Flt dt=Time.d()); // get linear velocity and angular velocity from 'from' and 'to' matrixes using 'dt' time delta !! matrixes must be normalized !!
  727. void GetVel(Vec &vel, Vec &ang_vel, C MatrixM &from, C MatrixM &to, Flt dt=Time.d()); // get linear velocity and angular velocity from 'from' and 'to' matrixes using 'dt' time delta !! matrixes must be normalized !!
  728. Flt GetLodDist2(C Vec &lod_center, C Matrix &matrix); // calculate squared distance from 'lod_center' transformed by 'matrix' to active camera, returned value can be used as parameter for 'Mesh.getDrawLod' methods
  729. Flt GetLodDist2(C Vec &lod_center, C MatrixM &matrix); // calculate squared distance from 'lod_center' transformed by 'matrix' to active camera, returned value can be used as parameter for 'Mesh.getDrawLod' methods
  730. #if EE_PRIVATE
  731. void InitMatrix();
  732. void SetMatrixCount(Int num=1); // set number of used matrixes and velocities
  733. void SetFurVelCount(Int num=1); // set number of used fur velocities
  734. void SetFastViewMatrix(C Matrix &view_matrix); // set view matrix
  735. void SetFastMatrix ( ); // set object matrix to 'MatrixIdentity'
  736. void SetFastMatrix (C Matrix &matrix ); // set object matrix
  737. void SetFastMatrix (C MatrixM &matrix ); // set object matrix
  738. void SetFastVel (Byte i, C Vec &vel ); // set i-th object velocity
  739. void SetFastVel ( C Vec &vel ); // set object velocity
  740. void SetFastVel ( ); // set object velocity to 'VecZero'
  741. void SetFastAngVel( C Vec &ang_vel_shader); // set object angular velocity
  742. void SetFastAngVel( ); // set object angular velocity to 'VecZero'
  743. void SetOneMatrix( ); // set object matrix to 'MatrixIdentity' and number of used matrixes to 1
  744. void SetOneMatrix(C Matrix &matrix); // set object matrix and number of used matrixes to 1
  745. void SetOneMatrix(C MatrixM &matrix); // set object matrix and number of used matrixes to 1
  746. void SetVelFur(C Matrix3 &view_matrix, C Vec &vel); // set velocity for fur effect
  747. void SetProjMatrix();
  748. void SetProjMatrix(Flt proj_offset);
  749. void SetMatrixVelRestore();
  750. void SetMatrixVelSplit (Byte *matrix, Int matrixes);
  751. void SetMatrixFurVelRestore();
  752. void SetMatrixFurVelSplit (Byte *matrix, Int matrixes);
  753. void SetAngVelShader(Vec &ang_vel_shader, C Vec &ang_vel, C Matrix3 &matrix);
  754. #endif
  755. void SetMatrix(C Matrix &matrix=MatrixIdentity ); // set active object rendering matrix and clear velocities to zero
  756. void SetMatrix(C MatrixM &matrix ); // set active object rendering matrix and clear velocities to zero
  757. void SetMatrix(C Matrix &matrix, C Vec &vel, C Vec &ang_vel=VecZero); // set active object rendering matrix and velocities
  758. void SetMatrix(C MatrixM &matrix, C Vec &vel, C Vec &ang_vel=VecZero); // set active object rendering matrix and velocities
  759. /******************************************************************************/