Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

renderwidget.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "renderwidget.h"
  2. #include <QTimer>
  3. #include <math.h>
  4. #include <QDebug>
  5. #include "entities/ugeentitycube.h"
  6. #include "entities/ugeentityaxes.h"
  7. #include "utils/wavefrontobj.h"
  8. #include "entities/ugeentitywavefrontobj.h"
  9. RenderWidget::RenderWidget(QWidget *parent) :
  10. QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
  11. , _radius(5.0)
  12. , _phi(45)
  13. , _theta(45)
  14. {
  15. _device = new OpenGLRenderDevice(this);
  16. _engine = new UGameEngine(_device);
  17. UGEEntityCube* cube = new UGEEntityCube(_engine);
  18. cube->setTextureId("test");
  19. // cube->move(Vector3D(0, 1, 0));
  20. // cube->hide();
  21. _engine->addEntity(cube);
  22. _engine->addEntity(new UGEEntityAxes(_engine));
  23. setMouseTracking(true);
  24. setFocusPolicy(Qt::StrongFocus);
  25. WaveFrontObj* wavefrontObj = new WaveFrontObj(this);
  26. wavefrontObj->openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
  27. UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, this);
  28. _engine->addEntity(obj);
  29. obj->hide();
  30. }
  31. void RenderWidget::initializeGL()
  32. {
  33. makeCurrent();
  34. _device->setClearColor(Qt::gray);
  35. _device->initialize(70, width(), height());
  36. _device->loadTextureFromFile("test", "/home/robin/Downloads/test.png");
  37. }
  38. void RenderWidget::paintGL()
  39. {
  40. float theta = fabs(_theta / 180.0 * M_PI);
  41. float phi = _phi / 180.0 * M_PI;
  42. Vector3D center = Vector3D(
  43. _radius * sin(theta) * sin(phi),
  44. _radius * cos(theta),
  45. _radius * sin(theta) * cos(phi)
  46. );
  47. _device->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
  48. _engine->draw();
  49. // _device->drawLine(ColorVector3D(Qt::black, 0, 0, 0), ColorVector3D(Qt::black, pos));
  50. // _device->drawPoint(ColorVector3D(Qt::magenta, 0.5, 0.5, 0.5));
  51. }
  52. void RenderWidget::resizeGL(int width, int height)
  53. {
  54. _device->resize(width, height);
  55. }
  56. float RenderWidget::normalizeAngle(float angle)
  57. {
  58. while (angle >= 360.0) {
  59. angle -= 360.0;
  60. }
  61. while (angle < 0.0) {
  62. angle += 360.0;
  63. }
  64. return angle;
  65. }
  66. void RenderWidget::mousePressEvent(QMouseEvent *event)
  67. {
  68. _lastPoint = event->pos();
  69. }
  70. void RenderWidget::mouseMoveEvent(QMouseEvent *event)
  71. {
  72. if (event->buttons() & Qt::LeftButton) {
  73. QPoint diff = event->pos() - _lastPoint;
  74. rotate(-diff.x(), -diff.y());
  75. _lastPoint = event->pos();
  76. }
  77. Vector3D dd = _device->get2DFrom3D(Vector3D(0.5, 0.5, 0.5));
  78. dd.setY(height() - dd.getY());
  79. // qDebug() << event->pos() << dd;
  80. pos = _device->get3DFrom2D(event->x(), height() - event->y());
  81. update();
  82. }
  83. void RenderWidget::wheelEvent(QWheelEvent *event)
  84. {
  85. _radius = qMax(2.0, _radius - event->delta() / 30.0);
  86. update();
  87. }
  88. void RenderWidget::keyPressEvent(QKeyEvent *event)
  89. {
  90. if (event->key() == Qt::Key_Up) {
  91. rotate(0.0, 1.0);
  92. }
  93. else if (event->key() == Qt::Key_Down) {
  94. rotate(0.0, -1.0);
  95. }
  96. else if (event->key() == Qt::Key_Left) {
  97. rotate(1.0, 0.0);
  98. }
  99. else if (event->key() == Qt::Key_Right) {
  100. rotate(-1.0, 0.0);
  101. }
  102. }
  103. void RenderWidget::rotate(float phi, float theta)
  104. {
  105. _phi = normalizeAngle(_phi + phi);
  106. if (_theta + theta >= 180.0) {
  107. theta = 179.0;
  108. }
  109. else if (_theta + theta < 1.0) {
  110. theta = 1.0;
  111. }
  112. else {
  113. _theta = normalizeAngle(_theta + theta);
  114. }
  115. update();
  116. }