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

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