From 02941624f918d2fcc66f02098eadf77b2696756a Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Mon, 26 Sep 2022 00:31:47 +0200 Subject: [PATCH] Repaired glTF2 animations for the Y-Up case --- neo/renderer/Model_gltf.cpp | 37 ++++++++++++++++++++----------------- neo/renderer/Model_gltf.h | 2 +- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/neo/renderer/Model_gltf.cpp b/neo/renderer/Model_gltf.cpp index 3f42932d..3ce2fd21 100644 --- a/neo/renderer/Model_gltf.cpp +++ b/neo/renderer/Model_gltf.cpp @@ -48,9 +48,8 @@ idCVar gltf_AnimSampleRate( "gltf_AnimSampleRate", "24", CVAR_SYSTEM | CVAR_INTE static const byte GLMB_VERSION = 101; static const unsigned int GLMB_MAGIC = ( 'M' << 24 ) | ( 'L' << 16 ) | ( 'G' << 8 ) | GLMB_VERSION; static const char* GLTF_SnapshotName = "_GLTF_Snapshot_"; -static const idAngles blenderToDoomAngels = idAngles( 0.0f, 0.0f, 90 ); -//static const idMat4 blenderToDoomTransform( blenderToDoomAngels.ToMat3(), vec3_origin ); -static const idMat4 blenderToDoomTransform = mat4_identity; +static const idMat4 blenderToDoomTransform( idAngles( 0.0f, 0.0f, 90 ).ToMat3(), vec3_origin ); +//static const idMat4 blenderToDoomTransform = mat4_identity; static idRenderModelGLTF* lastMeshFromFile = nullptr; bool idRenderModelStatic::ConvertGltfMeshToModelsurfaces( const gltfMesh* mesh ) @@ -58,34 +57,34 @@ bool idRenderModelStatic::ConvertGltfMeshToModelsurfaces( const gltfMesh* mesh ) return false; } -void idRenderModelGLTF::ProcessNode( gltfNode* modelNode, idMat4 trans, gltfData* data ) +void idRenderModelGLTF::ProcessNode_r( gltfNode* modelNode, idMat4 parentTransform, gltfData* data ) { auto& meshList = data->MeshList(); auto& nodeList = data->NodeList(); gltfData::ResolveNodeMatrix( modelNode ); - idMat4 curTrans = trans * modelNode->matrix; + idMat4 nodeToWorldTransform = parentTransform * modelNode->matrix; if( modelNode->mesh >= 0 ) { gltfMesh* targetMesh = meshList[modelNode->mesh]; - idMat4 newTrans; + idMat4 animTransform; if( !animIds.Num() ) { - newTrans = curTrans; + animTransform = nodeToWorldTransform; } else { - newTrans = mat4_identity; + animTransform = mat4_identity; } for( auto prim : targetMesh->primitives ) { //ConvertFromMeshGltf should only be used for the map, ConvertGltfMeshToModelsurfaces should be used. - auto* mesh = MapPolygonMesh::ConvertFromMeshGltf( prim, data, newTrans ); + auto* mesh = MapPolygonMesh::ConvertFromMeshGltf( prim, data, animTransform * blenderToDoomTransform ); modelSurface_t surf; gltfMaterial* mat = NULL; @@ -138,7 +137,7 @@ void idRenderModelGLTF::ProcessNode( gltfNode* modelNode, idMat4 trans, gltfData for( auto& child : modelNode->children ) { - ProcessNode( nodeList[child], curTrans, data ); + ProcessNode_r( nodeList[child], nodeToWorldTransform, data ); } } @@ -243,7 +242,7 @@ void idRenderModelGLTF::InitFromFile( const char* fileName ) hasAnimations = totalAnims > 0; model_state = hasAnimations ? DM_CACHED : DM_STATIC; - ProcessNode( root, mat4_identity, data ); + ProcessNode_r( root, mat4_identity, data ); if( surfaces.Num() <= 0 ) { @@ -538,7 +537,7 @@ idList GetPose( idList& bones, idJointMat* poseMat ) } idJointQuat& pose = ret[i]; - pose.q = ( trans.ToMat3().Transpose().ToQuat() ); + pose.q = ( trans.ToMat3().ToQuat() ); pose.t = idVec3( trans[0][3], trans[1][3], trans[2][3] ); pose.w = pose.q.CalcW(); } @@ -830,11 +829,15 @@ idFile_Memory* idRenderModelGLTF::GetAnimBin( idStr animName , const ID_TIME_T { if( node->parent == nullptr ) { - q = blenderToDoomAngels.ToQuat() * animBones[i][b].rotation; - if( animBones[i].Num() == 1 ) - { - q = -animBones[i][b].rotation; - } + //q = blenderToDoomAngels.ToQuat() * animBones[i][b].rotation; + + q = blenderToDoomTransform.ToMat3().ToQuat() * animBones[i][b].rotation; + + //if( animBones[i].Num() == 1 ) + //{ + // this is not hit + // q = -animBones[i][b].rotation; + //} } else { diff --git a/neo/renderer/Model_gltf.h b/neo/renderer/Model_gltf.h index a8f2023c..c8a5bf32 100644 --- a/neo/renderer/Model_gltf.h +++ b/neo/renderer/Model_gltf.h @@ -57,7 +57,7 @@ public: static idFile_Memory* GetAnimBin( idStr animName, const ID_TIME_T sourceTimeStamp ); int rootID; private: - void ProcessNode( gltfNode* modelNode, idMat4 trans, gltfData* data ); + void ProcessNode_r( gltfNode* modelNode, idMat4 trans, gltfData* data ); void UpdateSurface( const struct renderEntity_s* ent, const idJointMat* entJoints, const idJointMat* entJointsInverted, modelSurface_t* surf, const modelSurface_t& sourceSurf ); void UpdateMd5Joints();