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.

ugeentitycube.cpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "ugeentitycube.h"
  2. UGEEntityCube::UGEEntityCube(QObject *parent)
  3. : UGEEntity(parent)
  4. , _size(1.0)
  5. {
  6. connect(this, SIGNAL(sizeChanged()), this, SLOT(needUpdate()));
  7. connect(this, SIGNAL(textureChanged()), this, SLOT(needUpdate()));
  8. }
  9. double UGEEntityCube::getSize() const
  10. {
  11. return _size;
  12. }
  13. void UGEEntityCube::onDraw(AbstractRenderDevice *device)
  14. {
  15. if (!_facesColor.empty()) {
  16. for (int i = 0; i < _facesColor.size(); ++i) {
  17. // drawPolygon(device, _facesColor[i]);
  18. device->drawPolygon(_facesColor[i]);
  19. }
  20. }
  21. else {
  22. for (int i = 0; i < _facesTexture.size(); ++i) {
  23. // drawPolygonTexture(device, _facesTexture[i], _textureId);
  24. device->drawPolygonTexture(_facesTexture[i], _textureId);
  25. }
  26. }
  27. }
  28. void UGEEntityCube::setSize(double size)
  29. {
  30. _size = size;
  31. emit sizeChanged();
  32. emit sizeChanged(_size);
  33. }
  34. void UGEEntityCube::increaseSize(double size)
  35. {
  36. _size += size;
  37. emit sizeChanged();
  38. emit sizeChanged(_size);
  39. }
  40. QVariant UGEEntityCube::getTextureId() const
  41. {
  42. return _textureId;
  43. }
  44. void UGEEntityCube::setTextureId(const QVariant &textureId)
  45. {
  46. _textureId = textureId;
  47. setColor(Qt::white);
  48. emit textureChanged();
  49. emit textureChanged(_textureId);
  50. }
  51. void UGEEntityCube::onUpdate()
  52. {
  53. double r = _size / 2;
  54. if (_textureId.isNull()) {
  55. _facesColor.clear();
  56. QList<ColorVector3D> points;
  57. points << ColorVector3D(getColor(), -r, -r, r) << ColorVector3D(getColor(), -r, r, r)
  58. << ColorVector3D(getColor(), r, r, r) << ColorVector3D(getColor(), r, -r, r)
  59. << ColorVector3D(getColor(), -r, -r, -r) << ColorVector3D(getColor(), -r, r, -r)
  60. << ColorVector3D(getColor(), r, r, -r) << ColorVector3D(getColor(), r, -r, -r);
  61. for (int i = 0; i < points.size(); ++i) {
  62. points[i] = getRealPoint(points[i]);
  63. }
  64. _facesColor.append(QList<ColorVector3D>() << points[3] << points[2] << points[1] << points[0]);
  65. _facesColor.append(QList<ColorVector3D>() << points[2] << points[3] << points[7] << points[6]);
  66. _facesColor.append(QList<ColorVector3D>() << points[6] << points[7] << points[4] << points[5]);
  67. _facesColor.append(QList<ColorVector3D>() << points[5] << points[4] << points[0] << points[1]);
  68. _facesColor.append(QList<ColorVector3D>() << points[1] << points[2] << points[6] << points[5]);
  69. _facesColor.append(QList<ColorVector3D>() << points[4] << points[7] << points[3] << points[0]);
  70. }
  71. else {
  72. _facesTexture.clear();
  73. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.25, 0.50), getColor(), r, -r, r)
  74. << TextureVector3D(Vector2D(0.25, 0.25), getColor(), r, r, r)
  75. << TextureVector3D(Vector2D(0.00, 0.25), getColor(), -r, r, r)
  76. << TextureVector3D(Vector2D(0.00, 0.50), getColor(), -r, -r, r));
  77. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.25, 0.50), getColor(), r, r, r)
  78. << TextureVector3D(Vector2D(0.25, 0.25), getColor(), r, -r, r)
  79. << TextureVector3D(Vector2D(0.50, 0.25), getColor(), r, -r, -r)
  80. << TextureVector3D(Vector2D(0.50, 0.50), getColor(), r, r, -r));
  81. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.50, 0.25), getColor(), r, r, -r)
  82. << TextureVector3D(Vector2D(0.50, 0.50), getColor(), r, -r, -r)
  83. << TextureVector3D(Vector2D(0.75, 0.50), getColor(), -r, -r, -r)
  84. << TextureVector3D(Vector2D(0.75, 0.25), getColor(), -r, r, -r));
  85. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.75, 0.25), getColor(), -r, r, -r)
  86. << TextureVector3D(Vector2D(0.75, 0.50), getColor(), -r, -r, -r)
  87. << TextureVector3D(Vector2D(1.00, 0.50), getColor(), -r, -r, r)
  88. << TextureVector3D(Vector2D(1.00, 0.25), getColor(), -r, r, r));
  89. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.25, 0.00), getColor(), -r, r, r)
  90. << TextureVector3D(Vector2D(0.25, 0.25), getColor(), r, r, r)
  91. << TextureVector3D(Vector2D(0.50, 0.25), getColor(), r, r, -r)
  92. << TextureVector3D(Vector2D(0.50, 0.00), getColor(), -r, r, -r));
  93. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.50, 0.75), getColor(), -r, -r, -r)
  94. << TextureVector3D(Vector2D(0.50, 0.50), getColor(), r, -r, -r)
  95. << TextureVector3D(Vector2D(0.25, 0.5), getColor(), r, -r, r)
  96. << TextureVector3D(Vector2D(0.25, 0.75), getColor(), -r, -r, r));
  97. for (int i = 0; i < _facesTexture.size(); ++i) {
  98. QList<TextureVector3D>& face = _facesTexture[i];
  99. for (int j = 0; j < face.size(); ++j) {
  100. face[j] = getRealPoint(face[j]);
  101. }
  102. }
  103. }
  104. }
  105. Vector3D UGEEntityCube::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &origin, bool *ok)
  106. {
  107. *ok = false;
  108. // Vector3D p1 = points[1] - p;
  109. // Vector3D p2 = points[1];
  110. // Vector3D n = p1.crossProduct(p2);
  111. Vector3D bestP;
  112. for (int i = 0; i < _facesTexture.size(); ++i){
  113. bool provi = false;
  114. QList<TextureVector3D> face = _facesTexture[i];
  115. Vector3D pinter = getVectorNearestFaceIntersection(face[0], face[1], face[2], vector, origin, &provi);
  116. if (provi) {
  117. bool isNearest = (origin - pinter).norm() < (origin - bestP).norm();
  118. bool isInFace = (_position - face[0]).norm() >= (_position - pinter).norm();
  119. if (isInFace && (!(*ok) || isNearest)) {
  120. bestP = pinter;
  121. *ok = true;
  122. }
  123. //qDebug() << bestP.getX() << bestP.getY() << bestP.getZ();
  124. }
  125. }
  126. return bestP;
  127. // Vector3D inv(1.0 / vector.getX(), 1.0 / vector.getY(), 1.0 / vector.getZ());
  128. // float tmin, tmax, tymin, tymax, tzmin, tzmax;
  129. // tmin = (parameters[r.sign[0]].x() - origin.getX()) * inv.getX();
  130. // tmax = (parameters[1-r.sign[0]].x() - origin.getX()) * inv.getX();
  131. // tymin = (parameters[r.sign[1]].y() - origin.getY()) * inv.getY();
  132. // tymax = (parameters[1-r.sign[1]].y() - origin.getY()) * inv.getY();
  133. // if ( (tmin > tymax) || (tymin > tmax) ) {
  134. // *ok = false;
  135. // return Vector3D();
  136. // }
  137. // if (tymin > tmin)
  138. // tmin = tymin;
  139. // if (tymax < tmax)
  140. // tmax = tymax;
  141. // tzmin = (parameters[r.sign[2]].z() - r.origin.z()) * r.inv_direction.z();
  142. // tzmax = (parameters[1-r.sign[2]].z() - r.origin.z()) * r.inv_direction.z();
  143. // if ( (tmin > tzmax) || (tzmin > tmax) ) {
  144. // *ok = false;
  145. // return Vector3D();
  146. // }
  147. // *ok = true;
  148. // return _position;
  149. // if (tzmin > tmin)
  150. // tmin = tzmin;
  151. // if (tzmax < tmax)
  152. // tmax = tzmax;
  153. // if ((tmin < t1) && (tmax > t0)) {
  154. // *ok = true;
  155. // return _position;
  156. // }
  157. // *ok = false;
  158. // return Vector3D();
  159. }