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
feat(gameenginedevice): give trees and shadowless props real shadows
Olcay Seygan800ef4c5e26a
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 49486f95d..67d5e3082 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -228,8 +228,22 @@ sparks are too small and too short-lived to earn it, and fire and glows are stilshade. It is one soft patch per plume rather than a shadow for every puff, so a battlefield full ofsmoke costs no more than a battlefield with one fire.-Both are switchable the same way: `ShadowsForProjectiles = No` and `ShadowsForParticles = No` in-`GameData.ini`.+Then the trees. Every one of them dropped the same round grey blob, the same size in every+direction, no matter where the sun was or how tall the tree was — and if you had shadows set to+the good kind rather than the cheap kind, the trees dropped nothing at all, so a forest sat on+clean ground next to buildings with long shadows behind them. A tree's shadow now leans away from+the sun and stretches by the tree's own height, the way everything else on the map does, and it+is drawn whichever shadow setting you are on. At noon it is a pool under the trunk; late in the+day the whole treeline lies across the ground in front of you.++And the scenery nobody gave a shadow to at all: fences, walls of tyres, rubbish piles, low+shrubs, the props that dress a map. They stood on the terrain with nothing under them, which is+the one thing the eye picks out instantly. Anything that cannot move and had no shadow now casts+a real one with its own shape — a fence throws a line of posts across the road. It costs nothing+to keep: the thing never moves, so its shadow is worked out once.++All of it is switchable the same way: `ShadowsForProjectiles = No`, `ShadowsForParticles = No`+and `ShadowsForProps = No` in `GameData.ini`.## It does not crashdiff --git a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.hindex 0bc91378e..d965e6930 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/GlobalData.h@@ -128,6 +128,7 @@ public:Bool m_useShadowVolumesForSkins; // "UseShadowVolumesForSkins": also cast volume shadows off skinned meshesBool m_useShadowDecals;Bool m_shadowsForProjectiles; // "ShadowsForProjectiles": missiles and bombs that have no shadow of their own get a decal one+ Bool m_shadowsForProps; // "ShadowsForProps": fences, rubbish, shrubs - scenery the art gave no shadow at allBool m_shadowsForParticles; // "ShadowsForParticles": big alpha-blended particle clouds drop a soft blob on the groundBool m_particleGroundBounce; // "-particlebounce": terrain collision on for every particle systemInt m_textureReductionFactor; //how much to cut texture resolution: 2 is half, 3 is quarter, etc.diff --git a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cppindex b67419d76..2a3736e49 100644--- a/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp+++ b/GeneralsMD/Code/GameEngine/Source/Common/GlobalData.cpp@@ -100,6 +100,7 @@ GlobalData* GlobalData::m_theOriginal = NULL;{ "UseShadowVolumesForSkins", INI::parseBool, NULL, offsetof( GlobalData, m_useShadowVolumesForSkins ) },{ "UseShadowDecals", INI::parseBool, NULL, offsetof( GlobalData, m_useShadowDecals ) },{ "ShadowsForProjectiles", INI::parseBool, NULL, offsetof( GlobalData, m_shadowsForProjectiles ) },+ { "ShadowsForProps", INI::parseBool, NULL, offsetof( GlobalData, m_shadowsForProps ) },{ "ShadowsForParticles", INI::parseBool, NULL, offsetof( GlobalData, m_shadowsForParticles ) },{ "TextureReductionFactor", INI::parseInt, NULL, offsetof( GlobalData, m_textureReductionFactor ) },{ "UseBehindBuildingMarker", INI::parseBool, NULL, offsetof( GlobalData, m_enableBehindBuildingMarkers ) },@@ -646,6 +647,7 @@ GlobalData::GlobalData()m_useShadowVolumesForSkins = TRUE; //on by default: the shipped INI has no entry for itm_useShadowDecals = FALSE;m_shadowsForProjectiles = TRUE; //on by default: the shipped INI has no entry for it+ m_shadowsForProps = TRUE; //likewise: scenery with no shadow of its own gets onem_shadowsForParticles = TRUE; //on by default: the shipped INI has no entry for itm_textureReductionFactor = -1;m_enableBehindBuildingMarkers = TRUE;diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.hindex 0cecc724d..c47d356c4 100644--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.h+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DTreeBuffer.h@@ -137,6 +137,7 @@ typedef struct {Bool m_halfTile; ///< Tiles are 64x64 pixels, half tile supports a 32x32 bit texture. Have to adjust the uv values.Vector3 m_offset;Real m_shadowSize; ///< Shadow radius.+ Real m_shadowHeight; ///< Model height, so the blob can be stretched away from the sun.Bool m_doShadow; ///< Draw shadow.} TTreeType;diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cppindex 26157d42d..1fa8e1705 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp@@ -1890,34 +1890,82 @@ static Bool promoteSkinShadowToVolume(RenderObjClass *robj, Shadow::ShadowTypeInreturn TRUE;}+/** Fill in the shadow this template asks for. A template that asks for nothing still gets one if it+is a projectile: a missile or a bomb with no shadow slides over the ground with nothing tying it to+the terrain. There can be dozens in the air at once, so it is always the cheap projected blob and+never a volume; a zero decal size makes addShadow take the size from the model's own bounding box.+Returns FALSE when this drawable gets no shadow at all. */+static Bool fillShadowInfoFromTemplate(const ThingTemplate *tmplate, Shadow::ShadowTypeInfo *shadowInfo, Bool allowPropVolume = TRUE)+{+ if (tmplate->getShadowType() == SHADOW_NONE)+ {+ //a prop the art gave no shadow at all - a fence, a rubbish pile, a shrub - gets a real volume+ //one: it never moves, so the shadow is built once and then costs nothing to keep. Clearing+ //allowPropVolume is the retry for a model with no usable shadow geometry: it drops to the blob.+ const Bool prop = TheGlobalData->m_shadowsForProps && tmplate->isKindOf(KINDOF_IMMOBILE) &&+ !tmplate->isKindOf(KINDOF_PROJECTILE);++ if (prop && allowPropVolume && TheGlobalData->m_useShadowVolumes)+ {+ shadowInfo->m_ShadowName[0] = 0;+ shadowInfo->allowUpdates = FALSE;+ shadowInfo->allowWorldAlign = TRUE;+ shadowInfo->m_type = SHADOW_VOLUME;+ shadowInfo->m_sizeX = 0.0f;+ shadowInfo->m_sizeY = 0.0f;+ shadowInfo->m_offsetX = 0.0f;+ shadowInfo->m_offsetY = 0.0f;+ return TRUE;+ }++ if (!prop && (!TheGlobalData->m_shadowsForProjectiles || !tmplate->isKindOf(KINDOF_PROJECTILE)))+ return FALSE;++ //most of these name no texture of their own, so ask for the round blob a sphere gets+ strcpy(shadowInfo->m_ShadowName, tmplate->getShadowTextureName().isEmpty() ? "shadow" : tmplate->getShadowTextureName().str());+ shadowInfo->allowUpdates = FALSE;+ shadowInfo->allowWorldAlign = TRUE;+ shadowInfo->m_type = SHADOW_DECAL;+ shadowInfo->m_sizeX = 0.0f;+ shadowInfo->m_sizeY = 0.0f;+ shadowInfo->m_offsetX = 0.0f;+ shadowInfo->m_offsetY = 0.0f;+ return TRUE;+ }++ strcpy(shadowInfo->m_ShadowName, tmplate->getShadowTextureName().str());+ DEBUG_ASSERTCRASH(shadowInfo->m_ShadowName[0] != ' ', ("this should be validated in ThingTemplate now"));+ shadowInfo->allowUpdates = FALSE; //shadow image will never update+ shadowInfo->allowWorldAlign = TRUE; //shadow image will wrap around world objects+ shadowInfo->m_type = (ShadowType)tmplate->getShadowType();+ shadowInfo->m_sizeX = tmplate->getShadowSizeX();+ shadowInfo->m_sizeY = tmplate->getShadowSizeY();+ shadowInfo->m_offsetX = tmplate->getShadowOffsetX();+ shadowInfo->m_offsetY = tmplate->getShadowOffsetY();+ return TRUE;+}+/** Create shadow resources if not already present. This is used to dynamically enable/disable shadows by the options screen*/void W3DModelDraw::allocateShadows(void){const ThingTemplate *tmplate=getDrawable()->getTemplate();//Check if we don't already have a shadow but need one for this type of model.- if (m_shadow == NULL && m_renderObject && TheW3DShadowManager && tmplate->getShadowType() != SHADOW_NONE)+ Shadow::ShadowTypeInfo shadowInfo;+ if (m_shadow == NULL && m_renderObject && TheW3DShadowManager && fillShadowInfoFromTemplate(tmplate, &shadowInfo)){- Shadow::ShadowTypeInfo shadowInfo;- strcpy(shadowInfo.m_ShadowName, tmplate->getShadowTextureName().str());- DEBUG_ASSERTCRASH(shadowInfo.m_ShadowName[0] != '\0', ("this should be validated in ThingTemplate now"));- shadowInfo.allowUpdates = FALSE; //shadow image will never update- shadowInfo.allowWorldAlign = TRUE; //shadow image will wrap around world objects- shadowInfo.m_type = (ShadowType)tmplate->getShadowType();- shadowInfo.m_sizeX = tmplate->getShadowSizeX();- shadowInfo.m_sizeY = tmplate->getShadowSizeY();- shadowInfo.m_offsetX = tmplate->getShadowOffsetX();- shadowInfo.m_offsetY = tmplate->getShadowOffsetY();-- Bool promotedToVolume = promoteSkinShadowToVolume(m_renderObject, &shadowInfo);+ //a projectile's decal is ours rather than the template's - never trade it for a volume+ Bool promotedToVolume = tmplate->getShadowType() != SHADOW_NONE &&+ promoteSkinShadowToVolume(m_renderObject, &shadowInfo);+ //a prop we handed a volume the template never asked for falls back the same way+ if (tmplate->getShadowType() == SHADOW_NONE && shadowInfo.m_type == SHADOW_VOLUME)+ promotedToVolume = TRUE;m_shadow = TheW3DShadowManager->addShadow(m_renderObject, &shadowInfo);if (m_shadow == NULL && promotedToVolume){ //no usable shadow geometry in this model - fall back to the decal the template asked for- shadowInfo.m_type = (ShadowType)tmplate->getShadowType();- shadowInfo.m_sizeX = tmplate->getShadowSizeX();- shadowInfo.m_sizeY = tmplate->getShadowSizeY();+ fillShadowInfoFromTemplate(tmplate, &shadowInfo, FALSE);m_shadow = TheW3DShadowManager->addShadow(m_renderObject, &shadowInfo);}@@ -3117,27 +3165,21 @@ void W3DModelDraw::setModelState(const ModelConditionInfo* newState)}// set up shadows- if (m_renderObject && TheW3DShadowManager && tmplate->getShadowType() != SHADOW_NONE)+ Shadow::ShadowTypeInfo shadowInfo;+ if (m_renderObject && TheW3DShadowManager && fillShadowInfoFromTemplate(tmplate, &shadowInfo)){- Shadow::ShadowTypeInfo shadowInfo;- strcpy(shadowInfo.m_ShadowName, tmplate->getShadowTextureName().str());- DEBUG_ASSERTCRASH(shadowInfo.m_ShadowName[0] != '\0', ("this should be validated in ThingTemplate now"));- shadowInfo.allowUpdates = FALSE; //shadow image will never update- shadowInfo.allowWorldAlign = TRUE; //shadow image will wrap around world objects- shadowInfo.m_type = (ShadowType)tmplate->getShadowType();- shadowInfo.m_sizeX = tmplate->getShadowSizeX();- shadowInfo.m_sizeY = tmplate->getShadowSizeY();- shadowInfo.m_offsetX = tmplate->getShadowOffsetX();- shadowInfo.m_offsetY = tmplate->getShadowOffsetY();- Bool promotedToVolume = promoteSkinShadowToVolume(m_renderObject, &shadowInfo);+ //a projectile's decal is ours rather than the template's - never trade it for a volume+ Bool promotedToVolume = tmplate->getShadowType() != SHADOW_NONE &&+ promoteSkinShadowToVolume(m_renderObject, &shadowInfo);+ //a prop we handed a volume the template never asked for falls back the same way+ if (tmplate->getShadowType() == SHADOW_NONE && shadowInfo.m_type == SHADOW_VOLUME)+ promotedToVolume = TRUE;m_shadow = TheW3DShadowManager->addShadow(m_renderObject, &shadowInfo, draw);if (m_shadow == NULL && promotedToVolume){ //no usable shadow geometry in this model - fall back to the decal the template asked for- shadowInfo.m_type = (ShadowType)tmplate->getShadowType();- shadowInfo.m_sizeX = tmplate->getShadowSizeX();- shadowInfo.m_sizeY = tmplate->getShadowSizeY();+ fillShadowInfoFromTemplate(tmplate, &shadowInfo, FALSE);m_shadow = TheW3DShadowManager->addShadow(m_renderObject, &shadowInfo, draw);}diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cppindex 082f95da1..aba40e7fa 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DTreeBuffer.cpp@@ -58,6 +58,7 @@ enum#include <stdio.h>#include <string.h>+#include <math.h>#include <assetmgr.h>#include <texture.h>#include "Common/MapReaderWriterInfo.h"@@ -1393,6 +1394,7 @@ Int W3DTreeBuffer::addTreeType(const W3DTreeDrawModuleData *data)m_treeTypes[m_numTreeTypes].m_data = data;m_treeTypes[m_numTreeTypes].m_offset = offset;m_treeTypes[m_numTreeTypes].m_shadowSize = (box.Extent.X + box.Extent.Y); // Average extent * 2. jba.+ m_treeTypes[m_numTreeTypes].m_shadowHeight = box.Center.Z + box.Extent.Z; // top of the model above its base.m_treeTypes[m_numTreeTypes].m_doShadow = data->m_doShadow;m_numTreeTypes++;return m_numTreeTypes-1;@@ -1609,7 +1611,16 @@ void W3DTreeBuffer::drawTrees(CameraClass * camera, RefRenderObjListIterator *pDInt curTree;// Draw tree shadows.- if (m_shadow && TheW3DProjectedShadowManager && TheGlobalData->m_useShadowDecals) {+ // Trees are batched vertices with no render object of their own, so a volume shadow is not+ // available to them - the shared blob is all there is. Lean and stretch it along the sun so it+ // at least points where every other shadow in the scene points, and draw it whenever either+ // shadow option is on: with volumes only, trees used to be the one thing standing on nothing.+ if (m_shadow && TheW3DProjectedShadowManager &&+ (TheGlobalData->m_useShadowDecals || TheGlobalData->m_useShadowVolumes)) {+ Vector3 sun(0.0f, 0.0f, 1.0f);+ if (TheW3DShadowManager) {+ sun = TheW3DShadowManager->getLightPosWorld(0);+ }for (curTree=0; curTree<m_numTrees; curTree++) {Int type = m_trees[curTree].treeType;if (type<0) { // deleted.@@ -1623,8 +1634,24 @@ void W3DTreeBuffer::drawTrees(CameraClass * camera, RefRenderObjListIterator *pDm_trees[curTree].m_toppleState == TOPPLE_DOWN) {continue;}- m_shadow->setSize(m_treeTypes[type].m_shadowSize, -m_treeTypes[type].m_shadowSize*factor);- m_shadow->setPosition(m_trees[curTree].location.X, m_trees[curTree].location.Y, m_trees[curTree].location.Z);+ const Real size = m_treeTypes[type].m_shadowSize;+ Real dirX = 1.0f, dirY = 0.0f, reach = 0.0f;+ const Real dx = m_trees[curTree].location.X - sun.X;+ const Real dy = m_trees[curTree].location.Y - sun.Y;+ const Real dz = sun.Z - m_trees[curTree].location.Z;+ const Real len = (Real)sqrt(dx*dx + dy*dy);+ if (len > 0.001f && dz > 1.0f) {+ dirX = dx/len;+ dirY = dy/len;+ reach = m_treeTypes[type].m_shadowHeight * len / dz;+ if (reach > 2.0f*size) {+ reach = 2.0f*size; // a low sun would otherwise smear one tree across half the map.+ }+ }+ m_shadow->setAngle((Real)atan2(dirY, dirX));+ m_shadow->setSize(size + reach, -size*factor);+ m_shadow->setPosition(m_trees[curTree].location.X + dirX*reach*0.5f,+ m_trees[curTree].location.Y + dirY*reach*0.5f, m_trees[curTree].location.Z);TheW3DProjectedShadowManager->queueDecal(m_shadow);}TheW3DProjectedShadowManager->flushDecals(m_shadow->getTexture(0), SHADOW_DECAL);