Remove KHR_binary_glTF code

Binary glTF is now part of the glTF2 spec. However, it’s implemented incorrectly, so will be temporarily removed
pull/1423/head
Daniel Hritzkiv 2017-09-07 17:32:41 -04:00
parent b4f5033d89
commit 816e6909ca
No known key found for this signature in database
GPG Key ID: D1D19875679D5CBF
6 changed files with 10 additions and 208 deletions

View 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 * Declares a glTF class to handle gltf/glb files
* *
* glTF Extensions Support: * glTF Extensions Support:
* KHR_binary_glTF: full
* KHR_materials_pbrSpecularGlossiness full * KHR_materials_pbrSpecularGlossiness full
*/ */
#ifndef GLTF2ASSET_H_INC #ifndef GLTF2ASSET_H_INC
@ -193,17 +192,6 @@ namespace glTF2
#include "./../include/assimp/Compiler/pushpack1.h" #include "./../include/assimp/Compiler/pushpack1.h"
#endif #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 #ifdef ASSIMP_API
#include "./../include/assimp/Compiler/poppack1.h" #include "./../include/assimp/Compiler/poppack1.h"
#endif #endif
@ -1049,7 +1037,6 @@ namespace glTF2
//! Keeps info about the enabled extensions //! Keeps info about the enabled extensions
struct Extensions struct Extensions
{ {
bool KHR_binary_glTF;
bool KHR_materials_pbrSpecularGlossiness; bool KHR_materials_pbrSpecularGlossiness;
} extensionsUsed; } extensionsUsed;
@ -1097,10 +1084,7 @@ namespace glTF2
} }
//! Main function //! Main function
void Load(const std::string& file, bool isBinary = false); void Load(const std::string& file);
//! Enables the "KHR_binary_glTF" extension on the asset
void SetAsBinary();
//! Search for an available name, starting from the given strings //! Search for an available name, starting from the given strings
std::string FindUniqueID(const std::string& str, const char* suffix); std::string FindUniqueID(const std::string& str, const char* suffix);
@ -1109,11 +1093,8 @@ namespace glTF2
{ return mBodyBuffer; } { return mBodyBuffer; }
private: private:
void ReadBinaryHeader(IOStream& stream);
void ReadExtensionsUsed(Document& doc); void ReadExtensionsUsed(Document& doc);
IOStream* OpenFile(std::string path, const char* mode, bool absolute = false); IOStream* OpenFile(std::string path, const char* mode, bool absolute = false);
}; };

View File

