Shkyera Engine
Easy to use, game engine for Python
Loading...
Searching...
No Matches
DirectionalLightComponent.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <string>
5
6#include <glm/glm.hpp>
7
10
11namespace shkyera {
12
14 public:
15 float intensity = 0.5;
16 glm::vec3 color = {1.0f, 1.0f, 1.0f};
17
18 inline static uint8_t LevelsOfDetail = 4;
19 inline static std::vector<float> CascadePlanes = {0.01, 5.0, 16.0, 32.0, 96.0};
20
21 glm::mat4 getLightSpaceMatrix(const glm::mat4& lightTransformMatrix,
24 auto frustumCorners = cameraComponent.getFrustumCornersWorldSpace(
26
28 glm::vec3 center = glm::vec3(0.0f);
29
30 for (const auto& corner : frustumCorners)
31 center += corner;
32 center /= frustumCorners.size();
33
34 glm::mat4 lightView = glm::lookAt(center + front * 50.0f, center, glm::vec3(0.0f, 1.0f, 0.0f));
35
36 glm::vec3 min = glm::vec3(std::numeric_limits<float>::max());
37 glm::vec3 max = glm::vec3(std::numeric_limits<float>::lowest());
38
39 for (const auto& corner : frustumCorners) {
40 glm::vec3 cornerInLightSpace = glm::vec3(lightView * glm::vec4(corner, 1.0f));
41 min = glm::min(min, cornerInLightSpace);
42 max = glm::max(max, cornerInLightSpace);
43 }
44
45 glm::mat4 lightProjection = glm::ortho(min.x, max.x, min.y, max.y, 0.1f, 100.0f);
47 }
48
49 static glm::vec3 getDirection(const glm::mat4& lightTransformMatrix) {
50 auto front = glm::normalize(glm::vec3{1, 0, 0} * glm::inverse(glm::mat3{lightTransformMatrix}));
51 return -front;
52 }
53};
54
55} // namespace shkyera
Definition CameraComponent.hpp:10
Definition TransformComponent.hpp:21
Definition Asset.hpp:6
Definition Clock.hpp:9
Definition DirectionalLightComponent.hpp:13
static glm::vec3 getDirection(const glm::mat4 &lightTransformMatrix)
Definition DirectionalLightComponent.hpp:49
glm::mat4 getLightSpaceMatrix(const glm::mat4 &lightTransformMatrix, const TransformComponent &cameraTransformComponent, const CameraComponent &cameraComponent, uint8_t levelOfDetail) const
Definition DirectionalLightComponent.hpp:21
float intensity
Definition DirectionalLightComponent.hpp:15
static uint8_t LevelsOfDetail
Definition DirectionalLightComponent.hpp:18
glm::vec3 color
Definition DirectionalLightComponent.hpp:16
static std::vector< float > CascadePlanes
Definition DirectionalLightComponent.hpp:19