vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
camera.h
1#pragma once
2
3#include <glm/glm.hpp>
4#include <glm/gtc/matrix_transform.hpp>
5
6#include "core/reflect.h"
7#include "ecs/entity.h"
8
9namespace Vkm::Engine {
10
11class Scene;
12
16enum class ProjectionType {
17 Perspective = 0,
18 Orthographic = 1,
19 Count
20};
21
25struct Camera {
26 ProjectionType projection = ProjectionType::Perspective;
28 float fovY = glm::radians(70.0f);
29 float orthoHeight = 10.0f;
31 float aspect = 0.0f;
32 float zNear = 0.1f;
33 float zFar = 1000.0f;
35 float focusDistance = 10.0f;
36 float dofAmount = 0.0f;
38 float dofMaxBlur = 0.011f;
39 bool active = true;
40
46 float viewmodelFovY = 0.0f;
47
49 static constexpr float VIEWMODEL_Z_NEAR = 0.01f;
50
58 static glm::mat4 computeProjection(const Camera& camera, float viewportAspect) {
59 const float aspect = camera.aspect > 0.0f ? camera.aspect : viewportAspect;
60 if (camera.projection == ProjectionType::Perspective) {
61 return glm::perspective(camera.fovY, aspect, camera.zNear, camera.zFar);
62 } else {
63 const float halfWidth = camera.orthoHeight * aspect;
64 return glm::ortho(
65 -halfWidth,
66 halfWidth,
67 -camera.orthoHeight,
68 camera.orthoHeight,
69 camera.zNear,
70 camera.zFar
71 );
72 }
73 }
74
84 static glm::mat4 computeViewmodelProjection(const Camera& camera, float viewportAspect) {
85 if (camera.projection != ProjectionType::Perspective) {
86 return computeProjection(camera, viewportAspect);
87 }
88 const float aspect = camera.aspect > 0.0f ? camera.aspect : viewportAspect;
89 const float fovY = camera.viewmodelFovY > 0.0f ? camera.viewmodelFovY : camera.fovY;
90 return glm::perspective(fovY, aspect, VIEWMODEL_Z_NEAR, camera.zFar);
91 }
92};
93
105EntityId findActiveCamera(const Scene& scene, EntityId cached = {});
106
107} // namespace Vkm::Engine
108
109VKM_ENUM_NAMES(::Vkm::Engine::ProjectionType, "Perspective", "Orthographic")
110
111VKM_REFLECT_BEGIN(::Vkm::Engine::Camera)
112 VKM_F(projection)
113 VKM_F(fovY)
114 VKM_F(orthoHeight)
115 VKM_F(aspect)
116 VKM_F(zNear)
117 VKM_F(zFar)
118 VKM_F(focusDistance)
119 VKM_F(dofAmount)
120 VKM_F(dofMaxBlur)
121 VKM_F(active)
122 VKM_F(viewmodelFovY)
123VKM_REFLECT_END()
Central registry managing entities and an open set of component types.
Definition scene.h:29
Component representing a camera, containing projection and view parameters.
Definition camera.h:25
float aspect
Width / height; <= 0 derives it from the viewport each frame.
Definition camera.h:31
float fovY
Vertical, radians.
Definition camera.h:28
static glm::mat4 computeProjection(const Camera &camera, float viewportAspect)
Compute the projection matrix for this camera.
Definition camera.h:58
float orthoHeight
Definition camera.h:29
float focusDistance
World distance held in sharp focus.
Definition camera.h:35
static constexpr float VIEWMODEL_Z_NEAR
Near plane of the viewmodel's projection: a weapon held close is not cut by the world's.
Definition camera.h:49
float dofAmount
Definition camera.h:36
float dofMaxBlur
Widest blur radius, fraction of viewport height (0..0.05)
Definition camera.h:38
float viewmodelFovY
Vertical field of view the eye draws Viewmodel meshes at, radians; 0 is fovY's.
Definition camera.h:46
static glm::mat4 computeViewmodelProjection(const Camera &camera, float viewportAspect)
The projection Viewmodel meshes draw through: viewmodelFovY, VIEWMODEL_Z_NEAR.
Definition camera.h:84