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 posed culling per render object and in the shadows

Olcay Seygan918e409175ff
diff --git a/GeneralsMD/Code/GameEngine/Include/GameClient/DrawableInfo.h b/GeneralsMD/Code/GameEngine/Include/GameClient/DrawableInfo.h
index 5e4938c75..96570836b 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameClient/DrawableInfo.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameClient/DrawableInfo.h
@@ -48,13 +48,12 @@ struct DrawableInfo
ERF_DELAYED_RENDER = ERF_IS_TRANSLUCENT|ERF_POTENTIAL_OCCLUDEE,
};
- DrawableInfo(void) : m_shroudStatusObjectID(INVALID_ID), m_drawable(NULL), m_ghostObject(NULL), m_flags(ERF_IS_NORMAL), m_animLeavesBindPose(FALSE) {}
+ DrawableInfo(void) : m_shroudStatusObjectID(INVALID_ID), m_drawable(NULL), m_ghostObject(NULL), m_flags(ERF_IS_NORMAL) {}
ObjectID m_shroudStatusObjectID; ///<since we sometimes have drawables without objects, this points to a parent object from which we pull shroud status.
Drawable *m_drawable; ///<pointer back to drawable containing this DrawableInfo
GhostObject *m_ghostObject; ///<pointer to ghostObject for this drawable used for fogged versions.
Int m_flags; ///<extra render settings flags that are tied to render objects with drawables.
- Bool m_animLeavesBindPose; ///<the model's animation carries it out of its bind-pose bounds, so culling tests the posed parts too
};
#endif // _DRAWABLEINFO_H_
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DModelDraw.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DModelDraw.h
index 324bee8b6..3cc0b8e7e 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DModelDraw.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/Module/W3DModelDraw.h
@@ -364,6 +364,10 @@ public:
/// Hands the frame's lit headlights to the Direct3D 11 frame, nearest the camera first. Once a
/// frame, after the smooth motion has placed every model and before anything is drawn.
static void lightHeadlights(const Vector3& cameraPosition);
+ /// For a render object whose animation carries it out of its bind pose, which is the volume W3D
+ /// culls by: the sphere round its parts posed at this frame. FALSE, and the sphere untouched,
+ /// for every other render object.
+ static Bool getPosedBoundingSphere(RenderObjClass *robj, SphereClass &sphere);
virtual void setShadowsEnabled(Bool enable);
virtual void releaseShadows(void); ///< frees all shadow resources used by this module - used by Options screen.
virtual void allocateShadows(void); ///< create shadow resources if not already present. Used by Options screen.
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp
index 5911ee704..ea8a331cf 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Drawable/Draw/W3DModelDraw.cpp
@@ -73,6 +73,7 @@
#include "W3DDevice/GameClient/WorldHeightMap.h"
#include "WW3D2/hanim.h"
#include "WW3D2/hlod.h"
+#include "WW3D2/htree.h"
#include "WW3D2/rendobj.h"
#include "WW3D2/mesh.h"
#include "WW3D2/meshmdl.h"
@@ -2481,28 +2482,83 @@ Real W3DModelDraw::getCurrentAnimFraction() const
//-------------------------------------------------------------------------------------------------
static Bool animLeavesBindPose(HAnimClass *anim, RenderObjClass *robj)
{
- const Real reach = robj->Get_Bounding_Sphere().Radius;
+ // A channel's translation is the bone's offset from its bind pose, and what this is for starts
+ // there and flies hundreds of units off: a bone hung straight off the root, at its bind pose on
+ // the first frame and past the sphere and the floor later. Infantry park the props they are not
+ // holding 2000 units away from the first frame on, and a building's doors and dishes swing a
+ // little past its sphere; neither needs anything but the bind pose
+ const HTreeClass *tree = robj->Get_HTree();
+ if (tree == NULL)
+ return FALSE;
+ const Real MIN_FLIGHT = 200.0f;
+ const Real reach = max(robj->Get_Bounding_Sphere().Radius, MIN_FLIGHT);
const Int lastFrame = anim->Get_Num_Frames() - 1;
for (Int pivot = 1; pivot < anim->Get_Num_Pivots(); ++pivot)
{
- if (!anim->Is_Node_Motion_Present(pivot))
+ if (tree->Get_Parent_Index(pivot) != 0 || !anim->Is_Node_Motion_Present(pivot))
continue;
Vector3 first, middle, last;
anim->Get_Translation(first, pivot, 0.0f);
anim->Get_Translation(middle, pivot, lastFrame * 0.5f);
anim->Get_Translation(last, pivot, (Real)lastFrame);
- if ((middle - first).Length() > reach || (last - first).Length() > reach)
+ if (first.Length() <= reach && (middle.Length() > reach || last.Length() > reach))
return TRUE;
}
return FALSE;
}
+// The render objects animLeavesBindPose said yes for: a handful at most, the cinematic transports.
+// Kept per render object, since one drawable can carry several models
+static std::vector<const RenderObjClass *> s_posedCullModels;
+
+static void setPosedCull(const RenderObjClass *robj, Bool posed)
+{
+ std::vector<const RenderObjClass *>::iterator it = std::find(s_posedCullModels.begin(), s_posedCullModels.end(), robj);
+ if (posed && it == s_posedCullModels.end())
+ s_posedCullModels.push_back(robj);
+ else if (!posed && it != s_posedCullModels.end())
+ s_posedCullModels.erase(it);
+}
+
+//-------------------------------------------------------------------------------------------------
+/** A culled model is not drawn, and drawing is what moves its animation on, so setting its transform
+ * again (which marks the hierarchy stale) and asking for a bone poses it at the current frame
+ * first. */
+//-------------------------------------------------------------------------------------------------
+Bool W3DModelDraw::getPosedBoundingSphere(RenderObjClass *robj, SphereClass &sphere)
+{
+ if (std::find(s_posedCullModels.begin(), s_posedCullModels.end(), robj) == s_posedCullModels.end())
+ return FALSE;
+
+ const Int parts = robj->Get_Num_Sub_Objects();
+ if (parts == 0)
+ return FALSE;
+
+ const Matrix3D transform = robj->Get_Transform();
+ robj->Set_Transform(transform);
+ robj->Get_Bone_Transform(0);
+ for (Int i = 0; i < parts; ++i)
+ {
+ RenderObjClass *part = robj->Get_Sub_Object(i);
+ if (i == 0)
+ sphere = part->Get_Bounding_Sphere();
+ else
+ sphere.Add_Sphere(part->Get_Bounding_Sphere());
+ part->Release_Ref();
+ }
+ return TRUE;
+}
+
//-------------------------------------------------------------------------------------------------
void W3DModelDraw::adjustAnimation(const ModelConditionInfo* prevState, Real prevAnimFraction)
{
if (!m_curState)
return;
+ // a state without an animation, or one that moves nothing far, culls by the bind pose again
+ if (m_renderObject)
+ setPosedCull(m_renderObject, FALSE);
+
// if the current state has m_animations associated, do the right thing
Int numAnims = m_curState->m_animations.size();
if (numAnims > 0)
@@ -2562,7 +2618,7 @@ void W3DModelDraw::adjustAnimation(const ModelConditionInfo* prevState, Real pre
}
m_renderObject->Set_Animation(animHandle, startFrame, m_curState->m_mode);
- getDrawable()->getDrawableInfo()->m_animLeavesBindPose = animLeavesBindPose(animHandle, m_renderObject);
+ setPosedCull(m_renderObject, animLeavesBindPose(animHandle, m_renderObject));
REF_PTR_RELEASE(animHandle);
animHandle = NULL;
@@ -3190,6 +3246,7 @@ void W3DModelDraw::nukeCurrentRender(Matrix3D* xform)
if (xform)
*xform = m_renderObject->Get_Transform();
W3DDisplay::m_3DScene->Remove_Render_Object(m_renderObject);
+ setPosedCull(m_renderObject, FALSE);
REF_PTR_RELEASE(m_renderObject);
m_renderObject = NULL;
}
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index a225ed3f1..0abbe9703 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -75,6 +75,7 @@
#include "Platform/RenderTypes.h"
#include "Lib/Clock.h" // Clock_Ticks: QueryPerformanceCounter on Windows, a monotonic clock elsewhere
#include "GameClient/ParticleSys.h"
+#include "W3DDevice/GameClient/Module/W3DModelDraw.h"
#include <algorithm>
#include <vector>
@@ -89,6 +90,16 @@
W3DVolumetricShadowManager *TheW3DVolumetricShadowManager=NULL;
extern const FrustumClass *shadowCameraFrustum; //defined in W3DShadow.
+// The sphere a caster's shadow is reckoned from: the bind pose, or its parts as they stand now when
+// its animation has carried it out of that pose (GLA 1's escape plane flies its whole takeoff in its
+// bones, and cast nothing once the runway spot it stands on left the frame).
+static SphereClass casterSphere(RenderObjClass *robj)
+{
+ SphereClass sphere = robj->Get_Bounding_Sphere();
+ W3DModelDraw::getPosedBoundingSphere(robj, sphere);
+ return sphere;
+}
+
///////////////////////////////////////////////////////////////////////////////
// DEFINITIONS ////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
@@ -2128,7 +2139,7 @@ void W3DVolumetricShadow::Update()
WWMath::Fabs(pos.Y - bcY) > (beY + extent) ||
WWMath::Fabs(pos.Z - bcZ) > (beZ + extent))
{
- if (!volumeShadowCanReachView( *shadowCameraFrustum, m_robj->Get_Bounding_Sphere(), TheTerrainRenderObject->getMinHeight() ))
+ if (!volumeShadowCanReachView( *shadowCameraFrustum, casterSphere(m_robj), TheTerrainRenderObject->getMinHeight() ))
return; //shadow can't be visible so no point in updating.
}
@@ -2148,10 +2159,10 @@ void W3DVolumetricShadow::Update()
WWMath::Fabs(pos.Y - bcY) > (beY + m_robjExtent) ||
WWMath::Fabs(pos.Z - bcZ) > (beZ + m_robjExtent))
{
- if (!volumeShadowCanReachView( *shadowCameraFrustum, m_robj->Get_Bounding_Sphere(), TheTerrainRenderObject->getMinHeight() ))
+ if (!volumeShadowCanReachView( *shadowCameraFrustum, casterSphere(m_robj), 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.
if (!m_extraExtrusionPadding)
@@ -4364,9 +4375,10 @@ void W3DVolumetricShadowManager::renderShadowMap( CameraClass &sceneCamera )
see. Culling by the camera meant a tank just off the left of the screen stopped casting,
so its shadow blinked out while the shadow of the tank beside it stayed - and scrolling
turned that into a row of shadows flickering along the edge of the frame. */
- const SphereClass &bound = robj->Get_Bounding_Sphere();
+ SphereClass bound = robj->Get_Bounding_Sphere();
+ const Bool posed = W3DModelDraw::getPosedBoundingSphere( robj, bound ); // see casterSphere
Vector3 inSun;
- Matrix3D::Inverse_Transform_Vector( transform, robj->Get_Position(), &inSun );
+ Matrix3D::Inverse_Transform_Vector( transform, posed ? bound.Center : robj->Get_Position(), &inSun );
const Real reach = halfWidth + bound.Radius;
if (inSun.X < -reach || inSun.X > reach || inSun.Y < -reach || inSun.Y > reach)
continue;
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
index db6a75e01..7a066d433 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp
@@ -74,6 +74,7 @@
#include "WW3D2/dx11runtime.h"
#include "WW3D2/ffshader.h"
#include "Platform/RenderTypes.h"
+#include "W3DDevice/GameClient/Module/W3DModelDraw.h"
#include <algorithm>
#include <vector>
@@ -431,27 +432,12 @@ Bool RTS3DScene::castRay(RayCollisionTestClass & raytest, Bool testAll, Int coll
// posedPartsInView
//=============================================================================
/** A model whose animation carries it out of its bind pose (W3DModelDraw's animLeavesBindPose) is
- * in view when any of its parts is, as they stand now. A culled model is not drawn, and drawing
- * is what moves its animation on, so setting its transform again (which marks the hierarchy stale)
- * and asking for a bone poses it at the current frame first. */
+ * in view when its parts are, as they stand now. */
//=============================================================================
static Bool posedPartsInView(RenderObjClass *robj, CameraClass *camera)
{
- const DrawableInfo *drawInfo = (const DrawableInfo *)robj->Get_User_Data();
- if (drawInfo == NULL || !drawInfo->m_animLeavesBindPose || robj->Class_ID() != RenderObjClass::CLASSID_HLOD)
- return FALSE;
-
- const Matrix3D transform = robj->Get_Transform();
- robj->Set_Transform(transform);
- robj->Get_Bone_Transform(0);
- Bool inView = FALSE;
- for (Int i = 0; i < robj->Get_Num_Sub_Objects() && !inView; ++i)
- {
- RenderObjClass *part = robj->Get_Sub_Object(i);
- inView = !camera->Cull_Sphere(part->Get_Bounding_Sphere());
- part->Release_Ref();
- }
- return inView;
+ SphereClass posed;
+ return W3DModelDraw::getPosedBoundingSphere(robj, posed) && !camera->Cull_Sphere(posed);
}
//=============================================================================