Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

renderwidget.cpp 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "renderwidget.h"
  2. #include <GL/glu.h>
  3. #include <QTimer>
  4. #include <math.h>
  5. RenderWidget::RenderWidget(QWidget *parent) :
  6. QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  7. , angle(0)
  8. , _fov(70)
  9. {
  10. }
  11. void RenderWidget::initializeGL()
  12. {
  13. makeCurrent();
  14. qglClearColor(Qt::gray);
  15. glEnable(GL_DEPTH_TEST);
  16. glEnable(GL_CULL_FACE);
  17. glShadeModel(GL_SMOOTH);
  18. glEnable(GL_MULTISAMPLE);
  19. glMatrixMode(GL_PROJECTION);
  20. gluPerspective(_fov, width()/height(), 0.1, 100.0);
  21. glMatrixMode(GL_MODELVIEW);
  22. animate();
  23. }
  24. void RenderWidget::paintGL()
  25. {
  26. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  27. glLoadIdentity();
  28. float rad = (float)angle / 180.0 * M_PI;
  29. float radius = 5.0f;
  30. gluLookAt(radius * cos(rad), 5.0f, radius * sin(rad),
  31. 0.0f, 0.0f, 0.0f,
  32. 0.0f, 1.0f, 0.0f);
  33. drawAxes();
  34. }
  35. void RenderWidget::resizeGL(int width, int height)
  36. {
  37. int side = qMin(width, height);
  38. glViewport((width - side) / 2, (height - side) / 2, side, side);
  39. }
  40. void RenderWidget::animate()
  41. {
  42. angle = (angle + 3) % 360;
  43. update();
  44. QTimer::singleShot(50, this, SLOT(animate()));
  45. }
  46. void RenderWidget::drawAxes()
  47. {
  48. glLineWidth(2.5);
  49. glColor3f(1.0, 0.0, 0.0);
  50. glBegin(GL_LINES);
  51. glVertex3f(0.0, 0.0, 0.0);
  52. glVertex3f(1.0, 0.0, 0.0);
  53. glEnd();
  54. glColor3f(0.0, 1.0, 0.0);
  55. glBegin(GL_LINES);
  56. glVertex3f(0.0, 0.0, 0.0);
  57. glVertex3f(0.0, 1.0, 0.0);
  58. glEnd();
  59. glColor3f(0.0, 0.0, 1.0);
  60. glBegin(GL_LINES);
  61. glVertex3f(0.0, 0.0, 0.0);
  62. glVertex3f(0.0, 0.0, 1.0);
  63. glEnd();
  64. }