define CheckValidFacesIndices also in release builds.

pull/3296/head
Kim Kulling 2020-06-23 21:11:18 +02:00
parent 6205af4efb
commit ae05dbb252
1 changed files with 186 additions and 217 deletions

View File

@ -48,12 +48,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/StringComparison.h> #include <assimp/StringComparison.h>
#include <assimp/StringUtils.h> #include <assimp/StringUtils.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/ai_assert.h> #include <assimp/ai_assert.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/importerdesc.h>
#include <assimp/commonMetaData.h> #include <assimp/commonMetaData.h>
#include <assimp/importerdesc.h>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/Importer.hpp>
#include <memory> #include <memory>
@ -69,8 +69,7 @@ static const aiImporterDesc desc = {
"", "",
"", "",
"", "",
aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour | aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
| aiImporterFlags_LimitedSupport | aiImporterFlags_Experimental,
0, 0,
0, 0,
0, 0,
@ -78,11 +77,8 @@ static const aiImporterDesc desc = {
"gltf glb" "gltf glb"
}; };
glTFImporter::glTFImporter() glTFImporter::glTFImporter() :
: BaseImporter() BaseImporter(), meshOffsets(), embeddedTexIdxs(), mScene(nullptr) {
, meshOffsets()
, embeddedTexIdxs()
, mScene( nullptr ) {
// empty // empty
} }
@ -115,8 +111,7 @@ bool glTFImporter::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool
return false; return false;
} }
inline inline void SetMaterialColorProperty(std::vector<int> &embeddedTexIdxs, Asset & /*r*/, glTF::TexProperty prop, aiMaterial *mat,
void SetMaterialColorProperty(std::vector<int>& embeddedTexIdxs, Asset& /*r*/, glTF::TexProperty prop, aiMaterial* mat,
aiTextureType texType, const char *pKey, unsigned int type, unsigned int idx) { aiTextureType texType, const char *pKey, unsigned int type, unsigned int idx) {
if (prop.texture) { if (prop.texture) {
if (prop.texture->source) { if (prop.texture->source) {
@ -177,24 +172,20 @@ void glTFImporter::ImportMaterials(glTF::Asset& r) {
} }
} }
static inline void SetFace(aiFace &face, int a) {
static inline void SetFace(aiFace& face, int a)
{
face.mNumIndices = 1; face.mNumIndices = 1;
face.mIndices = new unsigned int[1]; face.mIndices = new unsigned int[1];
face.mIndices[0] = a; face.mIndices[0] = a;
} }
static inline void SetFace(aiFace& face, int a, int b) static inline void SetFace(aiFace &face, int a, int b) {
{
face.mNumIndices = 2; face.mNumIndices = 2;
face.mIndices = new unsigned int[2]; face.mIndices = new unsigned int[2];
face.mIndices[0] = a; face.mIndices[0] = a;
face.mIndices[1] = b; face.mIndices[1] = b;
} }
static inline void SetFace(aiFace& face, int a, int b, int c) static inline void SetFace(aiFace &face, int a, int b, int c) {
{
face.mNumIndices = 3; face.mNumIndices = 3;
face.mIndices = new unsigned int[3]; face.mIndices = new unsigned int[3];
face.mIndices[0] = a; face.mIndices[0] = a;
@ -202,9 +193,7 @@ static inline void SetFace(aiFace& face, int a, int b, int c)
face.mIndices[2] = c; face.mIndices[2] = c;
} }
#ifdef ASSIMP_BUILD_DEBUG static inline bool CheckValidFacesIndices(aiFace *faces, unsigned nFaces, unsigned nVerts) {
static inline bool CheckValidFacesIndices(aiFace* faces, unsigned nFaces, unsigned nVerts)
{
for (unsigned i = 0; i < nFaces; ++i) { for (unsigned i = 0; i < nFaces; ++i) {
for (unsigned j = 0; j < faces[i].mNumIndices; ++j) { for (unsigned j = 0; j < faces[i].mNumIndices; ++j) {
unsigned idx = faces[i].mIndices[j]; unsigned idx = faces[i].mIndices[j];
@ -214,10 +203,8 @@ static inline bool CheckValidFacesIndices(aiFace* faces, unsigned nFaces, unsign
} }
return true; return true;
} }
#endif // ASSIMP_BUILD_DEBUG
void glTFImporter::ImportMeshes(glTF::Asset& r) void glTFImporter::ImportMeshes(glTF::Asset &r) {
{
std::vector<aiMesh *> meshes; std::vector<aiMesh *> meshes;
unsigned int k = 0; unsigned int k = 0;
@ -227,13 +214,10 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)
Mesh &mesh = r.meshes[m]; Mesh &mesh = r.meshes[m];
// Check if mesh extensions is used // Check if mesh extensions is used
if(mesh.Extension.size() > 0) if (mesh.Extension.size() > 0) {
{ for (Mesh::SExtension *cur_ext : mesh.Extension) {
for(Mesh::SExtension* cur_ext : mesh.Extension)
{
#ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC #ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC
if(cur_ext->Type == Mesh::SExtension::EType::Compression_Open3DGC) if (cur_ext->Type == Mesh::SExtension::EType::Compression_Open3DGC) {
{
// Limitations for meshes when using Open3DGC-compression. // 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? // 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 // Because glTF is very flexibly. But in fact it ugly flexible. Every primitive can has own set of accessors and accessors can
@ -248,8 +232,7 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)
Ref<Buffer> buf = r.buffers.Get(o3dgc_ext->Buffer); Ref<Buffer> buf = r.buffers.Get(o3dgc_ext->Buffer);
buf->EncodedRegion_SetCurrent(mesh.id); buf->EncodedRegion_SetCurrent(mesh.id);
} } else
else
#endif #endif
{ {
throw DeadlyImportError("GLTF: Can not import mesh: unknown mesh extension (code: \"" + to_string(cur_ext->Type) + throw DeadlyImportError("GLTF: Can not import mesh: unknown mesh extension (code: \"" + to_string(cur_ext->Type) +
@ -311,7 +294,6 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)
} }
} }
aiFace *faces = 0; aiFace *faces = 0;
unsigned int nFaces = 0; unsigned int nFaces = 0;
@ -388,8 +370,7 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)
} }
break; break;
} }
} } else { // no indices provided so directly generate from counts
else { // no indices provided so directly generate from counts
// use the already determined count as it includes checks // use the already determined count as it includes checks
unsigned int count = aim->mNumVertices; unsigned int count = aim->mNumVertices;
@ -513,8 +494,7 @@ void glTFImporter::ImportCameras(glTF::Asset& r) {
} }
} }
void glTFImporter::ImportLights(glTF::Asset& r) void glTFImporter::ImportLights(glTF::Asset &r) {
{
if (!r.lights.Size()) return; if (!r.lights.Size()) return;
mScene->mNumLights = r.lights.Size(); mScene->mNumLights = r.lights.Size();
@ -527,16 +507,20 @@ void glTFImporter::ImportLights(glTF::Asset& r)
switch (l.type) { switch (l.type) {
case Light::Type_directional: case Light::Type_directional:
ail->mType = aiLightSource_DIRECTIONAL; break; ail->mType = aiLightSource_DIRECTIONAL;
break;
case Light::Type_spot: case Light::Type_spot:
ail->mType = aiLightSource_SPOT; break; ail->mType = aiLightSource_SPOT;
break;
case Light::Type_ambient: case Light::Type_ambient:
ail->mType = aiLightSource_AMBIENT; break; ail->mType = aiLightSource_AMBIENT;
break;
default: // Light::Type_point default: // Light::Type_point
ail->mType = aiLightSource_POINT; break; ail->mType = aiLightSource_POINT;
break;
} }
CopyValue(l.color, ail->mColorAmbient); CopyValue(l.color, ail->mColorAmbient);
@ -552,9 +536,7 @@ void glTFImporter::ImportLights(glTF::Asset& r)
} }
} }
aiNode *ImportNode(aiScene *pScene, glTF::Asset &r, std::vector<unsigned int> &meshOffsets, glTF::Ref<glTF::Node> &ptr) {
aiNode* ImportNode(aiScene* pScene, glTF::Asset& r, std::vector<unsigned int>& meshOffsets, glTF::Ref<glTF::Node>& ptr)
{
Node &node = *ptr; Node &node = *ptr;
aiNode *ainode = new aiNode(node.id); aiNode *ainode = new aiNode(node.id);
@ -573,8 +555,7 @@ aiNode* ImportNode(aiScene* pScene, glTF::Asset& r, std::vector<unsigned int>& m
aiMatrix4x4 &matrix = ainode->mTransformation; aiMatrix4x4 &matrix = ainode->mTransformation;
if (node.matrix.isPresent) { if (node.matrix.isPresent) {
CopyValue(node.matrix.value, matrix); CopyValue(node.matrix.value, matrix);
} } else {
else {
if (node.translation.isPresent) { if (node.translation.isPresent) {
aiVector3D trans; aiVector3D trans;
CopyValue(node.translation.value, trans); CopyValue(node.translation.value, trans);
@ -591,7 +572,6 @@ aiNode* ImportNode(aiScene* pScene, glTF::Asset& r, std::vector<unsigned int>& m
matrix = s * matrix; matrix = s * matrix;
} }
if (node.rotation.isPresent) { if (node.rotation.isPresent) {
aiQuaternion rot; aiQuaternion rot;
CopyValue(node.rotation.value, rot); CopyValue(node.rotation.value, rot);
@ -629,8 +609,7 @@ aiNode* ImportNode(aiScene* pScene, glTF::Asset& r, std::vector<unsigned int>& m
return ainode; return ainode;
} }
void glTFImporter::ImportNodes(glTF::Asset& r) void glTFImporter::ImportNodes(glTF::Asset &r) {
{
if (!r.scene) return; if (!r.scene) return;
std::vector<Ref<Node>> rootNodes = r.scene->nodes; std::vector<Ref<Node>> rootNodes = r.scene->nodes;
@ -639,8 +618,7 @@ void glTFImporter::ImportNodes(glTF::Asset& r)
unsigned int numRootNodes = unsigned(rootNodes.size()); unsigned int numRootNodes = unsigned(rootNodes.size());
if (numRootNodes == 1) { // a single root node: use it if (numRootNodes == 1) { // a single root node: use it
mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]); mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]);
} } else if (numRootNodes > 1) { // more than one root node: create a fake root
else if (numRootNodes > 1) { // more than one root node: create a fake root
aiNode *root = new aiNode("ROOT"); aiNode *root = new aiNode("ROOT");
root->mChildren = new aiNode *[numRootNodes]; root->mChildren = new aiNode *[numRootNodes];
for (unsigned int i = 0; i < numRootNodes; ++i) { for (unsigned int i = 0; i < numRootNodes; ++i) {
@ -656,8 +634,7 @@ void glTFImporter::ImportNodes(glTF::Asset& r)
//} //}
} }
void glTFImporter::ImportEmbeddedTextures(glTF::Asset& r) void glTFImporter::ImportEmbeddedTextures(glTF::Asset &r) {
{
embeddedTexIdxs.resize(r.images.Size(), -1); embeddedTexIdxs.resize(r.images.Size(), -1);
int numEmbeddedTexs = 0; int numEmbeddedTexs = 0;
@ -703,32 +680,26 @@ void glTFImporter::ImportEmbeddedTextures(glTF::Asset& r)
} }
} }
void glTFImporter::ImportCommonMetadata(glTF::Asset& a) void glTFImporter::ImportCommonMetadata(glTF::Asset &a) {
{
ai_assert(mScene->mMetaData == nullptr); ai_assert(mScene->mMetaData == nullptr);
const bool hasVersion = !a.asset.version.empty(); const bool hasVersion = !a.asset.version.empty();
const bool hasGenerator = !a.asset.generator.empty(); const bool hasGenerator = !a.asset.generator.empty();
const bool hasCopyright = !a.asset.copyright.empty(); const bool hasCopyright = !a.asset.copyright.empty();
if (hasVersion || hasGenerator || hasCopyright) if (hasVersion || hasGenerator || hasCopyright) {
{
mScene->mMetaData = new aiMetadata; mScene->mMetaData = new aiMetadata;
if (hasVersion) if (hasVersion) {
{
mScene->mMetaData->Add(AI_METADATA_SOURCE_FORMAT_VERSION, aiString(a.asset.version)); mScene->mMetaData->Add(AI_METADATA_SOURCE_FORMAT_VERSION, aiString(a.asset.version));
} }
if (hasGenerator) if (hasGenerator) {
{
mScene->mMetaData->Add(AI_METADATA_SOURCE_GENERATOR, aiString(a.asset.generator)); mScene->mMetaData->Add(AI_METADATA_SOURCE_GENERATOR, aiString(a.asset.generator));
} }
if (hasCopyright) if (hasCopyright) {
{
mScene->mMetaData->Add(AI_METADATA_SOURCE_COPYRIGHT, aiString(a.asset.copyright)); mScene->mMetaData->Add(AI_METADATA_SOURCE_COPYRIGHT, aiString(a.asset.copyright));
} }
} }
} }
void glTFImporter::InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) void glTFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
{
// clean all member arrays // clean all member arrays
meshOffsets.clear(); meshOffsets.clear();
embeddedTexIdxs.clear(); embeddedTexIdxs.clear();
@ -739,7 +710,6 @@ void glTFImporter::InternReadFile(const std::string& pFile, aiScene* pScene, IOS
glTF::Asset asset(pIOHandler); glTF::Asset asset(pIOHandler);
asset.Load(pFile, GetExtension(pFile) == "glb"); asset.Load(pFile, GetExtension(pFile) == "glb");
// //
// Copy the data out // Copy the data out
// //
@ -761,4 +731,3 @@ void glTFImporter::InternReadFile(const std::string& pFile, aiScene* pScene, IOS
} }
#endif // ASSIMP_BUILD_NO_GLTF_IMPORTER #endif // ASSIMP_BUILD_NO_GLTF_IMPORTER