update to parse glb files

This commit is contained in:
raveenajain 2019-02-07 15:08:04 -08:00
parent 632d6e8a73
commit 446176d3a5
2 changed files with 64 additions and 6 deletions

67
libraries/fbx/src/GLTFSerializer.cpp Normal file → Executable file
View file

@ -124,6 +124,31 @@ bool GLTFSerializer::getObjectArrayVal(const QJsonObject& object, const QString&
return _defined;
}
QByteArray GLTFSerializer::setGLBChunks(const QByteArray& data) {
int byte = 4;
int jsonStart = data.indexOf("JSON", Qt::CaseSensitive);
int binStart = data.indexOf("BIN", Qt::CaseSensitive);
int jsonLength, binLength;
QByteArray jsonLengthChunk, binLengthChunk;
jsonLengthChunk = data.mid(jsonStart - byte, byte);
QDataStream tempJsonLen(jsonLengthChunk);
tempJsonLen.setByteOrder(QDataStream::LittleEndian);
tempJsonLen >> jsonLength;
QByteArray jsonChunk = data.mid(jsonStart + byte, jsonLength);
if (binStart != -1) {
binLengthChunk = data.mid(binStart - byte, byte);
QDataStream tempBinLen(binLengthChunk);
tempBinLen.setByteOrder(QDataStream::LittleEndian);
tempBinLen >> binLength;
_glbBinary = data.mid(binStart + byte, binLength);
}
return jsonChunk;
}
int GLTFSerializer::getMeshPrimitiveRenderingMode(const QString& type)
{
if (type == "POINTS") {
@ -309,6 +334,14 @@ bool GLTFSerializer::addBuffer(const QJsonObject& object) {
GLTFBuffer buffer;
getIntVal(object, "byteLength", buffer.byteLength, buffer.defined);
if (_url.toString().endsWith("glb")) {
if (!_glbBinary.isEmpty()) {
buffer.blob = _glbBinary;
} else {
return false;
}
}
if (getStringVal(object, "uri", buffer.uri, buffer.defined)) {
if (!readBinary(buffer.uri, buffer.blob)) {
return false;
@ -530,9 +563,16 @@ bool GLTFSerializer::addTexture(const QJsonObject& object) {
bool GLTFSerializer::parseGLTF(const QByteArray& data) {
PROFILE_RANGE_EX(resource_parse, __FUNCTION__, 0xffff0000, nullptr);
QJsonDocument d = QJsonDocument::fromJson(data);
QByteArray jsonChunk = data;
if (_url.toString().endsWith("glb") && data.indexOf("glTF") == 0 && data.contains("JSON")) {
jsonChunk = setGLBChunks(data);
}
QJsonDocument d = QJsonDocument::fromJson(jsonChunk);
QJsonObject jsFile = d.object();
bool success = setAsset(jsFile);
if (success) {
QJsonArray accessors;
@ -904,6 +944,10 @@ MediaType GLTFSerializer::getMediaType() const {
MediaType mediaType("gltf");
mediaType.extensions.push_back("gltf");
mediaType.webMediaTypes.push_back("model/gltf+json");
mediaType.extensions.push_back("glb");
mediaType.webMediaTypes.push_back("model/gltf-binary");
return mediaType;
}
@ -912,9 +956,9 @@ std::unique_ptr<hfm::Serializer::Factory> GLTFSerializer::getFactory() const {
}
HFMModel::Pointer GLTFSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) {
_url = url;
_url = url;
// Normalize url for local files
QUrl normalizeUrl = DependencyManager::get<ResourceManager>()->normalizeURL(_url);
if (normalizeUrl.scheme().isEmpty() || (normalizeUrl.scheme() == "file")) {
@ -943,7 +987,7 @@ bool GLTFSerializer::readBinary(const QString& url, QByteArray& outdata) {
bool success;
std::tie<bool, QByteArray>(success, outdata) = requestData(binaryUrl);
return success;
}
@ -1003,7 +1047,7 @@ QNetworkReply* GLTFSerializer::request(QUrl& url, bool isTest) {
HFMTexture GLTFSerializer::getHFMTexture(const GLTFTexture& texture) {
HFMTexture fbxtex = HFMTexture();
fbxtex.texcoordSet = 0;
if (texture.defined["source"]) {
QString url = _file.images[texture.source].uri;
QString fname = QUrl(url).fileName();
@ -1011,6 +1055,17 @@ HFMTexture GLTFSerializer::getHFMTexture(const GLTFTexture& texture) {
qCDebug(modelformat) << "fname: " << fname;
fbxtex.name = fname;
fbxtex.filename = textureUrl.toEncoded();
if (_url.toString().endsWith("glb") && !_glbBinary.isEmpty()) {
int bufferView = _file.images[texture.source].bufferView;
GLTFBufferView& imagesBufferview = _file.bufferviews[bufferView];
int offset = imagesBufferview.byteOffset;
int length = imagesBufferview.byteLength;
fbxtex.content = _glbBinary.mid(offset, length);
fbxtex.filename = textureUrl.toEncoded().append(texture.source);
}
}
return fbxtex;
}

3
libraries/fbx/src/GLTFSerializer.h Normal file → Executable file
View file

@ -709,6 +709,7 @@ public:
private:
GLTFFile _file;
QUrl _url;
QByteArray _glbBinary;
glm::mat4 getModelTransform(const GLTFNode& node);
@ -731,6 +732,8 @@ private:
QVector<double>& values, QMap<QString, bool>& defined);
bool getObjectArrayVal(const QJsonObject& object, const QString& fieldname,
QJsonArray& objects, QMap<QString, bool>& defined);
QByteArray setGLBChunks(const QByteArray& data);
int getMaterialAlphaMode(const QString& type);
int getAccessorType(const QString& type);