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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "ugeentitycube.h"
  2. UGEEntityCube::UGEEntityCube(QObject *parent)
  3. : UGEEntity(parent)
  4. , _size(1.0)
  5. {
  6. updateFaces();
  7. connect(this, SIGNAL(colorChanged()), this, SLOT(updateFaces()));
  8. connect(this, SIGNAL(sizeChanged()), this, SLOT(updateFaces()));
  9. connect(this, SIGNAL(textureChanged()), this, SLOT(updateFaces()));
  10. }
  11. double UGEEntityCube::getSize() const
  12. {
  13. return _size;
  14. }
  15. void UGEEntityCube::draw(AbstractRenderDevice *device)
  16. {
  17. if (!_facesColor.empty()) {
  18. for (int i = 0; i < _facesColor.size(); ++i) {
  19. drawPolygon(device, _facesColor[i]);
  20. }
  21. }
  22. else {
  23. for (int i = 0; i < _facesTexture.size(); ++i) {
  24. drawPolygonTexture(device, _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::updateFaces()
  52. {
  53. _facesColor.clear();
  54. _facesTexture.clear();
  55. double r = _size / 2;
  56. if (_textureId.isNull()) {
  57. QList<ColorVector3D> points;
  58. points << 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. << ColorVector3D(getColor(), r, r, -r) << ColorVector3D(getColor(), r, -r, -r);
  62. _facesColor.append(QList<ColorVector3D>() << points[3] << points[2] << points[1] << points[0]);
  63. _facesColor.append(QList<ColorVector3D>() << points[2] << points[3] << points[7] << points[6]);
  64. _facesColor.append(QList<ColorVector3D>() << points[6] << points[7] << points[4] << points[5]);
  65. _facesColor.append(QList<ColorVector3D>() << points[5] << points[4] << points[0] << points[1]);
  66. _facesColor.append(QList<ColorVector3D>() << points[1] << points[2] << points[6] << points[5]);
  67. _facesColor.append(QList<ColorVector3D>() << points[4] << points[7] << points[3] << points[0]);
  68. }
  69. else {
  70. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.25, 0.50), getColor(), r, -r, r)
  71. << TextureVector3D(Vector2D(0.25, 0.25), getColor(), r, r, r)
  72. << TextureVector3D(Vector2D(0.00, 0.25), getColor(), -r, r, r)
  73. << TextureVector3D(Vector2D(0.00, 0.50), getColor(), -r, -r, r));
  74. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.25, 0.50), getColor(), r, r, r)
  75. << TextureVector3D(Vector2D(0.25, 0.25), getColor(), r, -r, r)
  76. << TextureVector3D(Vector2D(0.50, 0.25), getColor(), r, -r, -r)
  77. << TextureVector3D(Vector2D(0.50, 0.50), getColor(), r, r, -r));
  78. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.50, 0.25), getColor(), r, r, -r)
  79. << TextureVector3D(Vector2D(0.50, 0.50), getColor(), r, -r, -r)
  80. << TextureVector3D(Vector2D(0.75, 0.50), getColor(), -r, -r, -r)
  81. << TextureVector3D(Vector2D(0.75, 0.25), getColor(), -r, r, -r));
  82. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.75, 0.25), getColor(), -r, r, -r)
  83. << TextureVector3D(Vector2D(0.75, 0.50), getColor(), -r, -r, -r)
  84. << TextureVector3D(Vector2D(1.00, 0.50), getColor(), -r, -r, r)
  85. << TextureVector3D(Vector2D(1.00, 0.25), getColor(), -r, r, r));
  86. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.25, 0.00), 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.00), getColor(), -r, r, -r));
  90. _facesTexture.append(QList<TextureVector3D>() << TextureVector3D(Vector2D(0.50, 0.75), getColor(), -r, -r, -r)
  91. << TextureVector3D(Vector2D(0.50, 0.50), getColor(), r, -r, -r)
  92. << TextureVector3D(Vector2D(0.25, 0.5), getColor(), r, -r, r)
  93. << TextureVector3D(Vector2D(0.25, 0.75), getColor(), -r, -r, r));
  94. }
  95. }