-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTrackballCamera.h
155 lines (148 loc) · 10 KB
/
TrackballCamera.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef TRACKBALLCAMERA_H
#define TRACKBALLCAMERA_H
//-------------------------------------------------------------------------------------------------------
/// @author Jack Diver
/// Modified from :-
/// Richard Southern (02/02/2017). Trackball workshop.
/// @note Changes were made to the interface of camera, mouse rotate and zoom are pure virtual.
/// @note The case statements have been removed in favour of CameraStates which use double dispatch,
/// based on the camera and camera's state, to act accordingly.
/// @note The bug with zooming was fixed by updating the last mouse position.
/// @note Rotation locking bug was fixed using a small epsilon before clamping.
//-------------------------------------------------------------------------------------------------------
#include "Camera.h"
#include <memory>
#include "CameraStates.h"
/**
* @brief The TrackballCamera class
*/
class TrackballCamera : public Camera
{
//-----------------------------------------------------------------------------------------------------
/// @brief Enum used to indicate the current state of the camera.
//-----------------------------------------------------------------------------------------------------
enum CAM_STATE
{
TRACKBALL_ZOOMING = 0,
TRACKBALL_ROTATING = 1,
TRACKBALL_PASSIVE = 2
};
public:
//-----------------------------------------------------------------------------------------------------
/// @brief Default constructor.
//-----------------------------------------------------------------------------------------------------
TrackballCamera() = default;
//-----------------------------------------------------------------------------------------------------
/// @brief Default copy constructor.
//-----------------------------------------------------------------------------------------------------
TrackballCamera(const TrackballCamera&) = default;
//-----------------------------------------------------------------------------------------------------
/// @brief Default copy assignment operator.
//-----------------------------------------------------------------------------------------------------
TrackballCamera& operator=(const TrackballCamera&) = default;
//-----------------------------------------------------------------------------------------------------
/// @brief Default move constructor.
//-----------------------------------------------------------------------------------------------------
TrackballCamera(TrackballCamera&&) = default;
//-----------------------------------------------------------------------------------------------------
/// @brief Default move assignment operator.
//-----------------------------------------------------------------------------------------------------
TrackballCamera& operator=(TrackballCamera&&) = default;
//-----------------------------------------------------------------------------------------------------
/// @brief Default destructor.
//-----------------------------------------------------------------------------------------------------
~TrackballCamera() override = default;
//-----------------------------------------------------------------------------------------------------
/// @brief Update function should be called to update the camera matrices, before retrieving them.
//-----------------------------------------------------------------------------------------------------
virtual void update() override;
//-----------------------------------------------------------------------------------------------------
/// @brief Handles key press events by calling the current camera state function.
/// @param [in] _key is the Qt key that has been pressed/released.
/// @param [in] _isPress whether this key has been pressed or released.
//-----------------------------------------------------------------------------------------------------
virtual void handleKey(const int _key, const bool _isPress) override;
//-----------------------------------------------------------------------------------------------------
/// @brief Used to respond to mouse movement by calling the current camera state function.
/// @param [in] _mousePos is the new position of the mouse.
//-----------------------------------------------------------------------------------------------------
virtual void handleMouseMove(const glm::vec2 &_mousePos) override;
//-----------------------------------------------------------------------------------------------------
/// @brief Used to respond to the mouse being pressed by calling the current camera state function.
/// @param [in] _action is the action that the mouse performed (which button was clicked).
//-----------------------------------------------------------------------------------------------------
virtual void handleMouseClick(const QMouseEvent &_action) override;
//-----------------------------------------------------------------------------------------------------
/// @brief Used to set the current zoom of the camera.
/// @param [in] _zoom is the new zoom value.
//-----------------------------------------------------------------------------------------------------
void setZoom(const float _zoom) noexcept;
//-----------------------------------------------------------------------------------------------------
/// @brief Used to set the current sensitivity of the camera.
/// @param [in] _sensitivity is the new sensitivity value.
//-----------------------------------------------------------------------------------------------------
void setSensitivity(const float _sensitivity) noexcept;
//-----------------------------------------------------------------------------------------------------
/// @brief Used to rotate the camera based on mouse movement.
/// @param [in] _mousePos is the current mouse position.
//-----------------------------------------------------------------------------------------------------
virtual void mouseRotate(const glm::vec2 &_mousePos) override;
//-----------------------------------------------------------------------------------------------------
/// @brief Used to zoom the camera based on mouse movement.
/// @param [in] _mousePos is the current mouse position.
//-----------------------------------------------------------------------------------------------------
virtual void mouseZoom(const glm::vec2 &_mousePos) override;
//-----------------------------------------------------------------------------------------------------
/// @brief Used to get the current cameras position.
/// @return The eye position.
//-----------------------------------------------------------------------------------------------------
virtual glm::vec3 getCameraEye() const noexcept override;
//-----------------------------------------------------------------------------------------------------
/// @brief Resets the camera to it's default position and orientation.
//-----------------------------------------------------------------------------------------------------
virtual void resetPosition() override;
private:
//-----------------------------------------------------------------------------------------------------
/// @brief Used to set the last yaw and pitch to the current values.
//-----------------------------------------------------------------------------------------------------
void updateYawPitch();
private:
//-----------------------------------------------------------------------------------------------------
/// @brief Alias for camera state pointers.
//-----------------------------------------------------------------------------------------------------
using statePtr = std::unique_ptr<CameraState>;
//-----------------------------------------------------------------------------------------------------
/// @brief An array of states for this camera, which can be indexed by the current state. The state
/// objects them-selves contain no data, so this variable can be static and const.
//-----------------------------------------------------------------------------------------------------
static const statePtr m_states[];
//-----------------------------------------------------------------------------------------------------
/// @brief The current state of the camera.
//-----------------------------------------------------------------------------------------------------
CAM_STATE m_currentState = TRACKBALL_PASSIVE;
//-----------------------------------------------------------------------------------------------------
/// @brief The current sensitivity of the camera.
//-----------------------------------------------------------------------------------------------------
float m_sensitivity = 0.01f;
//-----------------------------------------------------------------------------------------------------
/// @brief The current zoom of the camera.
//-----------------------------------------------------------------------------------------------------
float m_zoom = 7.5f;
//-----------------------------------------------------------------------------------------------------
/// @brief The current yaw of the camera.
//-----------------------------------------------------------------------------------------------------
float m_yaw = 0.0f;
//-----------------------------------------------------------------------------------------------------
/// @brief The current pitch of the camera.
//-----------------------------------------------------------------------------------------------------
float m_pitch = 0.0f;
//-----------------------------------------------------------------------------------------------------
/// @brief The last yaw of the camera.
//-----------------------------------------------------------------------------------------------------
float m_lastYaw = 0.0f;
//-----------------------------------------------------------------------------------------------------
/// @brief The last pitch of the camera.
//-----------------------------------------------------------------------------------------------------
float m_lastPitch = 0.0f;
};
#endif // TRACKBALLCAMERA_H