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 5.0KB

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