Drawing3D.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "Drawing3D.h"
  2. using namespace Framework;
  3. // Contents of the Drawable3D class
  4. // Constructor
  5. Drawable3D::Drawable3D()
  6. : ReferenceCounter()
  7. {
  8. welt = welt.identity();
  9. pos = Vec3<float>(0, 0, 0);
  10. angle = Vec3<float>(0, 0, 0);
  11. lastTickReturn = 0;
  12. rend = 0;
  13. alpha = 0;
  14. radius = 0;
  15. size = 1.f;
  16. }
  17. Drawable3D::~Drawable3D() {}
  18. // Sets the position of the drawing in the world
  19. // p: The position
  20. void Drawable3D::setPosition(const Vec3<float>& p)
  21. {
  22. pos = p;
  23. rend = 1;
  24. }
  25. // Sets the position of the drawing in the world
  26. // x: The x position
  27. // y: The y position
  28. // z: The z position
  29. void Drawable3D::setPosition(float x, float y, float z)
  30. {
  31. pos.x = x;
  32. pos.y = y;
  33. pos.z = z;
  34. rend = 1;
  35. }
  36. // Sets the position of the drawing in the world
  37. // x: The x position
  38. void Drawable3D::setX(float x)
  39. {
  40. pos.x = x;
  41. rend = 1;
  42. }
  43. // Sets the position of the drawing in the world
  44. // y: The y position
  45. void Drawable3D::setY(float y)
  46. {
  47. pos.y = y;
  48. rend = 1;
  49. }
  50. // Sets the position of the drawing in the world
  51. // z: The z position
  52. void Drawable3D::setZ(float z)
  53. {
  54. pos.z = z;
  55. rend = 1;
  56. }
  57. // Sets the rotation of the drawing in the world
  58. // d: The rotation around the x, y and z axis
  59. void Drawable3D::setRotation(const Vec3<float>& d)
  60. {
  61. angle = d;
  62. rend = 1;
  63. }
  64. // Sets the rotation of the drawing in the world
  65. // xWinkel: The rotation around the x axis
  66. // yWinkel: The rotation around the y axis
  67. // zWinkel: The rotation around the z axis
  68. void Drawable3D::setRotation(float xWinkel, float yWinkel, float zWinkel)
  69. {
  70. angle.x = xWinkel;
  71. angle.y = yWinkel;
  72. angle.z = zWinkel;
  73. rend = 1;
  74. }
  75. // Sets the rotation of the drawing in the world
  76. // winkel: The rotation around the x axis
  77. void Drawable3D::setRotationX(float winkel)
  78. {
  79. angle.x = winkel;
  80. rend = 1;
  81. }
  82. // Sets the rotation of the drawing in the world
  83. // winkel: The rotation around the y axis
  84. void Drawable3D::setRotationY(float winkel)
  85. {
  86. angle.y = winkel;
  87. rend = 1;
  88. }
  89. // Sets the rotation of the drawing in the world
  90. // winkel: The rotation around the z axis
  91. void Drawable3D::setRotationZ(float winkel)
  92. {
  93. angle.z = winkel;
  94. rend = 1;
  95. }
  96. // Sets whether the object contains partially or fully transparent areas
  97. // a: true if partially or fully transparent areas are present
  98. void Drawable3D::setAlpha(bool a)
  99. {
  100. alpha = a;
  101. rend = 1;
  102. }
  103. //! Sets the scaling
  104. void Drawable3D::setSize(float size)
  105. {
  106. this->size = size;
  107. rend = 1;
  108. }
  109. float Framework::Drawable3D::getSize() const
  110. {
  111. return size;
  112. }
  113. // Calculates the matrices of all bones of the drawing's skeleton
  114. // viewProj: The multiplied camera matrices
  115. // matBuffer: An array of matrices to be filled
  116. // return: The number of matrices the drawing requires
  117. int Drawable3D::calculateMatrices(
  118. const Mat4<float>& viewProj, Mat4<float>* matBuffer)
  119. {
  120. matBuffer[0] = viewProj * welt;
  121. return 1;
  122. }
  123. // Processes a mouse event
  124. // me: The mouse event to be processed
  125. void Drawable3D::doMouseEvent(MouseEvent3D& me) {}
  126. // Processes a keyboard event
  127. // te: The keyboard event to be processed
  128. void Drawable3D::doKeyboardEvent(KeyboardEvent& te) {}
  129. // Processes the elapsed time
  130. // tickval: The time in seconds since the last call of the function
  131. // return: true if the object has changed, false
  132. // otherwise.
  133. bool Drawable3D::tick(double tickval)
  134. {
  135. if (rend)
  136. {
  137. welt = welt.translation(pos) * welt.rotationZ(angle.z)
  138. * welt.rotationX(angle.x) * welt.rotationY(angle.y)
  139. * welt.scaling(size);
  140. rend = 0;
  141. lastTickReturn = 1;
  142. return 1;
  143. }
  144. lastTickReturn = 0;
  145. return 0;
  146. }
  147. // Returns whether the object contains partially or fully transparent areas
  148. bool Drawable3D::hasAlpha() const
  149. {
  150. return alpha;
  151. }
  152. // Returns the radius of a sphere that encloses the entire model
  153. float Drawable3D::getRadius() const
  154. {
  155. return radius;
  156. }
  157. // Returns a point representing the position of the drawing in the world
  158. const Vec3<float>& Drawable3D::getPos() const
  159. {
  160. return pos;
  161. }
  162. // Returns the X position of the drawing in the world
  163. float Drawable3D::getX() const
  164. {
  165. return pos.x;
  166. }
  167. // Returns the Y position of the drawing in the world
  168. float Drawable3D::getY() const
  169. {
  170. return pos.y;
  171. }
  172. // Returns the Z position of the drawing in the world
  173. float Drawable3D::getZ() const
  174. {
  175. return pos.z;
  176. }
  177. // Returns a vector representing the rotation of the drawing in the world.
  178. // x is the rotation around the X axis in radians, etc.
  179. const Vec3<float>& Drawable3D::getRotation() const
  180. {
  181. return angle;
  182. }
  183. // Returns the rotation around the X axis in radians
  184. float Drawable3D::getXRotation() const
  185. {
  186. return angle.x;
  187. }
  188. // Returns the rotation around the Y axis in radians
  189. float Drawable3D::getYRotation() const
  190. {
  191. return angle.y;
  192. }
  193. // Returns the rotation around the Z axis in radians
  194. float Drawable3D::getZRotation() const
  195. {
  196. return angle.z;
  197. }
  198. // Returns the matrix that translates the drawing into world space
  199. const Mat4<float>& Drawable3D::getMatrix() const
  200. {
  201. return welt;
  202. }
  203. //! Calculates for a point in local drawing coordinates the point in
  204. //! world coordinates by applying rotation, scaling and translation
  205. Vec3<float> Drawable3D::applyWorldTransformation(
  206. const Vec3<float>& modelPos) const
  207. {
  208. return welt * modelPos;
  209. }
  210. bool Framework::Drawable3D::getLastTickReturn() const
  211. {
  212. return lastTickReturn;
  213. }