Drawing3D.cpp 5.2 KB

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