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): cast a climbing missile's shadow along the sun ray

Olcay Seygan3cb3ee6f7041
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a4bb696dd..d6db58dac 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1243,7 +1243,9 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same
deck shadows the deck alone, and the side of the bridge that faces away from the sun is in shade.
- Infantry shadows are built from the pose: arms, head, weapon, moving with him.
- `UseShadowVolumesForSkins = No` puts the old flat blobs back.
-- Scuds, rockets and falling bombs cast a shadow running along the ground.
+- Scuds, rockets and falling bombs cast a shadow running along the ground. A missile climbing
+ straight up throws a thin streak away from the sun now. It used to lay a short blob on the ground
+ that spun every frame, as if the missile were tumbling.
- An aircraft's shadow lands where the sun puts it, not underneath the aircraft. Every plane and helicopter in the game's own data asks for a sun "no lower than 89 degrees", so each one cast its shadow straight down and towed it along directly below itself at any height, while the tank beside it was lit by the map's real sun and threw its shadow off to the side. Aircraft now take the same sun as everything else: a helicopter's shadow slides out from under it as it climbs, and a plane's runs ahead of or behind it depending on the hour the map is set at. On a map lit by a very low sun the shadow is held to what a 30 degree sun would cast, so it does not stretch across half the screen. The flat blob shadows under missiles and bombs follow the same ray.
- A big smoke cloud darkens the ground under it and fades as it does.
- Smoke and dust stand in the sunlight. A column off a burning building throws a soft shadow as thick as the smoke is: a puff of dust barely tints the sand, a black plume darkens the ground, the tanks and the houses under it and the smoke behind it. The column itself is bright on the side facing the sun and goes dark on the far side, and a cloud drifting through a building's shadow greys over with everything else in there. A cloud used to cast a round patch straight down whichever way the sun stood, and took no shade from anything. Fire casts nothing, it is light. A machine that cannot run the newer picture keeps the round patch.
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DProjectedShadow.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DProjectedShadow.h
index c75f92912..fc103bf31 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DProjectedShadow.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DProjectedShadow.h
@@ -124,6 +124,7 @@ class W3DProjectedShadow : public Shadow
Bool m_allowWorldAlign; /// wrap shadow around world geometry - else align perpendicular to local z-axis.
Real m_decalOffsetU; /// texture coordinate offset so not centered at object origin.
Real m_decalOffsetV; /// texture coordinate offset so not centered at object origin.
+ Real m_decalCenterU; /// object-space X of the model's box centre, for a decal laid down the sun ray.
Int m_flags; /// custom rendering flags
virtual void release(void) {TheW3DProjectedShadowManager->removeShadow(this);} ///<release shadow from manager
};
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
index eaaacb144..60120a80b 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
@@ -133,6 +133,10 @@ int nShadowDecalVertsInBatch=0;
//darkness the multiplicative blob was asking for: shadow.dds is 45% grey at its centre with an
//alpha of about 0.85 there, and 1 - 0.85*160/255 is the same 45%.
#define DECAL_SHADOW_ALPHA 160
+//queueDecal lays the long axis of anything this high above the ground down the sun ray, and lets it
+//grow to at most this many times the decal's own length doing it.
+#define DECAL_AIRBORNE_HEIGHT 1.0f
+#define DECAL_MAX_STRETCH 4.0f
int SHADOW_DECAL_VERTEX_SIZE=32768;
int SHADOW_DECAL_INDEX_SIZE=65536;
@@ -940,12 +944,16 @@ void W3DProjectedShadowManager::queueDecal(W3DProjectedShadow *shadow)
Not for something on a bridge: that decal is already drawn at the bridge's own height, and
sliding it sideways would drop the shadow in the river. */
+ Bool sunCasts = FALSE;
+ Vector3 toSun(0.0f, 0.0f, 1.0f);
+ Real heightAboveGround = 0.0f;
if (layerHeight == 0.0f && TheW3DShadowManager != NULL && TheTerrainLogic != NULL)
{
- const Real heightAboveGround = objPos.Z - TheTerrainLogic->getGroundHeight(objPos.X, objPos.Y);
- const Vector3 &toSun = TheW3DShadowManager->getLightPosWorld(0);
+ heightAboveGround = objPos.Z - TheTerrainLogic->getGroundHeight(objPos.X, objPos.Y);
+ toSun = TheW3DShadowManager->getLightPosWorld(0);
const Real MIN_SUN_HEIGHT = 0.01f; // a sun on the horizon casts a shadow of infinite length
- if (heightAboveGround > 0.0f && toSun.Z > MIN_SUN_HEIGHT)
+ sunCasts = toSun.Z > MIN_SUN_HEIGHT;
+ if (heightAboveGround > 0.0f && sunCasts)
{
const Real alongRay = heightAboveGround / toSun.Z;
objPos.X -= toSun.X * alongRay;
@@ -957,6 +965,33 @@ void W3DProjectedShadowManager::queueDecal(W3DProjectedShadow *shadow)
uVector=objXform.Get_X_Vector();
+ /* The decal's long axis goes down the sun ray too, not straight down. Dropping the axis's
+ height and normalising what was left laid every shadow down as if its object were level: a
+ Scud climbing nose up threw a missile lying on its side, and since the level part of an
+ axis pointing at the sky is nothing but the locomotor's wobble, that lying missile swung
+ to a new heading every frame. Projected along the ray, a vertical missile throws the
+ streak a pole throws, pointing away from the sun and as long as the sun makes it, and the
+ shadow of the model's middle rather than of its tail is what sits at the decal's centre.
+ Only for something in the air: a unit on a slope is pitched as well, and its decal is a
+ footprint on that slope, not a shadow on a level floor. */
+ Real stretch = 1.0f;
+ if (sunCasts && heightAboveGround > DECAL_AIRBORNE_HEIGHT)
+ {
+ const Real axisLength = uVector.Length();
+ const Real down = uVector.Z / toSun.Z;
+ uVector.X -= toSun.X * down;
+ uVector.Y -= toSun.Y * down;
+ objPos.X -= toSun.X * down * shadow->m_decalCenterU;
+ objPos.Y -= toSun.Y * down * shadow->m_decalCenterU;
+ uVector.Z = 0.0f;
+ if (axisLength > 0.0f)
+ {
+ //never narrower than the decal is wide, unless it was that already
+ const Real minStretch = __min(1.0f, fabs(shadow->m_decalSizeY) / shadow->m_decalSizeX);
+ stretch = __max(__min(uVector.Length() / axisLength, DECAL_MAX_STRETCH), minStretch);
+ }
+ }
+
uVector.Z=0.0f;
vecLength=uVector.Length();
if (vecLength != 0.0f) //prevent divide by zero
@@ -980,7 +1015,7 @@ void W3DProjectedShadowManager::queueDecal(W3DProjectedShadow *shadow)
//Compute bounding box of projection
Vector3 boxCorners[4]; //top-left, top-right, bottom-right, bottom-left
- dx = shadow->m_decalSizeX;
+ dx = shadow->m_decalSizeX * stretch;
dy = shadow->m_decalSizeY;
Vector3 left_x=-dx * (uVector * (0.5f + shadow->m_decalOffsetU));
Vector3 right_x = dx * (uVector * (0.5f - shadow->m_decalOffsetU));
@@ -1004,7 +1039,7 @@ void W3DProjectedShadowManager::queueDecal(W3DProjectedShadow *shadow)
min_y = __min(min_y,boxCorners[bi].Y);
}
- uVector *= shadow->m_oowDecalSizeX;
+ uVector *= shadow->m_oowDecalSizeX / stretch;
vVector *= shadow->m_oowDecalSizeY;
uOffset = shadow->m_decalOffsetU + 0.5f;
vOffset = shadow->m_decalOffsetV + 0.5f;
@@ -2034,6 +2069,7 @@ W3DProjectedShadow* W3DProjectedShadowManager::addShadow(RenderObjClass *robj, S
shadow->m_decalOffsetU= decalOffsetX;
shadow->m_decalOffsetV= decalOffsetY;
+ shadow->m_decalCenterU= box.Center.X;
shadow->m_flags = allowSunDirection;
@@ -2294,6 +2330,7 @@ W3DProjectedShadow::W3DProjectedShadow(void)
m_lastObjPosition.Set(0,0,0);
m_type = SHADOW_NONE; /// type of projection
m_allowWorldAlign = FALSE; /// wrap shadow around world geometry - else align perpendicular to local z-axis.
+ m_decalCenterU = 0.0f;
m_isEnabled = TRUE;
m_isInvisibleEnabled = FALSE;
for (Int i=0; i<MAX_SHADOW_LIGHTS; i++)