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.

feat(w3ddevice): trees cast their real shape into the d3d11 sun map

Olcay Seygan105a831fbfac
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d1c585c0a..5b02b449a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,38 +8,18 @@ found and fixed — EA's own, not port damage.**
---
-## Sun and smoke shadow parity on Linux
+## Trees cast the shadow of their own crowns
-- Linux draws mapped sun and smoke shadows across terrain, buildings, units,
- trees and bridges. Sorted cloaked casters retain their partial coverage.
-- Existing shadow options remain authoritative. Unavailable resources retain
- legacy shadows; legacy water and reflections keep their existing paths.
-- Windows and macOS keep their existing renderer behavior. See
- [the Linux shadow guide](docs/sdl-sun-shadows.md) for diagnostics and limits.
+- A tree's shadow used to be its outline laid flat at the foot of the trunk, which sank into the ground on a slope and never darkened a tank parked under it. Now every tree, the palms included, puts its real leaves into the sun's shadow, gaps between the fronds and all, and that shadow falls over the hillside and over the units standing in the wood. Direct3D 11 on Windows and Linux both draw it; the Direct3D 9 renderer keeps the old flat shadows.
+- Linux gets the sun's shadows Windows already had, soft under a helicopter and hard under a tank, and the smoke shadow that darkens a battlefield under a thick plume. Your own cloaked units still cast only a faint part of theirs.
## Modern water on Linux
-- Modern is an opt-in under Options > Water style; Classic stays the default.
- Water rolls in waves with sunlight following the crests into the shallows.
- Shores redirect waves along their banks and gather foam; vessels leave wakes.
-- Modern water preserves map-authored depth and opacity, matches material rules
- to the effective water color and texture, and adds pollution and bubbles only
- to matching water.
-- A standalone editor uses the game's water renderer and material rules. Invalid
- saved colors leave the current colors intact; default exports protect the
- loaded rule file. Explicit export destinations remain available.
-
-## Volumetric hazard clouds
-
-- Options > Graphics has a Cloud style choice, Classic or Volumetric, that switches live.
- Classic, the original toxin and radiation effects, stays the default; Volumetric is an
- opt-in while its cost in a crowded battle is measured.
-- Volumetric draws the toxin, beta, gamma and nuclear fields as moving smoke with the
- hazards' actual radii and lifetimes. Low smoke follows the terrain and stirs around
- visible infantry; nuclear fields fade in across their whole area with darker billows,
- rising embers and heat shimmer. Gameplay damage and simulation are unchanged.
-- It needs a renderer that supports it, today the SDL GPU renderer on Linux and macOS.
- Elsewhere the choice is hidden. A field it cannot admit keeps the Classic effect.
+- Options has a Water Style box. Modern rolls the sea in waves whose crests catch the sun all the way into the shallows, and at the shore they turn along the bank and pile up foam. Boats push foam off their hulls. Classic stays the default, and either way a map keeps the water colour and depth its maker gave it. Linux only for now.
+
+## Volumetric hazard clouds on Linux and macOS
+
+- Options has a Cloud Style box. Volumetric draws toxin, anthrax and nuclear fields as moving smoke at the size and for the time the hazard really lasts: low smoke hugs the ground and curls around infantry walking through it, and a nuclear field fades in across its whole width with dark billows and embers rising through the heat shimmer. The damage is exactly what it was. Classic stays the default until the cost in a big battle is measured, and on Windows the box is hidden.
## A rank ladder for every army
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.h
index 29ee22693..5bf29ff17 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.h
@@ -202,7 +202,7 @@ public:
void stepTrees(void); ///< W3DTreeBuffer::stepTrees, at the render's sync point
void takeWaypointsForRender(void); ///< W3DWaypointBuffer::takeReadsForRender, at the render's sync point
void renderTrees(CameraClass * camera); ///< renders the tree buffer.
- void renderTreeShadowCasters(RenderInfoClass &sunInfo, CameraClass *sceneCamera, const SunShadowCasterCull &cull);
+ Int renderTreeShadowCasters(RenderInfoClass &sunInfo, CameraClass *sceneCamera, const SunShadowCasterCull &cull); ///< the trees drawn into the sun's map, counted
void addProp(Int id, Coord3D location, Real angle, Real scale, const AsciiString &modelName);
void removeProp(Int id);
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.h
index fa23ff1ec..d667d747e 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.h
@@ -227,7 +227,8 @@ public:
void drawTrees(CameraClass * camera, RefRenderObjListIterator *pDynamicLightsIterator,
const CameraClass *sunCamera = NULL);
void stepTrees(void); ///< the toppling and pushed trees, once a logic frame, at the render's sync point
- void drawModelShadowCasters(RenderInfoClass &sunInfo, const SunShadowCasterCull &cull);
+ Int drawModelShadowCasters(RenderInfoClass &sunInfo, const SunShadowCasterCull &cull); ///< the object trees drawn into the sun's map
+ Int getSunCasterTrees(void) const {return m_sunCasterTrees;} ///< batched trees the last sun pass drew
/// Called when the view changes, and sort key needs to be recalculated.
/// Normally sortKey gets calculated when a tree becomes visible.
void doFullUpdate(void) {m_updateAllKeys = true;};
@@ -266,6 +267,7 @@ private:
struct ShadowRange { Int first, count; };
std::vector<ShadowRange> m_shadowRanges[MAX_BUFFERS];
Bool m_lastShadowsForProps = FALSE;
+ Int m_sunCasterTrees = 0;
IDirect3DPixelShader9 * m_dwTreePixelShader; ///<D3D pixel shader
IDirect3DVertexShader9 * m_dwTreeVertexShader; ///<D3D vertex shader
IDirect3DVertexDeclaration9 * m_treeVertexDeclaration; ///<what D3D8 carried beside the shader
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp
index 53d863899..baad9ed6e 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp
@@ -2951,15 +2951,15 @@ flushVertexBuffer1:
/** Renders (draws) the trees. Since the trees are transparent, this has to be
called after flush. */
//=============================================================================
-void BaseHeightMapRenderObjClass::renderTreeShadowCasters(RenderInfoClass &sunInfo, CameraClass *sceneCamera, const SunShadowCasterCull &cull)
+Int BaseHeightMapRenderObjClass::renderTreeShadowCasters(RenderInfoClass &sunInfo, CameraClass *sceneCamera, const SunShadowCasterCull &cull)
{
- if (!m_map || !Scene || !m_treeBuffer) return;
+ if (!m_map || !Scene || !m_treeBuffer) return 0;
DX8Wrapper::Set_Transform(D3DTS_WORLD, Transform);
DX8Wrapper::Set_Material(m_vertexMaterialClass);
RTS3DScene *scene = (RTS3DScene *)Scene;
RefRenderObjListIterator lights(scene->getDynamicLights());
m_treeBuffer->drawTrees(sceneCamera, &lights, &sunInfo.Camera);
- m_treeBuffer->drawModelShadowCasters(sunInfo,cull);
+ return m_treeBuffer->getSunCasterTrees() + m_treeBuffer->drawModelShadowCasters(sunInfo,cull);
}
void BaseHeightMapRenderObjClass::renderTrees(CameraClass * camera)
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index 4b695c345..23b8a97ad 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -4178,7 +4178,11 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
{
// A failed, disabled or empty pass must not expose the previous view's shadow.
SunShadow::Clear(DX8Wrapper::_Get_D3D_Device());
+#if defined(__linux__)
+ // The SDL device reads the D3D9 state it was handed. Under Direct3D 11 nothing reads it back,
+ // and a flush here would send every held-back state to a device that draws nothing, every frame.
DX8Wrapper::Flush_Deferred_D3D9_State();
+#endif
// Trees historically cast under either shadow option. Smoke has its own option;
// it may keep an otherwise empty depth capture alive without enabling geometry.
#if defined(__linux__)
@@ -4408,10 +4412,8 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
sunInfo.drawEffect = DRAW_EFFECT_NONE;
// Use the trees actually present. The loader can override ShowTrees for multiplayer,
// and changing that preference does not remove trees from an already loaded map.
-#if defined(__linux__)
if (geometryShadows)
- TheTerrainRenderObject->renderTreeShadowCasters(sunInfo, &sceneCamera, casterCull);
-#endif
+ castersCounted += TheTerrainRenderObject->renderTreeShadowCasters(sunInfo, &sceneCamera, casterCull);
TheDX8MeshRenderer.Flush();
/* A mesh the material system calls translucent goes to the sort lists rather than to the mesh
renderer, and a helicopter's rotor disc is one of them: without this the map holds the
@@ -4520,8 +4522,8 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
ground - handed the frame back to the volumes for that one frame, so the whole scene's
shadows changed style and back again as the camera moved. Backend scene ownership is
installed only after successful parameters. */
- DX8Wrapper::Flush_Deferred_D3D9_State();
#if defined(__linux__)
+ DX8Wrapper::Flush_Deferred_D3D9_State(); // as at the top: only the SDL device reads it
if (captured)
SunShadow::Parameters(DX8Wrapper::_Get_D3D_Device(), depthBias, strength, widest,
narrowest, penumbra / worldPerTexel, unitsPerUnitOfDepth, skyFill);
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
index cb7927f33..6168834f8 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp
@@ -398,16 +398,50 @@ void W3DTreeBuffer::cull(const CameraClass * camera, const CameraClass *sunCamer
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);
+ /* The sun's box holds trees the screen does not, and they go into the same buffers the screen
+ draws from. The fill takes trees in index order and stops at MAX_BUFFERS, so a box over dense
+ woodland (the freecam's is the whole map) would push trees in plain view out. The screen's own
+ trees are counted first and the box only adds what the room left over holds.
+ ponytail: one whole buffer is kept spare for what each buffer's last break wastes; an exact
+ packing would have to fill the screen's trees first, which the fill loop does not do. */
+ Int roomVertices = 0;
+ Int roomIndices = 0;
+ if (sunCamera) {
+ m_sunCasterTrees = 0;
+ roomVertices = (MAX_BUFFERS - 1) * MAX_TREE_VERTEX;
+ roomIndices = (MAX_BUFFERS - 1) * MAX_TREE_INDEX;
+ for (curTree=0; curTree<m_numTrees; curTree++) {
+ const Int type = m_trees[curTree].treeType;
+ if (type < 0 || m_treeTypes[type].m_mesh == 0)
+ continue;
+ if (camera->Cull_Sphere(m_trees[curTree].bounds) && !(castsSilhouette && shadowCanReachView(camera->Get_Frustum(),
+ m_trees[curTree].bounds, m_trees[curTree].location.Z, stretchX, stretchY)))
+ continue;
+ roomVertices -= m_treeTypes[type].m_mesh->Peek_Model()->Get_Vertex_Count();
+ roomIndices -= 3 * m_treeTypes[type].m_mesh->Peek_Model()->Get_Polygon_Count();
+ }
+ }
for (curTree=0; curTree<m_numTrees; curTree++) {
Bool doKey = false; // We calculate the key when a tree becomes visible.
+ const Int type = m_trees[curTree].treeType;
Bool visible = !camera->Cull_Sphere(m_trees[curTree].bounds);
- // Keep one shared buffer for both views. A tree outside the screen can still
- // cast onto it; reculling to just the screen would also rebuild it twice a frame.
- if (!visible && sunCamera)
- visible = !sunCamera->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);
+ // Keep one shared buffer for both views. A tree outside the screen can still
+ // cast onto it; reculling to just the screen would also rebuild it twice a frame.
+ if (!visible && sunCamera && type >= 0 && m_treeTypes[type].m_mesh != 0
+ && !sunCamera->Cull_Sphere(m_trees[curTree].bounds)) {
+ const Int vertices = m_treeTypes[type].m_mesh->Peek_Model()->Get_Vertex_Count();
+ const Int indices = 3 * m_treeTypes[type].m_mesh->Peek_Model()->Get_Polygon_Count();
+ if (vertices <= roomVertices && indices <= roomIndices) {
+ visible = TRUE;
+ roomVertices -= vertices;
+ roomIndices -= indices;
+ }
+ }
+ if (sunCamera && visible && type >= 0 && (m_treeTypes[type].m_doShadow || TheGlobalData->m_shadowsForProps))
+ ++m_sunCasterTrees; // what loadTreesInVertexAndIndexBuffers gives a shadow range
if (visible != m_trees[curTree].visible) {
m_trees[curTree].visible=visible;
m_anythingChanged = true;
@@ -1858,17 +1892,23 @@ them on screen at once - a palm is forty vertices.
Colour comes from the vertex, not the texture, so the ground gets shade and not a picture of a
palm; alpha comes from the texture, so the gaps between the fronds are gaps in the shadow. */
//=============================================================================
-void W3DTreeBuffer::drawModelShadowCasters(RenderInfoClass &sunInfo, const SunShadowCasterCull &cull)
+Int W3DTreeBuffer::drawModelShadowCasters(RenderInfoClass &sunInfo, const SunShadowCasterCull &cull)
{
+ Int drawn = 0;
for (Int i=0; i<m_numModelShadows; ++i) {
TModelShadow &model = m_modelShadows[i];
+ Bool cast = FALSE;
for (Int k=0; k<model.numMesh; ++k) {
MeshClass *mesh = model.mesh[k];
if (mesh->Is_Not_Hidden_At_All() && !sunInfo.Camera.Cull_Sphere(mesh->Get_Bounding_Sphere())
- && cull.Visible(mesh->Get_Bounding_Sphere()))
+ && cull.Visible(mesh->Get_Bounding_Sphere())) {
mesh->Render(sunInfo);
+ cast = TRUE;
+ }
}
+ if (cast) ++drawn;
}
+ return drawn;
}
void W3DTreeBuffer::drawModelShadows(CameraClass *camera, Real stretchX, Real stretchY)
@@ -2179,11 +2219,9 @@ void W3DTreeBuffer::stepTrees( void )
void W3DTreeBuffer::drawTrees(CameraClass * camera, RefRenderObjListIterator *pDynamicLightsIterator,
const CameraClass *sunCamera)
{
-#if defined(__linux__)
+ // While the sun's map holds the frame the trees are in it as their own shape, so the silhouettes,
+ // the blob decals and the palms' flattened copies would shadow the ground a second time.
const Bool mapped = SunShadow::OwnsSceneShadows(DX8Wrapper::_Get_D3D_Device());
-#else
- const Bool mapped = FALSE;
-#endif
SunShadow::ReceiverExclusion sunReceivers(DX8Wrapper::_Get_D3D_Device(), !mapped);
USE_PERF_TIMER(Tree_Render)
if (!m_isTerrainPass && !sunCamera) {
@@ -2287,12 +2325,11 @@ void W3DTreeBuffer::drawTrees(CameraClass * camera, RefRenderObjListIterator *pD
// the pushed aside and toppling trees moved in stepTrees, at the sync point
-#if defined(__linux__)
+ // ShadowsForProps decides which trees get a range in the sun pass, so a change rebuilds them.
if (m_lastShadowsForProps != TheGlobalData->m_shadowsForProps) {
m_lastShadowsForProps = TheGlobalData->m_shadowsForProps;
m_anythingChanged = true;
}
-#endif
if (m_anythingChanged) {
loadTreesInVertexAndIndexBuffers(pDynamicLightsIterator);
m_anythingChanged = false;
diff --git a/docs/porting/windows-impact.md b/docs/porting/windows-impact.md
index 80159a1a5..b82708725 100644
--- a/docs/porting/windows-impact.md
+++ b/docs/porting/windows-impact.md
@@ -451,8 +451,11 @@ This feature starts directly from reference/3.1.0. Sun capture, tree capture,
shadow-option admission and queued cloak coverage are enabled on Linux. The
Windows Direct3D 11 backend sources remain unchanged; the shared adapter forwards
existing calls and returns empty queued coverage. Windows retains its original
-water programs, tree silhouettes, caster selection and constant-cache comparisons.
-macOS refuses the new scene capture and retains its existing shadows. Shared API,
+water programs, caster selection and constant-cache comparisons. Tree capture is
+on under Direct3D 11 too since the Windows follow-up: the batched trees and the
+object palms draw into the sun's map, and their silhouettes, blob decals and
+flattened palm copies stand down while `SunShadow::OwnsSceneShadows` holds; under
+`-d3d9` no map takes the frame and the silhouettes stay. macOS refuses the new scene capture and retains its existing shadows. Shared API,
shader and queue metadata still compile on these platforms; native builds and
runtime validation remain required. Linux tests do not establish that evidence.