Foundation
Loading...
Searching...
No Matches
Camera.hpp
Go to the documentation of this file.
1#pragma once
2#include <Math/Math.hpp>
4#include <SDL3/SDL.h>
5using namespace Foundation::Math;
6
7// Orbit camera with WASD fly-through. Shared by the editor and the renderer examples.
8// Drives a right-handed, reverse-Z view/projection (matches the Foundation renderer UBO).
10{
11 static constexpr char kControlsText[] = "Mouse Left: Rotate | Mouse Right: Pan | Mouse Wheel: Zoom | WASD: Move | Shift: Fast | Space: Pause";
12
14 float radius;
15 quat rot;
16 float zNear, fovY, aspect;
17 mat4 view, proj;
18 float moveSpeed = 2.0f;
19 // WASD key state
20 bool keyW = false, keyA = false, keyS = false, keyD = false;
21 bool keyShift = false;
22 // Called every frame: continuously move center based on WASD state
23 bool UpdateMovement(float dt)
24 {
25 vec3 moveDir(0.0f);
26 vec3 forward = rot * vec3(0, 0, -1); // camera forward (note: view direction is -Z)
27 vec3 right = rot * vec3(1, 0, 0);
28 if (keyW) moveDir += forward;
29 if (keyS) moveDir -= forward;
30 if (keyA) moveDir -= right;
31 if (keyD) moveDir += right;
32 if (glm::dot(moveDir, moveDir) > 1e-6f)
33 {
34 moveDir = glm::normalize(moveDir);
35 float speed = moveSpeed * (keyShift ? 4.0f : 1.0f);
36 center += moveDir * speed * dt;
37 return true;
38 }
39 return false;
40 }
41
42 bool Update(SDL_Event const& event)
43 {
44 bool updated = false;
45 if (event.type == SDL_EVENT_MOUSE_MOTION)
46 {
47 SDL_MouseButtonFlags mouseState = SDL_GetMouseState(NULL, NULL);
48 if (mouseState & SDL_BUTTON_LMASK)
49 {
50 float yawDelta = -event.motion.xrel * 1e-2f;
51 float pitchDelta = -event.motion.yrel * 1e-2f;
52 quat yawRot = angleAxis(yawDelta, vec3(0, 1, 0));
53 quat pitchRot = angleAxis(pitchDelta, vec3(1, 0, 0));
54 rot = normalize(yawRot * rot * pitchRot);
55 updated = true;
56 }
57 if (mouseState & SDL_BUTTON_RMASK)
58 {
59 vec3 right = rot * vec3(1, 0, 0);
60 vec3 up = rot * vec3(0, 1, 0);
61 center -= right * (event.motion.xrel * radius * 1e-3f);
62 center += up * (event.motion.yrel * radius * 1e-3f);
63 updated = true;
64 }
65 }
66 if (event.type == SDL_EVENT_MOUSE_WHEEL)
67 {
68 radius -= event.wheel.y * radius * 1e-1f;
69 radius = radius < 1e-3f ? 1e-3f : radius;
70 updated = true;
71 }
72 // ---
74 vec3 dir = rot * vec3(0, 0, 1);
75 position = center + radius * dir;
77 return updated;
78 }
79};
Definition Decompose.hpp:4
vec3 float3
Definition Math.hpp:26
mat4 infinitePerspectiveRHReverseZ(float fovY, float a, float zNear)
Definition ModelViewProjection.hpp:7
mat4 viewMatrixRHReverseZ(vec3 pos, quat rot)
Definition ModelViewProjection.hpp:26
Definition Camera.hpp:10
bool Update(SDL_Event const &event)
Definition Camera.hpp:42
bool keyD
Definition Camera.hpp:20
mat4 view
Definition Camera.hpp:17
float3 center
Definition Camera.hpp:13
float aspect
Definition Camera.hpp:16
float zNear
Definition Camera.hpp:16
float fovY
Definition Camera.hpp:16
float moveSpeed
Definition Camera.hpp:18
bool keyS
Definition Camera.hpp:20
bool keyW
Definition Camera.hpp:20
bool keyA
Definition Camera.hpp:20
static constexpr char kControlsText[]
Definition Camera.hpp:11
mat4 proj
Definition Camera.hpp:17
float radius
Definition Camera.hpp:14
bool UpdateMovement(float dt)
Definition Camera.hpp:23
bool keyShift
Definition Camera.hpp:21
float3 position
Definition Camera.hpp:13
quat rot
Definition Camera.hpp:15