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.0KB

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