您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ugeentity.cpp 5.7KB

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