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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. RenderWidget::RenderWidget(QWidget *parent) :
  10. QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  11. , _radius(5.0)
  12. , _phi(45)
  13. , _theta(45)
  14. {
  15. _device = new OpenGLRenderDevice(this);
  16. _engine = new UGameEngine(_device);
  17. UGEEntityCube* cube = new UGEEntityCube(_engine);
  18. // cube->move(Vector3D(0, 1, 0));
  19. cube->hide();
  20. _engine->addEntity(cube);
  21. _engine->addEntity(new UGEEntityAxes(_engine));
  22. setMouseTracking(true);
  23. setFocusPolicy(Qt::StrongFocus);
  24. WaveFrontObj* wavefrontObj = new WaveFrontObj(this);
  25. wavefrontObj->openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
  26. UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, this);
  27. _engine->addEntity(obj);
  28. // obj->hide();
  29. }
  30. void RenderWidget::initializeGL()
  31. {
  32. makeCurrent();
  33. _device->setClearColor(Qt::gray);
  34. _device->initialize(70, width(), height());
  35. }
  36. void RenderWidget::paintGL()
  37. {
  38. float theta = fabs(_theta / 180.0 * M_PI);
  39. float phi = _phi / 180.0 * M_PI;
  40. Vector3D center = Vector3D(
  41. _radius * sin(theta) * sin(phi),
  42. _radius * cos(theta),
  43. _radius * sin(theta) * cos(phi)
  44. );
  45. _device->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
  46. _engine->draw();
  47. _device->drawLine(ColorVector3D(Qt::black, 0, 0, 0), ColorVector3D(Qt::black, pos));
  48. _device->drawPoint(ColorVector3D(Qt::magenta, 0.5, 0.5, 0.5));
  49. }
  50. void RenderWidget::resizeGL(int width, int height)
  51. {
  52. _device->resize(width, height);
  53. }
  54. float RenderWidget::normalizeAngle(float angle)
  55. {
  56. while (angle >= 360.0) {
  57. angle -= 360.0;
  58. }
  59. while (angle < 0.0) {
  60. angle += 360.0;
  61. }
  62. return angle;
  63. }
  64. void RenderWidget::mousePressEvent(QMouseEvent *event)
  65. {
  66. _lastPoint = event->pos();
  67. }
  68. void RenderWidget::mouseMoveEvent(QMouseEvent *event)
  69. {
  70. if (event->buttons() & Qt::LeftButton) {
  71. QPoint diff = event->pos() - _lastPoint;
  72. rotate(-diff.x(), -diff.y());
  73. _lastPoint = event->pos();
  74. }
  75. Vector3D dd = _device->get2DFrom3D(Vector3D(0.5, 0.5, 0.5));
  76. dd.setY(height() - dd.getY());
  77. // qDebug() << event->pos() << dd;
  78. pos = _device->get3DFrom2D(event->x(), height() - event->y());
  79. update();
  80. }
  81. void RenderWidget::wheelEvent(QWheelEvent *event)
  82. {
  83. _radius = qMax(2.0, _radius - event->delta() / 30.0);
  84. update();
  85. }
  86. void RenderWidget::keyPressEvent(QKeyEvent *event)
  87. {
  88. if (event->key() == Qt::Key_Up) {
  89. rotate(0.0, 1.0);
  90. }
  91. else if (event->key() == Qt::Key_Down) {
  92. rotate(0.0, -1.0);
  93. }
  94. else if (event->key() == Qt::Key_Left) {
  95. rotate(1.0, 0.0);
  96. }
  97. else if (event->key() == Qt::Key_Right) {
  98. rotate(-1.0, 0.0);
  99. }
  100. }
  101. void RenderWidget::rotate(float phi, float theta)
  102. {
  103. _phi = normalizeAngle(_phi + phi);
  104. if (_theta + theta >= 180.0) {
  105. theta = 179.0;
  106. }
  107. else if (_theta + theta < 1.0) {
  108. theta = 1.0;
  109. }
  110. else {
  111. _theta = normalizeAngle(_theta + theta);
  112. }
  113. update();
  114. }