]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/shadows/dynamicshadowsrender.h
0e4ef6b70cd29ee220845531cf2a6644db2c0832
[dragonfireclient.git] / src / client / shadows / dynamicshadowsrender.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 <string>
23 #include <vector>
24 #include "irrlichttypes_extrabloated.h"
25 #include "client/shadows/dynamicshadows.h"
26
27 class ShadowDepthShaderCB;
28 class shadowScreenQuad;
29 class shadowScreenQuadCB;
30
31 enum E_SHADOW_MODE : u8
32 {
33         ESM_RECEIVE = 0,
34         ESM_BOTH,
35 };
36
37 struct NodeToApply
38 {
39         NodeToApply(scene::ISceneNode *n,
40                         E_SHADOW_MODE m = E_SHADOW_MODE::ESM_BOTH) :
41                         node(n),
42                         shadowMode(m){};
43         bool operator<(const NodeToApply &other) const { return node < other.node; };
44
45         scene::ISceneNode *node;
46
47         E_SHADOW_MODE shadowMode{E_SHADOW_MODE::ESM_BOTH};
48         bool dirty{false};
49 };
50
51 class ShadowRenderer
52 {
53 public:
54         ShadowRenderer(IrrlichtDevice *device, Client *client);
55
56         ~ShadowRenderer();
57
58         void initialize();
59
60         /// Adds a directional light shadow map (Usually just one (the sun) except in
61         /// Tattoine ).
62         size_t addDirectionalLight();
63         DirectionalLight &getDirectionalLight(u32 index = 0);
64         size_t getDirectionalLightCount() const;
65         f32 getMaxShadowFar() const;
66
67         /// Adds a shadow to the scene node.
68         /// The shadow mode can be ESM_BOTH, or ESM_RECEIVE.
69         /// ESM_BOTH casts and receives shadows
70         /// ESM_RECEIVE only receives but does not cast shadows.
71         ///
72         void addNodeToShadowList(scene::ISceneNode *node,
73                         E_SHADOW_MODE shadowMode = ESM_BOTH);
74         void removeNodeFromShadowList(scene::ISceneNode *node);
75
76         void update(video::ITexture *outputTarget = nullptr);
77         void drawDebug();
78
79         video::ITexture *get_texture()
80         {
81                 return shadowMapTextureFinal;
82         }
83
84
85         bool is_active() const { return m_shadows_enabled && shadowMapTextureFinal != nullptr; }
86         void setTimeOfDay(float isDay) { m_time_day = isDay; };
87         void setShadowIntensity(float shadow_intensity);
88
89         s32 getShadowSamples() const { return m_shadow_samples; }
90         float getShadowStrength() const { return m_shadows_enabled ? m_shadow_strength : 0.0f; }
91         float getTimeOfDay() const { return m_time_day; }
92
93         f32 getPerspectiveBiasXY() { return m_perspective_bias_xy; }
94         f32 getPerspectiveBiasZ() { return m_perspective_bias_z; }
95
96 private:
97         video::ITexture *getSMTexture(const std::string &shadow_map_name,
98                         video::ECOLOR_FORMAT texture_format,
99                         bool force_creation = false);
100
101         void renderShadowMap(video::ITexture *target, DirectionalLight &light,
102                         scene::E_SCENE_NODE_RENDER_PASS pass =
103                                         scene::ESNRP_SOLID);
104         void renderShadowObjects(video::ITexture *target, DirectionalLight &light);
105         void mixShadowsQuad();
106         void updateSMTextures();
107
108         void disable();
109         void enable() { m_shadows_enabled = m_shadows_supported; }
110
111         // a bunch of variables
112         scene::ISceneManager *m_smgr{nullptr};
113         video::IVideoDriver *m_driver{nullptr};
114         Client *m_client{nullptr};
115         video::ITexture *shadowMapClientMap{nullptr};
116         video::ITexture *shadowMapClientMapFuture{nullptr};
117         video::ITexture *shadowMapTextureFinal{nullptr};
118         video::ITexture *shadowMapTextureDynamicObjects{nullptr};
119         video::ITexture *shadowMapTextureColors{nullptr};
120
121         std::vector<DirectionalLight> m_light_list;
122         std::vector<NodeToApply> m_shadow_node_array;
123
124         float m_shadow_strength;
125         float m_shadow_strength_gamma;
126         float m_shadow_map_max_distance;
127         float m_shadow_map_texture_size;
128         float m_time_day{0.0f};
129         int m_shadow_samples;
130         bool m_shadow_map_texture_32bit;
131         bool m_shadows_enabled;
132         bool m_shadows_supported;
133         bool m_shadow_map_colored;
134         u8 m_map_shadow_update_frames; /* Use this number of frames to update map shaodw */
135         u8 m_current_frame{0}; /* Current frame */
136         f32 m_perspective_bias_xy;
137         f32 m_perspective_bias_z;
138
139         video::ECOLOR_FORMAT m_texture_format{video::ECOLOR_FORMAT::ECF_R16F};
140         video::ECOLOR_FORMAT m_texture_format_color{video::ECOLOR_FORMAT::ECF_R16G16};
141
142         // Shadow Shader stuff
143
144         void createShaders();
145         std::string readShaderFile(const std::string &path);
146
147         s32 depth_shader{-1};
148         s32 depth_shader_entities{-1};
149         s32 depth_shader_trans{-1};
150         s32 mixcsm_shader{-1};
151
152         ShadowDepthShaderCB *m_shadow_depth_cb{nullptr};
153         ShadowDepthShaderCB *m_shadow_depth_entity_cb{nullptr};
154         ShadowDepthShaderCB *m_shadow_depth_trans_cb{nullptr};
155
156         shadowScreenQuad *m_screen_quad{nullptr};
157         shadowScreenQuadCB *m_shadow_mix_cb{nullptr};
158 };