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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 scale;
  107. scale.setScalar(0, 0, _scale.getX());
  108. scale.setScalar(1, 1, _scale.getY());
  109. scale.setScalar(2, 2, _scale.getZ());
  110. Vector3D p = scale.multMatrix(pos);
  111. Matrix3x3 rot = getRotationMatrix(_rotation);
  112. return (rot.multMatrix(p) + _position);
  113. }
  114. ColorVector3D UGEEntity::getRealPoint(const ColorVector3D &pos)
  115. {
  116. return ColorVector3D(pos.getColor(), getRealPoint((Vector3D)pos));
  117. }
  118. TextureVector3D UGEEntity::getRealPoint(const TextureVector3D &pos)
  119. {
  120. return TextureVector3D(pos.getTextureCoord(), getRealPoint((ColorVector3D)pos));
  121. }
  122. Matrix3x3 UGEEntity::getRotationMatrix(const Vector3D &rotation)
  123. {
  124. Vector3D r = Tools::degreeToRad(rotation);
  125. Matrix3x3 mx;
  126. mx.setToIdentity();
  127. mx.setScalar(1, 1, cos(r.getX()));
  128. mx.setScalar(1, 2, -sin(r.getX()));
  129. mx.setScalar(2, 1, sin(r.getX()));
  130. mx.setScalar(2, 2, cos(r.getX()));
  131. Matrix3x3 my;
  132. my.setToIdentity();
  133. my.setScalar(0, 0, cos(r.getY()));
  134. my.setScalar(0, 2, sin(r.getY()));
  135. my.setScalar(2, 0, -sin(r.getY()));
  136. my.setScalar(2, 2, cos(r.getY()));
  137. Matrix3x3 mz;
  138. mz.setToIdentity();
  139. mz.setScalar(0, 0, cos(r.getZ()));
  140. mz.setScalar(0, 1, -sin(r.getZ()));
  141. mz.setScalar(1, 0, sin(r.getZ()));
  142. mz.setScalar(1, 1, cos(r.getZ()));
  143. return mx.multMatrix(my).multMatrix(mz);
  144. }
  145. void UGEEntity::drawPoint(AbstractRenderDevice *device, const ColorVector3D &point)
  146. {
  147. device->drawPoint(getRealPoint(point));
  148. }
  149. void UGEEntity::drawLine(AbstractRenderDevice *device, const ColorVector3D &begin, const ColorVector3D &end, double width)
  150. {
  151. device->drawLine(getRealPoint(begin), getRealPoint(end), width);
  152. }
  153. void UGEEntity::drawPolygon(AbstractRenderDevice *device, QList<ColorVector3D> points)
  154. {
  155. for (int i = 0; i < points.size(); ++i) {
  156. points[i] = getRealPoint(points[i]);
  157. }
  158. device->drawPolygon(points);
  159. }
  160. void UGEEntity::drawPolygonTexture(AbstractRenderDevice *device, QList<TextureVector3D> points, const QVariant &textureId)
  161. {
  162. for (int i = 0; i < points.size(); ++i) {
  163. points[i] = getRealPoint(points[i]);
  164. }
  165. device->drawPolygonTexture(points, textureId);
  166. }