Small bugfix when it comes to entity rotations in .glb maps

This commit is contained in:
Robert Beckebans 2025-01-21 19:07:40 +01:00
parent 7a30d447b7
commit 4904fdbf1b

View file

@ -3,7 +3,7 @@
Doom 3 BFG Edition GPL Source Code
Copyright (C) 2022 Harrie van Ginneken
Copyright (C) 2022 Robert Beckebans
Copyright (C) 2022-2025 Robert Beckebans
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
@ -32,7 +32,6 @@ If you have questions concerning this license or the applicable additional terms
// files import as y-up. Use this transform to change the model to z-up.
static const idMat4 blenderToDoomTransform( idAngles( 0.0f, 0.0f, 90 ).ToMat3(), vec3_origin );
//static const idMat4 blenderToDoomTransform = mat4_identity;
MapPolygonMesh* MapPolygonMesh::ConvertFromMeshGltf( const gltfMesh_Primitive* prim, gltfData* _data , const idMat4& transform )
{
@ -409,7 +408,7 @@ static void AddMeshesToWorldspawn_r( idMapEntity* entity, gltfNode* node, const
}
};
void ResolveLight( gltfData* data, idMapEntity* newEntity, gltfNode* node )
static void ResolveLight( gltfData* data, idMapEntity* newEntity, gltfNode* node )
{
assert( node && node->extensions.KHR_lights_punctual );
@ -493,7 +492,7 @@ void ResolveLight( gltfData* data, idMapEntity* newEntity, gltfNode* node )
}
void ResolveEntity( gltfData* data, idMapEntity* newEntity, gltfNode* node )
static void ResolveEntity( gltfData* data, idMapEntity* newEntity, gltfNode* node )
{
const char* classname = node->extras.strPairs.GetString( "classname" );
@ -533,9 +532,11 @@ void ResolveEntity( gltfData* data, idMapEntity* newEntity, gltfNode* node )
if( node->extensions.KHR_lights_punctual != nullptr )
{
ResolveLight( data, newEntity, node );
return;
}
// HarrievG: TODO cleanup this was done by try & error until it worked
#if 0
if( node->camera >= 0 && !newEntity->epairs.FindKey( "rotation" ) )
{
idQuat q = entityToWorldTransform.ToMat3().ToQuat();
@ -545,7 +546,7 @@ void ResolveEntity( gltfData* data, idMapEntity* newEntity, gltfNode* node )
else if( idStr::Icmp( classname, "info_player_start" ) == 0 && !newEntity->epairs.FindKey( "rotation" ) )
{
idQuat q = entityToWorldTransform.ToMat3().ToQuat();
q = idAngles( -90.0f, 0.0, -90.0f ).ToQuat() * q * blenderToDoomTransform.ToMat3().ToQuat();
q = idAngles( 90.0f, 0.0, -90.0f ).ToQuat() * q * blenderToDoomTransform.ToMat3().ToQuat();
newEntity->epairs.SetMatrix( "rotation", q.ToMat3() );
}
else if( node->extras.strPairs.GetBool( "useNodeOrientation", false ) )
@ -561,6 +562,24 @@ void ResolveEntity( gltfData* data, idMapEntity* newEntity, gltfNode* node )
//idMat3 rot = ( blenderToDoomTransform * entityToWorldTransform ).ToMat3();
newEntity->epairs.SetMatrix( "rotation", rot );
}
#else
if( node->camera >= 0 && !newEntity->epairs.FindKey( "rotation" ) )
{
idQuat q = entityToWorldTransform.ToMat3().ToQuat();
q = idAngles( 90.0f, 0.0, -90.0f ).ToQuat() * q * blenderToDoomTransform.ToMat3().ToQuat();
newEntity->epairs.SetMatrix( "rotation", q.ToMat3() );
}
else
{
idQuat q = entityToWorldTransform.ToMat3().ToQuat();
q = idAngles( 0.0f, 0.0f, -90.0f ).ToQuat() * q * blenderToDoomTransform.ToMat3().ToQuat();
newEntity->epairs.SetMatrix( "rotation", q.ToMat3() );
// FIXME this should be
//idMat3 rot = ( blenderToDoomTransform * entityToWorldTransform ).ToMat3();
//newEntity->epairs.SetMatrix( "rotation", rot );
}
#endif
#if 0
for( int i = 0; i < newEntity->epairs.GetNumKeyVals(); i++ )
@ -572,20 +591,23 @@ void ResolveEntity( gltfData* data, idMapEntity* newEntity, gltfNode* node )
#endif
}
int FindEntities( gltfData* data, idMapEntity::EntityListRef entities, gltfNode* node , idDict epairs , idMapEntity* worldspawn )
static int FindEntities_r( gltfData* data, idMapEntity::EntityListRef entities, gltfNode* node , idDict epairs , idMapEntity* worldspawn )
{
int entityCount = 0;
const char* classname = node->extras.strPairs.GetString( "classname" );
// skip all nodes with "worldspawn." or "BSP" in the name
if( idStr::Icmpn( node->name, "BSP", 3 ) != 0
&& idStr::Icmpn( node->name, "worldspawn.", 11 ) != 0 )
if( idStr::Icmpn( node->name, "BSP", 3 ) == 0 || idStr::Icmpn( node->name, "worldspawn.", 11 ) == 0 )
{
AddMeshesToWorldspawn_r( worldspawn, node, mat4_identity, data );
}
else
{
idStr classnameStr = node->extras.strPairs.GetString( "classname" );
// skip everything that is not an entity
if( !classnameStr.IsEmpty() )
// skip everything that is not an entity except lights
if( !classnameStr.IsEmpty() || node->extensions.KHR_lights_punctual )
{
auto* newEntity = new( TAG_IDLIB_GLTF ) idMapEntity();
entities.Append( newEntity );
@ -604,14 +626,10 @@ int FindEntities( gltfData* data, idMapEntity::EntityListRef entities, gltfNode*
}
}
}
else
{
AddMeshesToWorldspawn_r( worldspawn, node, mat4_identity, data );
}
for( auto& child : node->children )
{
entityCount += FindEntities( data, entities, data->NodeList()[child], epairs , worldspawn );
entityCount += FindEntities_r( data, entities, data->NodeList()[child], epairs , worldspawn );
}
return entityCount;
@ -648,7 +666,14 @@ int idMapEntity::GetEntities( gltfData* data, EntityListRef entities, int sceneI
else
{
idStr classnameStr = node->extras.strPairs.GetString( "classname" );
bool skipInline = !node->extras.strPairs.GetBool( "inline", true );
idStr model = node->extras.strPairs.GetString( "model" );
// inline should be false by default because drag n drop in the asset browser in Blender is Append by default
// however it would break previous maps
// in Doom 3 maps inline entities have as model key the same name as the entity name
const bool defaultInline = ( idStr::Icmp( model.c_str(), node->name.c_str() ) == 0 );
bool skipInline = !node->extras.strPairs.GetBool( "inline", defaultInline );
idDict epairs;
// skip everything that is not an entity except lights
@ -678,10 +703,11 @@ int idMapEntity::GetEntities( gltfData* data, EntityListRef entities, int sceneI
epairs.Copy( node->extras.strPairs );
}
}
// add entities from all subnodes
for( auto& child : node->children )
{
entityCount += FindEntities( data, entities, data->NodeList()[child] , epairs, worldspawn );
entityCount += FindEntities_r( data, entities, data->NodeList()[child] , epairs, worldspawn );
}
}
}