Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(w3ddevice): keep casting shadows from casters just off screen

Olcay Seygan5c1774cc5550
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4607b776a..7ff724bd5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1236,6 +1236,11 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same
lies well outside it, so buildings, trees and tanks up there stood on bright ground with nothing
under them. The square now opens to whatever the camera sees, up to two and a half times as wide.
At a map's normal playing height nothing changes.
+- A shadow stays on the ground when the thing casting it is just off the edge of the screen. A tree
+ standing past the edge was dropped along with its shadow, so scrolling made shadows on the ground
+ in view go out all at once; on Tournament Desert about eleven trees at a time were missing that
+ way. Buildings and units on the older renderer did the same when they stood just past the edge.
+ Now anything whose shadow can land in view keeps casting it.
- A building stands on the ground instead of sitting on top of it. There is a soft patch under every
structure's footprint, the darkening a corner traps in daylight, and the shadow it casts starts at
its own wall rather than a step away from it. The frame's corners and creases get the same
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShadow.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShadow.h
index 0e24029e7..2575f677c 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShadow.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShadow.h
@@ -73,4 +73,9 @@ protected:
extern W3DShadowManager *TheW3DShadowManager;
+class FrustumClass;
+class SphereClass;
+Bool shadowCanReachView( const FrustumClass &view, const SphereClass &body, Real groundZ, Real runX, Real runY ); ///< whether a body's shadow, running runX, runY sideways per unit of drop down to groundZ, can reach the view
+Bool volumeShadowCanReachView( const FrustumClass &view, const SphereClass &body, Real groundZ ); ///< the same along the ray from the shadow light's position
+
#endif //__W3DSHADOW_H_
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DShadow.cpp
index c59c389cf..bdd8297f3 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DShadow.cpp
@@ -66,6 +66,39 @@ Vector3 LightPosWorld[ MAX_SHADOW_LIGHTS ] =
Vector3( 94.0161f, 50.499f, 200.0f)
};
+/** Whether anything a body casts can land in the view. The body is its bounding sphere and its
+ shadow is that sphere swept down to groundZ, the lowest the shadow can land, moving runX, runY
+ sideways for every unit it drops: the sun's own slope, or whatever the caller's shadow is held
+ to. Only the four sides of the view are tested, which is all a shadow lying on the ground
+ needs, and each on its own, so a capsule past a corner of the view can pass when it misses it:
+ the test can let in a caster that reaches nothing, never leave out one that does. A caster
+ just off the edge of the screen whose shadow falls onto it passes, so its shadow does not go
+ out the moment its body leaves the frame. */
+Bool shadowCanReachView( const FrustumClass &view, const SphereClass &body, Real groundZ, Real runX, Real runY )
+{
+ const Real drop = WWMath::Max( body.Center.Z - groundZ + body.Radius, 0.0f );
+ const Vector3 downwind = body.Center + Vector3( runX, runY, -1.0f ) * drop;
+ for (Int side = 1; side <= 4; ++side)
+ {
+ const PlaneClass &plane = view.Planes[ side ];
+ if (Vector3::Dot_Product( plane.N, body.Center ) - plane.D > body.Radius
+ && Vector3::Dot_Product( plane.N, downwind ) - plane.D > body.Radius)
+ return FALSE;
+ }
+ return TRUE;
+}
+
+/** The same for a stencil volume, which is pushed away from the light's position rather than along
+ one direction for the whole map: from a caster in the far corner the light stands some way off
+ the direction it has from the map's origin. */
+Bool volumeShadowCanReachView( const FrustumClass &view, const SphereClass &body, Real groundZ )
+{
+ const Vector3 toLight = TheW3DShadowManager->getLightPosWorld( 0 ) - body.Center;
+ if (toLight.Z <= 0.01f * toLight.Length())
+ return TRUE; // a light at the horizon throws every shadow across the whole map
+ return shadowCanReachView( view, body, groundZ, -toLight.X / toLight.Z, -toLight.Y / toLight.Z );
+}
+
DECLARE_PERF_TIMER(shadowsRender)
void DoShadows(RenderInfoClass & rinfo, Bool stencilPass)
{
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index 5d3189384..ae146df11 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -2108,8 +2108,11 @@ void W3DVolumetricShadow::Update()
if (WWMath::Fabs(pos.X - bcX) > (beX + extent) ||
WWMath::Fabs(pos.Y - bcY) > (beY + extent) ||
WWMath::Fabs(pos.Z - bcZ) > (beZ + extent))
- return; //shadow can't be visible so no point in updating.
-
+ {
+ if (!volumeShadowCanReachView( *shadowCameraFrustum, m_robj->Get_Bounding_Sphere(), TheTerrainRenderObject->getMinHeight() ))
+ return; //shadow can't be visible so no point in updating.
+ }
+
//this unit is above ground, extend shadow volume to reach lowest point on the terrain plus extra bit to make
//sure shadow goes under ground.
updateVolumes(fabs(pos.Z - TheTerrainRenderObject->getMinHeight()) + SHADOW_EXTRUSION_BUFFER);
@@ -2118,10 +2121,17 @@ void W3DVolumetricShadow::Update()
{ //normal object that is not floating above ground so we don't need to extend the shadow lower than the object's
//base since it should be sitting directly at ground level.
+ /* The box around the visible terrain plus the caster's own radius is quick, and it is not
+ enough: a building's shadow under a low sun runs further than the building is wide, and one
+ standing just past that box laid its shadow in view and lost it all at once. What fails
+ the box gets the slower question, whether its shadow can reach the view at all. */
if (WWMath::Fabs(pos.X - bcX) > (beX + m_robjExtent) ||
WWMath::Fabs(pos.Y - bcY) > (beY + m_robjExtent) ||
WWMath::Fabs(pos.Z - bcZ) > (beZ + m_robjExtent))
- return; //shadow can't be visible so no point in updating.
+ {
+ if (!volumeShadowCanReachView( *shadowCameraFrustum, m_robj->Get_Bounding_Sphere(), TheTerrainRenderObject->getMinHeight() ))
+ return; //shadow can't be visible so no point in updating.
+ }
//check if this object has never had it's extrusion length updated. Will only be true for
//immobile objects because finding an optimal extrusion length is expensive.
@@ -4079,22 +4089,32 @@ public:
}
};
-/** How wide the sun's box has to be to hold the ground the tactical camera sees. Each edge of the
- view is followed from the near plane to where it meets the lowest ground on the map, or to the
- far plane if it never does, and the four points are taken into the sun's frame around the look
- point: the box is as wide as the furthest of them, rounded up to a step. Ground higher than the
- lowest is nearer the camera along the same edge and so inside already. */
-static Real shadowMapHalfWidth( const CameraClass &sceneCamera, const Matrix3D &sunAtFocus, Real lowestGround )
+/** Where an edge of the view crosses the level z, held to the stretch between its near and far
+ planes. */
+static Vector3 viewEdgeAtHeight( const Vector3 &nearPoint, const Vector3 &farPoint, Real z )
+{
+ if (nearPoint.Z <= z)
+ return nearPoint;
+ if (farPoint.Z >= z)
+ return farPoint;
+ return nearPoint + (farPoint - nearPoint) * ((nearPoint.Z - z) / (nearPoint.Z - farPoint.Z));
+}
+
+/** How wide the sun's box has to be to hold the ground the tactical camera sees. Every ground point
+ in view lies on an edge-bounded stretch of the view between the highest and the lowest ground on
+ the map, so each edge is cut at both heights and the eight points are taken into the sun's frame
+ around the look point: the box is as wide as the furthest of them, rounded up to a step. The
+ lowest level alone would miss a hill at the bottom of the screen, whose point sits much nearer
+ the camera and can land outside all four low ones in the sun's frame. */
+static Real shadowMapHalfWidth( const CameraClass &sceneCamera, const Matrix3D &sunAtFocus, Real lowestGround,
+ Real highestGround )
{
const Vector3 *corners = sceneCamera.Get_Frustum().Corners;
Real needed = 0.0f;
- for (Int i = 0; i < 4; ++i)
+ for (Int i = 0; i < 8; ++i)
{
- const Vector3 &nearPoint = corners[ i ];
- const Vector3 &farPoint = corners[ i + 4 ];
- Vector3 ground = farPoint;
- if (farPoint.Z < lowestGround)
- ground = nearPoint + (farPoint - nearPoint) * ((nearPoint.Z - lowestGround) / (nearPoint.Z - farPoint.Z));
+ const Vector3 ground = viewEdgeAtHeight( corners[ i & 3 ], corners[ (i & 3) + 4 ],
+ (i < 4) ? lowestGround : highestGround );
Vector3 inSun;
Matrix3D::Inverse_Transform_Vector( sunAtFocus, ground, &inSun );
needed = WWMath::Max( needed, WWMath::Max( WWMath::Fabs( inSun.X ), WWMath::Fabs( inSun.Y ) ) );
@@ -4153,7 +4173,8 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
transform.Look_At( focus + toSun * SHADOW_MAP_SUN_DISTANCE, focus, 0.0f );
const Real lowestReceiver = TheTerrainRenderObject->getMinHeight();
- const Real halfWidth = shadowMapHalfWidth( sceneCamera, transform, lowestReceiver );
+ const Real halfWidth = shadowMapHalfWidth( sceneCamera, transform, lowestReceiver,
+ TheTerrainRenderObject->getMaxHeight() );
CameraClass sun;
sun.Set_Projection_Type( CameraClass::ORTHO );
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
index bdfa83598..200519de3 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
@@ -298,6 +298,41 @@ static ShaderClass modelShadowShader(SC_MODEL_SHADOW);
// knows nothing about. Cap the stretch at this many tree heights.
#define TREE_SHADOW_MAX_STRETCH 3.0f
+// The sun, as how far a tree's shadow reaches sideways for every unit of height. Both kinds of
+// tree shadow are the same shear about the ground, and this is the number it shears by. FALSE
+// with no sun high enough to cast one.
+static Bool treeShadowStretch(Real &stretchX, Real &stretchY)
+{
+ stretchX = 0.0f;
+ stretchY = 0.0f;
+ if (TheW3DShadowManager == NULL) {
+ return false;
+ }
+ Vector3 &sunPos = TheW3DShadowManager->getLightPosWorld(0);
+ if (sunPos.Z <= 1.0f) {
+ return false;
+ }
+ stretchX = -sunPos.X / sunPos.Z;
+ stretchY = -sunPos.Y / sunPos.Z;
+ Real stretchSq = stretchX*stretchX + stretchY*stretchY;
+ if (stretchSq > TREE_SHADOW_MAX_STRETCH*TREE_SHADOW_MAX_STRETCH) {
+ Real scale = TREE_SHADOW_MAX_STRETCH / WWMath::Sqrt(stretchSq);
+ stretchX *= scale;
+ stretchY *= scale;
+ }
+ return true;
+}
+
+// Shadows are wanted under either option. The silhouette pass needs the vertex shader, since
+// flattening the tree onto the ground is the shader's own sway arithmetic with different constants.
+// Hardware without one keeps the blob, and so does classic graphics, because the blob is what every
+// tree had in 2003.
+static Bool treeSilhouetteShadows(IDirect3DVertexShader9 *treeVertexShader)
+{
+ return (TheGlobalData->m_useShadowDecals || TheGlobalData->m_useShadowVolumes) && treeVertexShader != 0
+ && TheW3DShadowManager != NULL && !TheGlobalData->m_classicGraphics;
+}
+
/*
#define SC_ALPHA_DETAIL ( SHADE_CNST(ShaderClass::PASS_LEQUAL, ShaderClass::DEPTH_WRITE_ENABLE, ShaderClass::COLOR_WRITE_ENABLE, ShaderClass::SRCBLEND_ONE, \
@@ -355,9 +390,18 @@ void W3DTreeBuffer::cull(const CameraClass * camera)
float z = zmod * camera_matrix[2][2] ;
m_cameraLookAtVector.Set(x,y,z);
+ /* A tree's silhouette shadow is drawn out of the same batch as the tree, so a tree culled by its
+ own outline took its shadow with it: the shadow of a tree just past the edge of the screen lay
+ on the ground in view and went out all at once. Where that shadow is drawn, a tree whose
+ shadow can reach the view is in the batch too. It lies flat at the tree's base. */
+ Real stretchX, stretchY;
+ const Bool castsSilhouette = treeSilhouetteShadows(m_dwTreeVertexShader) && treeShadowStretch(stretchX, stretchY);
for (curTree=0; curTree<m_numTrees; curTree++) {
Bool doKey = false; // We calculate the key when a tree becomes visible.
Bool visible = !camera->Cull_Sphere(m_trees[curTree].bounds);
+ if (!visible && castsSilhouette)
+ visible = shadowCanReachView(camera->Get_Frustum(), m_trees[curTree].bounds, m_trees[curTree].location.Z,
+ stretchX, stretchY);
if (visible != m_trees[curTree].visible) {
m_trees[curTree].visible=visible;
m_anythingChanged = true;
@@ -1822,6 +1866,12 @@ void W3DTreeBuffer::drawModelShadows(CameraClass *camera, Real stretchX, Real st
ms.visible = true;
}
}
+ //and when its shadow can reach the screen, not only its trunk: one standing just past the edge
+ //lays its shadow in view, and culling by the trunk took that out all at once
+ for (k=0; k<ms.numMesh && !ms.visible; k++) {
+ ms.visible = shadowCanReachView(camera->Get_Frustum(), ms.mesh[k]->Get_Bounding_Sphere(), ms.baseZ,
+ stretchX, stretchY);
+ }
if (ms.visible) {
numVisible++;
}
@@ -2111,11 +2161,7 @@ void W3DTreeBuffer::drawTrees(CameraClass * camera, RefRenderObjListIterator *pD
// Shadows are wanted under either option: the loop used to be gated on m_useShadowDecals alone,
// so with volume shadows picked trees were the one thing in the scene standing on nothing.
Bool drawShadows = TheGlobalData->m_useShadowDecals || TheGlobalData->m_useShadowVolumes;
- // The silhouette pass needs the vertex shader, since flattening the tree onto the ground is the
- // shader's own sway arithmetic with different constants. Hardware without one keeps the blob,
- // and so does classic graphics, because the blob is what every tree had in 2003.
- Bool silhouetteShadows = drawShadows && m_dwTreeVertexShader != 0 && TheW3DShadowManager != NULL &&
- !TheGlobalData->m_classicGraphics;
+ Bool silhouetteShadows = treeSilhouetteShadows(m_dwTreeVertexShader);
// Draw tree shadows.
// Trees are batched vertices with no render object of their own, so the one shared blob decal
@@ -2252,27 +2298,8 @@ void W3DTreeBuffer::drawTrees(CameraClass * camera, RefRenderObjListIterator *pD
#endif
- //
- // The sun, as how far a shadow reaches sideways for every unit of height. Both kinds of tree
- // shadow are the same shear about the ground, and this is the number it shears by.
- //
- Real stretchX = 0.0f;
- Real stretchY = 0.0f;
- Bool haveSun = false;
- if (TheW3DShadowManager) {
- Vector3 &sunPos = TheW3DShadowManager->getLightPosWorld(0);
- if (sunPos.Z > 1.0f) {
- stretchX = -sunPos.X / sunPos.Z;
- stretchY = -sunPos.Y / sunPos.Z;
- Real stretchSq = stretchX*stretchX + stretchY*stretchY;
- if (stretchSq > TREE_SHADOW_MAX_STRETCH*TREE_SHADOW_MAX_STRETCH) {
- Real scale = TREE_SHADOW_MAX_STRETCH / WWMath::Sqrt(stretchSq);
- stretchX *= scale;
- stretchY *= scale;
- }
- haveSun = true;
- }
- }
+ Real stretchX, stretchY;
+ Bool haveSun = treeShadowStretch(stretchX, stretchY);
//
// The trees the map placed as objects are not in this buffer, so they are drawn whether it has