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