sculpto
directional_light_component.h
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file directional_light_component.h
3 * \brief Directional light component class implementation module.
4 *
5 * \author Sabitov Kirill
6 * \date 05 July 2022
7 *********************************************************************/
8
9#pragma once
10
11#include "base.h"
13
14namespace scl
15{
18 {
19 private:
20 bool IsShadows {};
21 shared<frame_buffer> ShadowMap {};
22 matr4 Projection {};
23 float BoxSize {};
24 float BoxDepth {};
25 int ShadowMapWidth {}, ShadowMapHeight {};
26
27 public:
30 float Strength { 1 };
31
32 public:
34 bool GetIsShadow() const { return IsShadows; }
36 float GetBoxSize() const { return BoxSize; }
38 float GetBoxDepth() const { return BoxDepth; }
40 const matr4 &GetProjection() const { return Projection; }
42 int GetShadowMapWidth() const { return ShadowMapWidth; }
44 int GetShadowMapHeight() const { return ShadowMapHeight; }
45 const shared<frame_buffer> &GetShadowMap() const { return ShadowMap; }
46
47 /*!*
48 * Enable directiona light shadow casting function.
49 *
50 * \param BoxSize - ortho projection shadow caster box width.
51 * \param BoxDepth - ortho projecction shadow caster box depth.
52 * \param ShadowMapWidth - shadow map frame buffer width in pixels.
53 * \param ShadowMapHeight - shadow map frame buffer height in pixels.
54 */
55 void EnableShadows(float BoxSize = 10, float BoxDepth = 100,
56 int ShadowMapWidth = 1000, int ShadowMapHeight = 1000)
57 {
58 this->IsShadows = true;
59 this->BoxSize = BoxSize;
60 this->BoxDepth = BoxDepth;
61 this->ShadowMapWidth = ShadowMapWidth, this->ShadowMapHeight = ShadowMapHeight;
62 Projection = matr4::Ortho(-BoxSize, BoxSize, -BoxSize, BoxSize, 1, BoxDepth);
63 ShadowMap = frame_buffer::Create(frame_buffer_props { ShadowMapWidth, ShadowMapHeight, 1, false, 0, 1 });
64 }
65
66 /*!*
67 * Disable directional light shadow casting function.
68 *
69 * \param None.
70 * \return None.
71 */
73 {
74 this->IsShadows = false;
75 if (ShadowMap) ShadowMap->Free();
76 ShadowMap.reset();
77 }
78
79 /*!*
80 * Set flag, showing wheather directiona light casing shadows or not function.
81 *
82 * \param IsShadows - flag, showing wheather directiona light casing shadows or not function.
83 * \return None.
84 */
85 void SetIsShadows(bool IsShadows)
86 {
87 if (IsShadows) EnableShadows();
88 else DisableShadows();
89 }
90
92 void SetBoxSize(float BoxSize)
93 {
94 this->BoxSize = BoxSize;
95 Projection = matr4::Ortho(-BoxSize, BoxSize, -BoxSize, BoxSize, 1, BoxDepth);
96 }
97
99 void SetBoxDepth(float BoxDepth)
100 {
101 this->BoxDepth = BoxDepth;
102 Projection = matr4::Ortho(-BoxSize, BoxSize, -BoxSize, BoxSize, 1, BoxDepth);
103 }
104
106 void SetShadowMapWidth(int ShadowMapWidth)
107 {
108 this->ShadowMapWidth = ShadowMapWidth;
109 if (!ShadowMap) return;
110 ShadowMap->Resize(ShadowMapWidth, ShadowMapHeight);
111 }
112
114 void SetShadowMapHeight(int ShadowMapHeight)
115 {
116 this->ShadowMapHeight = ShadowMapHeight;
117 if (!ShadowMap) return;
118 ShadowMap->Resize(ShadowMapWidth, ShadowMapHeight);
119 }
120
121 public:
124 directional_light_component(const vec3 &Color, bool IsShadows = false, float BoxSize = 10, float BoxDepth = 0,
125 int ShadowMapWidth = 1000, int ShadowMapHeight = 1000) : Color(Color)
126 {
127 if (IsShadows) EnableShadows(BoxSize, BoxDepth, ShadowMapWidth, ShadowMapHeight);
128 }
130 };
131}
Topology object basis class for mesh creating implementation module.
static shared< frame_buffer > Create(const frame_buffer_props &Props)
static matr4 Ortho(float Left, float Right, float Bottom, float Top, float Near, float Far)
Definition: matr4.h:375
Frame buffer interface implementation module.
Definition: base.h:33
std::shared_ptr< T > shared
Definition: smart_ptr.h:15
void EnableShadows(float BoxSize=10, float BoxDepth=100, int ShadowMapWidth=1000, int ShadowMapHeight=1000)
const shared< frame_buffer > & GetShadowMap() const
directional_light_component(const directional_light_component &Other)=default
directional_light_component(const vec3 &Color, bool IsShadows=false, float BoxSize=10, float BoxDepth=0, int ShadowMapWidth=1000, int ShadowMapHeight=1000)