Repository navigation
Expand file tree
/
Copy pathfree_camera.cpp
More file actions
40 lines (32 loc) · 1.47 KB
/
Copy pathfree_camera.cpp
File metadata and controls
40 lines (32 loc) · 1.47 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
#include "free_camera.h"
#include "renderer.h"
#include <imgui.h>
void FreeCamera::update() {
using namespace NRE;
const ImGuiIO& io = ImGui::GetIO();
if (!io.WantCaptureKeyboard && isKeyPressed(Key::Tab).value()) setLooking(!looking);
if (isKeyPressed(Key::Escape).value()) setLooking(false);
if (looking) {
glm::vec2 d = getMouseDelta().value(); // pixels; +y is down
(void)rotateCamera(d.x * sensitivity, -d.y * sensitivity);
}
// Don't move while typing in an ImGui text box.
if (io.WantCaptureKeyboard && !looking) return;
glm::vec3 move{0.0f};
if (isKeyDown(Key::W).value()) move.z += 1;
if (isKeyDown(Key::S).value()) move.z -= 1;
if (isKeyDown(Key::D).value()) move.x += 1;
if (isKeyDown(Key::A).value()) move.x -= 1;
if (isKeyDown(Key::Space).value()) move.y += 1;
if (isKeyDown(Key::LeftShift).value()) move.y -= 1;
if (move == glm::vec3{0.0f}) return;
float s = speed * (isKeyDown(Key::LeftControl).value() ? fastMultiplier : 1.0f);
(void)moveCameraRelative(glm::normalize(move) * s * getDeltaTime().value());
}
void FreeCamera::setLooking(bool on) {
looking = on;
(void)NRE::setCursorMode(on ? NRE::CursorMode::Locked : NRE::CursorMode::Normal);
// Keep ImGui from reacting to the hidden cursor while looking.
if (on) ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouse;
else ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouse;
}