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);
79 video::ITexture *get_texture()
81 return shadowMapTextureFinal;
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);
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; }
93 f32 getPerspectiveBiasXY() { return m_perspective_bias_xy; }
94 f32 getPerspectiveBiasZ() { return m_perspective_bias_z; }
97 video::ITexture *getSMTexture(const std::string &shadow_map_name,
98 video::ECOLOR_FORMAT texture_format,
99 bool force_creation = false);
101 void renderShadowMap(video::ITexture *target, DirectionalLight &light,
102 scene::E_SCENE_NODE_RENDER_PASS pass =
104 void renderShadowObjects(video::ITexture *target, DirectionalLight &light);
105 void mixShadowsQuad();
106 void updateSMTextures();
109 void enable() { m_shadows_enabled = m_shadows_supported; }
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};
121 std::vector<DirectionalLight> m_light_list;
122 std::vector<NodeToApply> m_shadow_node_array;
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;
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};
142 // Shadow Shader stuff
144 void createShaders();
145 std::string readShaderFile(const std::string &path);
147 s32 depth_shader{-1};
148 s32 depth_shader_entities{-1};
149 s32 depth_shader_trans{-1};
150 s32 mixcsm_shader{-1};
152 ShadowDepthShaderCB *m_shadow_depth_cb{nullptr};
153 ShadowDepthShaderCB *m_shadow_depth_entity_cb{nullptr};
154 ShadowDepthShaderCB *m_shadow_depth_trans_cb{nullptr};
156 shadowScreenQuad *m_screen_quad{nullptr};
157 shadowScreenQuadCB *m_shadow_mix_cb{nullptr};