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.
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"
28 using m4f = core::matrix4;
30 void DirectionalLight::createSplitMatrices(const Camera *cam)
34 v3f look = cam->getDirection();
36 // camera view tangents
37 float tanFovY = tanf(cam->getFovY() * 0.5f);
38 float tanFovX = tanf(cam->getFovX() * 0.5f);
40 // adjusted frustum boundaries
41 float sfNear = future_frustum.zNear;
42 float sfFar = adjustDist(future_frustum.zFar, cam->getFovY());
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;
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);
57 // Create a vector to the frustum far corner
58 const v3f &viewUp = cam->getCameraNode()->getUpVector();
59 v3f viewRight = look.crossProduct(viewUp);
61 v3f farCorner = (look + viewRight * tanFovX + viewUp * tanFovY).normalize();
62 // Compute the frustumBoundingSphere radius
63 v3f boundVec = (camPos + farCorner * sfFar) - newCenter;
64 radius = boundVec.getLength();
65 // boundVec.getLength();
66 float vvolume = radius;
67 v3f frustumCenter = newCenter;
68 v3f eye_displacement = direction * vvolume;
70 // we must compute the viewmat with the position - the camera offset
71 // but the future_frustum position must be the actual world position
72 v3f eye = frustumCenter - eye_displacement;
73 future_frustum.position = world_center - eye_displacement;
74 future_frustum.length = vvolume;
75 future_frustum.ViewMat.buildCameraLookAtMatrixLH(eye, frustumCenter, v3f(0.0f, 1.0f, 0.0f));
76 future_frustum.ProjOrthMat.buildProjectionMatrixOrthoLH(future_frustum.length,
77 future_frustum.length, -future_frustum.length,
78 future_frustum.length,false);
79 future_frustum.camera_offset = cam->getOffset();
82 DirectionalLight::DirectionalLight(const u32 shadowMapResolution,
83 const v3f &position, video::SColorf lightColor,
85 diffuseColor(lightColor),
86 farPlane(farValue), mapRes(shadowMapResolution), pos(position)
89 void DirectionalLight::update_frustum(const Camera *cam, Client *client, bool force)
94 float zNear = cam->getCameraNode()->getNearValue();
95 float zFar = getMaxFarValue();
97 ///////////////////////////////////
98 // update splits near and fars
99 future_frustum.zNear = zNear;
100 future_frustum.zFar = zFar;
102 // update shadow frustum
103 createSplitMatrices(cam);
104 // get the draw list for shadows
105 client->getEnv().getClientMap().updateDrawListShadow(
106 getPosition(), getDirection(), future_frustum.length);
107 should_update_map_shadow = true;
110 // when camera offset changes, adjust the current frustum view matrix to avoid flicker
111 v3s16 cam_offset = cam->getOffset();
112 if (cam_offset != shadow_frustum.camera_offset) {
114 shadow_frustum.ViewMat.rotateVect(rotated_offset, intToFloat(cam_offset - shadow_frustum.camera_offset, BS));
115 shadow_frustum.ViewMat.setTranslation(shadow_frustum.ViewMat.getTranslation() + rotated_offset);
116 shadow_frustum.camera_offset = cam_offset;
120 void DirectionalLight::commitFrustum()
125 shadow_frustum = future_frustum;
129 void DirectionalLight::setDirection(v3f dir)
132 direction.normalize();
135 v3f DirectionalLight::getPosition() const
137 return shadow_frustum.position;
140 const m4f &DirectionalLight::getViewMatrix() const
142 return shadow_frustum.ViewMat;
145 const m4f &DirectionalLight::getProjectionMatrix() const
147 return shadow_frustum.ProjOrthMat;
150 const m4f &DirectionalLight::getFutureViewMatrix() const
152 return future_frustum.ViewMat;
155 const m4f &DirectionalLight::getFutureProjectionMatrix() const
157 return future_frustum.ProjOrthMat;
160 m4f DirectionalLight::getViewProjMatrix()
162 return shadow_frustum.ProjOrthMat * shadow_frustum.ViewMat;