CG_Labs 2021.2
Loading...
Searching...
No Matches
FPSCamera.inl
Go to the documentation of this file.
1template<typename T, glm::precision P>
2FPSCamera<T, P>::FPSCamera(T fovy, T aspect, T nnear, T nfar) : mWorld(), mMovementSpeed(1), mMouseSensitivity(1), mFov(fovy), mAspect(aspect), mNear(nnear), mFar(nfar), mProjection(), mProjectionInverse(), mMousePosition(glm::tvec2<T, P>(0.0f))
3{
4 SetProjection(fovy, aspect, nnear, nfar);
5}
6
7template<typename T, glm::precision P>
11
12template<typename T, glm::precision P>
13void FPSCamera<T, P>::SetProjection(T fovy, T aspect, T nnear, T nfar)
14{
15 mFov = fovy;
16 mAspect = aspect;
17 mNear = nnear;
18 mFar = nfar;
19 mProjection = glm::perspective(fovy, aspect, nnear, nfar);
20 mProjectionInverse = glm::inverse(mProjection);
23template<typename T, glm::precision P>
26 SetProjection(fovy, mAspect, mNear, mFar);
27}
29template<typename T, glm::precision P>
32 return mFov;
34
35template<typename T, glm::precision P>
37{
38 SetProjection(mFov, a, mNear, mFar);
39}
40
41template<typename T, glm::precision P>
43{
44 return mAspect;
45}
46
47
48template<typename T, glm::precision P>
49void FPSCamera<T, P>::Update(std::chrono::microseconds deltaTime, InputHandler &ih, bool ignoreKeyEvents, bool ignoreMouseEvents)
50{
51 glm::tvec2<T, P> newMousePosition = glm::tvec2<T, P>(ih.GetMousePosition().x, ih.GetMousePosition().y);
52 glm::tvec2<T, P> mouse_diff = newMousePosition - mMousePosition;
53 mMousePosition = newMousePosition;
54
55 if (!ih.IsMouseCapturedByUI() && !ignoreMouseEvents && (ih.GetMouseState(GLFW_MOUSE_BUTTON_LEFT) & PRESSED)) {
56 mouse_diff.y = -mouse_diff.y;
57 mouse_diff *= mMouseSensitivity;
58
59 mWorld.PreRotateX(mouse_diff.y);
60 mWorld.RotateY(-mouse_diff.x);
61 }
62
63 if (!ih.IsKeyboardCapturedByUI() && !ignoreKeyEvents) {
64 T move = 0.0f, strafe = 0.0f, levitate = 0.0f;
65 if ((ih.GetKeycodeState(GLFW_KEY_W) & PRESSED)) move += 1.0f;
66 if ((ih.GetKeycodeState(GLFW_KEY_S) & PRESSED)) move -= 1.0f;
67 if ((ih.GetKeycodeState(GLFW_KEY_A) & PRESSED)) strafe -= 1.0f;
68 if ((ih.GetKeycodeState(GLFW_KEY_D) & PRESSED)) strafe += 1.0f;
69 if ((ih.GetKeycodeState(GLFW_KEY_Q) & PRESSED)) levitate -= 1.0f;
70 if ((ih.GetKeycodeState(GLFW_KEY_E) & PRESSED)) levitate += 1.0f;
71
72 T const movementModifier = ((ih.GetKeycodeState(GLFW_KEY_LEFT_CONTROL) & PRESSED)) ? 0.25f : ((ih.GetKeycodeState(GLFW_KEY_LEFT_SHIFT) & PRESSED)) ? 4.0f : 1.0f;
73 auto const deltaTime_s = std::chrono::duration<T>(deltaTime);
74 auto const movementChange = movementModifier * (mWorld.GetFront() * move + mWorld.GetRight() * strafe + mWorld.GetUp() * levitate);
75 auto const movementSpeed = mMovementSpeed * movementChange;
76
77 mWorld.Translate(movementSpeed * deltaTime_s.count());
78 }
79}
80
81template<typename T, glm::precision P>
83{
84 return mWorld.GetMatrix();
85}
86
87template<typename T, glm::precision P>
89{
90 return mWorld.GetMatrixInverse();
91}
92
93template<typename T, glm::precision P>
95{
96 return GetViewToWorldMatrix() * mProjectionInverse;
97}
98
99template<typename T, glm::precision P>
101{
102 return mProjection * GetWorldToViewMatrix();
103}
104
105template<typename T, glm::precision P>
107{
108 return mProjectionInverse;
109}
110
111template<typename T, glm::precision P>
113{
114 return mProjection;
115}
116
117template<typename T, glm::precision P>
118glm::tvec3<T, P> FPSCamera<T, P>::GetClipToWorld(glm::tvec3<T, P> xyw)
119{
120 glm::tvec4<T, P> vv = glm::tvec4<T, P>(GetClipToView(xyw), static_cast<T>(1));
121 glm::tvec3<T, P> wv = mWorld.GetMatrix() * vv;
122 return wv;
123}
124
125template<typename T, glm::precision P>
126glm::tvec3<T, P> FPSCamera<T, P>::GetClipToView(glm::tvec3<T, P> xyw)
127{
128 return xyw * glm::tvec3<T, P>(mProjectionInverse[0][0], mProjectionInverse[1][1], static_cast<T>(-1));
129}
#define PRESSED
Definition InputHandler.h:13
void Update(std::chrono::microseconds deltaTime, InputHandler &ih, bool ignoreKeyEvents=false, bool ignoreMouseEvents=false)
Definition FPSCamera.inl:49
glm::tmat4x4< T, P > GetClipToViewMatrix()
Definition FPSCamera.inl:106
glm::tmat4x4< T, P > GetWorldToViewMatrix()
Definition FPSCamera.inl:88
T GetAspect()
Definition FPSCamera.inl:42
~FPSCamera()
Definition FPSCamera.inl:8
glm::tmat4x4< T, P > GetViewToClipMatrix()
Definition FPSCamera.inl:112
glm::tmat4x4< T, P > GetViewToWorldMatrix()
Definition FPSCamera.inl:82
void SetProjection(T fovy, T aspect, T nnear, T nfar)
Definition FPSCamera.inl:13
T GetFov()
Definition FPSCamera.inl:30
glm::tmat4x4< T, P > GetWorldToClipMatrix()
Definition FPSCamera.inl:100
glm::tvec3< T, P > GetClipToWorld(glm::tvec3< T, P > xyw)
Definition FPSCamera.inl:118
void SetAspect(T a)
Definition FPSCamera.inl:36
glm::tmat4x4< T, P > GetClipToWorldMatrix()
Definition FPSCamera.inl:94
FPSCamera(T fovy, T aspect, T nnear, T nfar)
Definition FPSCamera.inl:2
void SetFov(T fovy)
Definition FPSCamera.inl:24
glm::tvec3< T, P > GetClipToView(glm::tvec3< T, P > xyw)
Definition FPSCamera.inl:126
Definition InputHandler.h:19
std::uint32_t GetMouseState(std::uint32_t button)
Definition InputHandler.cpp:100
std::uint32_t GetKeycodeState(int key)
Definition InputHandler.cpp:95
bool IsMouseCapturedByUI() const
Definition InputHandler.cpp:115
glm::vec2 GetMousePosition()
Definition InputHandler.cpp:110
bool IsKeyboardCapturedByUI() const
Definition InputHandler.cpp:120