Remove KHR_binary_glTF code
authorDaniel Hritzkiv <daniel.hritzkiv@gmail.com>
Thu, 7 Sep 2017 21:32:41 +0000 (17:32 -0400)
committerDaniel Hritzkiv <daniel.hritzkiv@gmail.com>
Mon, 11 Sep 2017 15:02:16 +0000 (11:02 -0400)
Binary glTF is now part of the glTF2 spec. However, it’s implemented incorrectly, so will be temporarily removed

code/glTF2Asset.h
code/glTF2Asset.inl
code/glTF2AssetWriter.h
code/glTF2AssetWriter.inl
code/glTF2Exporter.cpp
code/glTF2Importer.cpp

index 8b226da..b093ec0 100644 (file)
@@ -43,7 +43,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  * Declares a glTF class to handle gltf/glb files
  *
  * glTF Extensions Support:
- *   KHR_binary_glTF: full
  *   KHR_materials_pbrSpecularGlossiness full
  */
 #ifndef GLTF2ASSET_H_INC
@@ -193,17 +192,6 @@ namespace glTF2
         #include "./../include/assimp/Compiler/pushpack1.h"
     #endif
 
-    //! For the KHR_binary_glTF extension (binary .glb file)
-    //! 20-byte header (+ the JSON + a "body" data section)
-    struct GLB_Header
-    {
-        uint8_t magic[4];     //!< Magic number: "glTF"
-        uint32_t version;     //!< Version number
-        uint32_t length;      //!< Total length of the Binary glTF, including header, scene, and body, in bytes
-        uint32_t sceneLength; //!< Length, in bytes, of the glTF scene
-        uint32_t sceneFormat; //!< Specifies the format of the glTF scene (see the SceneFormat enum)
-    } PACK_STRUCT;
-
     #ifdef ASSIMP_API
         #include "./../include/assimp/Compiler/poppack1.h"
     #endif
@@ -1049,7 +1037,6 @@ namespace glTF2
         //! Keeps info about the enabled extensions
         struct Extensions
         {
-            bool KHR_binary_glTF;
             bool KHR_materials_pbrSpecularGlossiness;
 
         } extensionsUsed;
@@ -1097,10 +1084,7 @@ namespace glTF2
         }
 
         //! Main function
-        void Load(const std::string& file, bool isBinary = false);
-
-        //! Enables the "KHR_binary_glTF" extension on the asset
-        void SetAsBinary();
+        void Load(const std::string& file);
 
         //! Search for an available name, starting from the given strings
         std::string FindUniqueID(const std::string& str, const char* suffix);
@@ -1109,11 +1093,8 @@ namespace glTF2
             { return mBodyBuffer; }
 
     private:
-        void ReadBinaryHeader(IOStream& stream);
-
         void ReadExtensionsUsed(Document& doc);
 
-
         IOStream* OpenFile(std::string path, const char* mode, bool absolute = false);
     };
 
index 03c2279..cbdac5d 100644 (file)
@@ -290,11 +290,6 @@ inline Buffer::~Buffer()
 
 inline const char* Buffer::TranslateId(Asset& r, const char* id)
 {
-    // Compatibility with old spec
-    if (r.extensionsUsed.KHR_binary_glTF && strcmp(id, "KHR_binary_glTF") == 0) {
-        return "binary_glTF";
-    }
-
     return id;
 }
 
