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.

renderwidget.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "renderwidget.h"
  2. #include <QTimer>
  3. #include <math.h>
  4. #include <QDebug>
  5. #include <QDir>
  6. #include "entities/ugeentitycube.h"
  7. #include "entities/ugeentityaxes.h"
  8. #include "utils/wavefrontobj.h"
  9. #include "entities/ugeentitywavefrontobj.h"
  10. #include "cameras/rotationcamera.h"
  11. #include "cameras/freeflycamera.h"
  12. RenderWidget::RenderWidget(QWidget *parent) :
  13. QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  14. {
  15. setMouseTracking(true);
  16. setFocusPolicy(Qt::StrongFocus);
  17. setAutoFillBackground(false);
  18. setAutoBufferSwap(false);
  19. #ifdef __APPLE__
  20. _assetsPath = "../../../assets/";
  21. #else
  22. _assetsPath = "./assets/";
  23. #endif
  24. _engine = new UGameEngine(new OpenGLRenderDevice(this));
  25. _camera = new FreeFlyCamera(_engine, this);
  26. _camera->setPosition(Vector3D(0, 1.5, 0));
  27. //_engine->addEntity(new UGEEntityAxes(_engine));
  28. // for (int i = 0; i < 1; ++i) {
  29. // UGEEntityCube* cube = new GameCube(_engine);
  30. // cube->setTextureId("rubiks");
  31. //// cube->rotate(Vector3D(0.0, 45.0, 45.0));
  32. // cube->move(Vector3D(0, i, i));
  33. //// cube->setScale(Vector3D(1.0, 2.0, 1.0));
  34. // _entities.append(cube);
  35. // }
  36. int xmax = 25;
  37. int zmax = 25;
  38. for (int x = 0; x < xmax; ++x) {
  39. for (int z = 0; z < zmax; ++z) {
  40. UGEEntityCube* cube = new UGEEntityCube(_engine);
  41. cube->setTextureId("rubiks");
  42. cube->move(Vector3D(x - (xmax / 2), 0, z - (zmax / 2)));
  43. }
  44. }
  45. WaveFrontObj* wavefrontObj = new WaveFrontObj(this);
  46. wavefrontObj->openFile(_assetsPath + "objs/enterprise/USSEnterprise.obj");
  47. UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, _engine);
  48. obj->move(Vector3D(0, 15, 0));
  49. UGEEntityCube* cube = new UGEEntityCube(_engine);
  50. cube->move(Vector3D(5, 17, 5));
  51. cube->setTextureId("dice");
  52. _entities.append(cube);
  53. cube = new UGEEntityCube(_engine);
  54. cube->move(Vector3D(-5, 17, 5));
  55. cube->setTextureId("dice");
  56. _entities.append(cube);
  57. cube = new UGEEntityCube(_engine);
  58. cube->move(Vector3D(-5, 17, -5));
  59. cube->setTextureId("dice");
  60. _entities.append(cube);
  61. cube = new UGEEntityCube(_engine);
  62. cube->move(Vector3D(5, 17, -5));
  63. cube->setTextureId("dice");
  64. _entities.append(cube);
  65. animate();
  66. }
  67. void RenderWidget::initializeGL()
  68. {
  69. srand(time(0));
  70. makeCurrent();
  71. _engine->setClearColor(Qt::gray);
  72. _engine->initialize(70, width(), height());
  73. _textures.clear();
  74. QDir texturesDir(_assetsPath + "textures");
  75. QFileInfoList files = texturesDir.entryInfoList(QStringList() << "*.png" << "*.jpg" << "*.jpeg");
  76. for (int i = 0; i < files.size(); ++i)
  77. {
  78. QFileInfo file = files[i];
  79. _engine->loadTextureFromFile(file.baseName(), file.absoluteFilePath());
  80. _textures.append(file.baseName());
  81. }
  82. }
  83. void RenderWidget::paintGL()
  84. {
  85. _camera->updateLookAt();
  86. _engine->draw();
  87. }
  88. void RenderWidget::resizeGL(int width, int height)
  89. {
  90. _engine->resize(width, height);
  91. }
  92. void RenderWidget::mousePressEvent(QMouseEvent *event)
  93. {
  94. _camera->mousePressEvent(event);
  95. bool left = event->buttons() & Qt::LeftButton;
  96. bool right = event->buttons() & Qt::RightButton;
  97. if (left || right) {
  98. Vector3D bestp;
  99. UGEEntity* entity = _engine->getVectorNearestIntesection(_camera->getDirection(), _camera->getPosition(), &bestp);
  100. if (entity) {
  101. for (int i = 0; i < _entities.size(); ++i) {
  102. if (_entities[i] == entity) {
  103. return;
  104. }
  105. }
  106. // UGEEntity* axe = new UGEEntityAxes(_engine);
  107. // axe->move(bestp);
  108. if (right) {
  109. entity->deleteLater();
  110. }
  111. else if (left) {
  112. Vector3D pos = entity->getPosition();
  113. Vector3D diff = pos - bestp;
  114. if (fmod(fabs(diff.getX()), 0.5) == 0) {
  115. pos.add(Vector3D(1 * (diff.getX() < 0 ? 1 : -1), 0, 0));
  116. }
  117. else if (fmod(fabs(diff.getY()), 0.5) == 0) {
  118. pos.add(Vector3D(0, 1 * (diff.getY() < 0 ? 1 : -1), 0));
  119. }
  120. else if (fmod(fabs(diff.getZ()), 0.5) == 0) {
  121. pos.add(Vector3D(0, 0, 1 * (diff.getZ() < 0 ? 1 : -1)));
  122. }
  123. QList<UGEEntity*> entities = _engine->getEntities();
  124. for (int i = 0; i < entities.size(); ++i) {
  125. UGEEntity* e = entities[i];
  126. if (e->getPosition() == pos) {
  127. return;
  128. }
  129. }
  130. int x = (rand() >= RAND_MAX / 2 ? 90 : 0) * (rand() >= RAND_MAX / 2 ? -1 : 1);
  131. int y = (rand() >= RAND_MAX / 2 ? 90 : 0) * (rand() >= RAND_MAX / 2 ? -1 : 1);
  132. int z = (rand() >= RAND_MAX / 2 ? 90 : 0) * (rand() >= RAND_MAX / 2 ? -1 : 1);
  133. QString texture = _textures[rand() / (double)RAND_MAX * _textures.size()];
  134. UGEEntityCube* cube = new UGEEntityCube(_engine);
  135. cube->move(pos);
  136. cube->rotate(Vector3D(x, y, z));
  137. cube->setTextureId(texture);
  138. }
  139. }
  140. }
  141. }
  142. void RenderWidget::mouseReleaseEvent(QMouseEvent *event)
  143. {
  144. _camera->mouseReleaseEvent(event);
  145. }
  146. void RenderWidget::mouseMoveEvent(QMouseEvent *event)
  147. {
  148. _camera->mouseMoveEvent(event);
  149. }
  150. void RenderWidget::mouseDoubleClickEvent(QMouseEvent *event)
  151. {
  152. _camera->mouseDoubleClickEvent(event);
  153. }
  154. void RenderWidget::wheelEvent(QWheelEvent *event)
  155. {
  156. _camera->wheelEvent(event);
  157. }
  158. void RenderWidget::keyPressEvent(QKeyEvent *event)
  159. {
  160. _camera->keyPressEvent(event);
  161. }
  162. void RenderWidget::keyReleaseEvent(QKeyEvent *event)
  163. {
  164. _camera->keyReleaseEvent(event);
  165. }
  166. void RenderWidget::paintEvent(QPaintEvent *event)
  167. {
  168. QGLWidget::paintEvent(event);
  169. int r = 20;
  170. QPainter p;
  171. p.begin(this);
  172. QPoint c(width() / 2, height() / 2);
  173. p.drawLine(c.x() - r, c.y(), c.x() + r, c.y());
  174. p.drawLine(c.x(), c.y() - r, c.x(), c.y() + r);
  175. p.end();
  176. swapBuffers();
  177. }
  178. void RenderWidget::animate()
  179. {
  180. for (int i = 0; i < _entities.size(); ++i) {
  181. _entities[i]->rotate(Vector3D(0.0, 2.0, 2.0));
  182. }
  183. QTimer::singleShot(20, this, SLOT(animate()));
  184. update();
  185. }