Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
CameraSoA.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <glm/glm.hpp>
4#include <glm/gtc/matrix_transform.hpp>
5
10struct CameraSoA {
11 std::vector<bool> isOrthographics;
12 std::vector<float> orthoSizes;
14 std::vector<float> fovs;
16 std::vector<float> aspects;
18 std::vector<float> nearPlanes;
20 std::vector<float> farPlanes;
21
23 std::vector<glm::mat4> viewProjMatrices; // output for GPU
24
28 void clear() {
29 isOrthographics.clear();
30 orthoSizes.clear();
31 fovs.clear();
32 aspects.clear();
33 nearPlanes.clear();
34 farPlanes.clear();
35 viewProjMatrices.clear();
36 }
37
48 size_t pushCamera(float fov, float aspect, float nearPlane, float farPlane, bool isOrthographic = false, float orthoSize = 5.0f) {
49 isOrthographics.push_back(isOrthographic);
50 orthoSizes.push_back(orthoSize);
51 fovs.push_back(fov);
52 aspects.push_back(aspect);
53 nearPlanes.push_back(nearPlane);
54 farPlanes.push_back(farPlane);
55 viewProjMatrices.emplace_back(1.0f); // placeholder
56 return fovs.size() - 1;
57 }
58
63 size_t size() const { return fovs.size(); }
64
70 template<typename TransformSoAType>
71 void updateViewProjections(const TransformSoAType& transforms) {
72 size_t n = size();
73 viewProjMatrices.resize(n); // ensure enough space
74
75 // Linear memory access -> cache-friendly
76 for (size_t i = 0; i < n; ++i) {
77 const glm::vec3& pos = transforms.positions[i];
78 const glm::vec3& rot = transforms.rotations[i];
79
80 float pitch = glm::radians(rot.x);
81 float yaw = glm::radians(rot.y);
82
83 glm::vec3 forward;
84 forward.x = cos(pitch) * cos(yaw);
85 forward.y = sin(pitch);
86 forward.z = cos(pitch) * sin(yaw);
87 forward = glm::normalize(forward);
88
89 glm::vec3 up(0.f, 1.f, 0.f);
90
91 glm::mat4 view = glm::lookAt(pos, pos + forward, up);
92 glm::mat4 proj;
93 if (isOrthographics[i]) {
94 float halfWidth = orthoSizes[i] * aspects[i];
95 float halfHeight = orthoSizes[i];
96 proj = glm::ortho(-halfWidth, halfWidth, -halfHeight, halfHeight, nearPlanes[i], farPlanes[i]);
97 } else {
98 proj = glm::perspective(glm::radians(fovs[i]), aspects[i], nearPlanes[i], farPlanes[i]);
99 }
100 proj[1][1] *= -1; // Vulkan Y flip
101
102 viewProjMatrices[i] = proj * view;
103 }
104 }
105};
106
Structure of Arrays representing camera components for parallel view projection calculations.
Definition CameraSoA.hpp:10
std::vector< float > aspects
Viewport aspect ratios.
Definition CameraSoA.hpp:16
size_t pushCamera(float fov, float aspect, float nearPlane, float farPlane, bool isOrthographic=false, float orthoSize=5.0f)
Adds camera components to SoA.
Definition CameraSoA.hpp:48
std::vector< float > nearPlanes
Distances to near clipping planes.
Definition CameraSoA.hpp:18
void updateViewProjections(const TransformSoAType &transforms)
Batch updates view projection matrices.
Definition CameraSoA.hpp:71
std::vector< bool > isOrthographics
Definition CameraSoA.hpp:11
std::vector< float > orthoSizes
Definition CameraSoA.hpp:12
std::vector< float > farPlanes
Distances to far clipping planes.
Definition CameraSoA.hpp:20
std::vector< float > fovs
Fields of view in degrees.
Definition CameraSoA.hpp:14
std::vector< glm::mat4 > viewProjMatrices
Computed view-projection matrices ready for GPU transfer.
Definition CameraSoA.hpp:23
size_t size() const
Returns total camera count.
Definition CameraSoA.hpp:63
void clear()
Clears all arrays.
Definition CameraSoA.hpp:28