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.

rotationcamera.cpp 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "rotationcamera.h"
  2. #include "utils/tools.h"
  3. RotationCamera::RotationCamera(UGameEngine* engine, QWidget* widget, double radius, double phi, double theta)
  4. : AbstractCamera(engine, widget)
  5. , _radius(radius)
  6. , _phi(phi)
  7. , _theta(theta)
  8. {
  9. }
  10. void RotationCamera::mousePressEvent(QMouseEvent *event)
  11. {
  12. _lastPoint = event->pos();
  13. }
  14. void RotationCamera::mouseMoveEvent(QMouseEvent *event)
  15. {
  16. if (event->buttons() & Qt::LeftButton) {
  17. QPoint diff = event->pos() - _lastPoint;
  18. rotate(-diff.x(), -diff.y());
  19. _lastPoint = event->pos();
  20. }
  21. }
  22. void RotationCamera::wheelEvent(QWheelEvent *event)
  23. {
  24. _radius = qMax(2.0, _radius - event->delta() / 30.0);
  25. }
  26. void RotationCamera::keyPressEvent(QKeyEvent *event)
  27. {
  28. if (event->key() == Qt::Key_Up) {
  29. rotate(0.0, 1.0);
  30. }
  31. else if (event->key() == Qt::Key_Down) {
  32. rotate(0.0, -1.0);
  33. }
  34. else if (event->key() == Qt::Key_Left) {
  35. rotate(1.0, 0.0);
  36. }
  37. else if (event->key() == Qt::Key_Right) {
  38. rotate(-1.0, 0.0);
  39. }
  40. }
  41. void RotationCamera::updateLookAt()
  42. {
  43. float theta = fabs(_theta / 180.0 * M_PI);
  44. float phi = _phi / 180.0 * M_PI;
  45. _position = Vector3D(
  46. _radius * sin(theta) * sin(phi),
  47. _radius * cos(theta),
  48. _radius * sin(theta) * cos(phi)
  49. );
  50. _direction = -_position;
  51. _engine->lookAt(_position, Vector3D(0.0f, 0.0f, 0.0f));
  52. }
  53. void RotationCamera::rotate(float phi, float theta)
  54. {
  55. _phi = Tools::normalizeAngle(_phi + phi);
  56. if (_theta + theta >= 180.0) {
  57. theta = 179.0;
  58. }
  59. else if (_theta + theta < 1.0) {
  60. theta = 1.0;
  61. }
  62. else {
  63. _theta = Tools::normalizeAngle(_theta + theta);
  64. }
  65. }