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

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