Crimson Engine
1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Toggle main menu visibility
Loading...
Searching...
No Matches
Camera.hpp
Go to the documentation of this file.
1
#pragma once
2
#include <glm/glm.hpp>
3
#include <glm/gtc/matrix_transform.hpp>
4
#include "
../components/Transform.hpp
"
5
#include "
core/EngineAPI.hpp
"
6
#include "
meta/ComponentReflection.hpp
"
7
8
13
// [ReflectClass]
14
struct
ENGINE_API
Camera
{
16
// [ReflectField]
17
bool
isOrthographic
=
false
;
19
// [ReflectField]
20
float
orthoSize
= 5.0f;
22
// [ReflectField]
23
float
fov
= 45.f;
25
// [ReflectField]
26
float
aspect
= 1.0f;
28
// [ReflectField]
29
float
nearPlane
= 0.1f;
31
// [ReflectField]
32
float
farPlane
= 100.f;
33
35
float
moveSpeed
= 5.f;
37
float
mouseSensitivity
= 0.1f;
38
39
40
46
glm::mat4
view
(
const
Transform
& transform)
const
{
47
// Convert Euler rotation (pitch, yaw, roll) to direction
48
float
pitch = glm::radians(transform.
rotation
.
x
);
49
float
yaw = glm::radians(transform.
rotation
.
y
);
50
51
glm::vec3 forward;
52
forward.x = cos(pitch) * cos(yaw);
53
forward.y = sin(pitch);
54
forward.z = cos(pitch) * sin(yaw);
55
forward = glm::normalize(forward);
56
57
glm::vec3 up(0.f, 1.f, 0.f);
58
return
glm::lookAt(transform.
position
, transform.
position
+ forward, up);
59
}
60
65
glm::mat4
projection
()
const
{
66
glm::mat4 proj;
67
if
(
isOrthographic
) {
68
float
halfWidth =
orthoSize
*
aspect
;
69
float
halfHeight =
orthoSize
;
70
float
actualNear = (
nearPlane
> 0.0f) ? -
farPlane
:
nearPlane
;
71
proj = glm::orthoRH_ZO(-halfWidth, halfWidth, -halfHeight, halfHeight, actualNear,
farPlane
);
72
}
else
{
73
proj = glm::perspectiveRH_ZO(glm::radians(
fov
),
aspect
,
nearPlane
,
farPlane
);
74
}
75
proj[1][1] *= -1;
// Vulkan's inverted Y
76
return
proj;
77
}
78
79
80
86
glm::mat4
viewProjection
(
const
Transform
& transform)
const
{
87
return
projection
() *
view
(transform);
88
}
89
};
90
91
REGISTER_COMPONENT
(
Camera
,
"Rendering & Lights/Camera"
);
92
93
94
95
96
97
98
ComponentReflection.hpp
REGISTER_COMPONENT
#define REGISTER_COMPONENT(Type, MenuPath)
Definition
ComponentReflection.hpp:156
EngineAPI.hpp
ENGINE_API
#define ENGINE_API
Definition
EngineAPI.hpp:10
Transform.hpp
Camera
Represents a camera component for rendering and viewing.
Definition
Camera.hpp:14
Camera::aspect
float aspect
Viewport aspect ratio (width / height).
Definition
Camera.hpp:26
Camera::orthoSize
float orthoSize
Vertical half-height size in world units when in Orthographic projection mode.
Definition
Camera.hpp:20
Camera::mouseSensitivity
float mouseSensitivity
Mouse look sensitivity multiplier.
Definition
Camera.hpp:37
Camera::viewProjection
glm::mat4 viewProjection(const Transform &transform) const
Calculates the view-projection matrix.
Definition
Camera.hpp:86
Camera::moveSpeed
float moveSpeed
Movement speed multiplier (units/sec) for camera fly controls.
Definition
Camera.hpp:35
Camera::nearPlane
float nearPlane
Distance to near clipping plane.
Definition
Camera.hpp:29
Camera::farPlane
float farPlane
Distance to far clipping plane.
Definition
Camera.hpp:32
Camera::isOrthographic
bool isOrthographic
True if camera projection mode is Orthographic, false if Perspective.
Definition
Camera.hpp:17
Camera::view
glm::mat4 view(const Transform &transform) const
Calculates the view matrix based on the given transform.
Definition
Camera.hpp:46
Camera::projection
glm::mat4 projection() const
Calculates the projection matrix.
Definition
Camera.hpp:65
Camera::fov
float fov
Vertical Field of View in degrees when in Perspective projection mode.
Definition
Camera.hpp:23
RotationField::y
float y
Definition
Transform.hpp:17
RotationField::x
float x
Definition
Transform.hpp:16
Transform
Represents the position, rotation, and scale of an entity in 3D space.
Definition
Transform.hpp:127
Transform::rotation
RotationField rotation
Definition
Transform.hpp:132
Transform::position
glm::vec3 position
Definition
Transform.hpp:130
engine
include
ecs
components
Camera.hpp
Generated by
1.17.0