-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
192 lines (160 loc) · 5.6 KB
/
camera.cpp
File metadata and controls
192 lines (160 loc) · 5.6 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
187
188
189
190
191
192
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
#include "template.h"
static constexpr bool LOAD_CAMERA = true;
static constexpr size_t SIZE_SAVE = sizeof(Camera) - sizeof(Cinemachine) + Cinemachine::GetSaveSize();
Camera::Camera()
{
if (!LOAD_CAMERA)
{
Reset();
return;
}
// try to load a camera
FILE* f = fopen( "camera.bin", "rb" );
if (f)
{
fread( this, 1, SIZE_SAVE, f );
fclose( f );
}
else
{
// setup a basic view frustum
Reset();
}
}
Camera::~Camera()
{
// save current camera
FILE* f = fopen( "camera.bin", "wb" );
fwrite( this, 1, SIZE_SAVE, f );
fclose( f );
}
void Camera::Reset()
{
camPos = float3(0.5f, 1.5f, -1.5f);
camTarget = float3(0.5f, 1.0f, 0.5f);
fov = 70.0f; // vertical FOV
aspect = (float)SCRWIDTH / (float)SCRHEIGHT;
nearPlane = 0.001f; // don't lower!
farPlane = 1000.f;
UpdateTransform();
m_cinemachine.m_path.Clear();
m_cinemachine.m_path.SetLooped(true);
m_cinemachine.m_path.AppendControlPoint(float3(-0.5f, 0.0f, -1.0f));
m_cinemachine.m_path.AppendControlPoint(float3(-0.5f, 0.5f, 0.5f));
m_cinemachine.m_path.AppendControlPoint(float3( 1.5f, 0.4f, -0.5f));
m_cinemachine.m_path.AppendControlPoint(float3( 1.5f, 0.0f, 1.0f));
}
Ray Camera::GetPrimaryRay(const float u, const float v) const
{
const float3 P = topLeft + u * horvec + v * vervec; // use cached horvec and vervec
return Ray(camPos, P - camPos);
// Note: no need to normalize primary rays in a pure voxel world
// TODO:
// - if we have other primitives as well, we *do* need to normalize!
// - there are far cooler camera models, e.g. try 'Panini projection'.
}
bool Camera::HandleInput(const float dt, bool automode)
{
m_cinemachine.m_splineT += 0.01f;
const ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureKeyboard)
{
return false;
}
bool changed = false;
if (automode)
{
const float3 next = m_cinemachine.m_path.GetPoint(m_cinemachine.m_splineT * m_cinemachine.m_speed);
const float3 newpos = next;
SetPosition(newpos);
LookAt(m_cinemachine.m_focusPoint);
changed = true;
}
else
{
if (!WindowHasFocus()) return false;
const float movementSpeed = 0.0015f * dt * m_movementSpeed;
const float rotationSpeed = 0.0015f * dt * m_rotationSpeed;
float3 ahead = normalize(camTarget - camPos);
const float3 tmpUp(0, 1, 0);
const float3 right = normalize(cross(tmpUp, ahead));
const float3 up = normalize(cross(ahead, right));
// --- Rotation ---
if (IsKeyDown(GLFW_KEY_UP)) ahead -= rotationSpeed * up, changed = true;
if (IsKeyDown(GLFW_KEY_DOWN)) ahead += rotationSpeed * up, changed = true;
if (IsKeyDown(GLFW_KEY_LEFT)) ahead -= rotationSpeed * right, changed = true;
if (IsKeyDown(GLFW_KEY_RIGHT)) ahead += rotationSpeed * right, changed = true;
if (changed) ahead = normalize(ahead); // Keep target exactly 1 unit away
// --- Movement ---
if (IsKeyDown(GLFW_KEY_A)) camPos -= movementSpeed * right, changed = true;
if (IsKeyDown(GLFW_KEY_D)) camPos += movementSpeed * right, changed = true;
if (IsKeyDown(GLFW_KEY_W)) camPos += movementSpeed * ahead, changed = true;
if (IsKeyDown(GLFW_KEY_S)) camPos -= movementSpeed * ahead, changed = true;
if (IsKeyDown(GLFW_KEY_SPACE)) camPos += movementSpeed * up, changed = true;
if (IsKeyDown(GLFW_KEY_LEFT_SHIFT)) camPos -= movementSpeed * up, changed = true;
if (IsKeyDown(GLFW_KEY_P)) { Reset(); return true; }
if (changed)
{
camTarget = camPos + ahead;
}
}
if (changed)
{
UpdateTransform();
}
return changed;
}
void Camera::GetViewMatrix(float* outMatrix) const
{
const float3 forward = normalize(camPos - camTarget);
const float3 right = normalize(cross(forward, float3(0, 1, 0)));
const float3 up = cross(right, forward);
// Column-major layout for ImGuizmo
outMatrix[0] = right.x; outMatrix[4] = right.y; outMatrix[8] = right.z; outMatrix[12] = -dot(right, camPos);
outMatrix[1] = up.x; outMatrix[5] = up.y; outMatrix[9] = up.z; outMatrix[13] = -dot(up, camPos);
outMatrix[2] = forward.x; outMatrix[6] = forward.y; outMatrix[10] = forward.z; outMatrix[14] = -dot(forward, camPos);
outMatrix[3] = 0; outMatrix[7] = 0; outMatrix[11] = 0; outMatrix[15] = 1.0f;
}
void Camera::GetProjectionMatrix(float* outMatrix) const
{
const float tanHalfFov = tanf(fov * 0.5f * PI / 180.0f);
for (int i = 0; i < 16; i++) outMatrix[i] = 0.0f;
outMatrix[0] = 1.0f / (aspect * tanHalfFov);
outMatrix[5] = 1.0f / (tanHalfFov);
outMatrix[10] = -(farPlane + nearPlane) / (farPlane - nearPlane);
outMatrix[11] = -1.0f;
outMatrix[14] = -(2.0f * farPlane * nearPlane) / (farPlane - nearPlane);
outMatrix[15] = 0.0f;
}
void Camera::Random()
{
camPos = normalize(float3(RandomFloat(), RandomFloat(), RandomFloat()) * 4.f) * RandomFloat(0.2f, 1.5f);
LookAt(float3(0.3f, 0.175f, 0.3f));
UpdateTransform();
}
void Camera::SetPosition(float3 pos)
{
camPos = pos;
UpdateTransform();
}
void Camera::LookAt(float3 target)
{
camTarget = target;
UpdateTransform();
}
void Camera::UpdateTransform()
{
const float3 forward = normalize(camTarget - camPos);
const float3 right = normalize(cross(float3(0, 1, 0), forward));
const float3 up = cross(forward, right);
const float halfHeight = tanf(fov * 0.5f * (PI / 180.0f));
const float halfWidth = halfHeight * aspect;
const float3 screenCenter = camPos + forward;
topLeft = screenCenter - (right * halfWidth) + (up * halfHeight);
topRight = screenCenter + (right * halfWidth) + (up * halfHeight);
bottomLeft = screenCenter - (right * halfWidth) - (up * halfHeight);
horvec = topRight - topLeft;
vervec = bottomLeft - topLeft;
}