Shkyera Engine
Easy to use, game engine for Python
Loading...
Searching...
No Matches
CameraComponent.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <glm/glm.hpp>
4
6#include <Math/Ray.hpp>
7
8namespace shkyera {
9
11 public:
12 enum class ProjectionType { Invalid = 0, Perspective = 1, Orthographic = 2 };
13
14 float fov{40.0f};
15 float aspectRatio{16.0f / 9.0f};
16 float nearPlane{0.1f}, farPlane{1000.0f};
18
20 glm::vec3 position = transformComponent.getPosition();
21 glm::vec3 orientation = transformComponent.getOrientation();
22
23 glm::vec3 front;
24 front.x = std::cos(orientation.y) * std::cos(orientation.x);
25 front.y = std::sin(orientation.x);
26 front.z = std::sin(orientation.y) * std::cos(orientation.x);
27 front = glm::normalize(front);
28
29 glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
30 glm::vec3 right = glm::normalize(glm::cross(front, up));
31 up = -glm::normalize(glm::cross(right, front));
32
33 return glm::lookAt(position, position + front, up);
34 }
35
37
38 Ray getRayAt(const TransformComponent& transformComponent, float x, float y) const {
40
41 glm::mat4 invViewProj = glm::inverse(getProjectionMatrix() * viewMatrix);
42
43 float ndcX = 2.0f * x - 1.0f;
44 float ndcY = 1.0f - 2.0f * y;
45
46 glm::vec4 nearPoint = glm::vec4(ndcX, ndcY, -1.0f, 1.0f);
47 glm::vec4 farPoint = glm::vec4(ndcX, ndcY, 1.0f, 1.0f);
48
49 glm::vec4 nearWorld = invViewProj * nearPoint;
50 glm::vec4 farWorld = invViewProj * farPoint;
51
53 farWorld /= farWorld.w;
54
55 glm::vec3 rayOrigin = glm::vec3(nearWorld);
56 glm::vec3 rayDirection = glm::normalize(glm::vec3(farWorld) - rayOrigin);
57
58 return {rayOrigin, rayDirection};
59 }
60
61 std::vector<glm::vec3> getFrustumCornersWorldSpace(float localNearPlane, float localFarPlane,
63 glm::mat4 invViewProj =
66
67 std::vector<glm::vec3> frustumCorners;
68 for (int x = -1; x <= 1; x += 2) {
69 for (int y = -1; y <= 1; y += 2) {
70 for (int z = -1; z <= 1; z += 2) {
71 glm::vec4 corner = invViewProj * glm::vec4(x, y, z, 1.0f);
72 corner /= corner.w;
73 frustumCorners.push_back(glm::vec3(corner));
74 }
75 }
76 }
77 return frustumCorners;
78 }
79
80 private:
83 return glm::perspective(glm::radians(fov), aspectRatio, localNearPlane, localFarPlane);
84 } else {
85 float orthoSize = fov; // Can be configurable
86 float halfWidth = orthoSize * aspectRatio * 0.5f;
87 float halfHeight = orthoSize * 0.5f;
89 }
90 }
91};
92
93} // namespace shkyera
Definition CameraComponent.hpp:10
float fov
Definition CameraComponent.hpp:14
ProjectionType
Definition CameraComponent.hpp:12
float farPlane
Definition CameraComponent.hpp:16
Ray getRayAt(const TransformComponent &transformComponent, float x, float y) const
Definition CameraComponent.hpp:38
ProjectionType projectionType
Definition CameraComponent.hpp:17
float nearPlane
Definition CameraComponent.hpp:16
glm::mat4 getViewMatrix(const TransformComponent &transformComponent) const
Definition CameraComponent.hpp:19
std::vector< glm::vec3 > getFrustumCornersWorldSpace(float localNearPlane, float localFarPlane, const TransformComponent &transformComponent) const
Definition CameraComponent.hpp:61
glm::mat4 getProjectionMatrix(ProjectionType projection, float localNearPlane, float localFarPlane) const
Definition CameraComponent.hpp:81
glm::mat4 getProjectionMatrix() const
Definition CameraComponent.hpp:36
float aspectRatio
Definition CameraComponent.hpp:15
Definition TransformComponent.hpp:21
Definition Asset.hpp:6
Definition Clock.hpp:9
Definition Ray.hpp:7