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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. {
  12. _device = new OpenGLRenderDevice(this);
  13. _engine = new UGameEngine(_device);
  14. setMouseTracking(false);
  15. }
  16. void RenderWidget::initializeGL()
  17. {
  18. makeCurrent();
  19. _device->setClearColor(Qt::gray);
  20. _device->initialize(70, width(), height());
  21. }
  22. void RenderWidget::paintGL()
  23. {
  24. float theta = _theta / 180.0 * M_PI;
  25. float phi = _phi / 180.0 * M_PI;
  26. _device->lookAt(Vector3D(
  27. _radius * sin(theta) * sin(phi),
  28. _radius * cos(theta),
  29. _radius * sin(theta) * cos(phi)
  30. ),
  31. Vector3D(0.0f, 0.0f, 0.0f));
  32. _engine->draw();
  33. drawAxes();
  34. }
  35. void RenderWidget::resizeGL(int width, int height)
  36. {
  37. _device->resize(width, height);
  38. }
  39. void RenderWidget::mousePressEvent(QMouseEvent *event)
  40. {
  41. _lastPoint = event->pos();
  42. }
  43. void RenderWidget::mouseMoveEvent(QMouseEvent *event)
  44. {
  45. QPoint diff = event->pos() - _lastPoint;
  46. _phi -= diff.x();
  47. _theta -= diff.y();
  48. qDebug() << _phi << _theta;
  49. _lastPoint = event->pos();
  50. update();
  51. }
  52. void RenderWidget::wheelEvent(QWheelEvent *event)
  53. {
  54. _radius = qMax(2.0, _radius - event->delta() / 20.0);
  55. update();
  56. }
  57. void RenderWidget::drawAxes()
  58. {
  59. _device->drawLine(ColorVector3D(Qt::red, 0.0, 0.0, 0.0), ColorVector3D(Qt::red, 1.0, 0.0, 0.0), 2.5);
  60. _device->drawLine(ColorVector3D(Qt::green, 0.0, 0.0, 0.0), ColorVector3D(Qt::green, 0.0, 1.0, 0.0), 2.5);
  61. _device->drawLine(ColorVector3D(Qt::blue, 0.0, 0.0, 0.0), ColorVector3D(Qt::blue, 0.0, 0.0, 1.0), 2.5);
  62. _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));
  63. }