From 0011a2b96e6f2b57393d7fde00fbdcde0edc5657 Mon Sep 17 00:00:00 2001 From: "Arthur Brainville (Ybalrid)" Date: Sun, 22 Jul 2018 15:03:00 +0200 Subject: [PATCH] Add interface to get mesh and datablock without creating an item Signed-off-by: Arthur Brainville (Ybalrid) --- include/Ogre_glTF.hpp | 35 +++++++++++++++++++++++ pluginTest/main.cpp | 2 +- src/Ogre_glTF.cpp | 65 +++++++++++++++++++++++++++++++++---------- 3 files changed, 87 insertions(+), 15 deletions(-) diff --git a/include/Ogre_glTF.hpp b/include/Ogre_glTF.hpp index 8928e7e..f7373dd 100644 --- a/include/Ogre_glTF.hpp +++ b/include/Ogre_glTF.hpp @@ -10,15 +10,45 @@ namespace Ogre_glTF //Forward declare main class class glTFLoader; + ///struct that contains a mesh and the datablock that should be used with it. + ///This represent what an extracted object from a glTF asset contains. + ///The mash should have a skeletonInstance attached to it the glTF file defined a skin; + struct MeshAndDataBlock + { + ///Pointer to the Ogre Mesh + Ogre::MeshPtr Mesh; + + ///Pointer to the HlmsDatablock. This should be a HlmsPbsDatablock + Ogre::HlmsDatablock* datablock; + }; + ///Plugin accessible interface that plugin users can use struct glTFLoaderInterface { ///Polymorphic dtor virtual ~glTFLoaderInterface() = default; + ///Get you an item from a GLB file loaded inside an Ogre resource group + /// \param name The name of the resource + /// \param smgr The scene manager where the Item will be used + /// \return pointer to a created item in your scene manager using the mesh in the glTF asset virtual Ogre::Item* getItemFromResource(const std::string& name, Ogre::SceneManager* smgr) = 0; + ///Get you an item from a GLB or a GLTF file from the filesystem. + /// \param name The name of the resource + /// \param smgr The scene manager where the Item will be used + /// \return pointer to a created item in your scene manager using the mesh in the glTF asset virtual Ogre::Item* getItemFromFileSystem(const std::string& fileName, Ogre::SceneManager* smgr) = 0; + + ///Get you a mesh and a material datablock from a GLB in the resource manager + /// \param name The name of the resource + /// \return a struct containing pointers to a mesh and a datablock + virtual MeshAndDataBlock getMeshFromResource(const std::string& name) = 0; + + ///Get you mesh and a material datablock from GLB or a GLTF file from the filesystem + /// \param name The name of the resource + /// \return a struct containing pointers to a mesh and a datablock + virtual MeshAndDataBlock getMeshFromFileSystem(const std::string& name) = 0; }; ///Class that hold the loaded content of a glTF file and that can create Ogre objects from it @@ -47,6 +77,9 @@ namespace Ogre_glTF ///Deleted asignment constructor : non copyable class loaderAdapter& operator=(const loaderAdapter&) = delete; + Ogre::MeshPtr getMesh() const; + Ogre::HlmsDatablock* getDatablock() const; + ///Construct an item for this object /// \param smgr pointer to the scene manager where we are creating the item Ogre::Item* getItem(Ogre::SceneManager* smgr) const; @@ -95,6 +128,8 @@ namespace Ogre_glTF Ogre::Item* getItemFromResource(const std::string& name, Ogre::SceneManager* smgr) override; Ogre::Item* getItemFromFileSystem(const std::string& fileName, Ogre::SceneManager* smgr) override; + MeshAndDataBlock getMeshFromResource(const std::string& name) override; + MeshAndDataBlock getMeshFromFileSystem(const std::string& name) override; ///Deleted copy contructor glTFLoader(const glTFLoader&) = delete; diff --git a/pluginTest/main.cpp b/pluginTest/main.cpp index ef89c47..fe20a48 100644 --- a/pluginTest/main.cpp +++ b/pluginTest/main.cpp @@ -108,7 +108,7 @@ int main() #endif - //Startup Ogre as you woul generally do it + //Startup Ogre as you would generally do it root->showConfigDialog(); root->getRenderSystem()->setConfigOption("FSAA", "16"); root->getRenderSystem()->setConfigOption("sRGB Gamma Conversion", "Yes"); diff --git a/src/Ogre_glTF.cpp b/src/Ogre_glTF.cpp index b9462f0..a8ded0d 100644 --- a/src/Ogre_glTF.cpp +++ b/src/Ogre_glTF.cpp @@ -61,22 +61,33 @@ loaderAdapter::~loaderAdapter() Ogre::Item* loaderAdapter::getItem(Ogre::SceneManager* smgr) const { - if(isOk()) { + if(isOk()) + { pimpl->textureImp.loadTextures(); - auto Mesh = pimpl->modelConv.getOgreMesh(); - if(pimpl->modelConv.hasSkins()) { - //load skeleton information - auto skeleton = pimpl->skeletonImp.getSkeleton(adapterName); - Mesh->_notifySkeleton(skeleton); - } + Ogre::MeshPtr Mesh = getMesh(); auto Item = smgr->createItem(Mesh); - Item->setDatablock(pimpl->materialLoad.getDatablock()); + Item->setDatablock(getDatablock()); return Item; } return nullptr; } +Ogre::MeshPtr loaderAdapter::getMesh() const +{ + auto Mesh = this->pimpl->modelConv.getOgreMesh(); + + if(this->pimpl->modelConv.hasSkins()) + { + //load skeleton information + auto skeleton = this->pimpl->skeletonImp.getSkeleton(this->adapterName); + Mesh->_notifySkeleton(skeleton); + } + return Mesh; +} + +Ogre::HlmsDatablock* loaderAdapter::getDatablock() const { return pimpl->materialLoad.getDatablock(); } + loaderAdapter::loaderAdapter(loaderAdapter&& other) noexcept : pimpl{ std::move(other.pimpl) } { //OgreLog("Moved adapter object..."); @@ -116,7 +127,8 @@ struct glTFLoader::glTFLoaderImpl for(size_t i{ 0 }; i < 4; ++i) probe >> buffer[i]; buffer[4] = 0; - if(std::string("glTF") == std::string(buffer.data())) { + if(std::string("glTF") == std::string(buffer.data())) + { //OgreLog("Detected binary file thanks to the magic number at the start!"); return FileType::Binary; } @@ -185,7 +197,8 @@ loaderAdapter glTFLoader::loadGlbResource(const std::string& name) const auto glbFile = glbManager.load(name, Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); loaderAdapter adapter; - if(glbFile) { + if(glbFile) + { loaderImpl->loadGlb(adapter, glbFile); adapter.pimpl->valid = true; } @@ -198,10 +211,7 @@ Ogre::Item* glTFLoader::getItemFromResource(const std::string& name, Ogre::Scene { OgreLog("Getting resource"); auto adapter = loadGlbResource(name); - if(adapter.isOk()) - { - OgreLog("Adapter is ok!"); - } + if(adapter.isOk()) { OgreLog("Adapter is ok!"); } else { OgreLog("Adapter is not okay!"); @@ -214,9 +224,36 @@ Ogre::Item* glTFLoader::getItemFromResource(const std::string& name, Ogre::Scene Ogre::Item* glTFLoader::getItemFromFileSystem(const std::string& fileName, Ogre::SceneManager* smgr) { auto adapter = loadFromFileSystem(fileName); + if(adapter.isOk()) { OgreLog("Adapter is ok!"); } + else + { + OgreLog("Adapter is not okay!"); + } return adapter.getItem(smgr); } +MeshAndDataBlock glTFLoader::getMeshFromFileSystem(const std::string& name) +{ + auto adapter = loadFromFileSystem(name); + if(adapter.isOk()) { OgreLog("Adapter is ok!"); } + else + { + OgreLog("Adapter is not okay!"); + } + return { adapter.getMesh(), adapter.getDatablock() }; +} + +MeshAndDataBlock glTFLoader::getMeshFromResource(const std::string& name) +{ + auto adapter = loadGlbResource(name); + if(adapter.isOk()) { OgreLog("Adapter is ok!"); } + else + { + OgreLog("Adapter is not okay!"); + } + return { adapter.getMesh(), adapter.getDatablock() }; +} + glTFLoader::glTFLoader(glTFLoader&& other) noexcept : loaderImpl(std::move(other.loaderImpl)) {} glTFLoader& glTFLoader::operator=(glTFLoader&& other) noexcept