Pārlūkot izejas kodu

Add support for importing both glTF and glTF2 files

John Senneker 8 gadi atpakaļ
vecāks
revīzija
19876e9822
4 mainītis faili ar 28 papildinājumiem un 40 dzēšanām
  1. 2 0
      code/ImporterRegistry.cpp
  2. 14 18
      code/glTF2Importer.cpp
  3. 0 4
      code/glTFAsset.inl
  4. 12 18
      code/glTFImporter.cpp

+ 2 - 0
code/ImporterRegistry.cpp

@@ -181,6 +181,7 @@ corresponding preprocessor flag to selectively disable formats.
 #   include "AssbinLoader.h"
 #endif
 #ifndef ASSIMP_BUILD_NO_GLTF_IMPORTER
+#   include "glTFImporter.h"
 #   include "glTF2Importer.h"
 #endif
 #ifndef ASSIMP_BUILD_NO_C4D_IMPORTER
@@ -335,6 +336,7 @@ void GetImporterInstanceList(std::vector< BaseImporter* >& out)
     out.push_back( new AssbinImporter() );
 #endif
 #if ( !defined ASSIMP_BUILD_NO_GLTF_IMPORTER )
+    out.push_back( new glTFImporter() );
     out.push_back( new glTF2Importer() );
 #endif
 #if ( !defined ASSIMP_BUILD_NO_C4D_IMPORTER )

+ 14 - 18
code/glTF2Importer.cpp

@@ -58,6 +58,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #include "glTF2Asset.h"
 // This is included here so WriteLazyDict<T>'s definition is found.
 #include "glTF2AssetWriter.h"
+#include <rapidjson/document.h>
+#include <rapidjson/rapidjson.h>
 
 using namespace Assimp;
 using namespace glTF2;
@@ -100,24 +102,18 @@ const aiImporterDesc* glTF2Importer::GetInfo() const
 
 bool glTF2Importer::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
 {
-    const std::string& extension = GetExtension(pFile);
-
-    if (extension == "gltf" || extension == "glb")
-        return true;
-
-    if ((checkSig || !extension.length()) && pIOHandler) {
-        char buffer[4];
-
-        std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile));
-        if (pStream && pStream->Read(buffer, sizeof(buffer), 1) == 1) {
-            if (memcmp(buffer, AI_GLB_MAGIC_NUMBER, sizeof(buffer)) == 0) {
-                return true; // Has GLB header
-            }
-            else if (memcmp(buffer, "{\r\n ", sizeof(buffer)) == 0
-                    || memcmp(buffer, "{\n  ", sizeof(buffer)) == 0) {
-                // seems a JSON file, and we're the only format that can read them
-                return true;
-            }
+    const std::string &extension = GetExtension(pFile);
+
+    if (extension != "gltf") // We currently can't read glTF2 binary files (.glb)
+        return false;
+
+    if (checkSig && pIOHandler) {
+        glTF2::Asset asset(pIOHandler);
+        try {
+            asset.Load(pFile, extension == "glb");
+            return asset.asset.version >= 2;
+        } catch (...) {
+            return false;
         }
     }
 

+ 0 - 4
code/glTFAsset.inl

@@ -1243,10 +1243,6 @@ inline void AssetMetadata::Read(Document& doc)
     }
 
     version = std::max(statedVersion, version);
-    if (version == 0) {
-        // if missing version, we'll assume version 1...
-        version = 1;
-    }
 
     if (version != 1) {
         char msg[128];

+ 12 - 18
code/glTFImporter.cpp

@@ -100,24 +100,18 @@ const aiImporterDesc* glTFImporter::GetInfo() const
 
 bool glTFImporter::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
 {
-    const std::string& extension = GetExtension(pFile);
-
-    if (extension == "gltf" || extension == "glb")
-        return true;
-
-    if ((checkSig || !extension.length()) && pIOHandler) {
-        char buffer[4];
-
-        std::unique_ptr<IOStream> pStream(pIOHandler->Open(pFile));
-        if (pStream && pStream->Read(buffer, sizeof(buffer), 1) == 1) {
-            if (memcmp(buffer, AI_GLB_MAGIC_NUMBER, sizeof(buffer)) == 0) {
-                return true; // Has GLB header
-            }
-            else if (memcmp(buffer, "{\r\n ", sizeof(buffer)) == 0
-                    || memcmp(buffer, "{\n  ", sizeof(buffer)) == 0) {
-                // seems a JSON file, and we're the only format that can read them
-                return true;
-            }
+    const std::string &extension = GetExtension(pFile);
+
+    if (extension != "gltf" && extension != "glb")
+        return false;
+
+    if (checkSig && pIOHandler) {
+        glTF::Asset asset(pIOHandler);
+        try {
+            asset.Load(pFile, extension == "glb");
+            return asset.asset.version < 2;
+        } catch (...) {
+            return false;
         }
     }