-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCamera.h
More file actions
186 lines (152 loc) · 6.4 KB
/
Camera.h
File metadata and controls
186 lines (152 loc) · 6.4 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* SimShip by Edouard Halbert
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
http://creativecommons.org/licenses/by-nc-nd/4.0/ */
#pragma once
#define NOMINMAX
#include <map>
#include <array>
#include <functional>
#include <iostream>
// glfw
#include <glfw/glfw3.h>
// glm
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/vector_angle.hpp>
using namespace std;
using namespace glm;
using InterpFunc = std::function<float(float)>;
enum eCameraMode{ ORBITAL, BRIDGE, FPS, CAMERA_MODE_COUNT };
enum eBridgeView { WHEEL, LEFT, RIGHT, BOW, STERN };
static float LinInterp(float t) { return t; }
static float SmoothStepInterp(float t) { return t * t * (3.0f - 2.0f * t); }
static float EaseInInterp(float t) { return t * t; }
static float EaseOutInterp(float t) { return 1.0f - (1.0f - t) * (1.0f - t); }
static float EaseInOutInterp(float t) { return t < 0.5f ? 2.f * t * t : 1.f - pow(-2.f * t + 2.f, 2) / 2.f; }
enum eInterpolation { Linear, SmoothStep, EaseIn, EaseOut, EaseInOut, COUNT };
class Camera
{
public:
// Init
void SetProjection(float fovy, int width, int height, float znear, float zfar);
void LookAt(vec3 posCamera, vec3 posTarget, vec3 up = { 0.f, 1.f, 0.f });
void SetZoom(float fovy);
void SetSpeeds(float movementSpeed, float rotationSpeed) { mMoveSpeed = movementSpeed; mRotateSpeed = rotationSpeed; }
void SetViewportSize(int width, int height);
void SetPosition(vec3 posCamera);
void SetInterpolation(eInterpolation type) { mInterpolation = type; }
void SetFirstUpdate(bool flag) { bFirstUpdate = flag; }
// Inputs
void KeyboardUpdate(int key, int scancode, int action, int mods);
void MousePosUpdate(double xpos, double ypos);
void MouseButtonUpdate(int button, int action, int mods);
void Animate(float deltaT, vec3& orbitalTarget, vec3& viewPos, vec3& viewTarget);
InterpFunc mInterpFunc = [](float t) { return t; }; // linear by default
// Get
vec3 GetPosition() const { return mPosition; }
mat4 GetProjection() const { return mMatProjection; }
mat4 GetView() const { return mMatView; }
mat4 GetViewProjection() const { return mMatViewProjection; }
mat4 GetViewReflexion() const { return mMatViewReflexion; }
vec3 GetDirection() { return mDirection; }
float GetZoom() const { return mFovyDeg; }
float GetInterpolationValue(float t) const;
eInterpolation GetInterpolation() const { return mInterpolation; }
bool IsUnchanged() const { return mIsUnchanged; }
eCameraMode GetMode() { return mCurrentMode; }
bool IsInViewFrustum(const vec3& position);
float GetHorizonViewportY() const;
float GetNorthAngleDEG();
float GetAttitudeDEG();
float GetRollDEG();
float GetOrbitRadius() { return mOrbitRadius; }
int GetViewportWidth() { return mWindowW; }
int GetViewportHeight() { return mWindowH; }
vec3 GetAt() const { return mDirection; }
vec3 GetUp() const { return mUp; }
// Orbital mode
void SetOrbitalMode();
void SetTarget(const vec3& target);
void AdjustOrbitRadius(float delta);
void SetMode(eCameraMode mode);
void ReturnToPreviousMode();
private:
void updateView();
void updateViewProjection();
void RotateCamera(float yaw, float pitch, float roll);
void MoveCamera(const vec3& delta);
bool mIsUnchanged;
mat4 mMatView; // transform from world space to screen UV [0, 1]
mat4 mMatProjection; // projection matrix (view space to clip space)
mat4 mMatViewProjection; // transform from world space to clip space [-1, 1] (WorldToView * Projection)
mat4 mMatViewReflexion; // transform from world space to screen UV [0, 1]
float mMoveSpeed = 0.001f; // movement speed in units/second
float mRotateSpeed = 0.0025f; // mouse sensitivity in radians/pixel
vec2 mMousePos;
vec2 mMousePosPrev;
vec3 mPosition; // in world space
vec3 mDirection; // normalized
vec3 mUp; // normalized
vec3 mRight; // normalized
vec3 mPositionTarget;
vec3 mDirectionTarget;
vec3 mUpTarget;
vec3 mRightTarget;
bool bFirstUpdate = true;
eInterpolation mInterpolation = eInterpolation::EaseInOut;
int mWindowW = 1600;
int mWindowH = 1000;
float mFovyDeg;
float mZnear;
float mZfar;
eCameraMode mCurrentMode;
eCameraMode mPreviousMode;
// Orbital mode
vec3 mTargetPos;
float mOrbitRadius;
float mOrbitYaw;
float mOrbitPitch;
typedef enum
{
MoveForward,
MoveBackward,
MoveLeft,
MoveRight,
MoveUp,
MoveDown,
Orbital,
Bridge,
Fps,
SpeedUp,
SlowDown,
KeyboardControlCount,
} KeyboardControls;
typedef enum
{
Left,
Middle,
Right,
MouseButtonCount,
MouseButtonFirst = Left,
} MouseButtons;
const map<int, int> mKeyboardMap = {
{ GLFW_KEY_W, KeyboardControls::MoveForward },
{ GLFW_KEY_S, KeyboardControls::MoveBackward },
{ GLFW_KEY_A, KeyboardControls::MoveLeft },
{ GLFW_KEY_D, KeyboardControls::MoveRight },
{ GLFW_KEY_E, KeyboardControls::MoveUp },
{ GLFW_KEY_Q, KeyboardControls::MoveDown },
{ GLFW_KEY_C, KeyboardControls::Orbital },
{ GLFW_KEY_B, KeyboardControls::Bridge },
{ GLFW_KEY_F, KeyboardControls::Fps },
{ GLFW_KEY_LEFT_SHIFT, KeyboardControls::SpeedUp },
{ GLFW_KEY_LEFT_CONTROL,KeyboardControls::SlowDown },
};
const std::map<int, int> mMouseButtonMap = {
{ GLFW_MOUSE_BUTTON_LEFT, MouseButtons::Left },
{ GLFW_MOUSE_BUTTON_MIDDLE, MouseButtons::Middle },
{ GLFW_MOUSE_BUTTON_RIGHT, MouseButtons::Right },
};
std::array<bool, KeyboardControls::KeyboardControlCount> mKeyboardState = { false };
std::array<bool, MouseButtons::MouseButtonCount> mMouseButtonState = { false };
};