]> git.lizzy.rs Git - minetest.git/blob - src/client/shadows/dynamicshadows.cpp
Distribute shadow map update over multiple frames to reduce stutter (#11422)
[minetest.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         float radius;
33         v3f newCenter;
34         v3f look = cam->getDirection();
35
36         // camera view tangents
37         float tanFovY = tanf(cam->getFovY() * 0.5f);
38         float tanFovX = tanf(cam->getFovX() * 0.5f);
39
40         // adjusted frustum boundaries
41         float sfNear = future_frustum.zNear;
42         float sfFar = adjustDist(future_frustum.zFar, cam->getFovY());
43
44         // adjusted camera positions
45         v3f camPos2 = cam->getPosition();
46         v3f camPos = v3f(camPos2.X - cam->getOffset().X * BS,
47                         camPos2.Y - cam->getOffset().Y * BS,
48                         camPos2.Z - cam->getOffset().Z * BS);
49         camPos += look * sfNear;
50         camPos2 += look * sfNear;
51
52         // center point of light frustum
53         float end = sfNear + sfFar;
54         newCenter = camPos + look * (sfNear + 0.05f * end);
55         v3f world_center = camPos2 + look * (sfNear + 0.05f * end);
56
57         // Create a vector to the frustum far corner
58         const v3f &viewUp = cam->getCameraNode()->getUpVector();
59         v3f viewRight = look.crossProduct(viewUp);
60
61         v3f farCorner = look + viewRight * tanFovX + viewUp * tanFovY;
62         // Compute the frustumBoundingSphere radius
63         v3f boundVec = (camPos + farCorner * sfFar) - newCenter;
64         radius = boundVec.getLength() * 2.0f;
65         // boundVec.getLength();
66         float vvolume = radius * 2.0f;
67
68         float texelsPerUnit = getMapResolution() / vvolume;
69         m4f mTexelScaling;
70         mTexelScaling.setScale(texelsPerUnit);
71
72         m4f mLookAt, mLookAtInv;
73
74         mLookAt.buildCameraLookAtMatrixLH(v3f(0.0f, 0.0f, 0.0f), -direction, v3f(0.0f, 1.0f, 0.0f));
75
76         mLookAt *= mTexelScaling;
77         mLookAtInv = mLookAt;
78         mLookAtInv.makeInverse();
79
80         v3f frustumCenter = newCenter;
81         mLookAt.transformVect(frustumCenter);
82         frustumCenter.X = floorf(frustumCenter.X); // clamp to texel increment
83         frustumCenter.Y = floorf(frustumCenter.Y); // clamp to texel increment
84         frustumCenter.Z = floorf(frustumCenter.Z);
85         mLookAtInv.transformVect(frustumCenter);
86         // probar radius multipliacdor en funcion del I, a menor I mas multiplicador
87         v3f eye_displacement = direction * vvolume;
88
89         // we must compute the viewmat with the position - the camera offset
90         // but the future_frustum position must be the actual world position
91         v3f eye = frustumCenter - eye_displacement;
92         future_frustum.position = world_center - eye_displacement;
93         future_frustum.length = vvolume;
94         future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, frustumCenter, v3f(0.0f, 1.0f, 0.0f));
95         future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(future_frustum.length,
96                         future_frustum.length, -future_frustum.length,
97                         future_frustum.length,false);
98         future_frustum.camera_offset = cam->getOffset();
99 }
100
101 DirectionalLight::DirectionalLight(const u32 shadowMapResolution,
102                 const v3f &position, video::SColorf lightColor,
103                 f32 farValue) :
104                 diffuseColor(lightColor),
105                 farPlane(farValue), mapRes(shadowMapResolution), pos(position)
106 {}
107
108 void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool force)
109 {
110         if (dirty && !force)
111                 return;
112
113         float zNear = cam->getCameraNode()->getNearValue();
114         float zFar = getMaxFarValue();
115
116         ///////////////////////////////////
117         // update splits near and fars
118         future_frustum.zNear = zNear;
119         future_frustum.zFar = zFar;
120
121         // update shadow frustum
122         createSplitMatrices(cam);
123         // get the draw list for shadows
124         client->getEnv().getClientMap().updateDrawListShadow(
125                         getPosition(), getDirection(), future_frustum.length);
126         should_update_map_shadow = true;
127         dirty = true;
128
129         // when camera offset changes, adjust the current frustum view matrix to avoid flicker
130         v3s16 cam_offset = cam->getOffset();
131         if (cam_offset != shadow_frustum.camera_offset) {
132                 v3f rotated_offset;
133                 shadow_frustum.ViewMat.rotateVect(rotated_offset, intToFloat(cam_offset - shadow_frustum.camera_offset, BS));
134                 shadow_frustum.ViewMat.setTranslation(shadow_frustum.ViewMat.getTranslation() + rotated_offset);
135                 shadow_frustum.camera_offset = cam_offset;
136         }
137 }
138
139 void DirectionalLight::commitFrustum()
140 {
141         if (!dirty)
142                 return;
143
144         shadow_frustum = future_frustum;
145         dirty = false;
146 }
147
148 void DirectionalLight::setDirection(v3f dir)
149 {
150         direction = -dir;
151         direction.normalize();
152 }
153
154 v3f DirectionalLight::getPosition() const
155 {
156         return shadow_frustum.position;
157 }
158
159 const m4f &DirectionalLight::getViewMatrix() const
160 {
161         return shadow_frustum.ViewMat;
162 }
163
164 const m4f &DirectionalLight::getProjectionMatrix() const
165 {
166         return shadow_frustum.ProjOrthMat;
167 }
168
169 const m4f &DirectionalLight::getFutureViewMatrix() const
170 {
171         return future_frustum.ViewMat;
172 }
173
174 const m4f &DirectionalLight::getFutureProjectionMatrix() const
175 {
176         return future_frustum.ProjOrthMat;
177 }
178
179 m4f DirectionalLight::getViewProjMatrix()
180 {
181         return shadow_frustum.ProjOrthMat * shadow_frustum.ViewMat;
182 }