From 24f2a1e9ab47588174d91360bf7617162f31867b Mon Sep 17 00:00:00 2001 From: cutealien Date: Tue, 19 Apr 2022 13:10:16 +0000 Subject: [PATCH] Rename IMeshLoader::setPreferredIndexType to setIndexTypeHint and give it a new enum. Giving the loader it's own enum allowed me to add more options. EITH_OPTIMAL which reduces buffers back to 16-bit if they don't need 32-bit. Using that in obj loader. Also 32-bit meshes with EMT_PARALLAX_MAP_SOLID material should now work in obj loader (untested as I got no test-case) git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6340 dfc29bdd-3216-0410-991c-e03cc46cb475 --- changes.txt | 4 ++-- include/IMeshLoader.h | 28 +++++++++++++++++++------- source/Irrlicht/COBJMeshFileLoader.cpp | 20 +++++++++--------- source/Irrlicht/COBJMeshFileLoader.h | 4 +++- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/changes.txt b/changes.txt index 92b9cd7..cc28922 100644 --- a/changes.txt +++ b/changes.txt @@ -2,9 +2,9 @@ Changes in 1.9 (not yet released) - Add IMeshBufffer::clone function to create buffer copies. CMeshManipulator::createMeshCopy uses that now and works now with all types of meshbuffers. - obj writer can now write 32 bit buffers -- obj meshloader can now load 32 bit buffers when setPreferredIndexType is set to EIT_32BIT. +- obj meshloader can now load 32 bit buffers when setIndexTypeHint is set correspondingly It's 16 bit meshes use now also an IDynamicMeshbuffer instead of an SMeshBuffer. -- Add IMeshLoader::setPreferredIndexType and getPreferredIndexType to allow setting hints for the loaders if users prefer 16 or 32 bit meshbuffers. +- Add IMeshLoader::setIndexTypeHint and getIndexTypeHint to allow setting hints for the loaders if users prefer 16 or 32 bit meshbuffers. - Add IMeshBuffer::getType to allow finding out which class type a meshbuffer has (similar to ISceneNode::getType). - Add IGUIImage::flip to flip/mirror images - IBillboardSceneNode got functions to access meshbuffers. So uv-coordinates can now be modified directly (previously only possible via texture matrix). diff --git a/include/IMeshLoader.h b/include/IMeshLoader.h index 065d76e..de0ffa6 100644 --- a/include/IMeshLoader.h +++ b/include/IMeshLoader.h @@ -30,7 +30,7 @@ class IMeshLoader : public virtual IReferenceCounted public: //! Constructor - IMeshLoader() : TextureLoader(0), PreferredIndexType(video::EIT_16BIT) {} + IMeshLoader() : TextureLoader(0), IndexTypeHint(EITH_16BIT) {} //! Destructor virtual ~IMeshLoader() @@ -79,6 +79,20 @@ public: return TextureLoader; } + enum E_INDEX_TYPE_HINT + { + //! Prefer to use 16-bit index buffers even if it breaks the mesh + EITH_16BIT, + + //! Allow using 32-bit index buffers + EITH_32BIT, + + //! Allow 32-bit, but copy back to 16-bit when 32 is not needed. + //! So tiny overhead on loading, but meshes are later more optimal + EITH_OPTIMAL + }; + + //! Give loader a hint if you would prefer 16 or 32 bit meshbuffers. /** Generally Irrlicht works with 16-bit meshbuffers so far. @@ -87,22 +101,22 @@ public: NOTE: Most loaders will ignore this hint so far, but hopefully will care about it in the future. */ - void setPreferredIndexType(irr::video::E_INDEX_TYPE typeHint) + void setIndexTypeHint(E_INDEX_TYPE_HINT typeHint) { - PreferredIndexType = typeHint; + IndexTypeHint = typeHint; } - //! Return current preference user has for the index-size of meshbuffers + //! Return current preference user has for the index type of meshbuffers /** Note that this is _not_ necessarily the type used by the meshloader */ - irr::video::E_INDEX_TYPE getPreferredIndexType() const + E_INDEX_TYPE_HINT getIndexTypeHint() const { - return PreferredIndexType; + return IndexTypeHint; } protected: IMeshTextureLoader* TextureLoader; - irr::video::E_INDEX_TYPE PreferredIndexType; + E_INDEX_TYPE_HINT IndexTypeHint; }; diff --git a/source/Irrlicht/COBJMeshFileLoader.cpp b/source/Irrlicht/COBJMeshFileLoader.cpp index 2249716..9fe99b0 100644 --- a/source/Irrlicht/COBJMeshFileLoader.cpp +++ b/source/Irrlicht/COBJMeshFileLoader.cpp @@ -82,7 +82,7 @@ IAnimatedMesh* COBJMeshFileLoader::createMesh(io::IReadFile* file) core::array > normalsBuffer(1000); core::array > textureCoordBuffer(1000); - SObjMtl * currMtl = new SObjMtl(PreferredIndexType); + SObjMtl * currMtl = new SObjMtl(getIndexTypeHint()); Materials.push_back(currMtl); u32 smoothingGroup=0; @@ -330,19 +330,21 @@ IAnimatedMesh* COBJMeshFileLoader::createMesh(io::IReadFile* file) { if ( Materials[m]->Meshbuffer->getIndexCount() > 0 ) { + if ( getIndexTypeHint() == EITH_OPTIMAL + && Materials[m]->Meshbuffer->getVertexCount() <= 65536 ) + { + Materials[m]->Meshbuffer->getIndexBuffer().setType(video::EIT_16BIT); + } + + Materials[m]->Meshbuffer->recalculateBoundingBox(); if (Materials[m]->RecalculateNormals) SceneManager->getMeshManipulator()->recalculateNormals(Materials[m]->Meshbuffer); if (Materials[m]->Meshbuffer->Material.MaterialType == video::EMT_PARALLAX_MAP_SOLID) { - SMesh tmp; - tmp.addMeshBuffer(Materials[m]->Meshbuffer); - IMesh* tangentMesh = SceneManager->getMeshManipulator()->createMeshWithTangents(&tmp); - mesh->addMeshBuffer(tangentMesh->getMeshBuffer(0)); - tangentMesh->drop(); + Materials[m]->Meshbuffer->getVertexBuffer().setType(video::EVT_TANGENTS); } - else - mesh->addMeshBuffer( Materials[m]->Meshbuffer ); + mesh->addMeshBuffer( Materials[m]->Meshbuffer ); } } @@ -579,7 +581,7 @@ void COBJMeshFileLoader::readMTL(const c8* fileName, const io::path& relPath) c8 mtlNameBuf[WORD_BUFFER_LENGTH]; bufPtr = goAndCopyNextWord(mtlNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); - currMaterial = new SObjMtl(PreferredIndexType); + currMaterial = new SObjMtl(getIndexTypeHint()); currMaterial->Name = mtlNameBuf; } break; diff --git a/source/Irrlicht/COBJMeshFileLoader.h b/source/Irrlicht/COBJMeshFileLoader.h index 2954056..096b0d7 100644 --- a/source/Irrlicht/COBJMeshFileLoader.h +++ b/source/Irrlicht/COBJMeshFileLoader.h @@ -42,7 +42,9 @@ private: struct SObjMtl { - SObjMtl(irr::video::E_INDEX_TYPE indexType) : IndexType(indexType), Meshbuffer(0), Bumpiness (1.0f), Illumination(0), + SObjMtl(E_INDEX_TYPE_HINT typeHint) + : IndexType(typeHint == EITH_16BIT ? video::EIT_16BIT : video::EIT_32BIT) + , Meshbuffer(0), Bumpiness (1.0f), Illumination(0), RecalculateNormals(false) { Meshbuffer = new CDynamicMeshBuffer(irr::video::EVT_STANDARD, IndexType);