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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. UGEEntityCube* cube = new UGEEntityCube(_engine);
  17. cube->move(Vector3D(0, 1, 0));
  18. _engine->addEntity(cube);
  19. setMouseTracking(false);
  20. setFocusPolicy(Qt::StrongFocus);
  21. }
  22. void RenderWidget::initializeGL()
  23. {
  24. makeCurrent();
  25. _device->setClearColor(Qt::gray);
  26. _device->initialize(70, width(), height());
  27. }
  28. void RenderWidget::paintGL()
  29. {
  30. float theta = fabs(_theta / 180.0 * M_PI);
  31. float phi = _phi / 180.0 * M_PI;
  32. Vector3D center = Vector3D(
  33. _radius * sin(theta) * sin(phi),
  34. _radius * cos(theta),
  35. _radius * sin(theta) * cos(phi)
  36. );
  37. _device->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
  38. _engine->draw();
  39. drawAxes();
  40. }
  41. void RenderWidget::resizeGL(int width, int height)
  42. {
  43. _device->resize(width, height);
  44. }
  45. float RenderWidget::normalizeAngle(float angle)
  46. {
  47. while (angle >= 360.0) {
  48. angle -= 360.0;
  49. }
  50. while (angle < 0.0) {
  51. angle += 360.0;
  52. }
  53. return angle;
  54. }
  55. void RenderWidget::mousePressEvent(QMouseEvent *event)
  56. {
  57. _lastPoint = event->pos();
  58. }
  59. void RenderWidget::mouseMoveEvent(QMouseEvent *event)
  60. {
  61. QPoint diff = event->pos() - _lastPoint;
  62. rotate(-diff.x(), -diff.y());
  63. _lastPoint = event->pos();
  64. }
  65. void RenderWidget::wheelEvent(QWheelEvent *event)
  66. {
  67. _radius = qMax(2.0, _radius - event->delta() / 20.0);
  68. update();
  69. }
  70. void RenderWidget::keyPressEvent(QKeyEvent *event)
  71. {
  72. if (event->key() == Qt::Key_Up) {
  73. rotate(0.0, 1.0);
  74. }
  75. else if (event->key() == Qt::Key_Down) {
  76. rotate(0.0, -1.0);
  77. }
  78. else if (event->key() == Qt::Key_Left) {
  79. rotate(1.0, 0.0);
  80. }
  81. else if (event->key() == Qt::Key_Right) {
  82. rotate(-1.0, 0.0);
  83. }
  84. }
  85. void RenderWidget::rotate(float phi, float theta)
  86. {
  87. _phi = normalizeAngle(_phi + phi);
  88. if (_theta + theta >= 180.0) {
  89. theta = 179.0;
  90. }
  91. else if (_theta + theta < 1.0) {
  92. theta = 1.0;
  93. }
  94. else {
  95. _theta = normalizeAngle(_theta + theta);
  96. }
  97. update();
  98. }
  99. void RenderWidget::drawAxes()
  100. {
  101. _device->drawLine(ColorVector3D(Qt::red, 0.0, 0.0, 0.0), ColorVector3D(Qt::red, 1.0, 0.0, 0.0), 2.5);
  102. _device->drawLine(ColorVector3D(Qt::green, 0.0, 0.0, 0.0), ColorVector3D(Qt::green, 0.0, 1.0, 0.0), 2.5);
  103. _device->drawLine(ColorVector3D(Qt::blue, 0.0, 0.0, 0.0), ColorVector3D(Qt::blue, 0.0, 0.0, 1.0), 2.5);
  104. _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));
  105. }