@ -290,11 +290,6 @@ inline Buffer::~Buffer()
inline const char* Buffer::TranslateId(Asset& r, const char* id) 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; return id;
} }
@ -630,28 +625,6 @@ inline Image::Image()
inline void Image::Read(Value& obj, Asset& r) 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 (!mDataLength) {
if (Value* uri = FindString(obj, "uri")) { if (Value* uri = FindString(obj, "uri")) {
const char* uristr = uri->GetString(); const char* uristr = uri->GetString();
@ -1016,40 +989,7 @@ inline void AssetMetadata::Read(Document& doc)
// Asset methods implementation // Asset methods implementation
// //
inline void Asset::ReadBinaryHeader(IOStream& stream) inline void Asset::Load(const std::string& pFile)
{
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)
{ {
mCurrentAssetDir.clear(); mCurrentAssetDir.clear();
int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\'))); 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"); throw DeadlyImportError("GLTF: Could not open file for reading");
} }
// is binary? then read the header mSceneLength = stream->FileSize();
if (isBinary) { mBodyLength = 0;
SetAsBinary(); // also creates the body buffer
ReadBinaryHeader(*stream);
}
else {
mSceneLength = stream->FileSize();
mBodyLength = 0;
}
// read the scene data // 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) inline void Asset::ReadExtensionsUsed(Document& doc)
{ {
Value* extsUsed = FindArray(doc, "extensionsUsed"); Value* extsUsed = FindArray(doc, "extensionsUsed");
@ -1155,7 +1078,6 @@ inline void Asset::ReadExtensionsUsed(Document& doc)
#define CHECK_EXT(EXT) \ #define CHECK_EXT(EXT) \
if (exts.find(#EXT) != exts.end()) extensionsUsed.EXT = true; if (exts.find(#EXT) != exts.end()) extensionsUsed.EXT = true;
CHECK_EXT(KHR_binary_glTF);
CHECK_EXT(KHR_materials_pbrSpecularGlossiness); CHECK_EXT(KHR_materials_pbrSpecularGlossiness);
#undef CHECK_EXT #undef CHECK_EXT

View 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 * Declares a class to write gltf/glb files
* *
* glTF Extensions Support: * glTF Extensions Support:
* KHR_binary_glTF: full
* KHR_materials_pbrSpecularGlossiness: full * KHR_materials_pbrSpecularGlossiness: full
*/ */
#ifndef GLTF2ASSETWRITER_H_INC #ifndef GLTF2ASSETWRITER_H_INC
@ -82,7 +81,6 @@ public:
AssetWriter(Asset& asset); AssetWriter(Asset& asset);
void WriteFile(const char* path); void WriteFile(const char* path);
void WriteGLBFile(const char* path);
}; };
} }

View File

@ -179,21 +179,7 @@ namespace glTF2 {
inline void Write(Value& obj, Image& img, AssetWriter& w) inline void Write(Value& obj, Image& img, AssetWriter& w)
{ {
std::string uri; std::string uri;
if (w.mAsset.extensionsUsed.KHR_binary_glTF && img.bufferView) { if (img.HasData()) {
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()) {
uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType); uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType);
uri += ";base64,"; uri += ";base64,";
Util::EncodeBase64(img.GetData(), img.GetDataLength(), uri); 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() inline void AssetWriter::WriteMetadata()
{ {
Value asset; Value asset;
@ -662,9 +577,6 @@ namespace glTF2 {
Value exts; Value exts;
exts.SetArray(); exts.SetArray();
{ {
//if (false)
// exts.PushBack(StringRef("KHR_binary_glTF"), mAl);
// This is used to export pbrSpecularGlossiness materials with GLTF 2. // This is used to export pbrSpecularGlossiness materials with GLTF 2.
if (this->mAsset.extensionsUsed.KHR_materials_pbrSpecularGlossiness) { if (this->mAsset.extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
exts.PushBack(StringRef("KHR_materials_pbrSpecularGlossiness"), mAl); exts.PushBack(StringRef("KHR_materials_pbrSpecularGlossiness"), mAl);

View File

@ -101,10 +101,6 @@ glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const ai
mAsset.reset( new Asset( pIOSystem ) ); mAsset.reset( new Asset( pIOSystem ) );
if (isBinary) {
mAsset->SetAsBinary();
}
ExportMetadata(); ExportMetadata();
ExportMaterials(); ExportMaterials();
@ -121,11 +117,7 @@ glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const ai
AssetWriter writer(*mAsset); AssetWriter writer(*mAsset);
if (isBinary) { writer.WriteFile(filename);
writer.WriteGLBFile(filename);
} else {
writer.WriteFile(filename);
}
} }
/* /*

View 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, 0,
0, 0,
@ -103,13 +103,13 @@ bool glTF2Importer::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool
{ {
const std::string &extension = GetExtension(pFile); 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; return false;
if (checkSig && pIOHandler) { if (checkSig && pIOHandler) {
glTF2::Asset asset(pIOHandler); glTF2::Asset asset(pIOHandler);
try { try {
asset.Load(pFile, extension == "glb"); asset.Load(pFile);
std::string version = asset.asset.version; std::string version = asset.asset.version;
return !version.empty() && version[0] == '2'; return !version.empty() && version[0] == '2';
} catch (...) { } catch (...) {
@ -615,15 +615,12 @@ void glTF2Importer::InternReadFile(const std::string& pFile, aiScene* pScene, IO
// read the asset file // read the asset file
glTF2::Asset asset(pIOHandler); glTF2::Asset asset(pIOHandler);
asset.Load(pFile, GetExtension(pFile) == "glb"); asset.Load(pFile);
// //
// Copy the data out // Copy the data out
// //
ImportEmbeddedTextures(asset); ImportEmbeddedTextures(asset);
ImportMaterials(asset); ImportMaterials(asset);