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

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