From 3c1bcc2153429c67419d35e23ceaee0377ed772a Mon Sep 17 00:00:00 2001 From: Robert Beckebans Date: Mon, 5 Sep 2022 19:29:11 +0200 Subject: [PATCH] Fixed Linux compile issues caused by glTF2 code --- neo/idlib/MapFile_gltf.cpp | 5 +- neo/idlib/containers/List.h | 70 ++++++++++++- neo/idlib/containers/StaticList.h | 8 ++ neo/idlib/gltfParser.cpp | 9 +- neo/idlib/gltfParser.h | 7 +- neo/idlib/gltfProperties.h | 163 +++++++++++++++++++++++++++++- neo/renderer/Model_gltf.cpp | 53 ++++------ neo/renderer/Model_gltf.h | 2 + 8 files changed, 269 insertions(+), 48 deletions(-) diff --git a/neo/idlib/MapFile_gltf.cpp b/neo/idlib/MapFile_gltf.cpp index 2f6a9103..5537c6ba 100644 --- a/neo/idlib/MapFile_gltf.cpp +++ b/neo/idlib/MapFile_gltf.cpp @@ -95,8 +95,11 @@ MapPolygonMesh* MapPolygonMesh::ConvertFromMeshGltf( const gltfMesh_Primitive* p Mem_Free( indices ); bool sizeSet = false; - for( auto& attrib : prim->attributes ) + //for( const auto& attrib : prim->attributes ) + for( int a = 0; a < prim->attributes.Num(); a++ ) { + gltfMesh_Primitive_Attribute* attrib = prim->attributes[a]; + gltfAccessor* attrAcc = data->AccessorList()[attrib->accessorIndex]; gltfBufferView* attrBv = data->BufferViewList()[attrAcc->bufferView]; gltfData* attrData = attrBv->parent; diff --git a/neo/idlib/containers/List.h b/neo/idlib/containers/List.h index 58c6ff0c..7ae0afe4 100644 --- a/neo/idlib/containers/List.h +++ b/neo/idlib/containers/List.h @@ -3,6 +3,7 @@ Doom 3 BFG Edition GPL Source Code Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. +Copyright (C) 2022 Stephen Pridham This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). @@ -30,6 +31,7 @@ If you have questions concerning this license or the applicable additional terms #define __LIST_H__ #include +#include /* =============================================================================== @@ -124,6 +126,7 @@ public: idList( int newgranularity = 16 ); idList( const idList& other ); + idList( std::initializer_list<_type_> initializerList ); ~idList(); void Clear(); // clear the list @@ -200,6 +203,7 @@ public: memTag = ( byte )tag_; }; + /* struct Iterator { _type_* p; @@ -225,6 +229,54 @@ public: { return Iterator{list + Num()}; }; + */ + + // Begin/End methods for range-based for loops. + _type_* begin() + { + if( num > 0 ) + { + return &list[0]; + } + else + { + return nullptr; + } + } + _type_* end() + { + if( num > 0 ) + { + return &list[num - 1]; + } + else + { + return nullptr; + } + } + + const _type_* begin() const + { + if( num > 0 ) + { + return &list[0]; + } + else + { + return nullptr; + } + } + const _type_* end() const + { + if( num > 0 ) + { + return &list[num - 1]; + } + else + { + return nullptr; + } + } private: int num; @@ -262,6 +314,14 @@ ID_INLINE idList<_type_, _tag_>::idList( const idList& other ) *this = other; } +template< typename _type_, memTag_t _tag_ > +ID_INLINE idList<_type_, _tag_>::idList( std::initializer_list<_type_> initializerList ) + : idList( 16 ) +{ + SetNum( initializerList.size() ); + std::copy( initializerList.begin(), initializerList.end(), list ); +} + /* ================ idList<_type_,_tag_>::~idList< _type_, _tag_ > @@ -312,7 +372,10 @@ ID_INLINE void idList<_type_, _tag_>::DeleteContents( bool clear ) for( i = 0; i < num; i++ ) { - delete list[ i ]; + if( list[i] ) + { + delete list[i]; + } list[ i ] = NULL; } @@ -548,7 +611,6 @@ ID_INLINE void idList<_type_, _tag_>::AssureSize( int newSize ) if( newSize > size ) { - if( granularity == 0 ) // this is a hack to fix our memset classes { granularity = 16; @@ -557,9 +619,9 @@ ID_INLINE void idList<_type_, _tag_>::AssureSize( int newSize ) newSize += granularity - 1; newSize -= newSize % granularity; Resize( newSize ); - } - num = newNum; + num = newNum; + } } /* diff --git a/neo/idlib/containers/StaticList.h b/neo/idlib/containers/StaticList.h index 83e965f7..1ae85a2f 100644 --- a/neo/idlib/containers/StaticList.h +++ b/neo/idlib/containers/StaticList.h @@ -47,6 +47,7 @@ public: idStaticList(); idStaticList( const idStaticList& other ); + idStaticList( std::initializer_list initializerList ); ~idStaticList(); void Clear(); // marks the list as empty. does not deallocate or intialize data. @@ -114,6 +115,13 @@ ID_INLINE idStaticList::idStaticList( const idStaticList *this = other; } +template +ID_INLINE idStaticList::idStaticList( std::initializer_list initializerList ) +{ + SetNum( initializerList.size() ); + std::copy( initializerList.begin(), initializerList.end(), list ); +} + /* ================ idStaticList::~idStaticList diff --git a/neo/idlib/gltfParser.cpp b/neo/idlib/gltfParser.cpp index 50ae2953..1cd55590 100644 --- a/neo/idlib/gltfParser.cpp +++ b/neo/idlib/gltfParser.cpp @@ -290,6 +290,11 @@ gltfPropertyArray::Iterator gltfPropertyArray::end() return Iterator{ this , endPtr}; } +gltfItemArray::~gltfItemArray() +{ + items.DeleteContents( true ); +} + int gltfItemArray::Fill( idLexer* lexer, idDict* strPairs ) { idToken token; @@ -1975,7 +1980,7 @@ bool GLTF_Parser::loadGLB( idStr filename ) int read = file->Read( ( void* )data, chunk_length ); if( read != chunk_length ) { - common->FatalError( "Could not read full chunk (%i bytes) in file %s", chunk_length, filename ); + common->FatalError( "Could not read full chunk (%i bytes) in file %s", chunk_length, filename.c_str() ); } length -= read; if( chunk_type == gltfChunk_Type_JSON ) @@ -2075,7 +2080,7 @@ bool GLTF_Parser::Parse() } else { - common->FatalError( "%s not fully loaded.", currentFile ); + common->FatalError( "%s not fully loaded.", currentFile.c_str() ); } buffersDone = false; diff --git a/neo/idlib/gltfParser.h b/neo/idlib/gltfParser.h index 7eb9a048..654443f6 100644 --- a/neo/idlib/gltfParser.h +++ b/neo/idlib/gltfParser.h @@ -37,6 +37,8 @@ If you have questions concerning this license or the applicable additional terms struct parsable { public: + virtual ~parsable() {} + virtual void parse( idToken& token ) = 0; virtual void parse( idToken& token , idLexer* parser ) {}; virtual idStr& Name() = 0; @@ -229,10 +231,7 @@ gltfItemClass( boolean, bool, if( token.Icmp( "true" ) == 0 ) *item = true; else class gltfItemArray { public: - ~gltfItemArray() - { - items.DeleteContents( true ); - } + ~gltfItemArray(); gltfItemArray() { }; int Num() { diff --git a/neo/idlib/gltfProperties.h b/neo/idlib/gltfProperties.h index 4102c123..42fd6b3a 100644 --- a/neo/idlib/gltfProperties.h +++ b/neo/idlib/gltfProperties.h @@ -752,9 +752,9 @@ public: ///////////////////////////////////////////////////////////////////////////// //// For these to function you need to add an private idList {target} -#define GLTFCACHEITEM(name,target) \ -gltf##name * name () { target.AssureSizeAlloc( target.Num()+1,idListNewElement); return target[target.Num()-1];} \ -const inline idList & ##name##List() { return target; } +//#define GLTFCACHEITEM(name,target) \ +//gltf##name * name () { target.AssureSizeAlloc( target.Num()+1,idListNewElement); return target[target.Num()-1];} \ +//const inline idList & ##name##List() { return target; } // URI's are resolved during parsing so that @@ -1254,6 +1254,162 @@ public: { return scene; } + +//#define GLTFCACHEITEM(name,target) \ +//gltf##name * name () { target.AssureSizeAlloc( target.Num()+1,idListNewElement); return target[target.Num()-1];} \ +//const inline idList & ##name##List() { return target; } + + gltfBuffer* Buffer() + { + buffers.AssureSizeAlloc( buffers.Num() + 1 , idListNewElement ); + return buffers[buffers.Num() - 1]; + } + const inline idList& BufferList() + { + return buffers; + } + + gltfSampler* Sampler() + { + samplers.AssureSizeAlloc( samplers.Num() + 1 , idListNewElement ); + return samplers[samplers.Num() - 1]; + } + const inline idList& SamplerList() + { + return samplers; + } + + gltfBufferView* BufferView() + { + bufferViews.AssureSizeAlloc( bufferViews.Num() + 1 , idListNewElement ); + return bufferViews[bufferViews.Num() - 1]; + } + const inline idList& BufferViewList() + { + return bufferViews; + } + + gltfImage* Image() + { + images.AssureSizeAlloc( images.Num() + 1 , idListNewElement ); + return images[images.Num() - 1]; + } + const inline idList& ImageList() + { + return images; + } + + gltfTexture* Texture() + { + textures.AssureSizeAlloc( textures.Num() + 1 , idListNewElement ); + return textures[textures.Num() - 1]; + } + const inline idList& TextureList() + { + return textures; + } + + gltfAccessor* Accessor() + { + accessors.AssureSizeAlloc( accessors.Num() + 1 , idListNewElement ); + return accessors[accessors.Num() - 1]; + } + const inline idList& AccessorList() + { + return accessors; + } + + gltfExtensionsUsed* ExtensionsUsed() + { + extensionsUsed.AssureSizeAlloc( accessors.Num() + 1 , idListNewElement ); + return extensionsUsed[extensionsUsed.Num() - 1]; + } + const inline idList& ExtensionsUsedList() + { + return extensionsUsed; + } + + gltfMesh* Mesh() + { + meshes.AssureSizeAlloc( meshes.Num() + 1 , idListNewElement ); + return meshes[meshes.Num() - 1]; + } + const inline idList& MeshList() + { + return meshes; + } + + gltfScene* Scene() + { + scenes.AssureSizeAlloc( scenes.Num() + 1 , idListNewElement ); + return scenes[scenes.Num() - 1]; + } + const inline idList& SceneList() + { + return scenes; + } + + gltfNode* Node() + { + nodes.AssureSizeAlloc( nodes.Num() + 1 , idListNewElement ); + return nodes[nodes.Num() - 1]; + } + const inline idList& NodeList() + { + return nodes; + } + + gltfCamera* Camera() + { + cameras.AssureSizeAlloc( cameras.Num() + 1 , idListNewElement ); + return cameras[cameras.Num() - 1]; + } + const inline idList& CameraList() + { + return cameras; + } + + gltfMaterial* Material() + { + materials.AssureSizeAlloc( materials.Num() + 1 , idListNewElement ); + return materials[materials.Num() - 1]; + } + const inline idList& MaterialList() + { + return materials; + } + + gltfExtensions* Extensions() + { + extensions.AssureSizeAlloc( extensions.Num() + 1 , idListNewElement ); + return extensions[extensions.Num() - 1]; + } + const inline idList& ExtensionsList() + { + return extensions; + } + + gltfAnimation* Animation() + { + animations.AssureSizeAlloc( animations.Num() + 1 , idListNewElement ); + return animations[animations.Num() - 1]; + } + const inline idList& AnimationList() + { + return animations; + } + + gltfSkin* Skin() + { + skins.AssureSizeAlloc( skins.Num() + 1 , idListNewElement ); + return skins[skins.Num() - 1]; + } + const inline idList& SkinList() + { + return skins; + } + + /* GLTFCACHEITEM( Buffer, buffers ) GLTFCACHEITEM( Sampler, samplers ) GLTFCACHEITEM( BufferView, bufferViews ) @@ -1269,6 +1425,7 @@ public: GLTFCACHEITEM( Extensions, extensions ) GLTFCACHEITEM( Animation, animations ) GLTFCACHEITEM( Skin, skins ) + */ //gltfCameraManager * cameraManager; private: diff --git a/neo/renderer/Model_gltf.cpp b/neo/renderer/Model_gltf.cpp index 2cf6bc6a..5590bed0 100644 --- a/neo/renderer/Model_gltf.cpp +++ b/neo/renderer/Model_gltf.cpp @@ -357,6 +357,20 @@ bool idRenderModelGLTF::LoadBinaryModel( idFile* file, const ID_TIME_T sourceTim return true; } +const idMD5Joint* idRenderModelGLTF::FindMD5Joint( const idStr& name ) const +{ + for( auto& joint : md5joints ) + { + if( joint.name == name ) + { + return &joint; + } + } + assert( 0 ); + static idMD5Joint staticJoint; + return &staticJoint; +} + void idRenderModelGLTF::UpdateMd5Joints() { md5joints.Clear(); @@ -386,20 +400,6 @@ void idRenderModelGLTF::UpdateMd5Joints() } } - auto findMd5Joint = [&]( idStr & name ) -> auto - { - for( auto& joint : md5joints ) - { - if( joint.name == name ) - { - return &joint; - } - } - assert( 0 ); - static idMD5Joint staticJoint; - return &staticJoint; - }; - for( int i = 0 ; i < bones.Num(); i++ ) { gltfNode* node = nodeList[bones[i]]; @@ -407,11 +407,11 @@ void idRenderModelGLTF::UpdateMd5Joints() { if( node->parent->name == ovrBoneName ) { - md5joints[i].parent = findMd5Joint( idStr( "origin" ) ); + md5joints[i].parent = FindMD5Joint( idStr( "origin" ) ); } else { - md5joints[i].parent = findMd5Joint( node->parent->name ); + md5joints[i].parent = FindMD5Joint( node->parent->name ); } } } @@ -715,7 +715,7 @@ idFile_Memory* idRenderModelGLTF::GetAnimBin( idStr animName , const ID_TIME_T // max_timestamp_value / totalFrames // but keeping it fixed for now. frameRate = gltf_AnimSampleRate.GetInteger(); - INT animLength = ( ( numFrames - 1 ) * 1000 + frameRate - 1 ) / frameRate; + int animLength = ( ( numFrames - 1 ) * 1000 + frameRate - 1 ) / frameRate; for( int i = 0; i < jointInfo.Num(); i++ ) { @@ -1152,28 +1152,13 @@ void idRenderModelGLTF::LoadModel() } } - auto findMd5Joint = [&]( idStr & name ) -> auto - { - for( auto& joint : md5joints ) - { - if( joint.name == name ) - { - return &joint; - } - } - assert( 0 ); - static idMD5Joint staticJoint; - return &staticJoint; - }; - - for( int i = 0; i < bones.Num(); i++ ) { auto* node = nodes[bones[i]]; if( i && node->parent && node->parent != root ) { - md5joints[i].parent = findMd5Joint( node->parent->name ); + md5joints[i].parent = FindMD5Joint( node->parent->name ); } } @@ -1658,7 +1643,7 @@ const idJointQuat* idRenderModelGLTF::GetDefaultPose() const int idRenderModelGLTF::NearestJoint( int surfaceNum, int a, int b, int c ) const { - for( modelSurface_t& surf : surfaces ) + for( const modelSurface_t& surf : surfaces ) { idDrawVert* verts = surf.geometry->verts; int numVerts = surf.geometry->numVerts; diff --git a/neo/renderer/Model_gltf.h b/neo/renderer/Model_gltf.h index 83b79ccc..6c7cdd0a 100644 --- a/neo/renderer/Model_gltf.h +++ b/neo/renderer/Model_gltf.h @@ -60,6 +60,8 @@ private: void ProcessNode( 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(); + + const idMD5Joint* FindMD5Joint( const idStr& name ) const; gltfData* data; gltfNode* root; bool fileExclusive;