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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }