Drawing3D.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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::setDrehung(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::setDrehung(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::setDrehungX(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::setDrehungY(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::setDrehungZ(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. // Calculates the matrices of all bones of the drawing's skeleton
  109. // viewProj: The multiplied camera matrices
  110. // matBuffer: An array of matrices to be filled
  111. // return: The number of matrices the drawing requires
  112. int Drawable3D::errechneMatrizen(
  113. const Mat4<float>& viewProj, Mat4<float>* matBuffer)
  114. {
  115. matBuffer[0] = viewProj * welt;
  116. return 1;
  117. }
  118. // Processes a mouse event
  119. // me: The mouse event to be processed
  120. void Drawable3D::doMouseEvent(MouseEvent3D& me) {}
  121. // Processes a keyboard event
  122. // te: The keyboard event to be processed
  123. void Drawable3D::doKeyboardEvent(KeyboardEvent& te) {}
  124. // Processes the elapsed time
  125. // tickval: The time in seconds since the last call of the function
  126. // return: true if the object has changed, false
  127. // otherwise.
  128. bool Drawable3D::tick(double tickval)
  129. {
  130. if (rend)
  131. {
  132. welt = welt.translation(pos) * welt.rotationZ(angle.z)
  133. * welt.rotationX(angle.x) * welt.rotationY(angle.y)
  134. * welt.scaling(size);
  135. rend = 0;
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. // Returns whether the object contains partially or fully transparent areas
  141. bool Drawable3D::hatAlpha() const
  142. {
  143. return alpha;
  144. }
  145. // Returns the radius of a sphere that encloses the entire model
  146. float Drawable3D::getRadius() const
  147. {
  148. return radius;
  149. }
  150. // Returns a point representing the position of the drawing in the world
  151. const Vec3<float>& Drawable3D::getPos() const
  152. {
  153. return pos;
  154. }
  155. // Returns the X position of the drawing in the world
  156. float Drawable3D::getX() const
  157. {
  158. return pos.x;
  159. }
  160. // Returns the Y position of the drawing in the world
  161. float Drawable3D::getY() const
  162. {
  163. return pos.y;
  164. }
  165. // Returns the Z position of the drawing in the world
  166. float Drawable3D::getZ() const
  167. {
  168. return pos.z;
  169. }
  170. // Returns a vector representing the rotation of the drawing in the world.
  171. // x is the rotation around the X axis in radians, etc.
  172. const Vec3<float>& Drawable3D::getDrehung() const
  173. {
  174. return angle;
  175. }
  176. // Returns the rotation around the X axis in radians
  177. float Drawable3D::getXDrehung() const
  178. {
  179. return angle.x;
  180. }
  181. // Returns the rotation around the Y axis in radians
  182. float Drawable3D::getYDrehung() const
  183. {
  184. return angle.y;
  185. }
  186. // Returns the rotation around the Z axis in radians
  187. float Drawable3D::getZDrehung() const
  188. {
  189. return angle.z;
  190. }
  191. // Returns the matrix that translates the drawing into world space
  192. const Mat4<float>& Drawable3D::getMatrix() const
  193. {
  194. return welt;
  195. }
  196. //! Calculates for a point in local drawing coordinates the point in
  197. //! world coordinates by applying rotation, scaling and translation
  198. Vec3<float> Drawable3D::applyWorldTransformation(
  199. const Vec3<float>& modelPos) const
  200. {
  201. return welt * modelPos;
  202. }