Allow for multple GLTF/GLB to be loaded at the same time.

This commit is contained in:
HarrievG 2022-08-07 15:56:46 +02:00
parent d0ff0a7f64
commit ecaf297ef6
3 changed files with 36 additions and 47 deletions

View file

@ -1948,7 +1948,7 @@ bool GLTF_Parser::loadGLB( idStr filename )
unsigned int chunk_type = 0; // 4 bytes
unsigned int chunk_length = 0; // 4 bytes
byte* data = nullptr;
gltfData* dataCache = gltfData::Data( filename );
gltfData* dataCache = gltfData::Data( filename, true );
currentAsset = dataCache;
int chunkCount = 0;
@ -2074,21 +2074,18 @@ bool GLTF_Parser::Parse()
bool GLTF_Parser::Load( idStr filename )
{
//seriously fix this; proper gltf data cache.
//.. and destroy it properly too!!
static idStr lastFile = "";
//next line still has to be fixed.
//gfx is not updated on command
common->SetRefreshOnPrint( true );
//.. and destroy data !!
if( lastFile == filename )
gltfData* data = gltfData::Data( filename );
currentFile = filename;
if( data != nullptr )
{
common->Warning( "Did not parse %s again", filename.c_str() );
currentAsset = data;
return true;
}
lastFile = filename;
currentFile = filename;
common->SetRefreshOnPrint( true );
if( filename.CheckExtension( ".glb" ) )
{
if( !loadGLB( filename ) )
@ -2104,7 +2101,7 @@ bool GLTF_Parser::Load( idStr filename )
common->FatalError( "Failed to read file" );
}
gltfData* data = gltfData::Data( filename );
data = gltfData::Data( filename , true );
data->FileName( filename );
byte* dataBuff = data->AddData( length );
currentAsset = data;

View file

@ -793,18 +793,26 @@ public:
static idHashIndex fileDataHash;
static idList<gltfData*> dataList;
//add data from filename
static gltfData* Data( idStr& fileName )
//add data for filename
static gltfData* Data( idStr& fileName, bool create = false )
{
dataList.AssureSizeAlloc( dataList.Num() + 1, idListNewElement<gltfData> );
dataList[dataList.Num() - 1]->FileName( fileName );
fileDataHash.Add( fileDataHash.GenerateKey( fileName ), dataList.Num() - 1 );
return dataList[dataList.Num() - 1];
}
//find data;
static gltfData* Data( const char* filename )
{
return dataList[fileDataHash.First( fileDataHash.GenerateKey( filename ) )];
int key = fileDataHash.GenerateKey( fileName );
int index = fileDataHash.GetFirst( key );
if( create && index == -1 )
{
index = dataList.Num( );
dataList.AssureSizeAlloc( index + 1, idListNewElement<gltfData> );
dataList[index]->FileName( fileName );
fileDataHash.Add( fileDataHash.GenerateKey( fileName ), index );
}
if( !create && index < 0 )
{
return nullptr;
}
return dataList[index];
}
static const idList<gltfData*>& DataList()
{

View file

@ -163,18 +163,7 @@ void idRenderModelGLTF::InitFromFile( const char* fileName )
model_state = DM_STATIC;
gltfManager::ExtractIdentifier( gltfFileName, meshID, meshName );
if( gltfParser->currentFile.Length() )
{
if( gltfParser->currentAsset && gltfParser->currentFile != gltfFileName )
{
common->FatalError( "multiple GLTF file loading not supported" );
}
}
else
{
gltfParser->Load( gltfFileName );
}
gltfParser->Load( gltfFileName );
timeStamp = fileSystem->GetTimestamp( gltfFileName );
data = gltfParser->currentAsset;
@ -275,6 +264,12 @@ bool idRenderModelGLTF::LoadBinaryModel( idFile* file, const ID_TIME_T sourceTim
hasAnimations = false;
fileExclusive = false; // not written.
root = nullptr;
if( !idRenderModelStatic::LoadBinaryModel( file, sourceTimeStamp ) )
{
return false;
}
unsigned int magic = 0;
file->ReadBig( magic );
@ -607,18 +602,7 @@ idFile_Memory* idRenderModelGLTF::GetAnimBin( idStr animName , const ID_TIME_T
idStr gltfFileName = idStr( animName );
idStr name;
gltfManager::ExtractIdentifier( gltfFileName, id, name );
if( gltfParser->currentFile.Length( ) )
{
if( gltfParser->currentAsset && gltfParser->currentFile != gltfFileName )
{
common->FatalError( "multiple GLTF file loading not supported" );
}
}
else
{
gltfParser->Load( gltfFileName );
}
gltfParser->Load( gltfFileName );
gltfData* data = gltfParser->currentAsset;