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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(w3ddevice): fit the sun's box round the frame's ground
Olcay Seygan331686a2792a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1f2f20075..14e900fbd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -8,6 +8,11 @@ found and fixed, EA's own, not port damage.**---+## Shadows reach the far ground++- Pull the camera right out, tilt it flat, and shadows used to stop along a line across the far ground. On Twilight Flame a whole base stood past that line with its power plants, trees and tanks on bare lit earth, each with no shadow under it. They were drawn for 2400 units round the spot you were looking at, while the far corner of that view is 3343 away. The shadows now go round what is on the screen and reach the top of it.+- Zoomed right out at the usual tilt, the same shadow picture covers less empty ground, so a shadow's edge is about a fifth finer: 1.44 units a dot down to 1.12 on Tournament Plains. In a crowd battle at that zoom the slowest frames read 14.00 ms with the change and 14.12 without it, which is no difference at all.+## Sky above, ground below- Sky And Ground Light, under Options, Effects, is a new setting, off by default until it has been looked at on more maps. EA gave every model one flat ambient colour, so the shaded side of a tank was the same dull tone from deck to belly. Now a face looking up takes the map's ambient lifted and bluer, and a face looking down takes it lowered and warmer. By day it is quiet, since the sun does most of the lighting: a War Factory's roof reads 4% brighter and 8% bluer on Golden Oasis. On Winter Wolf that roof goes from cream to frost, 11% bluer, and on Dark Night it gains 8% while the ground beside it stays the same, so a night map stays a night map. In a battle of three armies and three superweapons the graphics card's frame reads 8.0 ms with it off and 8.15 with it on, less than two runs of one setting differ by. The ground and the batched trees keep their light, and so do the Direct3D 9 picture and Classic Graphics.diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/SunShadowCasterCull.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/SunShadowCasterCull.hindex 23aeb23f7..9cd730e65 100644--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/SunShadowCasterCull.h+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/SunShadowCasterCull.h@@ -34,6 +34,17 @@ inline bool SunShadowReachesTheFrame( const FrustumClass &view, const Vector3 &ureturn nearest.Length2() <= reach * reach;}+/** Where the sun's box sits on one axis of its own frame, the look point at zero and lo to hi the+ ground the frame shows, which holds the look point. The box goes round that stretch. When the+ stretch is wider than the box it stays as near the look point as it can with no texel past+ either end, so what falls off is the far ground and never the ground at the camera's feet. */+inline float SunBoxMiddle( float lo, float hi, float halfWidth )+{+ if (hi - lo <= 2.0f * halfWidth)+ return (lo + hi) * 0.5f;+ return WWMath::Clamp( 0.0f, lo + halfWidth, hi - halfWidth );+}+struct SunShadowCasterCull {const FrustumClass &View;Vector3 ToSun;diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cppindex 0d2b68716..1e13e0fb2 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp@@ -127,15 +127,21 @@ const Real cosAngleToCare = cos ((0.2 * PI) / 180.0); //1.5 degree difference// The sun's depth buffer and the box it covers. 2048 texels over 1800 world units is about one// texel to the metre at the game's own scale, which is finer than the stencil edge it replaces.-// The box is centred on what the tactical camera looks at and is never narrower than that; it-// opens to the ground the camera sees once a zoom shows more (shadowMapHalfWidth), because at-// full zoom-out the top of the screen is about 1100 units past the look point and the ground-// there had no shadow at all. The widest it goes is the cap below, 2.3 units a texel.+// The box goes round the ground the tactical camera sees and is never narrower than that; it opens+// once a zoom or a tilt shows more (shadowMapHalfWidth), because at full zoom-out the top of the+// screen is about 1100 units past the look point and the ground there had no shadow at all. The+// widest it goes is the cap below, 2.3 units a texel.#define SHADOW_MAP_TEXELS 2048#define SHADOW_MAP_HALF_WIDTH 900.0f#define SHADOW_MAP_WIDEST_HALF_WIDTH 2400.0f// The box grows in these steps, so the texel grid holds still while the zoom does.#define SHADOW_MAP_HALF_WIDTH_STEP 64.0f+// How far the box stays past the frame's ground: the filter at its widest reads about 16 units beside+// a pixel, and a read off the map's edge would smear its last texel along the frame's corner.+#define SHADOW_MAP_FIT_MARGIN 32.0f+// How far above the highest ground the frame's receivers reach: a roof at the bottom of the frame is+// nearer the camera than the ground under it, and past the box it would read as a lit strip.+#define SHADOW_MAP_RECEIVER_HEADROOM 150.0f#define SHADOW_MAP_SUN_DISTANCE 2000.0f#define SHADOW_MAP_NEAR_CLIP 10.0f#define SHADOW_MAP_FAR_CLIP 4000.0f@@ -4280,25 +4286,44 @@ static Vector3 viewEdgeAtHeight( const Vector3 &nearPoint, const Vector3 &farPoireturn 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. */+/** What the frame wants of the sun's box, for -shadowmapreport: half the width the box goes round, and+ what the same ground would ask of a box centred on the look point. */+static Real s_sunBoxWanted = 0.0f;+static Real s_sunBoxWantedCentred = 0.0f;++/** How wide the sun's box has to be to hold the ground the tactical camera sees, and where its middle+ goes. 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 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. The look point is nowhere near the middle of that shape, the far+ side of the frame reaching further than the near one, so a box centred on it spends texels on+ ground no pixel reads. The box goes round the shape instead. */static Real shadowMapHalfWidth( const CameraClass &sceneCamera, const Matrix3D &sunAtFocus, Real lowestGround,- Real highestGround )+ Real highestGround, Vector3 *middle ){const Vector3 *corners = sceneCamera.Get_Frustum().Corners;- Real needed = 0.0f;+ Real loX = 0.0f, hiX = 0.0f, loY = 0.0f, hiY = 0.0f;+ Real centred = 0.0f;for (Int i = 0; i < 8; ++i){const Vector3 ground = viewEdgeAtHeight( corners[ i & 3 ], corners[ (i & 3) + 4 ],- (i < 4) ? lowestGround : highestGround );+ (i < 4) ? lowestGround : highestGround + SHADOW_MAP_RECEIVER_HEADROOM );Vector3 inSun;Matrix3D::Inverse_Transform_Vector( sunAtFocus, ground, &inSun );- needed = WWMath::Max( needed, WWMath::Max( WWMath::Fabs( inSun.X ), WWMath::Fabs( inSun.Y ) ) );+ loX = WWMath::Min( loX, inSun.X );+ hiX = WWMath::Max( hiX, inSun.X );+ loY = WWMath::Min( loY, inSun.Y );+ hiY = WWMath::Max( hiY, inSun.Y );+ centred = WWMath::Max( centred, WWMath::Max( WWMath::Fabs( inSun.X ), WWMath::Fabs( inSun.Y ) ) );}+ loX -= SHADOW_MAP_FIT_MARGIN;+ hiX += SHADOW_MAP_FIT_MARGIN;+ loY -= SHADOW_MAP_FIT_MARGIN;+ hiY += SHADOW_MAP_FIT_MARGIN;+ const Real needed = WWMath::Max( hiX - loX, hiY - loY ) * 0.5f;+ s_sunBoxWanted = needed;+ s_sunBoxWantedCentred = centred;/* It grows at once and shrinks only a whole step late. The look point's own ground height movesthe footprint in the sun's frame as the camera scrolls over hills, and a box that followed itboth ways would hop across a step and back, and every shadow edge on the screen with it. */@@ -4308,6 +4333,7 @@ static Real shadowMapHalfWidth( const CameraClass &sceneCamera, const Matrix3D &const Real stepped = WWMath::Ceil( needed / SHADOW_MAP_HALF_WIDTH_STEP ) * SHADOW_MAP_HALF_WIDTH_STEP;held = WWMath::Clamp( stepped, SHADOW_MAP_HALF_WIDTH, SHADOW_MAP_WIDEST_HALF_WIDTH );}+ Matrix3D::Transform_Vector( sunAtFocus, Vector3( SunBoxMiddle( loX, hiX, held ), SunBoxMiddle( loY, hiY, held ), 0.0f ), middle );return held;}@@ -4348,7 +4374,7 @@ static void wholeMapSunBox( const Vector3 &toSun, Vector3 *focus, Real *halfWidt/** The sun's depth pass. The casters are the ones that cast a volume today, drawn again from thesun into a depth buffer nothing samples yet, so this phase can be proved on its own: with itoff the frame is what it was, and with it on the map has the world in it and the frame is- still what it was. The box is square and centred on what the tactical camera is looking at,+ still what it was. The box is square and goes round what the tactical camera can see,which is the ground the player can see; a cascade is only worth it once the box has to covermore than one view. SHADOW-MAP-PLAN.md phase 1. */void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )@@ -4421,6 +4447,7 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )toSun.Normalize();Vector3 focus;+ Vector3 boxMiddle;Real halfWidth;Real sunDistance = SHADOW_MAP_SUN_DISTANCE;Real farClip = SHADOW_MAP_FAR_CLIP;@@ -4430,6 +4457,8 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera ){wholeMapSunBox( toSun, &focus, &halfWidth, &sunDistance, &farClip );transform.Look_At( focus + toSun * sunDistance, focus, 0.0f );+ boxMiddle = focus;+ s_sunBoxWanted = s_sunBoxWantedCentred = halfWidth;}else{@@ -4437,8 +4466,12 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )TheTacticalView->getPosition( &look );focus.Set( look.x, look.y, TheTerrainRenderObject->getDrawHeight( look.x, look.y ) );transform.Look_At( focus + toSun * sunDistance, focus, 0.0f );+ Vector3 middle;halfWidth = shadowMapHalfWidth( sceneCamera, transform, lowestReceiver,- TheTerrainRenderObject->getMaxHeight() );+ TheTerrainRenderObject->getMaxHeight(), &middle );+ // Slid along the sun's own ray back to the look point's height, which moves nothing on the map+ // and keeps the box's depth, below, reckoned from the height it always was.+ boxMiddle = middle + toSun * ((focus.Z - middle.Z) / WWMath::Max( toSun.Z, 0.01f ));/* The box's depth has to hold the ground its width holds. Level ground a distance across thebox toward or away from the sun sits that distance times the sun's cotangent nearer or furtheralong the ray, so a box opened for the zoom with a low sun put the top of the screen past the@@ -4461,17 +4494,17 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )/* The box has to sit on whole texels of its own map, or every scroll of the camera slides thegrid under the world by a fraction of a texel and every shadow edge crawls and sparkles. The- look point is taken into the sun's own frame, rounded to the texel it lands in, and taken back+ box's middle is taken into the sun's own frame, rounded to the texel it lands in, and taken backout: the box then moves in texel steps and a shadow that did not move does not shimmer. Theframe it is rounded in has to stay put in the world. Rounded in the box's own frame, which is- centred on the look point, the look point is always at zero and rounds to itself: the grid+ centred on that middle, the middle is always at zero and rounds to itself: the gridslid on every scroll all the same. The sun's frame on the world's origin turns the same way,since only the sun's direction decides that, and holds still. */const Real texelWidth = (2.0f * halfWidth) / (Real)texels;Matrix3D sunAtOrigin;sunAtOrigin.Look_At( toSun, Vector3( 0.0f, 0.0f, 0.0f ), 0.0f );Vector3 focusInSun;- Matrix3D::Inverse_Transform_Vector( sunAtOrigin, focus, &focusInSun );+ Matrix3D::Inverse_Transform_Vector( sunAtOrigin, boxMiddle, &focusInSun );focusInSun.X = WWMath::Floor( focusInSun.X / texelWidth + 0.5f ) * texelWidth;focusInSun.Y = WWMath::Floor( focusInSun.Y / texelWidth + 0.5f ) * texelWidth;Vector3 snapped;@@ -4780,7 +4813,8 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )oldMeshes.concat(entry);}}- DEBUG_LOG(("SHADOWMAP: %d casters, %d left out as reaching nothing in view, %d light meshes left out, lean %d draws %d instances (%d alpha tested, %d skinned), old %u draws, old meshes:%s, %s\n",+ DEBUG_LOG(("SHADOWMAP: box half width %.0f, frame wants %.0f (%.0f centred on the look point), %.2f units a texel, %d casters, %d left out as reaching nothing in view, %d light meshes left out, lean %d draws %d instances (%d alpha tested, %d skinned), old %u draws, old meshes:%s, %s\n",+ halfWidth, s_sunBoxWanted, s_sunBoxWantedCentred, texelWidth,castersCounted, castersOutOfView, MeshClass::Shadow_Pass_Light_Meshes,DX8MeshRendererClass::Shadow_Lean_Draws, DX8MeshRendererClass::Shadow_Lean_Instances,DX8MeshRendererClass::Shadow_Lean_Alpha_Tested, DX8MeshRendererClass::Shadow_Lean_Skinned,diff --git a/GeneralsMD/Code/Tests/test_ww3d2.cpp b/GeneralsMD/Code/Tests/test_ww3d2.cppindex 0fce7d14c..466eef95b 100644--- a/GeneralsMD/Code/Tests/test_ww3d2.cpp+++ b/GeneralsMD/Code/Tests/test_ww3d2.cpp@@ -30,6 +30,7 @@#include "quat.h"#include "render2dsentence.h"#include "fullscreenfit.h"+#include "../GameEngineDevice/Include/W3DDevice/GameClient/SunShadowCasterCull.h"/** W3DMPO_GLUE gives every pooled class an operator new that routes through@@ -79,6 +80,27 @@ void freeFromW3DMemPool(void * /*pool*/, void *p)::operator delete(raw);}+/*=========================================================================+ SunBoxMiddle - the sun's box round the ground the frame shows+ =========================================================================*/++TEST(sun_box_goes_round_the_frames_ground)+{+ /* the far side reaches 1500 past the look point, the near side 500: a box 1000 each way fits */+ CHECK_EQ(SunBoxMiddle(-500.0f, 1500.0f, 1000.0f), 500.0f);+ /* a box wider than the ground stays in the middle of it */+ CHECK_EQ(SunBoxMiddle(-500.0f, 1500.0f, 2400.0f), 500.0f);+}++TEST(sun_box_past_its_cap_keeps_the_ground_at_the_cameras_feet)+{+ /* 5500 of ground under a box 4800 wide: the near end stays in and the far 700 falls off */+ CHECK_EQ(SunBoxMiddle(-500.0f, 5000.0f, 2400.0f), 1900.0f);+ CHECK_EQ(SunBoxMiddle(-5000.0f, 500.0f, 2400.0f), -1900.0f);+ /* with ground far past both sides it sits on the look point */+ CHECK_EQ(SunBoxMiddle(-3000.0f, 3000.0f, 2400.0f), 0.0f);+}+/*=========================================================================fullscreenfit - where a scaled fullscreen picture goes on its monitor=========================================================================*/