3 Copyright (C) 2021 Liso <anlismon@gmail.com>
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.
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.
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.
24 #include "irrlichttypes_extrabloated.h"
25 #include "client/shadows/dynamicshadows.h"
27 class ShadowDepthShaderCB;
28 class shadowScreenQuad;
29 class shadowScreenQuadCB;
31 enum E_SHADOW_MODE : u8
39 NodeToApply(scene::ISceneNode *n,
40 E_SHADOW_MODE m = E_SHADOW_MODE::ESM_BOTH) :
43 bool operator<(const NodeToApply &other) const { return node < other.node; };
45 scene::ISceneNode *node;
47 E_SHADOW_MODE shadowMode{E_SHADOW_MODE::ESM_BOTH};
54 ShadowRenderer(IrrlichtDevice *device, Client *client);
60 /// Adds a directional light shadow map (Usually just one (the sun) except in
62 size_t addDirectionalLight();
63 DirectionalLight &getDirectionalLight(u32 index = 0);
64 size_t getDirectionalLightCount() const;
65 f32 getMaxShadowFar() const;
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.
72 void addNodeToShadowList(scene::ISceneNode *node,
73 E_SHADOW_MODE shadowMode = ESM_BOTH);
74 void removeNodeFromShadowList(scene::ISceneNode *node);
76 void update(video::ITexture *outputTarget = nullptr);
77 void setForceUpdateShadowMap() { m_force_update_shadow_map = true; }
80 video::ITexture *get_texture()
82 return shadowMapTextureFinal;
86 bool is_active() const { return m_shadows_enabled && shadowMapTextureFinal != nullptr; }
87 void setTimeOfDay(float isDay) { m_time_day = isDay; };
88 void setShadowIntensity(float shadow_intensity);
90 s32 getShadowSamples() const { return m_shadow_samples; }
91 float getShadowStrength() const { return m_shadows_enabled ? m_shadow_strength : 0.0f; }
92 float getTimeOfDay() const { return m_time_day; }
94 f32 getPerspectiveBiasXY() { return m_perspective_bias_xy; }
95 f32 getPerspectiveBiasZ() { return m_perspective_bias_z; }
98 video::ITexture *getSMTexture(const std::string &shadow_map_name,
99 video::ECOLOR_FORMAT texture_format,
100 bool force_creation = false);
102 void renderShadowMap(video::ITexture *target, DirectionalLight &light,
103 scene::E_SCENE_NODE_RENDER_PASS pass =
105 void renderShadowObjects(video::ITexture *target, DirectionalLight &light);
106 void mixShadowsQuad();
107 void updateSMTextures();
110 void enable() { m_shadows_enabled = m_shadows_supported; }
112 // a bunch of variables
113 scene::ISceneManager *m_smgr{nullptr};
114 video::IVideoDriver *m_driver{nullptr};
115 Client *m_client{nullptr};
116 video::ITexture *shadowMapClientMap{nullptr};
117 video::ITexture *shadowMapClientMapFuture{nullptr};
118 video::ITexture *shadowMapTextureFinal{nullptr};
119 video::ITexture *shadowMapTextureDynamicObjects{nullptr};
120 video::ITexture *shadowMapTextureColors{nullptr};
122 std::vector<DirectionalLight> m_light_list;
123 std::vector<NodeToApply> m_shadow_node_array;
125 float m_shadow_strength;
126 float m_shadow_strength_gamma;
127 float m_shadow_map_max_distance;
128 float m_shadow_map_texture_size;
129 float m_time_day{0.0f};
130 int m_shadow_samples;
131 bool m_shadow_map_texture_32bit;
132 bool m_shadows_enabled;
133 bool m_shadows_supported;
134 bool m_shadow_map_colored;
135 bool m_force_update_shadow_map;
136 u8 m_map_shadow_update_frames; /* Use this number of frames to update map shaodw */
137 u8 m_current_frame{0}; /* Current frame */
138 f32 m_perspective_bias_xy;
139 f32 m_perspective_bias_z;
141 video::ECOLOR_FORMAT m_texture_format{video::ECOLOR_FORMAT::ECF_R16F};
142 video::ECOLOR_FORMAT m_texture_format_color{video::ECOLOR_FORMAT::ECF_R16G16};
144 // Shadow Shader stuff
146 void createShaders();
147 std::string readShaderFile(const std::string &path);
149 s32 depth_shader{-1};
150 s32 depth_shader_entities{-1};
151 s32 depth_shader_trans{-1};
152 s32 mixcsm_shader{-1};
154 ShadowDepthShaderCB *m_shadow_depth_cb{nullptr};
155 ShadowDepthShaderCB *m_shadow_depth_entity_cb{nullptr};
156 ShadowDepthShaderCB *m_shadow_depth_trans_cb{nullptr};
158 shadowScreenQuad *m_screen_quad{nullptr};
159 shadowScreenQuadCB *m_shadow_mix_cb{nullptr};