You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ugeentity.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "ugeentity.h"
  2. #include "utils/tools.h"
  3. UGEEntity::UGEEntity(QObject *parent)
  4. : QObject(parent)
  5. , _scale(1.0, 1.0, 1.0)
  6. , _visible(true)
  7. {
  8. }
  9. UGEEntity::~UGEEntity()
  10. {
  11. }
  12. Vector3D UGEEntity::getPosition() const
  13. {
  14. return _position;
  15. }
  16. void UGEEntity::setPosition(const Vector3D &position)
  17. {
  18. _position = position;
  19. emit positionChanged();
  20. emit positionChanged(_position);
  21. }
  22. void UGEEntity::move(const Vector3D &move)
  23. {
  24. _position += move;
  25. emit positionChanged();
  26. emit positionChanged(_position);
  27. }
  28. //Vector3D UGEEntity::getSpeed() const
  29. //{
  30. // return _speed;
  31. //}
  32. //void UGEEntity::setSpeed(const Vector3D &speed)
  33. //{
  34. // _speed = speed;
  35. //}
  36. //void UGEEntity::accelerate(const Vector3D &speed)
  37. //{
  38. // _speed += speed;
  39. //}
  40. Vector3D UGEEntity::getRotation() const
  41. {
  42. return _rotation;
  43. }
  44. void UGEEntity::setRotation(const Vector3D &rotation)
  45. {
  46. _rotation = Tools::normalizeAngle(rotation);
  47. emit rotationChanged();
  48. emit rotationChanged(rotation);
  49. }
  50. void UGEEntity::rotate(const Vector3D &rotation)
  51. {
  52. _rotation = Tools::normalizeAngle(rotation + _rotation);
  53. emit rotationChanged();
  54. emit rotationChanged(rotation);
  55. }
  56. Vector3D UGEEntity::getScale() const
  57. {
  58. return _scale;
  59. }
  60. void UGEEntity::setScale(const Vector3D &scale)
  61. {
  62. _scale = scale;
  63. emit scaleChanged();
  64. emit scaleChanged(_scale);
  65. }
  66. void UGEEntity::scale(const Vector3D &scale)
  67. {
  68. _scale += scale;
  69. emit scaleChanged();
  70. emit scaleChanged(_scale);
  71. }
  72. bool UGEEntity::isVisible() const
  73. {
  74. return _visible;
  75. }
  76. void UGEEntity::setVisible(bool visible)
  77. {
  78. _visible = visible;
  79. emit visibilityChanged();
  80. emit visibilityChanged(_visible);
  81. }
  82. void UGEEntity::show()
  83. {
  84. _visible = true;
  85. emit visibilityChanged();
  86. emit visibilityChanged(_visible);
  87. }
  88. void UGEEntity::hide()
  89. {
  90. _visible = false;
  91. emit visibilityChanged();
  92. emit visibilityChanged(_visible);
  93. }
  94. QColor UGEEntity::getColor() const
  95. {
  96. return _color;
  97. }
  98. void UGEEntity::setColor(const QColor &color)
  99. {
  100. _color = color;
  101. emit colorChanged();
  102. emit colorChanged(_color);
  103. }
  104. Vector3D UGEEntity::getRealPoint(const Vector3D &pos)
  105. {
  106. Matrix3x3 trans = getTransformationMatrix();
  107. return (trans.multMatrix(pos) + _position);
  108. }
  109. ColorVector3D UGEEntity::getRealPoint(const ColorVector3D &pos)
  110. {
  111. return ColorVector3D(pos.getColor(), getRealPoint((Vector3D)pos));
  112. }
  113. TextureVector3D UGEEntity::getRealPoint(const TextureVector3D &pos)
  114. {
  115. return TextureVector3D(pos.getTextureCoord(), getRealPoint((ColorVector3D)pos));
  116. }
  117. Matrix3x3 UGEEntity::getTransformationMatrix() const
  118. {
  119. return getRotationMatrix().multMatrix(getScaleMatrix());
  120. }
  121. Matrix3x3 UGEEntity::getScaleMatrix() const
  122. {
  123. Matrix3x3 scale;
  124. scale.setScalar(0, 0, _scale.getX());
  125. scale.setScalar(1, 1, _scale.getY());
  126. scale.setScalar(2, 2, _scale.getZ());
  127. return scale;
  128. }
  129. Matrix3x3 UGEEntity::getRotationMatrix() const
  130. {
  131. Vector3D r = Tools::degreeToRad(_rotation);
  132. Matrix3x3 mx;
  133. mx.setToIdentity();
  134. mx.setScalar(1, 1, cos(r.getX()));
  135. mx.setScalar(1, 2, -sin(r.getX()));
  136. mx.setScalar(2, 1, sin(r.getX()));
  137. mx.setScalar(2, 2, cos(r.getX()));
  138. Matrix3x3 my;
  139. my.setToIdentity();
  140. my.setScalar(0, 0, cos(r.getY()));
  141. my.setScalar(0, 2, sin(r.getY()));
  142. my.setScalar(2, 0, -sin(r.getY()));
  143. my.setScalar(2, 2, cos(r.getY()));
  144. Matrix3x3 mz;
  145. mz.setToIdentity();
  146. mz.setScalar(0, 0, cos(r.getZ()));
  147. mz.setScalar(0, 1, -sin(r.getZ()));
  148. mz.setScalar(1, 0, sin(r.getZ()));
  149. mz.setScalar(1, 1, cos(r.getZ()));
  150. return mx.multMatrix(my).multMatrix(mz);
  151. }
  152. void UGEEntity::drawPoint(AbstractRenderDevice *device, const ColorVector3D &point)
  153. {
  154. device->drawPoint(getRealPoint(point));
  155. }
  156. void UGEEntity::drawLine(AbstractRenderDevice *device, const ColorVector3D &begin, const ColorVector3D &end, double width)
  157. {
  158. device->drawLine(getRealPoint(begin), getRealPoint(end), width);
  159. }
  160. void UGEEntity::drawPolygon(AbstractRenderDevice *device, QList<ColorVector3D> points)
  161. {
  162. for (int i = 0; i < points.size(); ++i) {
  163. points[i] = getRealPoint(points[i]);
  164. }
  165. device->drawPolygon(points);
  166. }
  167. void UGEEntity::drawPolygonTexture(AbstractRenderDevice *device, QList<TextureVector3D> points, const QVariant &textureId)
  168. {
  169. for (int i = 0; i < points.size(); ++i) {
  170. points[i] = getRealPoint(points[i]);
  171. }
  172. device->drawPolygonTexture(points, textureId);
  173. }