Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

renderwidget.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "renderwidget.h"
  2. #include <QTimer>
  3. #include <math.h>
  4. #include <QDebug>
  5. #include "entities/ugeentitycube.h"
  6. #include "entities/ugeentityaxes.h"
  7. #include "utils/wavefrontobj.h"
  8. #include "entities/ugeentitywavefrontobj.h"
  9. #include "cameras/rotationcamera.h"
  10. #include "cameras/freeflycamera.h"
  11. RenderWidget::RenderWidget(QWidget *parent) :
  12. QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  13. {
  14. setMouseTracking(true);
  15. setFocusPolicy(Qt::StrongFocus);
  16. _engine = new UGameEngine(new OpenGLRenderDevice(this));
  17. _camera = new FreeFlyCamera(_engine, this);
  18. _engine->addEntity(new UGEEntityAxes(_engine));
  19. for (int i = 0; i < 100; ++i) {
  20. UGEEntityCube* cube = new UGEEntityCube(_engine);
  21. cube->setTextureId("test");
  22. cube->rotate(Vector3D(0.0, 45.0, 45.0));
  23. cube->move(Vector3D(0, i, i));
  24. cube->setScale(Vector3D(1.0, 2.0, 1.0));
  25. _engine->addEntity(cube);
  26. _entities.append(cube);
  27. }
  28. WaveFrontObj* wavefrontObj = new WaveFrontObj(this);
  29. wavefrontObj->openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
  30. UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, this);
  31. _engine->addEntity(obj);
  32. _entities.append(obj);
  33. animate();
  34. }
  35. void RenderWidget::initializeGL()
  36. {
  37. makeCurrent();
  38. _engine->setClearColor(Qt::gray);
  39. _engine->initialize(70, width(), height());
  40. _engine->loadTextureFromFile("test", "/home/robin/Downloads/test.png");
  41. }
  42. void RenderWidget::paintGL()
  43. {
  44. _camera->updateLookAt();
  45. _engine->draw();
  46. // _device->drawLine(ColorVector3D(Qt::black, 0, 0, 0), ColorVector3D(Qt::black, pos));
  47. // _device->drawPoint(ColorVector3D(Qt::magenta, 0.5, 0.5, 0.5));
  48. }
  49. void RenderWidget::resizeGL(int width, int height)
  50. {
  51. _engine->resize(width, height);
  52. }
  53. void RenderWidget::mousePressEvent(QMouseEvent *event)
  54. {
  55. _camera->mousePressEvent(event);
  56. }
  57. void RenderWidget::mouseReleaseEvent(QMouseEvent *event)
  58. {
  59. _camera->mouseReleaseEvent(event);
  60. }
  61. void RenderWidget::mouseMoveEvent(QMouseEvent *event)
  62. {
  63. _camera->mouseMoveEvent(event);
  64. // Vector3D dd = _device->get2DFrom3D(Vector3D(0.5, 0.5, 0.5));
  65. // dd.setY(height() - dd.getY());
  66. // qDebug() << event->pos() << dd;
  67. // pos = _device->get3DFrom2D(event->x(), height() - event->y());
  68. }
  69. void RenderWidget::mouseDoubleClickEvent(QMouseEvent *event)
  70. {
  71. _camera->mouseDoubleClickEvent(event);
  72. }
  73. void RenderWidget::wheelEvent(QWheelEvent *event)
  74. {
  75. _camera->wheelEvent(event);
  76. }
  77. void RenderWidget::keyPressEvent(QKeyEvent *event)
  78. {
  79. _camera->keyPressEvent(event);
  80. }
  81. void RenderWidget::keyReleaseEvent(QKeyEvent *event)
  82. {
  83. _camera->keyReleaseEvent(event);
  84. }
  85. void RenderWidget::animate()
  86. {
  87. // for (int i = 0; i < _entities.size(); ++i) {
  88. // _entities[i]->rotate(Vector3D(0.0, 2.0, 2.0));
  89. // }
  90. QTimer::singleShot(20, this, SLOT(animate()));
  91. update();
  92. }