Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

renderwidget.cpp 3.6KB

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