]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/shadows/dynamicshadows.cpp
Adjust shadowmap distortion to use entire SM texture (#12166)
[dragonfireclient.git] / src / client / shadows / dynamicshadows.cpp
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 #include <cmath>
21
22 #include "client/shadows/dynamicshadows.h"
23 #include "client/client.h"
24 #include "client/clientenvironment.h"
25 #include "client/clientmap.h"
26 #include "client/camera.h"
27
28 using m4f = core::matrix4;
29
30 void DirectionalLight::createSplitMatrices(const Camera *cam)
31 {
32         v3f newCenter;
33         v3f look = cam->getDirection();
34
35         // camera view tangents
36         float tanFovY = tanf(cam->getFovY() * 0.5f);
37         float tanFovX = tanf(cam->getFovX() * 0.5f);
38
39         // adjusted frustum boundaries
40         float sfNear = future_frustum.zNear;
41         float sfFar = adjustDist(future_frustum.zFar, cam->getFovY());
42
43         // adjusted camera positions
44         v3f cam_pos_world = cam->getPosition();
45         v3f cam_pos_scene = v3f(cam_pos_world.X - cam->getOffset().X * BS,
46                         cam_pos_world.Y - cam->getOffset().Y * BS,
47                         cam_pos_world.Z - cam->getOffset().Z * BS);
48         cam_pos_scene += look * sfNear;
49         cam_pos_world += look * sfNear;
50
51         // center point of light frustum
52         v3f center_scene = cam_pos_scene + look * 0.35 * (sfFar - sfNear);
53         v3f center_world = cam_pos_world + look * 0.35 * (sfFar - sfNear);
54
55         // Create a vector to the frustum far corner
56         const v3f &viewUp = cam->getCameraNode()->getUpVector();
57         v3f viewRight = look.crossProduct(viewUp);
58
59         v3f farCorner = (look + viewRight * tanFovX + viewUp * tanFovY).normalize();
60         // Compute the frustumBoundingSphere radius
61         v3f boundVec = (cam_pos_scene + farCorner * sfFar) - center_scene;
62         float radius = boundVec.getLength();
63         float length = radius * 3.0f;
64         v3f eye_displacement = direction * length;
65
66         // we must compute the viewmat with the position - the camera offset
67         // but the future_frustum position must be the actual world position
68         v3f eye = center_scene - eye_displacement;
69         future_frustum.player = cam_pos_scene;
70         future_frustum.position = center_world - eye_displacement;
71         future_frustum.length = length;
72         future_frustum.radius = radius;
73         future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, center_scene, v3f(0.0f, 1.0f, 0.0f));
74         future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(radius, radius, 
75                         0.0f, length, false);
76         future_frustum.camera_offset = cam->getOffset();
77 }
78
79 DirectionalLight::DirectionalLight(const u32 shadowMapResolution,
80                 const v3f &position, video::SColorf lightColor,
81                 f32 farValue) :
82                 diffuseColor(lightColor),
83                 farPlane(farValue), mapRes(shadowMapResolution), pos(position)
84 {}
85
86 void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool force)
87 {
88         if (dirty && !force)
89                 return;
90
91         float zNear = cam->getCameraNode()->getNearValue();
92         float zFar = getMaxFarValue();
93         if (!client->getEnv().getClientMap().getControl().range_all)
94                 zFar = MYMIN(zFar, client->getEnv().getClientMap().getControl().wanted_range * BS);
95
96         ///////////////////////////////////
97         // update splits near and fars
98         future_frustum.zNear = zNear;
99         future_frustum.zFar = zFar;
100
101         // update shadow frustum
102         createSplitMatrices(cam);
103         // get the draw list for shadows
104         client->getEnv().getClientMap().updateDrawListShadow(
105                         getPosition(), getDirection(), future_frustum.radius, future_frustum.length);
106         should_update_map_shadow = true;
107         dirty = true;
108
109         // when camera offset changes, adjust the current frustum view matrix to avoid flicker
110         v3s16 cam_offset = cam->getOffset();
111         if (cam_offset != shadow_frustum.camera_offset) {
112                 v3f rotated_offset;
113                 shadow_frustum.ViewMat.rotateVect(rotated_offset, intToFloat(cam_offset - shadow_frustum.camera_offset, BS));
114                 shadow_frustum.ViewMat.setTranslation(shadow_frustum.ViewMat.getTranslation() + rotated_offset);
115                 shadow_frustum.player += intToFloat(shadow_frustum.camera_offset - cam->getOffset(), BS);
116                 shadow_frustum.camera_offset = cam_offset;
117         }
118 }
119
120 void DirectionalLight::commitFrustum()
121 {
122         if (!dirty)
123                 return;
124
125         shadow_frustum = future_frustum;
126         dirty = false;
127 }
128
129 void DirectionalLight::setDirection(v3f dir)
130 {
131         direction = -dir;
132         direction.normalize();
133 }
134
135 v3f DirectionalLight::getPosition() const
136 {
137         return shadow_frustum.position;
138 }
139
140 v3f DirectionalLight::getPlayerPos() const
141 {
142         return shadow_frustum.player;
143 }
144
145 v3f DirectionalLight::getFuturePlayerPos() const
146 {
147         return future_frustum.player;
148 }
149
150 const m4f &DirectionalLight::getViewMatrix() const
151 {
152         return shadow_frustum.ViewMat;
153 }
154
155 const m4f &DirectionalLight::getProjectionMatrix() const
156 {
157         return shadow_frustum.ProjOrthMat;
158 }
159
160 const m4f &DirectionalLight::getFutureViewMatrix() const
161 {
162         return future_frustum.ViewMat;
163 }
164
165 const m4f &DirectionalLight::getFutureProjectionMatrix() const
166 {
167         return future_frustum.ProjOrthMat;
168 }
169
170 m4f DirectionalLight::getViewProjMatrix()
171 {
172         return shadow_frustum.ProjOrthMat * shadow_frustum.ViewMat;
173 }