]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/shadows/dynamicshadows.h
Adjust shadowmap distortion to use entire SM texture (#12166)
[dragonfireclient.git] / src / client / shadows / dynamicshadows.h
1 /*
2 Minetest
3 Copyright (C) 2021 Liso <anlismon@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "irrlichttypes_bloated.h"
23 #include <matrix4.h>
24 #include "util/basic_macros.h"
25 #include "constants.h"
26
27 class Camera;
28 class Client;
29
30 struct shadowFrustum
31 {
32         f32 zNear{0.0f};
33         f32 zFar{0.0f};
34         f32 length{0.0f};
35         f32 radius{0.0f};
36         core::matrix4 ProjOrthMat;
37         core::matrix4 ViewMat;
38         v3f position;
39         v3f player;
40         v3s16 camera_offset;
41 };
42
43 class DirectionalLight
44 {
45 public:
46         DirectionalLight(const u32 shadowMapResolution,
47                         const v3f &position,
48                         video::SColorf lightColor = video::SColor(0xffffffff),
49                         f32 farValue = 100.0f);
50         ~DirectionalLight() = default;
51
52         //DISABLE_CLASS_COPY(DirectionalLight)
53
54         void update_frustum(const Camera *cam, Client *client, bool force = false);
55
56         // when set direction is updated to negative normalized(direction)
57         void setDirection(v3f dir);
58         v3f getDirection() const{
59                 return direction;
60         };
61         v3f getPosition() const;
62         v3f getPlayerPos() const;
63         v3f getFuturePlayerPos() const;
64
65         /// Gets the light's matrices.
66         const core::matrix4 &getViewMatrix() const;
67         const core::matrix4 &getProjectionMatrix() const;
68         const core::matrix4 &getFutureViewMatrix() const;
69         const core::matrix4 &getFutureProjectionMatrix() const;
70         core::matrix4 getViewProjMatrix();
71
72         /// Gets the light's far value.
73         f32 getMaxFarValue() const
74         {
75                 return farPlane * BS;
76         }
77
78
79         /// Gets the light's color.
80         const video::SColorf &getLightColor() const
81         {
82                 return diffuseColor;
83         }
84
85         /// Sets the light's color.
86         void setLightColor(const video::SColorf &lightColor)
87         {
88                 diffuseColor = lightColor;
89         }
90
91         /// Gets the shadow map resolution for this light.
92         u32 getMapResolution() const
93         {
94                 return mapRes;
95         }
96
97         bool should_update_map_shadow{true};
98
99         void commitFrustum();
100
101 private:
102         void createSplitMatrices(const Camera *cam);
103
104         video::SColorf diffuseColor;
105
106         f32 farPlane;
107         u32 mapRes;
108
109         v3f pos;
110         v3f direction{0};
111         shadowFrustum shadow_frustum;
112         shadowFrustum future_frustum;
113         bool dirty{false};
114 };