2020-07-05 18:22:31 +00:00
|
|
|
|
/*
|
2015-11-08 19:29:05 +00:00
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
|
Copyright (c) 2006-2022, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
|
2015-11-08 19:29:05 +00:00
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
|
derived from this software without specific prior
|
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
*/
|
2015-11-27 00:24:45 +00:00
|
|
|
|
|
2020-07-05 18:22:31 +00:00
|
|
|
|
#if !defined(ASSIMP_BUILD_NO_GLTF_IMPORTER) && !defined(ASSIMP_BUILD_NO_GLTF1_IMPORTER)
|
2015-11-27 00:24:45 +00:00
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
|
#include "AssetLib/glTF/glTFImporter.h"
|
|
|
|
|
#include "AssetLib/glTF/glTFAsset.h"
|
2021-04-16 18:44:40 +00:00
|
|
|
|
#if !defined(ASSIMP_BUILD_NO_EXPORT)
|
2020-05-02 13:14:38 +00:00
|
|
|
|
#include "AssetLib/glTF/glTFAssetWriter.h"
|
2021-04-16 18:44:40 +00:00
|
|
|
|
#endif
|
2019-06-10 21:26:00 +00:00
|
|
|
|
#include "PostProcessing/MakeVerboseFormat.h"
|
|
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
|
#include <assimp/StringComparison.h>
|
|
|
|
|
#include <assimp/StringUtils.h>
|
2015-11-25 21:03:39 +00:00
|
|
|
|
#include <assimp/ai_assert.h>
|
2019-12-05 11:25:12 +00:00
|
|
|
|
#include <assimp/commonMetaData.h>
|
2020-06-23 19:11:18 +00:00
|
|
|
|
#include <assimp/importerdesc.h>
|
|
|
|
|
#include <assimp/scene.h>
|
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
|
#include <assimp/Importer.hpp>
|
2017-02-22 16:20:26 +00:00
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
|
#include <memory>
|
2015-11-25 21:03:39 +00:00
|
|
|
|
|
2015-11-26 01:36:22 +00:00
|
|
|
|
using namespace Assimp;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
using namespace glTF;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
|
|
|
|
//
|
2015-11-29 18:13:51 +00:00
|
|
|
|
// glTFImporter
|
2015-11-26 01:36:22 +00:00
|
|
|
|
//
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
static const aiImporterDesc desc = {
|
|
|
|
|
"glTF Importer",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
|
|
|
|
"",
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour | aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
|
2015-11-29 18:13:51 +00:00
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
"gltf glb"
|
|
|
|
|
};
|
2015-11-27 01:31:18 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
glTFImporter::glTFImporter() :
|
|
|
|
|
BaseImporter(), meshOffsets(), embeddedTexIdxs(), mScene(nullptr) {
|
2015-12-31 15:00:19 +00:00
|
|
|
|
// empty
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-25 16:20:13 +00:00
|
|
|
|
glTFImporter::~glTFImporter() = default;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
const aiImporterDesc *glTFImporter::GetInfo() const {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
return &desc;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
bool glTFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const {
|
2021-04-23 22:17:50 +00:00
|
|
|
|
glTF::Asset asset(pIOHandler);
|
|
|
|
|
try {
|
|
|
|
|
asset.Load(pFile, GetExtension(pFile) == "glb");
|
2022-08-16 12:52:43 +00:00
|
|
|
|
return asset.asset;
|
2021-04-23 22:17:50 +00:00
|
|
|
|
} catch (...) {
|
2018-01-18 21:28:44 +00:00
|
|
|
|
return false;
|
2019-09-10 20:13:54 +00:00
|
|
|
|
}
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
inline void SetMaterialColorProperty(std::vector<int> &embeddedTexIdxs, Asset & /*r*/, glTF::TexProperty prop, aiMaterial *mat,
|
|
|
|
|
aiTextureType texType, const char *pKey, unsigned int type, unsigned int idx) {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (prop.texture) {
|
|
|
|
|
if (prop.texture->source) {
|
|
|
|
|
aiString uri(prop.texture->source->uri);
|
|
|
|
|
|
|
|
|
|
int texIdx = embeddedTexIdxs[prop.texture->source.GetIndex()];
|
|
|
|
|
if (texIdx != -1) { // embedded
|
|
|
|
|
// setup texture reference string (copied from ColladaLoader::FindFilenameForEffectTexture)
|
|
|
|
|
uri.data[0] = '*';
|
|
|
|
|
uri.length = 1 + ASSIMP_itoa10(uri.data + 1, MAXLEN - 1, texIdx);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
mat->AddProperty(&uri, _AI_MATKEY_TEXTURE_BASE, texType, 0);
|
|
|
|
|
}
|
2019-09-10 20:13:54 +00:00
|
|
|
|
} else {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
aiColor4D col;
|
|
|
|
|
CopyValue(prop.color, col);
|
Don’t ignore rgba(1,1,1,1) color properties
Do not ignore rgba(1,1,1,1) material properties when importing glTF1. While a white diffuse color may be the default value for a default assimp material, `1,1,1,1` is a very explicit color value for ambient/specular/emissive color properties.
Closes #1434
2017-09-18 22:16:48 +00:00
|
|
|
|
mat->AddProperty(&col, 1, pKey, type, idx);
|
2015-11-27 01:31:18 +00:00
|
|
|
|
}
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void glTFImporter::ImportMaterials(glTF::Asset &r) {
|
2016-04-28 18:50:01 +00:00
|
|
|
|
mScene->mNumMaterials = unsigned(r.materials.Size());
|
2020-06-23 19:11:18 +00:00
|
|
|
|
mScene->mMaterials = new aiMaterial *[mScene->mNumMaterials];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiMaterial *aimat = mScene->mMaterials[i] = new aiMaterial();
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
Material &mat = r.materials[i];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
/*if (!mat.name.empty())*/ {
|
|
|
|
|
aiString str(mat.id /*mat.name*/);
|
|
|
|
|
aimat->AddProperty(&str, AI_MATKEY_NAME);
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
SetMaterialColorProperty(embeddedTexIdxs, r, mat.ambient, aimat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT);
|
|
|
|
|
SetMaterialColorProperty(embeddedTexIdxs, r, mat.diffuse, aimat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE);
|
2015-11-29 18:13:51 +00:00
|
|
|
|
SetMaterialColorProperty(embeddedTexIdxs, r, mat.specular, aimat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR);
|
2018-01-10 19:02:41 +00:00
|
|
|
|
SetMaterialColorProperty(embeddedTexIdxs, r, mat.emission, aimat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE);
|
|
|
|
|
|
|
|
|
|
aimat->AddProperty(&mat.doubleSided, 1, AI_MATKEY_TWOSIDED);
|
|
|
|
|
|
|
|
|
|
if (mat.transparent && (mat.transparency != 1.0f)) {
|
|
|
|
|
aimat->AddProperty(&mat.transparency, 1, AI_MATKEY_OPACITY);
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (mat.shininess > 0.f) {
|
|
|
|
|
aimat->AddProperty(&mat.shininess, 1, AI_MATKEY_SHININESS);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (mScene->mNumMaterials == 0) {
|
|
|
|
|
mScene->mNumMaterials = 1;
|
2019-10-08 14:15:16 +00:00
|
|
|
|
// Delete the array of length zero created above.
|
|
|
|
|
delete[] mScene->mMaterials;
|
2020-06-23 19:11:18 +00:00
|
|
|
|
mScene->mMaterials = new aiMaterial *[1];
|
2015-11-29 18:13:51 +00:00
|
|
|
|
mScene->mMaterials[0] = new aiMaterial();
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
static inline void SetFace(aiFace &face, int a) {
|
2015-11-26 01:36:22 +00:00
|
|
|
|
face.mNumIndices = 1;
|
|
|
|
|
face.mIndices = new unsigned int[1];
|
|
|
|
|
face.mIndices[0] = a;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
static inline void SetFace(aiFace &face, int a, int b) {
|
2015-11-26 01:36:22 +00:00
|
|
|
|
face.mNumIndices = 2;
|
|
|
|
|
face.mIndices = new unsigned int[2];
|
|
|
|
|
face.mIndices[0] = a;
|
|
|
|
|
face.mIndices[1] = b;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
static inline void SetFace(aiFace &face, int a, int b, int c) {
|
2015-11-26 01:36:22 +00:00
|
|
|
|
face.mNumIndices = 3;
|
|
|
|
|
face.mIndices = new unsigned int[3];
|
|
|
|
|
face.mIndices[0] = a;
|
|
|
|
|
face.mIndices[1] = b;
|
|
|
|
|
face.mIndices[2] = c;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
static inline bool CheckValidFacesIndices(aiFace *faces, unsigned nFaces, unsigned nVerts) {
|
2016-05-10 14:25:15 +00:00
|
|
|
|
for (unsigned i = 0; i < nFaces; ++i) {
|
|
|
|
|
for (unsigned j = 0; j < faces[i].mNumIndices; ++j) {
|
|
|
|
|
unsigned idx = faces[i].mIndices[j];
|
|
|
|
|
if (idx >= nVerts)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void glTFImporter::ImportMeshes(glTF::Asset &r) {
|
|
|
|
|
std::vector<aiMesh *> meshes;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
unsigned int k = 0;
|
2020-02-14 07:41:55 +00:00
|
|
|
|
meshOffsets.clear();
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
for (unsigned int m = 0; m < r.meshes.Size(); ++m) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
Mesh &mesh = r.meshes[m];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
// Check if mesh extensions is used
|
|
|
|
|
if (mesh.Extension.size() > 0) {
|
2016-08-14 13:27:16 +00:00
|
|
|
|
#ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC
|
2020-07-14 18:07:36 +00:00
|
|
|
|
for (Mesh::SExtension *cur_ext : mesh.Extension) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
if (cur_ext->Type == Mesh::SExtension::EType::Compression_Open3DGC) {
|
|
|
|
|
// Limitations for meshes when using Open3DGC-compression.
|
|
|
|
|
// It's a current limitation of sp... Specification have not this part still - about mesh compression. Why only one primitive?
|
|
|
|
|
// Because glTF is very flexibly. But in fact it ugly flexible. Every primitive can has own set of accessors and accessors can
|
|
|
|
|
// point to a-a-a-a-any part of buffer (through bufferview of course) and even to another buffer. We know that "Open3DGC-compression"
|
|
|
|
|
// is applicable only to part of buffer. As we can't guaranty continuity of the data for decoder, we will limit quantity of primitives.
|
|
|
|
|
// Yes indices, coordinates etc. still can br stored in different buffers, but with current specification it's a exporter problem.
|
|
|
|
|
// Also primitive can has only one of "POSITION", "NORMAL" and less then "AI_MAX_NUMBER_OF_TEXTURECOORDS" of "TEXCOORD". All accessor
|
|
|
|
|
// of primitive must point to one continuous region of the buffer.
|
|
|
|
|
if (mesh.primitives.size() > 2) throw DeadlyImportError("GLTF: When using Open3DGC compression then only one primitive per mesh are allowed.");
|
|
|
|
|
|
|
|
|
|
Mesh::SCompression_Open3DGC *o3dgc_ext = (Mesh::SCompression_Open3DGC *)cur_ext;
|
|
|
|
|
Ref<Buffer> buf = r.buffers.Get(o3dgc_ext->Buffer);
|
|
|
|
|
|
|
|
|
|
buf->EncodedRegion_SetCurrent(mesh.id);
|
|
|
|
|
} else
|
|
|
|
|
{
|
2021-03-09 20:08:28 +00:00
|
|
|
|
throw DeadlyImportError("GLTF: Can not import mesh: unknown mesh extension (code: \"", ai_to_string(cur_ext->Type),
|
2020-06-23 19:11:18 +00:00
|
|
|
|
"\"), only Open3DGC is supported.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-14 18:07:36 +00:00
|
|
|
|
#endif
|
2020-06-23 19:11:18 +00:00
|
|
|
|
} // if(mesh.Extension.size() > 0)
|
|
|
|
|
|
|
|
|
|
meshOffsets.push_back(k);
|
2016-04-28 18:50:01 +00:00
|
|
|
|
k += unsigned(mesh.primitives.size());
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
for (unsigned int p = 0; p < mesh.primitives.size(); ++p) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
Mesh::Primitive &prim = mesh.primitives[p];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiMesh *aim = new aiMesh();
|
2015-11-29 18:13:51 +00:00
|
|
|
|
meshes.push_back(aim);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
aim->mName = mesh.id;
|
|
|
|
|
if (mesh.primitives.size() > 1) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
ai_uint32 &len = aim->mName.length;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
aim->mName.data[len] = '-';
|
2016-04-28 18:50:01 +00:00
|
|
|
|
len += 1 + ASSIMP_itoa10(aim->mName.data + len + 1, unsigned(MAXLEN - len - 1), p);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
switch (prim.mode) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case PrimitiveMode_POINTS:
|
|
|
|
|
aim->mPrimitiveTypes |= aiPrimitiveType_POINT;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PrimitiveMode_LINES:
|
|
|
|
|
case PrimitiveMode_LINE_LOOP:
|
|
|
|
|
case PrimitiveMode_LINE_STRIP:
|
|
|
|
|
aim->mPrimitiveTypes |= aiPrimitiveType_LINE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PrimitiveMode_TRIANGLES:
|
|
|
|
|
case PrimitiveMode_TRIANGLE_STRIP:
|
|
|
|
|
case PrimitiveMode_TRIANGLE_FAN:
|
|
|
|
|
aim->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
|
|
|
|
break;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
Mesh::Primitive::Attributes &attr = prim.attributes;
|
2016-08-03 15:06:38 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
if (attr.position.size() > 0 && attr.position[0]) {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
aim->mNumVertices = attr.position[0]->count;
|
|
|
|
|
attr.position[0]->ExtractData(aim->mVertices);
|
2020-06-23 19:11:18 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
if (attr.normal.size() > 0 && attr.normal[0]) attr.normal[0]->ExtractData(aim->mNormals);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2016-08-29 18:05:29 +00:00
|
|
|
|
for (size_t tc = 0; tc < attr.texcoord.size() && tc < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++tc) {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
attr.texcoord[tc]->ExtractData(aim->mTextureCoords[tc]);
|
|
|
|
|
aim->mNumUVComponents[tc] = attr.texcoord[tc]->GetNumComponents();
|
2016-05-10 14:25:15 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiVector3D *values = aim->mTextureCoords[tc];
|
2016-05-10 14:25:15 +00:00
|
|
|
|
for (unsigned int i = 0; i < aim->mNumVertices; ++i) {
|
|
|
|
|
values[i].y = 1 - values[i].y; // Flip Y coords
|
|
|
|
|
}
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiFace *faces = 0;
|
2018-07-04 23:49:08 +00:00
|
|
|
|
unsigned int nFaces = 0;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2018-07-04 23:49:08 +00:00
|
|
|
|
if (prim.indices) {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
unsigned int count = prim.indices->count;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
Accessor::Indexer data = prim.indices->GetIndexer();
|
2016-05-10 14:25:15 +00:00
|
|
|
|
ai_assert(data.IsValid());
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
switch (prim.mode) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case PrimitiveMode_POINTS: {
|
|
|
|
|
nFaces = count;
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
for (unsigned int i = 0; i < count; ++i) {
|
|
|
|
|
SetFace(faces[i], data.GetUInt(i));
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2020-06-23 19:11:18 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case PrimitiveMode_LINES: {
|
|
|
|
|
nFaces = count / 2;
|
|
|
|
|
if (nFaces * 2 != count) {
|
|
|
|
|
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
|
|
|
|
|
count = nFaces * 2;
|
|
|
|
|
}
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
for (unsigned int i = 0; i < count; i += 2) {
|
|
|
|
|
SetFace(faces[i / 2], data.GetUInt(i), data.GetUInt(i + 1));
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2020-06-23 19:11:18 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case PrimitiveMode_LINE_LOOP:
|
|
|
|
|
case PrimitiveMode_LINE_STRIP: {
|
|
|
|
|
nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0);
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
SetFace(faces[0], data.GetUInt(0), data.GetUInt(1));
|
|
|
|
|
for (unsigned int i = 2; i < count; ++i) {
|
|
|
|
|
SetFace(faces[i - 1], faces[i - 2].mIndices[1], data.GetUInt(i));
|
|
|
|
|
}
|
|
|
|
|
if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop
|
|
|
|
|
SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]);
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2020-06-23 19:11:18 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case PrimitiveMode_TRIANGLES: {
|
|
|
|
|
nFaces = count / 3;
|
|
|
|
|
if (nFaces * 3 != count) {
|
|
|
|
|
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
|
|
|
|
|
count = nFaces * 3;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2020-06-23 19:11:18 +00:00
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
for (unsigned int i = 0; i < count; i += 3) {
|
|
|
|
|
SetFace(faces[i / 3], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2));
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2020-06-23 19:11:18 +00:00
|
|
|
|
break;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case PrimitiveMode_TRIANGLE_STRIP: {
|
|
|
|
|
nFaces = count - 2;
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
SetFace(faces[0], data.GetUInt(0), data.GetUInt(1), data.GetUInt(2));
|
|
|
|
|
for (unsigned int i = 3; i < count; ++i) {
|
|
|
|
|
SetFace(faces[i - 2], faces[i - 1].mIndices[1], faces[i - 1].mIndices[2], data.GetUInt(i));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PrimitiveMode_TRIANGLE_FAN:
|
|
|
|
|
nFaces = count - 2;
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
SetFace(faces[0], data.GetUInt(0), data.GetUInt(1), data.GetUInt(2));
|
|
|
|
|
for (unsigned int i = 3; i < count; ++i) {
|
|
|
|
|
SetFace(faces[i - 2], faces[0].mIndices[0], faces[i - 1].mIndices[2], data.GetUInt(i));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else { // no indices provided so directly generate from counts
|
2018-07-04 23:37:57 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
// use the already determined count as it includes checks
|
2018-07-04 23:37:57 +00:00
|
|
|
|
unsigned int count = aim->mNumVertices;
|
|
|
|
|
|
|
|
|
|
switch (prim.mode) {
|
|
|
|
|
case PrimitiveMode_POINTS: {
|
|
|
|
|
nFaces = count;
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
for (unsigned int i = 0; i < count; ++i) {
|
|
|
|
|
SetFace(faces[i], i);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PrimitiveMode_LINES: {
|
|
|
|
|
nFaces = count / 2;
|
2019-10-08 11:51:08 +00:00
|
|
|
|
if (nFaces * 2 != count) {
|
|
|
|
|
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
|
|
|
|
|
count = nFaces * 2;
|
|
|
|
|
}
|
2018-07-04 23:37:57 +00:00
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
for (unsigned int i = 0; i < count; i += 2) {
|
|
|
|
|
SetFace(faces[i / 2], i, i + 1);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PrimitiveMode_LINE_LOOP:
|
|
|
|
|
case PrimitiveMode_LINE_STRIP: {
|
|
|
|
|
nFaces = count - ((prim.mode == PrimitiveMode_LINE_STRIP) ? 1 : 0);
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
SetFace(faces[0], 0, 1);
|
|
|
|
|
for (unsigned int i = 2; i < count; ++i) {
|
|
|
|
|
SetFace(faces[i - 1], faces[i - 2].mIndices[1], i);
|
|
|
|
|
}
|
|
|
|
|
if (prim.mode == PrimitiveMode_LINE_LOOP) { // close the loop
|
|
|
|
|
SetFace(faces[count - 1], faces[count - 2].mIndices[1], faces[0].mIndices[0]);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PrimitiveMode_TRIANGLES: {
|
|
|
|
|
nFaces = count / 3;
|
2019-10-08 11:51:08 +00:00
|
|
|
|
if (nFaces * 3 != count) {
|
|
|
|
|
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
|
|
|
|
|
count = nFaces * 3;
|
|
|
|
|
}
|
2018-07-04 23:37:57 +00:00
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
for (unsigned int i = 0; i < count; i += 3) {
|
|
|
|
|
SetFace(faces[i / 3], i, i + 1, i + 2);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PrimitiveMode_TRIANGLE_STRIP: {
|
|
|
|
|
nFaces = count - 2;
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
SetFace(faces[0], 0, 1, 2);
|
|
|
|
|
for (unsigned int i = 3; i < count; ++i) {
|
|
|
|
|
SetFace(faces[i - 2], faces[i - 1].mIndices[1], faces[i - 1].mIndices[2], i);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case PrimitiveMode_TRIANGLE_FAN:
|
|
|
|
|
nFaces = count - 2;
|
|
|
|
|
faces = new aiFace[nFaces];
|
|
|
|
|
SetFace(faces[0], 0, 1, 2);
|
|
|
|
|
for (unsigned int i = 3; i < count; ++i) {
|
|
|
|
|
SetFace(faces[i - 2], faces[0].mIndices[0], faces[i - 1].mIndices[2], i);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2018-07-04 23:49:08 +00:00
|
|
|
|
if (faces) {
|
|
|
|
|
aim->mFaces = faces;
|
|
|
|
|
aim->mNumFaces = nFaces;
|
2020-06-23 19:05:42 +00:00
|
|
|
|
const bool validRes = CheckValidFacesIndices(faces, nFaces, aim->mNumVertices);
|
|
|
|
|
if (!validRes) {
|
|
|
|
|
ai_assert(validRes);
|
|
|
|
|
ASSIMP_LOG_WARN("Invalid number of faces detected.");
|
|
|
|
|
}
|
2018-07-04 23:49:08 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (prim.material) {
|
|
|
|
|
aim->mMaterialIndex = prim.material.GetIndex();
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
meshOffsets.push_back(k);
|
2016-04-05 21:23:53 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
CopyVector(meshes, mScene->mMeshes, mScene->mNumMeshes);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void glTFImporter::ImportCameras(glTF::Asset &r) {
|
2019-09-10 20:13:54 +00:00
|
|
|
|
if (!r.cameras.Size()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
mScene->mNumCameras = r.cameras.Size();
|
2020-06-23 19:11:18 +00:00
|
|
|
|
mScene->mCameras = new aiCamera *[r.cameras.Size()];
|
2015-11-29 18:13:51 +00:00
|
|
|
|
for (size_t i = 0; i < r.cameras.Size(); ++i) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
Camera &cam = r.cameras[i];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiCamera *aicam = mScene->mCameras[i] = new aiCamera();
|
2015-11-29 18:13:51 +00:00
|
|
|
|
|
|
|
|
|
if (cam.type == Camera::Perspective) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aicam->mAspect = cam.perspective.aspectRatio;
|
2019-09-14 17:32:47 +00:00
|
|
|
|
aicam->mHorizontalFOV = cam.perspective.yfov * ((aicam->mAspect == 0.f) ? 1.f : aicam->mAspect);
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aicam->mClipPlaneFar = cam.perspective.zfar;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
aicam->mClipPlaneNear = cam.perspective.znear;
|
2019-09-10 20:13:54 +00:00
|
|
|
|
} else {
|
|
|
|
|
aicam->mClipPlaneFar = cam.ortographic.zfar;
|
|
|
|
|
aicam->mClipPlaneNear = cam.ortographic.znear;
|
|
|
|
|
aicam->mHorizontalFOV = 0.0;
|
2019-09-14 07:12:08 +00:00
|
|
|
|
aicam->mAspect = 1.0f;
|
2019-09-14 17:32:47 +00:00
|
|
|
|
if (0.f != cam.ortographic.ymag) {
|
|
|
|
|
aicam->mAspect = cam.ortographic.xmag / cam.ortographic.ymag;
|
2019-09-14 07:12:08 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void glTFImporter::ImportLights(glTF::Asset &r) {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (!r.lights.Size()) return;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
mScene->mNumLights = r.lights.Size();
|
2020-06-23 19:11:18 +00:00
|
|
|
|
mScene->mLights = new aiLight *[r.lights.Size()];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
for (size_t i = 0; i < r.lights.Size(); ++i) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
Light &l = r.lights[i];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiLight *ail = mScene->mLights[i] = new aiLight();
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
switch (l.type) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case Light::Type_directional:
|
|
|
|
|
ail->mType = aiLightSource_DIRECTIONAL;
|
|
|
|
|
break;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case Light::Type_spot:
|
|
|
|
|
ail->mType = aiLightSource_SPOT;
|
|
|
|
|
break;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
case Light::Type_ambient:
|
|
|
|
|
ail->mType = aiLightSource_AMBIENT;
|
|
|
|
|
break;
|
2015-11-29 19:17:06 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
default: // Light::Type_point
|
|
|
|
|
ail->mType = aiLightSource_POINT;
|
|
|
|
|
break;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
CopyValue(l.color, ail->mColorAmbient);
|
|
|
|
|
CopyValue(l.color, ail->mColorDiffuse);
|
|
|
|
|
CopyValue(l.color, ail->mColorSpecular);
|
2016-04-05 21:23:53 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
ail->mAngleOuterCone = l.falloffAngle;
|
|
|
|
|
ail->mAngleInnerCone = l.falloffExponent; // TODO fix this, it does not look right at all
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
ail->mAttenuationConstant = l.constantAttenuation;
|
|
|
|
|
ail->mAttenuationLinear = l.linearAttenuation;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
ail->mAttenuationQuadratic = l.quadraticAttenuation;
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiNode *ImportNode(aiScene *pScene, glTF::Asset &r, std::vector<unsigned int> &meshOffsets, glTF::Ref<glTF::Node> &ptr) {
|
|
|
|
|
Node &node = *ptr;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiNode *ainode = new aiNode(node.id);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (!node.children.empty()) {
|
2016-04-28 18:50:01 +00:00
|
|
|
|
ainode->mNumChildren = unsigned(node.children.size());
|
2020-06-23 19:11:18 +00:00
|
|
|
|
ainode->mChildren = new aiNode *[ainode->mNumChildren];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
for (unsigned int i = 0; i < ainode->mNumChildren; ++i) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiNode *child = ImportNode(pScene, r, meshOffsets, node.children[i]);
|
2015-11-29 18:13:51 +00:00
|
|
|
|
child->mParent = ainode;
|
|
|
|
|
ainode->mChildren[i] = child;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiMatrix4x4 &matrix = ainode->mTransformation;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (node.matrix.isPresent) {
|
|
|
|
|
CopyValue(node.matrix.value, matrix);
|
2020-06-23 19:11:18 +00:00
|
|
|
|
} else {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (node.translation.isPresent) {
|
2015-11-26 01:36:22 +00:00
|
|
|
|
aiVector3D trans;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
CopyValue(node.translation.value, trans);
|
|
|
|
|
aiMatrix4x4 t;
|
|
|
|
|
aiMatrix4x4::Translation(trans, t);
|
|
|
|
|
matrix = t * matrix;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (node.scale.isPresent) {
|
2015-11-26 01:36:22 +00:00
|
|
|
|
aiVector3D scal(1.f);
|
2015-11-29 18:13:51 +00:00
|
|
|
|
CopyValue(node.scale.value, scal);
|
|
|
|
|
aiMatrix4x4 s;
|
|
|
|
|
aiMatrix4x4::Scaling(scal, s);
|
|
|
|
|
matrix = s * matrix;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (node.rotation.isPresent) {
|
2015-11-26 01:36:22 +00:00
|
|
|
|
aiQuaternion rot;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
CopyValue(node.rotation.value, rot);
|
|
|
|
|
matrix = aiMatrix4x4(rot.GetMatrix()) * matrix;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (!node.meshes.empty()) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
for (size_t i = 0; i < node.meshes.size(); ++i) {
|
|
|
|
|
int idx = node.meshes[i].GetIndex();
|
|
|
|
|
count += meshOffsets[idx + 1] - meshOffsets[idx];
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
ainode->mNumMeshes = count;
|
|
|
|
|
ainode->mMeshes = new unsigned int[count];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
int k = 0;
|
|
|
|
|
for (size_t i = 0; i < node.meshes.size(); ++i) {
|
|
|
|
|
int idx = node.meshes[i].GetIndex();
|
2016-04-28 18:50:01 +00:00
|
|
|
|
for (unsigned int j = meshOffsets[idx]; j < meshOffsets[idx + 1]; ++j, ++k) {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
ainode->mMeshes[k] = j;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (node.camera) {
|
|
|
|
|
pScene->mCameras[node.camera.GetIndex()]->mName = ainode->mName;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (node.light) {
|
|
|
|
|
pScene->mLights[node.light.GetIndex()]->mName = ainode->mName;
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
|
|
|
|
return ainode;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void glTFImporter::ImportNodes(glTF::Asset &r) {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (!r.scene) return;
|
2015-11-08 19:29:05 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
std::vector<Ref<Node>> rootNodes = r.scene->nodes;
|
2015-11-08 19:29:05 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
// The root nodes
|
2016-04-28 18:50:01 +00:00
|
|
|
|
unsigned int numRootNodes = unsigned(rootNodes.size());
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (numRootNodes == 1) { // a single root node: use it
|
|
|
|
|
mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]);
|
2020-06-23 19:11:18 +00:00
|
|
|
|
} else if (numRootNodes > 1) { // more than one root node: create a fake root
|
|
|
|
|
aiNode *root = new aiNode("ROOT");
|
|
|
|
|
root->mChildren = new aiNode *[numRootNodes];
|
2015-11-29 18:13:51 +00:00
|
|
|
|
for (unsigned int i = 0; i < numRootNodes; ++i) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiNode *node = ImportNode(mScene, r, meshOffsets, rootNodes[i]);
|
2015-11-29 18:13:51 +00:00
|
|
|
|
node->mParent = root;
|
|
|
|
|
root->mChildren[root->mNumChildren++] = node;
|
|
|
|
|
}
|
|
|
|
|
mScene->mRootNode = root;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
2015-11-08 19:29:05 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
//if (!mScene->mRootNode) {
|
|
|
|
|
// mScene->mRootNode = new aiNode("EMPTY");
|
|
|
|
|
//}
|
2015-11-08 19:29:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void glTFImporter::ImportEmbeddedTextures(glTF::Asset &r) {
|
2015-11-29 18:13:51 +00:00
|
|
|
|
embeddedTexIdxs.resize(r.images.Size(), -1);
|
2015-11-08 19:29:05 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
int numEmbeddedTexs = 0;
|
|
|
|
|
for (size_t i = 0; i < r.images.Size(); ++i) {
|
|
|
|
|
if (r.images[i].HasData())
|
|
|
|
|
numEmbeddedTexs += 1;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (numEmbeddedTexs == 0)
|
|
|
|
|
return;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
mScene->mTextures = new aiTexture *[numEmbeddedTexs];
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
// Add the embedded textures
|
|
|
|
|
for (size_t i = 0; i < r.images.Size(); ++i) {
|
2018-01-02 18:09:22 +00:00
|
|
|
|
Image &img = r.images[i];
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (!img.HasData()) continue;
|
2015-11-08 19:29:05 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
int idx = mScene->mNumTextures++;
|
|
|
|
|
embeddedTexIdxs[i] = idx;
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
aiTexture *tex = mScene->mTextures[idx] = new aiTexture();
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
size_t length = img.GetDataLength();
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void *data = img.StealData();
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-01-31 18:08:04 +00:00
|
|
|
|
tex->mFilename = img.name;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
tex->mWidth = static_cast<unsigned int>(length);
|
|
|
|
|
tex->mHeight = 0;
|
2020-06-23 19:11:18 +00:00
|
|
|
|
tex->pcData = reinterpret_cast<aiTexel *>(data);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (!img.mimeType.empty()) {
|
2020-06-23 19:11:18 +00:00
|
|
|
|
const char *ext = strchr(img.mimeType.c_str(), '/') + 1;
|
2015-11-29 18:13:51 +00:00
|
|
|
|
if (ext) {
|
|
|
|
|
if (strcmp(ext, "jpeg") == 0) ext = "jpg";
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
size_t len = strlen(ext);
|
|
|
|
|
if (len <= 3) {
|
|
|
|
|
strcpy(tex->achFormatHint, ext);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
}
|
2015-11-29 18:13:51 +00:00
|
|
|
|
}
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void glTFImporter::ImportCommonMetadata(glTF::Asset &a) {
|
2019-12-05 11:25:12 +00:00
|
|
|
|
ai_assert(mScene->mMetaData == nullptr);
|
2019-12-05 11:37:22 +00:00
|
|
|
|
const bool hasVersion = !a.asset.version.empty();
|
|
|
|
|
const bool hasGenerator = !a.asset.generator.empty();
|
2019-12-09 14:30:12 +00:00
|
|
|
|
const bool hasCopyright = !a.asset.copyright.empty();
|
2020-06-23 19:11:18 +00:00
|
|
|
|
if (hasVersion || hasGenerator || hasCopyright) {
|
2019-12-05 11:37:22 +00:00
|
|
|
|
mScene->mMetaData = new aiMetadata;
|
2020-06-23 19:11:18 +00:00
|
|
|
|
if (hasVersion) {
|
2019-12-05 11:37:22 +00:00
|
|
|
|
mScene->mMetaData->Add(AI_METADATA_SOURCE_FORMAT_VERSION, aiString(a.asset.version));
|
|
|
|
|
}
|
2020-06-23 19:11:18 +00:00
|
|
|
|
if (hasGenerator) {
|
2019-12-05 11:37:22 +00:00
|
|
|
|
mScene->mMetaData->Add(AI_METADATA_SOURCE_GENERATOR, aiString(a.asset.generator));
|
|
|
|
|
}
|
2020-06-23 19:11:18 +00:00
|
|
|
|
if (hasCopyright) {
|
2019-12-09 14:30:12 +00:00
|
|
|
|
mScene->mMetaData->Add(AI_METADATA_SOURCE_COPYRIGHT, aiString(a.asset.copyright));
|
|
|
|
|
}
|
2019-12-05 11:37:22 +00:00
|
|
|
|
}
|
2019-12-05 11:25:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-23 19:11:18 +00:00
|
|
|
|
void glTFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
|
2019-06-05 16:51:49 +00:00
|
|
|
|
// clean all member arrays
|
|
|
|
|
meshOffsets.clear();
|
|
|
|
|
embeddedTexIdxs.clear();
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
this->mScene = pScene;
|
2016-04-05 21:23:53 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
// read the asset file
|
|
|
|
|
glTF::Asset asset(pIOHandler);
|
|
|
|
|
asset.Load(pFile, GetExtension(pFile) == "glb");
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
//
|
|
|
|
|
// Copy the data out
|
|
|
|
|
//
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
ImportEmbeddedTextures(asset);
|
|
|
|
|
ImportMaterials(asset);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
ImportMeshes(asset);
|
2015-11-26 01:36:22 +00:00
|
|
|
|
|
2015-11-29 18:13:51 +00:00
|
|
|
|
ImportCameras(asset);
|
|
|
|
|
ImportLights(asset);
|
|
|
|
|
|
|
|
|
|
ImportNodes(asset);
|
2019-12-05 13:52:34 +00:00
|
|
|
|
ImportCommonMetadata(asset);
|
2015-11-27 00:16:48 +00:00
|
|
|
|
|
|
|
|
|
if (pScene->mNumMeshes == 0) {
|
|
|
|
|
pScene->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
|
|
|
|
|
}
|
2015-11-08 19:29:05 +00:00
|
|
|
|
}
|
2015-11-26 19:49:00 +00:00
|
|
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_GLTF_IMPORTER
|