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(ww3d2): blend raw animation poses between whole frames

Olcay Seygand56c624bd228
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d861b638d..021c4c497 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -320,6 +320,7 @@ found and fixed, EA's own, not port damage.**
- Mission timers keep EA's clock. A map's countdown still ticks 30 times a second, because the missions read a running timer's number and act on it: the second American mission declares victory when its "Win" timer drops below 1700, and on the first 60-step builds that came after 38 seconds instead of 10. The cutscene cues, the random music pick in the campaigns and the generals' challenges, the shell map's camera loop and the lightning on Eastern Everglades and Rocky Rampage were off the same way, 30 maps in all, and run on their original timing again. Custom maps get the same.
- Normal speed is 60 steps a second. Turbo mode, the one other speed a match can have, runs 120 of the same steps a second (see Turbo mode above). Replays still play as fast as you ask.
- On a screen faster than 60 Hz the armies glide. The battle moves in 60 steps a second, so a 144 or 240 Hz monitor showed every step two to four times over and a tank crossing the map moved in small hops. Now each picture draws every unit part of the way between its last two steps. The picture runs one step, 16.7 ms, behind the battle; the battle itself does not change, and a replay drawn with it, without it and with no picture at all ends on the same checksum. A 60 Hz screen gets nothing it did not have. `-nosmoothmotion` turns it off for a run, `-smoothmotion` turns it on whatever the screen.
+- Helicopter rotors used to jump between thirty poses a second. On a 60 Hz or faster screen they now turn through the poses in between, and so do other model parts moving at an ordinary pace. A part the art cuts from one spot to another, or one moving very fast, still jumps, and the extra pictures a heavy battle draws between steps hold the blades still.
- The battle's arithmetic runs on a thread of its own, beside the drawing, on Windows. The game used to work out a step and then draw, one after the other, so a heavy step stalled the picture; now the screen keeps drawing while the next step is worked out, which pays most in the late game, where the steps are heaviest. In a late 4-player match, uncapped, frames slower than 16.7 ms (one 60 Hz refresh) fell from 23% to 0.8%, the slowest one frame in a hundred fell from 24.0 to 16.5 ms live and from 16.0-16.5 to 10.6-12.5 ms in a replay, and the average frame went from 15.2 to 11.8 ms. The battle still takes 60 steps a second, no more and no fewer. A match too heavy for its clock used to pay for the thread: the catch-up steps after the picture ignored the step already run beside it, so each frame ran one step more than without the thread. Eight Hard AIs on Twilight Flame at minute 20, run at four times normal speed so they cannot keep up: one frame in a thousand took 26.8 ms with the thread and 24.0 without. Both now spend the same budget, and with the thread that frame is 21.5 ms and the average frame 11.9 ms against 13.9 without, at 12% more steps a second. No menu or saved setting turns the thread off; `-logicthread 0` stays on the command line to chase a desync, and Mac and Linux keep the old order for now. Network games now draw beside the step as well. Two seats at 240 ms of ping and 5% packet loss, with 400 units fighting: one frame in a thousand went from 18.5-21.5 ms to 10.3-12.5, one in a hundred from 13.0-14.8 to 8.3-9.0, and frames over 16.7 ms from 44-95 a match to 6-9. An order reaches the battle and the screen as fast as it did, and a seat with the thread off still agrees with one that has it on. A seat whose drawing takes longer than a step keeps the old order, so a slow graphics card holds the room no lower than before. Network machines compare checksums every 10 steps where they used to compare every step, so nine steps in ten skip a pass over the whole world, and a desync is caught at most 10 steps late. A step too long for one refresh no longer freezes the screen in single player: the game waits for it as long as a picture can still arrive on time, then draws one more, timed to finish as the step does. Eight Hard AIs and 2000 objects at minute 17, two copies racing on one machine so both met the same noise: one frame in a thousand went from 64.0 to 19.0 ms, frames over 16.7 ms from all of them to 1.6%, and the battle took 29.1 steps a second against 29.7. Those extra pictures move the camera and the effects; units still move once a step, so this smooths a heavy match without making it faster. The game also stopped asking the disc drive for its disc every five seconds during a match, which cost 7 to 11 ms a time on a machine with an empty drive.
- That thread went off after 3.3.0, whose crash reports traced the random crashes in the middle of a match to the drawing and the battle writing to the same memory at the same moment. It is back because those collisions are closed. A model or animation the battle loads for the first time now waits for the picture to finish, the trees a unit pushes through move between frames, the team colours and the list of every object stay put while a picture is drawn, and the shadows thrown on units stop asking the battle where things stand while it is moving them. The test that decided it is a 4-against-4 match of 18000 steps, drawn with the thread on, drawn with it off and run with no picture at all, ending on the same checksum each time with the counter that flags the drawing reading the battle at zero.
- Opening the pause menu no longer makes boats snap to a different angle or parked aircraft sink into the carrier deck. Boats keep their rocking angle and aircraft remain above the deck while the game is paused.
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/htree.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/htree.cpp
index d11248f71..5e5ac52c1 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/htree.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/htree.cpp
@@ -610,8 +610,37 @@ void HTreeClass::Anim_Update(const Matrix3D & root,HAnimClass * motion,float fra
}
}
-/*Customized version of the above which excludes interpolation and assumes HRawAnimClass
-For use by 'Generals' -MW*/
+/* A bone stepping further than this on any axis between two frames holds the nearest frame on all three, or
+ it would zigzag. The art parks a prop off the map by moving it 2000 units in a frame (BONE-GUN, BONE_ROCK,
+ BONE_BOTTLE), and frames in between would draw it flying there. The test cannot tell that from real motion
+ this fast, PSAIRCARRIERTP's Z among it, which keeps stepping as it always did. In the art's units; a
+ Comanche's rotor has a radius of 26. An infinite or NaN step fails the test too, as UISABOTR's junk does. */
+static const float RAW_ANIM_CUT_STEP = 20.0f;
+
+/* The same for a turn of more than 90 degrees between two frames, read as |dot| below cos 45. Blending takes
+ the short way round, so the bone in CBGWMill1_DG that hops 177 degrees would whirl backward, and a death
+ clip's hop would smear the limb. The largest step on a PROPELLER pivot is 40 degrees, the Scout Drone's. */
+static const float RAW_ANIM_CUT_QUAT_DOT = 0.7071f;
+
+static inline void rawAnimTranslation(const NodeMotionStruct * motion, int frame, Vector3 & trans)
+{
+ trans.Set(0.0f,0.0f,0.0f);
+ if (motion->X != NULL)
+ motion->X->Get_Vector(frame,&(trans[0]));
+ if (motion->Y != NULL)
+ motion->Y->Get_Vector(frame,&(trans[1]));
+ if (motion->Z != NULL)
+ motion->Z->Get_Vector(frame,&(trans[2]));
+}
+
+/*Customized version of the above which assumes HRawAnimClass
+For use by 'Generals' -MW
+
+EA's took the nearest whole frame, which was one new pose a picture while W3D's clock moved one frame's length
+a picture. On the wall clock it held a rotor still for several pictures and jumped 18 degrees, so this blends
+rotation and translation, each except across a cut. A whole frame reads exactly what it read: the pristine
+bones logic asks for are taken at one (ModelConditionInfo::validateCachedBones), and visibility stays on the
+nearest frame so a part still shows and hides when it did.*/
void HTreeClass::Anim_Update(const Matrix3D & root,HRawAnimClass * motion,float frame)
{
PivotClass *pivot,*endpivot,*lastAnimPivot;
@@ -624,9 +653,15 @@ void HTreeClass::Anim_Update(const Matrix3D & root,HRawAnimClass * motion,float
//Get integer frame
int iframe=WWMath::Float_To_Long(frame);
- if (iframe >= motion->Get_Num_Frames())
+ if (iframe >= motion->Get_Num_Frames())
iframe = 0;
+ // how far past a whole frame this picture is; the last frame has nothing after it to blend toward
+ const int frame0 = (int)frame;
+ float ratio = frame - (float)frame0;
+ if (frame0 >= motion->Get_Num_Frames() - 1)
+ ratio = 0.0f;
+
Vector3 trans;
Quaternion q;
Matrix3D mtx;
@@ -649,23 +684,43 @@ void HTreeClass::Anim_Update(const Matrix3D & root,HRawAnimClass * motion,float
{
// animation
- trans.Set(0.0f,0.0f,0.0f);
Matrix3D *xform=&pivot->Transform;
- if (nodeMotion->X != NULL)
- nodeMotion->X->Get_Vector(iframe,&(trans[0]));
- if (nodeMotion->Y != NULL)
- nodeMotion->Y->Get_Vector(iframe,&(trans[1]));
- if (nodeMotion->Z != NULL)
- nodeMotion->Z->Get_Vector(iframe,&(trans[2]));
+ if (ratio > 0.0f)
+ {
+ Vector3 next;
+ rawAnimTranslation(nodeMotion, frame0, trans);
+ rawAnimTranslation(nodeMotion, frame0 + 1, next);
+ if (WWMath::Fabs(next.X - trans.X) <= RAW_ANIM_CUT_STEP
+ && WWMath::Fabs(next.Y - trans.Y) <= RAW_ANIM_CUT_STEP
+ && WWMath::Fabs(next.Z - trans.Z) <= RAW_ANIM_CUT_STEP)
+ Vector3::Lerp(trans, next, ratio, &trans);
+ else
+ rawAnimTranslation(nodeMotion, iframe, trans);
+ }
+ else
+ rawAnimTranslation(nodeMotion, iframe, trans);
if (ScaleFactor == 1.0f)
xform->Translate(trans);
else
xform->Translate(trans*ScaleFactor);
- if (nodeMotion->Q != NULL)
- { nodeMotion->Q->Get_Vector_As_Quat(iframe, q);
+ if (nodeMotion->Q != NULL)
+ {
+ if (ratio > 0.0f)
+ {
+ Quaternion q1;
+ nodeMotion->Q->Get_Vector_As_Quat(frame0, q);
+ nodeMotion->Q->Get_Vector_As_Quat(frame0 + 1, q1);
+ const float dot = q.X * q1.X + q.Y * q1.Y + q.Z * q1.Z + q.W * q1.W;
+ if (WWMath::Fabs(dot) >= RAW_ANIM_CUT_QUAT_DOT)
+ Fast_Slerp(q, q, q1, ratio);
+ else
+ nodeMotion->Q->Get_Vector_As_Quat(iframe, q);
+ }
+ else
+ nodeMotion->Q->Get_Vector_As_Quat(iframe, q);
#ifdef ALLOW_TEMPORARIES
*xform = *xform * ::Build_Matrix3D(q,mtx);
#else
diff --git a/GeneralsMD/Code/Tests/test_ww3d2.cpp b/GeneralsMD/Code/Tests/test_ww3d2.cpp
index 466eef95b..4b0fcca9c 100644
--- a/GeneralsMD/Code/Tests/test_ww3d2.cpp
+++ b/GeneralsMD/Code/Tests/test_ww3d2.cpp
@@ -1986,3 +1986,168 @@ TEST(sorting_entry_tiles_come_from_the_corners_with_a_margin)
CHECK( sort_tiles_whole( sort_tiles_of( broken, 3, clip ) ) );
CHECK( sort_tiles_whole( sort_tiles_of( far_right, 3, broken_clip ) ) );
}
+
+// ---------------------------------------------------------------------------------------
+// A raw animation's pose between two frames. One bone, six frames:
+// frame 0 1 2 3 4 5
+// degrees 0 18 36 213 231 30
+// X 0 10 20 30 40 15
+// Y 0 0 2000 2000 2000 0
+// 0 to 1 is clean. 1 to 2 jumps on Y, the way the art parks a prop off the map. 2 to 3 hops 177 degrees. Frame
+// 4's quaternion is written negated, the same turn on the far side of the sphere, so 3 to 4 is a clean 18 degrees.
+// Frame 5 stands close enough to the identity past its end that a blend off the last frame would not count as a cut.
+#include "assetmgr.h"
+#include "chunkio.h"
+#include "hrawanim.h"
+#include "htree.h"
+#include "matrix3d.h"
+#include "ramfile.h"
+#include "wwmath.h"
+
+static void write_pose_channel( ChunkSaveClass &csave, int type, int vector_len, const float *values )
+{
+ W3dAnimChannelStruct channel;
+ memset( &channel, 0, sizeof( channel ) );
+ channel.LastFrame = 5;
+ channel.VectorLen = (uint16)vector_len;
+ channel.Flags = (uint16)type;
+ channel.Pivot = 1;
+ channel.Data[ 0 ] = values[ 0 ];
+ csave.Begin_Chunk( W3D_CHUNK_ANIMATION_CHANNEL );
+ csave.Write( &channel, sizeof( channel ) );
+ csave.Write( values + 1, ( 6 * vector_len - 1 ) * sizeof( float ) );
+ csave.End_Chunk();
+}
+
+static void write_pose_assets( RAMFileClass &file )
+{
+ file.Open( FileClass::WRITE );
+ ChunkSaveClass csave( &file );
+
+ W3dHierarchyStruct hierarchy;
+ memset( &hierarchy, 0, sizeof( hierarchy ) );
+ hierarchy.Version = W3D_CURRENT_HTREE_VERSION;
+ strcpy( hierarchy.Name, "POSETREE" );
+ hierarchy.NumPivots = 2;
+
+ W3dPivotStruct pivots[ 2 ];
+ memset( pivots, 0, sizeof( pivots ) );
+ strcpy( pivots[ 0 ].Name, "ROOT" );
+ pivots[ 0 ].ParentIdx = 0xffffffff;
+ pivots[ 0 ].Rotation.Q[ 3 ] = 1.0f;
+ strcpy( pivots[ 1 ].Name, "BONE" );
+ pivots[ 1 ].Rotation.Q[ 3 ] = 1.0f;
+
+ csave.Begin_Chunk( W3D_CHUNK_HIERARCHY );
+ csave.Begin_Chunk( W3D_CHUNK_HIERARCHY_HEADER );
+ csave.Write( &hierarchy, sizeof( hierarchy ) );
+ csave.End_Chunk();
+ csave.Begin_Chunk( W3D_CHUNK_PIVOTS );
+ csave.Write( pivots, sizeof( pivots ) );
+ csave.End_Chunk();
+ csave.End_Chunk();
+
+ W3dAnimHeaderStruct header;
+ memset( &header, 0, sizeof( header ) );
+ header.Version = W3D_CURRENT_HANIM_VERSION;
+ strcpy( header.Name, "POSE" );
+ strcpy( header.HierarchyName, "POSETREE" );
+ header.NumFrames = 6;
+ header.FrameRate = 30;
+
+ const float x[ 6 ] = { 0.0f, 10.0f, 20.0f, 30.0f, 40.0f, 15.0f };
+ const float y[ 6 ] = { 0.0f, 0.0f, 2000.0f, 2000.0f, 2000.0f, 0.0f };
+ const float degrees[ 6 ] = { 0.0f, 18.0f, 36.0f, 213.0f, 231.0f, 30.0f };
+ float q[ 24 ];
+ for( int frame = 0; frame < 6; ++frame )
+ {
+ const float half = degrees[ frame ] * 0.5f * WWMATH_PI / 180.0f;
+ const float sign = frame == 4 ? -1.0f : 1.0f;
+ q[ frame * 4 + 0 ] = 0.0f;
+ q[ frame * 4 + 1 ] = 0.0f;
+ q[ frame * 4 + 2 ] = sign * WWMath::Sin( half );
+ q[ frame * 4 + 3 ] = sign * WWMath::Cos( half );
+ }
+
+ csave.Begin_Chunk( W3D_CHUNK_ANIMATION );
+ csave.Begin_Chunk( W3D_CHUNK_ANIMATION_HEADER );
+ csave.Write( &header, sizeof( header ) );
+ csave.End_Chunk();
+ write_pose_channel( csave, ANIM_CHANNEL_X, 1, x );
+ write_pose_channel( csave, ANIM_CHANNEL_Y, 1, y );
+ write_pose_channel( csave, ANIM_CHANNEL_Q, 4, q );
+ csave.End_Chunk();
+
+ file.Close();
+}
+
+static float pose_degrees( const Matrix3D &pose )
+{
+ return atan2f( pose[ 1 ][ 0 ], pose[ 0 ][ 0 ] ) * 180.0f / WWMATH_PI;
+}
+
+TEST(raw_anim_pose_between_two_frames_lies_between_them)
+{
+ WWMath::Init();
+
+ static char buffer[ 4096 ];
+ RAMFileClass file( buffer, sizeof( buffer ) );
+ write_pose_assets( file );
+
+ WW3DAssetManager manager;
+ FileClass &assets = file;
+ CHECK( manager.Load_3D_Assets( assets ) );
+ HTreeClass *tree = manager.Get_HTree( "POSETREE" );
+ HAnimClass *anim = manager.Get_HAnim( "POSETREE.POSE" );
+ CHECK( tree != NULL );
+ CHECK( anim != NULL );
+ if( tree == NULL || anim == NULL )
+ return;
+ CHECK_EQ( anim->Class_ID(), (int)HAnimClass::CLASSID_HRAWANIM );
+
+ const Matrix3D root( true );
+ HRawAnimClass *raw = (HRawAnimClass *)anim;
+ const int bone = tree->Get_Bone_Index( "BONE" );
+
+ /* a whole frame is that frame, to the last digit of what the channel holds */
+ tree->Anim_Update( root, raw, 2.0f );
+ CHECK_NEAR( pose_degrees( tree->Get_Transform( bone ) ), 36.0f, 0.01f );
+ CHECK_EQ( tree->Get_Transform( bone ).Get_X_Translation(), 20.0f );
+ CHECK_EQ( tree->Get_Transform( bone ).Get_Y_Translation(), 2000.0f );
+
+ /* between frames 0 and 1 it lies between them */
+ tree->Anim_Update( root, raw, 0.5f );
+ CHECK_NEAR( pose_degrees( tree->Get_Transform( bone ) ), 9.0f, 0.1f );
+ CHECK_NEAR( tree->Get_Transform( bone ).Get_X_Translation(), 5.0f, 0.001f );
+
+ /* a jump on Y holds all three axes on the frame nearest, or X would zigzag; the turn still blends */
+ tree->Anim_Update( root, raw, 1.25f );
+ CHECK_EQ( tree->Get_Transform( bone ).Get_X_Translation(), 10.0f );
+ CHECK_EQ( tree->Get_Transform( bone ).Get_Y_Translation(), 0.0f );
+ CHECK_NEAR( pose_degrees( tree->Get_Transform( bone ) ), 22.5f, 0.1f );
+ tree->Anim_Update( root, raw, 1.75f );
+ CHECK_EQ( tree->Get_Transform( bone ).Get_X_Translation(), 20.0f );
+ CHECK_EQ( tree->Get_Transform( bone ).Get_Y_Translation(), 2000.0f );
+ CHECK_NEAR( pose_degrees( tree->Get_Transform( bone ) ), 31.5f, 0.1f );
+
+ /* a hop over 90 degrees holds the frame nearest, or it would sweep the short way round; X still blends.
+ 213 degrees reads back as -147 */
+ tree->Anim_Update( root, raw, 2.25f );
+ CHECK_NEAR( pose_degrees( tree->Get_Transform( bone ) ), 36.0f, 0.01f );
+ CHECK_NEAR( tree->Get_Transform( bone ).Get_X_Translation(), 22.5f, 0.001f );
+ tree->Anim_Update( root, raw, 2.75f );
+ CHECK_NEAR( pose_degrees( tree->Get_Transform( bone ) ), -147.0f, 0.01f );
+ CHECK_NEAR( tree->Get_Transform( bone ).Get_X_Translation(), 27.5f, 0.001f );
+
+ /* the same small turn written as the negated quaternion is no hop: halfway from 213 to 231 is 222, or -138 */
+ tree->Anim_Update( root, raw, 3.5f );
+ CHECK_NEAR( pose_degrees( tree->Get_Transform( bone ) ), -138.0f, 0.1f );
+
+ /* past the last frame there is nothing to blend toward: frame 5 itself, where a blend toward the channel's
+ identity past its end would read 18 degrees and 9 units */
+ tree->Anim_Update( root, raw, 5.4f );
+ CHECK_NEAR( pose_degrees( tree->Get_Transform( bone ) ), 30.0f, 0.01f );
+ CHECK_EQ( tree->Get_Transform( bone ).Get_X_Translation(), 15.0f );
+
+ anim->Release_Ref();
+}