@@ -630,28 +625,6 @@ inline Image::Image()
 
 inline void Image::Read(Value& obj, Asset& r)
 {
-    // Check for extensions first (to detect binary embedded data)
-    if (Value* extensions = FindObject(obj, "extensions")) {
-        if (r.extensionsUsed.KHR_binary_glTF) {
-            if (Value* ext = FindObject(*extensions, "KHR_binary_glTF")) {
-
-                width  = MemberOrDefault(*ext, "width", 0);
-                height = MemberOrDefault(*ext, "height", 0);
-
-                ReadMember(*ext, "mimeType", mimeType);
-
-                if (Value* bufferViewVal = FindUInt(*ext, "bufferView")) {
-                    Ref<BufferView> bv = r.bufferViews.Retrieve(bufferViewVal->GetUint());
-                    if (bv) {
-                        mDataLength = bv->byteLength;
-                        mData = new uint8_t[mDataLength];
-                        memcpy(mData, bv->buffer->GetPointer() + bv->byteOffset, mDataLength);
-                    }
-                }
-            }
-        }
-    }
-
     if (!mDataLength) {
         if (Value* uri = FindString(obj, "uri")) {
             const char* uristr = uri->GetString();
@@ -1016,40 +989,7 @@ inline void AssetMetadata::Read(Document& doc)
 // Asset methods implementation
 //
 
-inline void Asset::ReadBinaryHeader(IOStream& stream)
-{
-    GLB_Header header;
-    if (stream.Read(&header, sizeof(header), 1) != 1) {
-        throw DeadlyImportError("GLTF: Unable to read the file header");
-    }
-
-    if (strncmp((char*)header.magic, AI_GLB_MAGIC_NUMBER, sizeof(header.magic)) != 0) {
-        throw DeadlyImportError("GLTF: Invalid binary glTF file");
-    }
-
-    AI_SWAP4(header.version);
-    asset.version = header.version;
-    if (header.version != 1) {
-        throw DeadlyImportError("GLTF: Unsupported binary glTF version");
-    }
-
-    AI_SWAP4(header.sceneFormat);
-    if (header.sceneFormat != SceneFormat_JSON) {
-        throw DeadlyImportError("GLTF: Unsupported binary glTF scene format");
-    }
-
-    AI_SWAP4(header.length);
-    AI_SWAP4(header.sceneLength);
-
-    mSceneLength = static_cast<size_t>(header.sceneLength);
-
-    mBodyOffset = sizeof(header)+mSceneLength;
-    mBodyOffset = (mBodyOffset + 3) & ~3; // Round up to next multiple of 4
-
-    mBodyLength = header.length - mBodyOffset;
-}
-
-inline void Asset::Load(const std::string& pFile, bool isBinary)
+inline void Asset::Load(const std::string& pFile)
 {
     mCurrentAssetDir.clear();
     int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\')));
@@ -1060,16 +1000,8 @@ inline void Asset::Load(const std::string& pFile, bool isBinary)
         throw DeadlyImportError("GLTF: Could not open file for reading");
     }
 
-    // is binary? then read the header
-    if (isBinary) {
-        SetAsBinary(); // also creates the body buffer
-        ReadBinaryHeader(*stream);
-    }
-    else {
-        mSceneLength = stream->FileSize();
-        mBodyLength = 0;
-    }
-
+    mSceneLength = stream->FileSize();
+    mBodyLength = 0;
 
     // read the scene data
 
@@ -1130,15 +1062,6 @@ inline void Asset::Load(const std::string& pFile, bool isBinary)
     }
 }
 
-inline void Asset::SetAsBinary()
-{
-    if (!extensionsUsed.KHR_binary_glTF) {
-        extensionsUsed.KHR_binary_glTF = true;
-        mBodyBuffer = buffers.Create("binary_glTF");
-        mBodyBuffer->MarkAsSpecial();
-    }
-}
-
 inline void Asset::ReadExtensionsUsed(Document& doc)
 {
     Value* extsUsed = FindArray(doc, "extensionsUsed");
@@ -1155,7 +1078,6 @@ inline void Asset::ReadExtensionsUsed(Document& doc)
     #define CHECK_EXT(EXT) \
         if (exts.find(#EXT) != exts.end()) extensionsUsed.EXT = true;
 
-    CHECK_EXT(KHR_binary_glTF);
     CHECK_EXT(KHR_materials_pbrSpecularGlossiness);
 
     #undef CHECK_EXT
index 97efbea..bce2b1b 100644 (file)
@@ -43,7 +43,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  * Declares a class to write gltf/glb files
  *
  * glTF Extensions Support:
- *   KHR_binary_glTF: full
  *   KHR_materials_pbrSpecularGlossiness: full
  */
 #ifndef GLTF2ASSETWRITER_H_INC
@@ -82,7 +81,6 @@ public:
     AssetWriter(Asset& asset);
 
     void WriteFile(const char* path);
-    void WriteGLBFile(const char* path);
 };
 
 }
index 1665bee..e62f237 100644 (file)
@@ -179,21 +179,7 @@ namespace glTF2 {
     inline void Write(Value& obj, Image& img, AssetWriter& w)
     {
         std::string uri;
-        if (w.mAsset.extensionsUsed.KHR_binary_glTF && img.bufferView) {
-            Value exts, ext;
-            exts.SetObject();
-            ext.SetObject();
-
-            ext.AddMember("bufferView", img.bufferView->index, w.mAl);
-
-            if (!img.mimeType.empty())
-                ext.AddMember("mimeType", StringRef(img.mimeType), w.mAl);
-
-            exts.AddMember("KHR_binary_glTF", ext, w.mAl);
-            obj.AddMember("extensions", exts, w.mAl);
-            return;
-        }
-        else if (img.HasData()) {
+        if (img.HasData()) {
             uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType);
             uri += ";base64,";
             Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri);
@@ -577,77 +563,6 @@ namespace glTF2 {
         }
     }
 
-    inline void AssetWriter::WriteGLBFile(const char* path)
-    {
-        std::unique_ptr<IOStream> outfile(mAsset.OpenFile(path, "wb", true));
-
-        if (outfile == 0) {
-            throw DeadlyExportError("Could not open output file: " + std::string(path));
-        }
-
-        // we will write the header later, skip its size
-        outfile->Seek(sizeof(GLB_Header), aiOrigin_SET);
-
-        StringBuffer docBuffer;
-        Writer<StringBuffer> writer(docBuffer);
-        mDoc.Accept(writer);
-
-        if (outfile->Write(docBuffer.GetString(), docBuffer.GetSize(), 1) != 1) {
-            throw DeadlyExportError("Failed to write scene data!");
-        }
-
-        WriteBinaryData(outfile.get(), docBuffer.GetSize());
-    }
-
-    inline void AssetWriter::WriteBinaryData(IOStream* outfile, size_t sceneLength)
-    {
-        //
-        // write the body data
-        //
-
-        size_t bodyLength = 0;
-        if (Ref<Buffer> b = mAsset.GetBodyBuffer()) {
-            bodyLength = b->byteLength;
-
-            if (bodyLength > 0) {
-                size_t bodyOffset = sizeof(GLB_Header) + sceneLength;
-                bodyOffset = (bodyOffset + 3) & ~3; // Round up to next multiple of 4
-
-                outfile->Seek(bodyOffset, aiOrigin_SET);
-
-                if (outfile->Write(b->GetPointer(), b->byteLength, 1) != 1) {
-                    throw DeadlyExportError("Failed to write body data!");
-                }
-            }
-        }
-
-        //
-        // write the header
-        //
-
-        GLB_Header header;
-        memcpy(header.magic, AI_GLB_MAGIC_NUMBER, sizeof(header.magic));
-
-        header.version = 2;
-        AI_SWAP4(header.version);
-
-        header.length = uint32_t(sizeof(header) + sceneLength + bodyLength);
-        AI_SWAP4(header.length);
-
-        header.sceneLength = uint32_t(sceneLength);
-        AI_SWAP4(header.sceneLength);
-
-        header.sceneFormat = SceneFormat_JSON;
-        AI_SWAP4(header.sceneFormat);
-
-        outfile->Seek(0, aiOrigin_SET);
-
-        if (outfile->Write(&header, 1, sizeof(header)) != sizeof(header)) {
-            throw DeadlyExportError("Failed to write the header!");
-        }
-    }
-
-
     inline void AssetWriter::WriteMetadata()
     {
         Value asset;
@@ -662,9 +577,6 @@ namespace glTF2 {
         Value exts;
         exts.SetArray();
         {
-            //if (false)
-            //    exts.PushBack(StringRef("KHR_binary_glTF"), mAl);
-
             // This is used to export pbrSpecularGlossiness materials with GLTF 2.
             if (this->mAsset.extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
                 exts.PushBack(StringRef("KHR_materials_pbrSpecularGlossiness"), mAl);
index f3e0835..e8c4212 100644 (file)
@@ -101,10 +101,6 @@ glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const ai
 
     mAsset.reset( new Asset( pIOSystem ) );
 
-    if (isBinary) {
-        mAsset->SetAsBinary();
-    }
-
     ExportMetadata();
 
     ExportMaterials();
@@ -121,11 +117,7 @@ glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const ai
 
     AssetWriter writer(*mAsset);
 
-    if (isBinary) {
-        writer.WriteGLBFile(filename);
-    } else {
-        writer.WriteFile(filename);
-    }
+    writer.WriteFile(filename);
 }
 
 /*
index d234afa..34f12b5 100644 (file)
@@ -74,7 +74,7 @@ static const aiImporterDesc desc = {
     "",
     "",
     "",
-    aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
+    aiImporterFlags_SupportTextFlavour | aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
     0,
     0,
     0,
@@ -103,13 +103,13 @@ bool glTF2Importer::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool
 {
     const std::string &extension = GetExtension(pFile);
 
-    if (extension != "gltf") // We currently can't read glTF2 binary files (.glb)
+    if (extension != "gltf") // We currently can't read glTF2 binary files (.glb), yet
         return false;
 
     if (checkSig && pIOHandler) {
         glTF2::Asset asset(pIOHandler);
         try {
-            asset.Load(pFile, extension == "glb");
+            asset.Load(pFile);
             std::string version = asset.asset.version;
             return !version.empty() && version[0] == '2';
         } catch (...) {
@@ -615,15 +615,12 @@ void glTF2Importer::InternReadFile(const std::string& pFile, aiScene* pScene, IO
 
     // read the asset file
     glTF2::Asset asset(pIOHandler);
-    asset.Load(pFile, GetExtension(pFile) == "glb");
-
+    asset.Load(pFile);
 
     //
     // Copy the data out
     //
 
-
-
     ImportEmbeddedTextures(asset);
     ImportMaterials(asset);