Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "renderwidget.h"
  2. #include <QTimer>
  3. #include <math.h>
  4. #include <QDebug>
  5. #include "entities/ugeentitycube.h"
  6. RenderWidget::RenderWidget(QWidget *parent) :
  7. QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  8. , angle(0)
  9. , _radius(5.0)
  10. , _phi(45)
  11. , _theta(45)
  12. , _reverse(false)
  13. {
  14. _device = new OpenGLRenderDevice(this);
  15. _engine = new UGameEngine(_device);
  16. _engine->addEntity(new UGEEntityCube(_engine));
  17. setMouseTracking(false);
  18. setFocusPolicy(Qt::StrongFocus);
  19. }
  20. void RenderWidget::initializeGL()
  21. {
  22. makeCurrent();
  23. _device->setClearColor(Qt::gray);
  24. _device->initialize(70, width(), height());
  25. }
  26. void RenderWidget::paintGL()
  27. {
  28. float theta = fabs(_theta / 180.0 * M_PI);
  29. float phi = _phi / 180.0 * M_PI;
  30. Vector3D center = Vector3D(
  31. _radius * sin(theta) * sin(phi),
  32. _radius * cos(theta),
  33. _radius * sin(theta) * cos(phi)
  34. );
  35. _device->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
  36. _engine->draw();
  37. drawAxes();
  38. }
  39. void RenderWidget::resizeGL(int width, int height)
  40. {
  41. _device->resize(width, height);
  42. }
  43. float RenderWidget::normalizeAngle(float angle)
  44. {
  45. while (angle >= 360.0) {
  46. angle -= 360.0;
  47. }
  48. while (angle < 0.0) {
  49. angle += 360.0;
  50. }
  51. return angle;
  52. }
  53. void RenderWidget::mousePressEvent(QMouseEvent *event)
  54. {
  55. _lastPoint = event->pos();
  56. }
  57. void RenderWidget::mouseMoveEvent(QMouseEvent *event)
  58. {
  59. QPoint diff = event->pos() - _lastPoint;
  60. rotate(-diff.x(), -diff.y());
  61. _lastPoint = event->pos();
  62. }
  63. void RenderWidget::wheelEvent(QWheelEvent *event)
  64. {
  65. _radius = qMax(2.0, _radius - event->delta() / 20.0);
  66. update();
  67. }
  68. void RenderWidget::keyPressEvent(QKeyEvent *event)
  69. {
  70. if (event->key() == Qt::Key_Up) {
  71. rotate(0.0, 1.0);
  72. }
  73. else if (event->key() == Qt::Key_Down) {
  74. rotate(0.0, -1.0);
  75. }
  76. else if (event->key() == Qt::Key_Left) {
  77. rotate(1.0, 0.0);
  78. }
  79. else if (event->key() == Qt::Key_Right) {
  80. rotate(-1.0, 0.0);
  81. }
  82. }
  83. void RenderWidget::rotate(float phi, float theta)
  84. {
  85. _phi = normalizeAngle(_phi + phi);
  86. if (_theta + theta >= 180.0) {
  87. theta = 179.0;
  88. }
  89. else if (_theta + theta < 1.0) {
  90. theta = 1.0;
  91. }
  92. else {
  93. _theta = normalizeAngle(_theta + theta);
  94. }
  95. update();
  96. }
  97. void RenderWidget::drawAxes()
  98. {
  99. _device->drawLine(ColorVector3D(Qt::red, 0.0, 0.0, 0.0), ColorVector3D(Qt::red, 1.0, 0.0, 0.0), 2.5);
  100. _device->drawLine(ColorVector3D(Qt::green, 0.0, 0.0, 0.0), ColorVector3D(Qt::green, 0.0, 1.0, 0.0), 2.5);
  101. _device->drawLine(ColorVector3D(Qt::blue, 0.0, 0.0, 0.0), ColorVector3D(Qt::blue, 0.0, 0.0, 1.0), 2.5);
  102. _device->drawPolygon(QList<ColorVector3D>() << ColorVector3D(Qt::red, 0, 0, 0) << ColorVector3D(Qt::green, 1, 0, 0) << ColorVector3D(Qt::transparent, 1, 1, 0) << ColorVector3D(Qt::blue, 0, 1, 0));
  103. }