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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "renderwidget.h"
  2. #include <QTimer>
  3. #include <math.h>
  4. RenderWidget::RenderWidget(QWidget *parent) :
  5. QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  6. , angle(0)
  7. {
  8. _device = new OpenGLRenderDevice(this);
  9. _engine = new UGameEngine(_device);
  10. }
  11. void RenderWidget::initializeGL()
  12. {
  13. makeCurrent();
  14. _device->setClearColor(Qt::gray);
  15. _device->initialize(70, width(), height());
  16. animate();
  17. }
  18. void RenderWidget::paintGL()
  19. {
  20. float rad = (float)angle / 180.0 * M_PI;
  21. float radius = 5.0f;
  22. _device->lookAt(Vector3D(radius * cos(rad), 5.0f, radius * sin(rad)),
  23. Vector3D(0.0f, 0.0f, 0.0f));
  24. _engine->draw();
  25. drawAxes();
  26. }
  27. void RenderWidget::resizeGL(int width, int height)
  28. {
  29. _device->resize(width, height);
  30. }
  31. void RenderWidget::animate()
  32. {
  33. angle = (angle + 3) % 360;
  34. update();
  35. QTimer::singleShot(50, this, SLOT(animate()));
  36. }
  37. void RenderWidget::drawAxes()
  38. {
  39. glLineWidth(2.5);
  40. glColor3f(1.0, 0.0, 0.0);
  41. glBegin(GL_LINES);
  42. glVertex3f(0.0, 0.0, 0.0);
  43. glVertex3f(1.0, 0.0, 0.0);
  44. glEnd();
  45. glColor3f(0.0, 1.0, 0.0);
  46. glBegin(GL_LINES);
  47. glVertex3f(0.0, 0.0, 0.0);
  48. glVertex3f(0.0, 1.0, 0.0);
  49. glEnd();
  50. glColor3f(0.0, 0.0, 1.0);
  51. glBegin(GL_LINES);
  52. glVertex3f(0.0, 0.0, 0.0);
  53. glVertex3f(0.0, 0.0, 1.0);
  54. glEnd();
  55. }