2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2020-01-20 13:53:12 +00:00
|
|
|
Copyright (c) 2006-2020, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-05-19 03:48:29 +00:00
|
|
|
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.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
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
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
|
|
|
|
|
|
|
|
#include "ObjFileImporter.h"
|
|
|
|
#include "ObjFileData.h"
|
2020-03-15 09:21:08 +00:00
|
|
|
#include "ObjFileParser.h"
|
2017-02-28 02:31:56 +00:00
|
|
|
#include <assimp/DefaultIOSystem.h>
|
2020-03-15 09:21:08 +00:00
|
|
|
#include <assimp/IOStreamBuffer.h>
|
2015-07-09 23:21:10 +00:00
|
|
|
#include <assimp/ai_assert.h>
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2020-03-15 09:21:08 +00:00
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/Importer.hpp>
|
2022-02-18 05:10:46 +00:00
|
|
|
#include <assimp/ObjMaterial.h>
|
2020-03-15 09:21:08 +00:00
|
|
|
#include <memory>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
static const aiImporterDesc desc = {
|
|
|
|
"Wavefront Object Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"surfaces not supported",
|
|
|
|
aiImporterFlags_SupportTextFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"obj"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned int ObjMinSize = 16;
|
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Default constructor
|
2020-03-15 09:21:08 +00:00
|
|
|
ObjFileImporter::ObjFileImporter() :
|
2020-08-27 15:05:09 +00:00
|
|
|
m_Buffer(),
|
|
|
|
m_pRootObject(nullptr),
|
|
|
|
m_strAbsPath(std::string(1, DefaultIOSystem().getOsSeparator())) {}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Destructor.
|
2018-05-06 22:07:41 +00:00
|
|
|
ObjFileImporter::~ObjFileImporter() {
|
2015-05-19 03:48:29 +00:00
|
|
|
delete m_pRootObject;
|
2018-05-06 22:07:41 +00:00
|
|
|
m_pRootObject = nullptr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-04-23 22:17:50 +00:00
|
|
|
// Returns true if file is an obj file.
|
|
|
|
bool ObjFileImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
|
|
|
|
static const char *tokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " };
|
2021-05-05 11:14:25 +00:00
|
|
|
return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens), 200, false, true);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-15 09:21:08 +00:00
|
|
|
const aiImporterDesc *ObjFileImporter::GetInfo() const {
|
2015-05-19 03:48:29 +00:00
|
|
|
return &desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Obj-file import implementation
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileImporter::InternReadFile(const std::string &file, aiScene *pScene, IOSystem *pIOHandler) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Read file into memory
|
2016-02-08 17:07:00 +00:00
|
|
|
static const std::string mode = "rb";
|
2020-09-01 16:30:31 +00:00
|
|
|
auto streamCloser = [&](IOStream *pStream) {
|
|
|
|
pIOHandler->Close(pStream);
|
|
|
|
};
|
|
|
|
std::unique_ptr<IOStream, decltype(streamCloser)> fileStream(pIOHandler->Open(file, mode), streamCloser);
|
2022-11-08 16:03:55 +00:00
|
|
|
if (!fileStream) {
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Failed to open file ", file, ".");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the file-size and validate it, throwing an exception when fails
|
2016-02-08 17:07:00 +00:00
|
|
|
size_t fileSize = fileStream->FileSize();
|
2020-03-15 09:21:08 +00:00
|
|
|
if (fileSize < ObjMinSize) {
|
|
|
|
throw DeadlyImportError("OBJ-file is too small.");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-10-11 10:29:40 +00:00
|
|
|
IOStreamBuffer<char> streamedBuffer;
|
2020-03-15 09:21:08 +00:00
|
|
|
streamedBuffer.open(fileStream.get());
|
2016-10-11 10:29:40 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Allocate buffer and read file into it
|
2016-10-11 10:29:40 +00:00
|
|
|
//TextFileToBuffer( fileStream.get(),m_Buffer);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// Get the model name
|
2020-03-15 09:21:08 +00:00
|
|
|
std::string modelName, folderName;
|
|
|
|
std::string::size_type pos = file.find_last_of("\\/");
|
|
|
|
if (pos != std::string::npos) {
|
|
|
|
modelName = file.substr(pos + 1, file.size() - pos - 1);
|
|
|
|
folderName = file.substr(0, pos);
|
|
|
|
if (!folderName.empty()) {
|
|
|
|
pIOHandler->PushDirectory(folderName);
|
2015-08-30 13:21:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-02-08 17:07:00 +00:00
|
|
|
modelName = file;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse the file into a temporary representation
|
2020-03-15 09:21:08 +00:00
|
|
|
ObjFileParser parser(streamedBuffer, modelName, pIOHandler, m_progress, file);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// And create the proper return structures out of it
|
|
|
|
CreateDataFromImport(parser.GetModel(), pScene);
|
|
|
|
|
2016-10-11 10:29:40 +00:00
|
|
|
streamedBuffer.close();
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// Clean up allocated storage for the next import
|
2015-05-19 03:48:29 +00:00
|
|
|
m_Buffer.clear();
|
2015-08-30 13:21:53 +00:00
|
|
|
|
|
|
|
// Pop directory stack
|
2020-03-15 09:21:08 +00:00
|
|
|
if (pIOHandler->StackSize() > 0) {
|
2015-08-30 13:21:53 +00:00
|
|
|
pIOHandler->PopDirectory();
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Create the data from parsed obj-file
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene *pScene) {
|
2021-08-12 19:13:07 +00:00
|
|
|
if (nullptr == pModel) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Create the root node of the scene
|
|
|
|
pScene->mRootNode = new aiNode;
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!pModel->mModelName.empty()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Set the name of the scene
|
2022-07-31 18:54:58 +00:00
|
|
|
pScene->mRootNode->mName.Set(pModel->mModelName);
|
2018-03-20 22:38:08 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
// This is a fatal error, so break down the application
|
|
|
|
ai_assert(false);
|
2015-05-19 03:52:10 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!pModel->mObjects.empty()) {
|
2018-09-03 18:33:56 +00:00
|
|
|
|
|
|
|
unsigned int meshCount = 0;
|
|
|
|
unsigned int childCount = 0;
|
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
for (auto object : pModel->mObjects) {
|
2020-03-15 09:21:08 +00:00
|
|
|
if (object) {
|
2018-09-03 18:33:56 +00:00
|
|
|
++childCount;
|
2020-02-12 22:10:07 +00:00
|
|
|
meshCount += (unsigned int)object->m_Meshes.size();
|
2018-09-03 18:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate space for the child nodes on the root node
|
2020-03-15 09:21:08 +00:00
|
|
|
pScene->mRootNode->mChildren = new aiNode *[childCount];
|
2018-09-03 18:33:56 +00:00
|
|
|
|
2018-08-12 21:18:12 +00:00
|
|
|
// Create nodes for the whole scene
|
2020-03-15 09:21:08 +00:00
|
|
|
std::vector<aiMesh *> MeshArray;
|
2018-09-03 18:33:56 +00:00
|
|
|
MeshArray.reserve(meshCount);
|
2022-07-31 18:54:58 +00:00
|
|
|
for (size_t index = 0; index < pModel->mObjects.size(); ++index) {
|
|
|
|
createNodes(pModel, pModel->mObjects[index], pScene->mRootNode, pScene, MeshArray);
|
2018-08-12 21:18:12 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-09-03 18:33:56 +00:00
|
|
|
ai_assert(pScene->mRootNode->mNumChildren == childCount);
|
|
|
|
|
2018-08-12 21:18:12 +00:00
|
|
|
// Create mesh pointer buffer for this scene
|
|
|
|
if (pScene->mNumMeshes > 0) {
|
2020-03-15 09:21:08 +00:00
|
|
|
pScene->mMeshes = new aiMesh *[MeshArray.size()];
|
2018-08-12 21:18:12 +00:00
|
|
|
for (size_t index = 0; index < MeshArray.size(); ++index) {
|
|
|
|
pScene->mMeshes[index] = MeshArray[index];
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 21:18:12 +00:00
|
|
|
// Create all materials
|
|
|
|
createMaterials(pModel, pScene);
|
2020-03-15 09:21:08 +00:00
|
|
|
} else {
|
2022-07-31 18:54:58 +00:00
|
|
|
if (pModel->mVertices.empty()) {
|
2020-03-15 09:21:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-12 21:18:12 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
std::unique_ptr<aiMesh> mesh(new aiMesh);
|
2018-08-12 21:18:12 +00:00
|
|
|
mesh->mPrimitiveTypes = aiPrimitiveType_POINT;
|
2022-07-31 18:54:58 +00:00
|
|
|
unsigned int n = (unsigned int)pModel->mVertices.size();
|
2018-08-12 21:18:12 +00:00
|
|
|
mesh->mNumVertices = n;
|
|
|
|
|
|
|
|
mesh->mVertices = new aiVector3D[n];
|
2022-07-31 18:54:58 +00:00
|
|
|
memcpy(mesh->mVertices, pModel->mVertices.data(), n * sizeof(aiVector3D));
|
2018-08-12 21:18:12 +00:00
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!pModel->mNormals.empty()) {
|
2018-08-12 21:18:12 +00:00
|
|
|
mesh->mNormals = new aiVector3D[n];
|
2022-07-31 18:54:58 +00:00
|
|
|
if (pModel->mNormals.size() < n) {
|
2018-08-14 22:22:28 +00:00
|
|
|
throw DeadlyImportError("OBJ: vertex normal index out of range");
|
|
|
|
}
|
2022-07-31 18:54:58 +00:00
|
|
|
memcpy(mesh->mNormals, pModel->mNormals.data(), n * sizeof(aiVector3D));
|
2018-08-12 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!pModel->mVertexColors.empty()) {
|
2018-08-12 21:18:12 +00:00
|
|
|
mesh->mColors[0] = new aiColor4D[mesh->mNumVertices];
|
|
|
|
for (unsigned int i = 0; i < n; ++i) {
|
2022-07-31 18:54:58 +00:00
|
|
|
if (i < pModel->mVertexColors.size()) {
|
|
|
|
const aiVector3D &color = pModel->mVertexColors[i];
|
2018-08-12 21:18:12 +00:00
|
|
|
mesh->mColors[0][i] = aiColor4D(color.x, color.y, color.z, 1.0);
|
2020-03-15 09:21:08 +00:00
|
|
|
} else {
|
2018-08-14 22:22:28 +00:00
|
|
|
throw DeadlyImportError("OBJ: vertex color index out of range");
|
2018-08-12 21:18:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-14 23:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pScene->mRootNode->mNumMeshes = 1;
|
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[1];
|
|
|
|
pScene->mRootNode->mMeshes[0] = 0;
|
2020-03-15 09:21:08 +00:00
|
|
|
pScene->mMeshes = new aiMesh *[1];
|
2018-08-14 23:27:56 +00:00
|
|
|
pScene->mNumMeshes = 1;
|
|
|
|
pScene->mMeshes[0] = mesh.release();
|
2018-08-12 21:18:12 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Creates all nodes of the model
|
2020-03-15 09:21:08 +00:00
|
|
|
aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pObject,
|
|
|
|
aiNode *pParent, aiScene *pScene,
|
|
|
|
std::vector<aiMesh *> &MeshArray) {
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(nullptr != pModel);
|
|
|
|
if (nullptr == pObject) {
|
|
|
|
return nullptr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Store older mesh size to be able to computes mesh offsets for new mesh instances
|
|
|
|
const size_t oldMeshSize = MeshArray.size();
|
|
|
|
aiNode *pNode = new aiNode;
|
|
|
|
|
|
|
|
pNode->mName = pObject->m_strObjName;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// If we have a parent node, store it
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(nullptr != pParent);
|
2020-03-15 09:21:08 +00:00
|
|
|
appendChildToParentNode(pParent, pNode);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
for (size_t i = 0; i < pObject->m_Meshes.size(); ++i) {
|
|
|
|
unsigned int meshId = pObject->m_Meshes[i];
|
|
|
|
aiMesh *pMesh = createTopology(pModel, pObject, meshId);
|
|
|
|
if (pMesh) {
|
2017-12-19 17:38:38 +00:00
|
|
|
if (pMesh->mNumFaces > 0) {
|
2020-03-15 09:21:08 +00:00
|
|
|
MeshArray.push_back(pMesh);
|
2017-12-19 17:38:38 +00:00
|
|
|
} else {
|
|
|
|
delete pMesh;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all nodes from the sub-objects stored in the current object
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!pObject->m_SubObjects.empty()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
size_t numChilds = pObject->m_SubObjects.size();
|
2020-03-15 09:21:08 +00:00
|
|
|
pNode->mNumChildren = static_cast<unsigned int>(numChilds);
|
|
|
|
pNode->mChildren = new aiNode *[numChilds];
|
2015-05-19 03:48:29 +00:00
|
|
|
pNode->mNumMeshes = 1;
|
2020-03-15 09:21:08 +00:00
|
|
|
pNode->mMeshes = new unsigned int[1];
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set mesh instances into scene- and node-instances
|
2020-03-15 09:21:08 +00:00
|
|
|
const size_t meshSizeDiff = MeshArray.size() - oldMeshSize;
|
|
|
|
if (meshSizeDiff > 0) {
|
|
|
|
pNode->mMeshes = new unsigned int[meshSizeDiff];
|
|
|
|
pNode->mNumMeshes = static_cast<unsigned int>(meshSizeDiff);
|
2015-05-19 03:48:29 +00:00
|
|
|
size_t index = 0;
|
2020-03-15 09:21:08 +00:00
|
|
|
for (size_t i = oldMeshSize; i < MeshArray.size(); ++i) {
|
|
|
|
pNode->mMeshes[index] = pScene->mNumMeshes;
|
2015-05-19 03:48:29 +00:00
|
|
|
pScene->mNumMeshes++;
|
2018-03-20 22:38:08 +00:00
|
|
|
++index;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
return pNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Create topology data
|
2020-03-15 09:21:08 +00:00
|
|
|
aiMesh *ObjFileImporter::createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData, unsigned int meshIndex) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Checking preconditions
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(nullptr != pModel);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (nullptr == pData) {
|
|
|
|
return nullptr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create faces
|
2022-07-31 18:54:58 +00:00
|
|
|
ObjFile::Mesh *pObjMesh = pModel->mMeshes[meshIndex];
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!pObjMesh) {
|
2020-06-23 19:05:42 +00:00
|
|
|
return nullptr;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2016-03-04 11:33:45 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (pObjMesh->m_Faces.empty()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
return nullptr;
|
2016-03-04 11:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 16:24:03 +00:00
|
|
|
std::unique_ptr<aiMesh> pMesh(new aiMesh);
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!pObjMesh->m_name.empty()) {
|
|
|
|
pMesh->mName.Set(pObjMesh->m_name);
|
2015-07-09 18:15:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) {
|
2022-07-30 19:52:33 +00:00
|
|
|
const ObjFile::Face *inp = pObjMesh->m_Faces[index];
|
2022-07-30 10:58:09 +00:00
|
|
|
//ai_assert(nullptr != inp);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-07-30 19:52:33 +00:00
|
|
|
if (inp->mPrimitiveType == aiPrimitiveType_LINE) {
|
|
|
|
pMesh->mNumFaces += static_cast<unsigned int>(inp->m_vertices.size() - 1);
|
2015-05-19 03:48:29 +00:00
|
|
|
pMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
|
2022-07-30 19:52:33 +00:00
|
|
|
} else if (inp->mPrimitiveType == aiPrimitiveType_POINT) {
|
|
|
|
pMesh->mNumFaces += static_cast<unsigned int>(inp->m_vertices.size());
|
2015-05-19 03:48:29 +00:00
|
|
|
pMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
|
|
|
|
} else {
|
|
|
|
++pMesh->mNumFaces;
|
2022-07-30 19:52:33 +00:00
|
|
|
if (inp->m_vertices.size() > 3) {
|
2015-05-19 03:48:29 +00:00
|
|
|
pMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
|
|
|
} else {
|
|
|
|
pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
unsigned int uiIdxCount(0u);
|
|
|
|
if (pMesh->mNumFaces > 0) {
|
|
|
|
pMesh->mFaces = new aiFace[pMesh->mNumFaces];
|
|
|
|
if (pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial) {
|
2015-05-19 03:48:29 +00:00
|
|
|
pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
unsigned int outIndex(0);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// Copy all data from all stored meshes
|
2020-03-15 09:21:08 +00:00
|
|
|
for (auto &face : pObjMesh->m_Faces) {
|
2022-07-30 19:52:33 +00:00
|
|
|
const ObjFile::Face *inp = face;
|
|
|
|
if (inp->mPrimitiveType == aiPrimitiveType_LINE) {
|
|
|
|
for (size_t i = 0; i < inp->m_vertices.size() - 1; ++i) {
|
2020-03-15 09:21:08 +00:00
|
|
|
aiFace &f = pMesh->mFaces[outIndex++];
|
2015-05-19 03:48:29 +00:00
|
|
|
uiIdxCount += f.mNumIndices = 2;
|
|
|
|
f.mIndices = new unsigned int[2];
|
|
|
|
}
|
|
|
|
continue;
|
2022-07-30 19:52:33 +00:00
|
|
|
} else if (inp->mPrimitiveType == aiPrimitiveType_POINT) {
|
|
|
|
for (size_t i = 0; i < inp->m_vertices.size(); ++i) {
|
2020-03-15 09:21:08 +00:00
|
|
|
aiFace &f = pMesh->mFaces[outIndex++];
|
2015-05-19 03:48:29 +00:00
|
|
|
uiIdxCount += f.mNumIndices = 1;
|
|
|
|
f.mIndices = new unsigned int[1];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
aiFace *pFace = &pMesh->mFaces[outIndex++];
|
2022-07-30 19:52:33 +00:00
|
|
|
const unsigned int uiNumIndices = (unsigned int)face->m_vertices.size();
|
2020-03-15 09:21:08 +00:00
|
|
|
uiIdxCount += pFace->mNumIndices = (unsigned int)uiNumIndices;
|
2015-05-19 03:48:29 +00:00
|
|
|
if (pFace->mNumIndices > 0) {
|
2020-03-15 09:21:08 +00:00
|
|
|
pFace->mIndices = new unsigned int[uiNumIndices];
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create mesh vertices
|
2017-12-19 16:24:03 +00:00
|
|
|
createVertexArray(pModel, pData, meshIndex, pMesh.get(), uiIdxCount);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2017-12-19 16:24:03 +00:00
|
|
|
return pMesh.release();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Creates a vertex array
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileImporter::createVertexArray(const ObjFile::Model *pModel,
|
|
|
|
const ObjFile::Object *pCurrentObject,
|
|
|
|
unsigned int uiMeshIndex,
|
|
|
|
aiMesh *pMesh,
|
|
|
|
unsigned int numIndices) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Checking preconditions
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(nullptr != pCurrentObject);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Break, if no faces are stored in object
|
2020-03-15 09:21:08 +00:00
|
|
|
if (pCurrentObject->m_Meshes.empty())
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Get current mesh
|
2022-07-31 18:54:58 +00:00
|
|
|
ObjFile::Mesh *pObjMesh = pModel->mMeshes[uiMeshIndex];
|
2020-06-23 19:05:42 +00:00
|
|
|
if (nullptr == pObjMesh || pObjMesh->m_uiNumIndices < 1) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
2016-09-06 08:42:02 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// Copy vertices of this mesh instance
|
|
|
|
pMesh->mNumVertices = numIndices;
|
2015-08-11 13:09:19 +00:00
|
|
|
if (pMesh->mNumVertices == 0) {
|
2020-03-15 09:21:08 +00:00
|
|
|
throw DeadlyImportError("OBJ: no vertices");
|
2018-09-03 18:33:56 +00:00
|
|
|
} else if (pMesh->mNumVertices > AI_MAX_VERTICES) {
|
2020-03-15 09:21:08 +00:00
|
|
|
throw DeadlyImportError("OBJ: Too many vertices");
|
2015-08-11 13:09:19 +00:00
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Allocate buffer for normal vectors
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!pModel->mNormals.empty() && pObjMesh->m_hasNormals)
|
2020-03-15 09:21:08 +00:00
|
|
|
pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2016-06-28 00:08:22 +00:00
|
|
|
// Allocate buffer for vertex-color vectors
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!pModel->mVertexColors.empty())
|
2020-03-15 09:21:08 +00:00
|
|
|
pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
|
2016-06-28 00:08:22 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Allocate buffer for texture coordinates
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!pModel->mTextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0]) {
|
|
|
|
pMesh->mNumUVComponents[0] = pModel->mTextureCoordDim;
|
2020-03-15 09:21:08 +00:00
|
|
|
pMesh->mTextureCoords[0] = new aiVector3D[pMesh->mNumVertices];
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Copy vertices, normals and textures into aiMesh instance
|
2019-02-26 23:15:03 +00:00
|
|
|
bool normalsok = true, uvok = true;
|
2015-05-19 03:48:29 +00:00
|
|
|
unsigned int newIndex = 0, outIndex = 0;
|
2022-07-30 19:52:33 +00:00
|
|
|
for (auto sourceFace : pObjMesh->m_Faces) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Copy all index arrays
|
2022-07-30 19:52:33 +00:00
|
|
|
for (size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < sourceFace->m_vertices.size(); vertexIndex++) {
|
|
|
|
const unsigned int vertex = sourceFace->m_vertices.at(vertexIndex);
|
2022-07-31 18:54:58 +00:00
|
|
|
if (vertex >= pModel->mVertices.size()) {
|
2020-03-15 09:21:08 +00:00
|
|
|
throw DeadlyImportError("OBJ: vertex index out of range");
|
2016-09-06 08:42:02 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (pMesh->mNumVertices <= newIndex) {
|
2017-12-23 16:34:39 +00:00
|
|
|
throw DeadlyImportError("OBJ: bad vertex index");
|
|
|
|
}
|
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
pMesh->mVertices[newIndex] = pModel->mVertices[vertex];
|
2015-05-19 03:52:10 +00:00
|
|
|
|
|
|
|
// Copy all normals
|
2022-07-31 18:54:58 +00:00
|
|
|
if (normalsok && !pModel->mNormals.empty() && vertexIndex < sourceFace->m_normals.size()) {
|
2022-07-30 19:52:33 +00:00
|
|
|
const unsigned int normal = sourceFace->m_normals.at(vertexIndex);
|
2022-07-31 18:54:58 +00:00
|
|
|
if (normal >= pModel->mNormals.size()) {
|
2019-02-26 23:15:03 +00:00
|
|
|
normalsok = false;
|
2020-03-15 09:21:08 +00:00
|
|
|
} else {
|
2022-07-31 18:54:58 +00:00
|
|
|
pMesh->mNormals[newIndex] = pModel->mNormals[normal];
|
2016-09-06 08:42:02 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2016-06-28 00:08:22 +00:00
|
|
|
// Copy all vertex colors
|
2022-07-31 18:54:58 +00:00
|
|
|
if (vertex < pModel->mVertexColors.size()) {
|
|
|
|
const aiVector3D &color = pModel->mVertexColors[vertex];
|
2020-03-15 09:21:08 +00:00
|
|
|
pMesh->mColors[0][newIndex] = aiColor4D(color.x, color.y, color.z, 1.0);
|
2016-06-28 00:08:22 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Copy all texture coordinates
|
2022-07-31 18:54:58 +00:00
|
|
|
if (uvok && !pModel->mTextureCoord.empty() && vertexIndex < sourceFace->m_texturCoords.size()) {
|
2022-07-30 19:52:33 +00:00
|
|
|
const unsigned int tex = sourceFace->m_texturCoords.at(vertexIndex);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
if (tex >= pModel->mTextureCoord.size()) {
|
2019-02-26 23:15:03 +00:00
|
|
|
uvok = false;
|
2020-03-15 09:21:08 +00:00
|
|
|
} else {
|
2022-07-31 18:54:58 +00:00
|
|
|
const aiVector3D &coord3d = pModel->mTextureCoord[tex];
|
2020-03-15 09:21:08 +00:00
|
|
|
pMesh->mTextureCoords[0][newIndex] = aiVector3D(coord3d.x, coord3d.y, coord3d.z);
|
2019-02-26 23:15:03 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get destination face
|
2020-03-15 09:21:08 +00:00
|
|
|
aiFace *pDestFace = &pMesh->mFaces[outIndex];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-07-30 19:52:33 +00:00
|
|
|
const bool last = (vertexIndex == sourceFace->m_vertices.size() - 1);
|
|
|
|
if (sourceFace->mPrimitiveType != aiPrimitiveType_LINE || !last) {
|
2020-03-15 09:21:08 +00:00
|
|
|
pDestFace->mIndices[outVertexIndex] = newIndex;
|
2015-05-19 03:48:29 +00:00
|
|
|
outVertexIndex++;
|
|
|
|
}
|
|
|
|
|
2022-07-30 19:52:33 +00:00
|
|
|
if (sourceFace->mPrimitiveType == aiPrimitiveType_POINT) {
|
2015-05-19 03:48:29 +00:00
|
|
|
outIndex++;
|
|
|
|
outVertexIndex = 0;
|
2022-07-30 19:52:33 +00:00
|
|
|
} else if (sourceFace->mPrimitiveType == aiPrimitiveType_LINE) {
|
2015-05-19 03:48:29 +00:00
|
|
|
outVertexIndex = 0;
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!last)
|
2015-05-19 03:48:29 +00:00
|
|
|
outIndex++;
|
|
|
|
|
|
|
|
if (vertexIndex) {
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!last) {
|
|
|
|
pMesh->mVertices[newIndex + 1] = pMesh->mVertices[newIndex];
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!sourceFace->m_normals.empty() && !pModel->mNormals.empty()) {
|
2020-03-15 09:21:08 +00:00
|
|
|
pMesh->mNormals[newIndex + 1] = pMesh->mNormals[newIndex];
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2022-07-31 18:54:58 +00:00
|
|
|
if (!pModel->mTextureCoord.empty()) {
|
2020-03-15 09:21:08 +00:00
|
|
|
for (size_t i = 0; i < pMesh->GetNumUVChannels(); i++) {
|
|
|
|
pMesh->mTextureCoords[i][newIndex + 1] = pMesh->mTextureCoords[i][newIndex];
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++newIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
pDestFace[-1].mIndices[1] = newIndex;
|
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (last) {
|
2015-05-19 03:48:29 +00:00
|
|
|
outIndex++;
|
|
|
|
}
|
|
|
|
++newIndex;
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
}
|
2019-02-26 23:15:03 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!normalsok) {
|
|
|
|
delete[] pMesh->mNormals;
|
2019-02-26 23:15:03 +00:00
|
|
|
pMesh->mNormals = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!uvok) {
|
|
|
|
delete[] pMesh->mTextureCoords[0];
|
2019-02-26 23:15:03 +00:00
|
|
|
pMesh->mTextureCoords[0] = nullptr;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Counts all stored meshes
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileImporter::countObjects(const std::vector<ObjFile::Object *> &rObjects, int &iNumMeshes) {
|
2015-05-19 03:48:29 +00:00
|
|
|
iNumMeshes = 0;
|
2020-03-15 09:21:08 +00:00
|
|
|
if (rObjects.empty())
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
iNumMeshes += static_cast<unsigned int>(rObjects.size());
|
|
|
|
for (auto object : rObjects) {
|
|
|
|
if (!object->m_SubObjects.empty()) {
|
2020-02-12 22:10:07 +00:00
|
|
|
countObjects(object->m_SubObjects, iNumMeshes);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Add clamp mode property to material if necessary
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileImporter::addTextureMappingModeProperty(aiMaterial *mat, aiTextureType type, int clampMode, int index) {
|
|
|
|
if (nullptr == mat) {
|
2016-09-06 08:42:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_U(type, index));
|
|
|
|
mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_V(type, index));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Creates the material
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileImporter::createMaterials(const ObjFile::Model *pModel, aiScene *pScene) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (nullptr == pScene) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
2016-09-06 08:42:02 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
const unsigned int numMaterials = (unsigned int)pModel->mMaterialLib.size();
|
2015-05-19 03:48:29 +00:00
|
|
|
pScene->mNumMaterials = 0;
|
2022-07-31 18:54:58 +00:00
|
|
|
if (pModel->mMaterialLib.empty()) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_DEBUG("OBJ: no materials specified");
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
pScene->mMaterials = new aiMaterial *[numMaterials];
|
|
|
|
for (unsigned int matIndex = 0; matIndex < numMaterials; matIndex++) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Store material name
|
2020-03-15 09:21:08 +00:00
|
|
|
std::map<std::string, ObjFile::Material *>::const_iterator it;
|
2022-07-31 18:54:58 +00:00
|
|
|
it = pModel->mMaterialMap.find(pModel->mMaterialLib[matIndex]);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// No material found, use the default material
|
2022-07-31 18:54:58 +00:00
|
|
|
if (pModel->mMaterialMap.end() == it)
|
2015-05-19 03:48:29 +00:00
|
|
|
continue;
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
aiMaterial *mat = new aiMaterial;
|
2015-05-19 03:48:29 +00:00
|
|
|
ObjFile::Material *pCurrentMaterial = (*it).second;
|
2020-03-15 09:21:08 +00:00
|
|
|
mat->AddProperty(&pCurrentMaterial->MaterialName, AI_MATKEY_NAME);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// convert illumination model
|
|
|
|
int sm = 0;
|
2020-03-15 09:21:08 +00:00
|
|
|
switch (pCurrentMaterial->illumination_model) {
|
2020-08-27 15:05:09 +00:00
|
|
|
case 0:
|
|
|
|
sm = aiShadingMode_NoShading;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
sm = aiShadingMode_Gouraud;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
sm = aiShadingMode_Phong;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sm = aiShadingMode_Gouraud;
|
|
|
|
ASSIMP_LOG_ERROR("OBJ: unexpected illumination model (0-2 recognized)");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
mat->AddProperty<int>(&sm, 1, AI_MATKEY_SHADING_MODEL);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-02-23 00:55:41 +00:00
|
|
|
// Preserve the original illum value
|
|
|
|
mat->AddProperty<int>(&pCurrentMaterial->illumination_model, 1, AI_MATKEY_OBJ_ILLUM);
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Adding material colors
|
2020-03-15 09:21:08 +00:00
|
|
|
mat->AddProperty(&pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT);
|
|
|
|
mat->AddProperty(&pCurrentMaterial->diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
mat->AddProperty(&pCurrentMaterial->specular, 1, AI_MATKEY_COLOR_SPECULAR);
|
|
|
|
mat->AddProperty(&pCurrentMaterial->emissive, 1, AI_MATKEY_COLOR_EMISSIVE);
|
|
|
|
mat->AddProperty(&pCurrentMaterial->shineness, 1, AI_MATKEY_SHININESS);
|
|
|
|
mat->AddProperty(&pCurrentMaterial->alpha, 1, AI_MATKEY_OPACITY);
|
|
|
|
mat->AddProperty(&pCurrentMaterial->transparent, 1, AI_MATKEY_COLOR_TRANSPARENT);
|
2022-03-11 21:29:59 +00:00
|
|
|
if (pCurrentMaterial->roughness)
|
|
|
|
mat->AddProperty(&pCurrentMaterial->roughness.Get(), 1, AI_MATKEY_ROUGHNESS_FACTOR);
|
|
|
|
if (pCurrentMaterial->metallic)
|
|
|
|
mat->AddProperty(&pCurrentMaterial->metallic.Get(), 1, AI_MATKEY_METALLIC_FACTOR);
|
|
|
|
if (pCurrentMaterial->sheen)
|
|
|
|
mat->AddProperty(&pCurrentMaterial->sheen.Get(), 1, AI_MATKEY_SHEEN_COLOR_FACTOR);
|
|
|
|
if (pCurrentMaterial->clearcoat_thickness)
|
|
|
|
mat->AddProperty(&pCurrentMaterial->clearcoat_thickness.Get(), 1, AI_MATKEY_CLEARCOAT_FACTOR);
|
|
|
|
if (pCurrentMaterial->clearcoat_roughness)
|
|
|
|
mat->AddProperty(&pCurrentMaterial->clearcoat_roughness.Get(), 1, AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR);
|
2021-12-20 18:43:46 +00:00
|
|
|
mat->AddProperty(&pCurrentMaterial->anisotropy, 1, AI_MATKEY_ANISOTROPY_FACTOR);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// Adding refraction index
|
2020-03-15 09:21:08 +00:00
|
|
|
mat->AddProperty(&pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// Adding textures
|
2017-04-21 08:09:26 +00:00
|
|
|
const int uvwIndex = 0;
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->texture.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_DIFFUSE(0));
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_DIFFUSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureAmbient.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_AMBIENT(0));
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_AMBIENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureEmissive.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureEmissive, AI_MATKEY_TEXTURE_EMISSIVE(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_EMISSIVE(0));
|
2017-04-21 08:09:26 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureSpecular.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_SPECULAR(0));
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_SPECULAR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureBump.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_HEIGHT(0));
|
2022-02-18 05:10:46 +00:00
|
|
|
if (pCurrentMaterial->bump_multiplier != 1.0) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->bump_multiplier, 1, AI_MATKEY_OBJ_BUMPMULT_HEIGHT(0));
|
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_HEIGHT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureNormal.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_NORMALS(0));
|
2022-02-18 05:10:46 +00:00
|
|
|
if (pCurrentMaterial->bump_multiplier != 1.0) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->bump_multiplier, 1, AI_MATKEY_OBJ_BUMPMULT_NORMALS(0));
|
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_NORMALS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureReflection[0].length) {
|
2015-10-15 13:46:35 +00:00
|
|
|
ObjFile::Material::TextureType type = 0 != pCurrentMaterial->textureReflection[1].length ?
|
2020-03-15 09:21:08 +00:00
|
|
|
ObjFile::Material::TextureReflectionCubeTopType :
|
|
|
|
ObjFile::Material::TextureReflectionSphereType;
|
2015-10-15 13:46:35 +00:00
|
|
|
|
|
|
|
unsigned count = type == ObjFile::Material::TextureReflectionSphereType ? 1 : 6;
|
2020-03-15 09:21:08 +00:00
|
|
|
for (unsigned i = 0; i < count; i++) {
|
2015-10-15 13:46:35 +00:00
|
|
|
mat->AddProperty(&pCurrentMaterial->textureReflection[i], AI_MATKEY_TEXTURE_REFLECTION(i));
|
2020-03-15 09:21:08 +00:00
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_REFLECTION(i));
|
2015-10-15 13:46:35 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (pCurrentMaterial->clamp[type])
|
2016-12-09 18:31:07 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_REFLECTION, 1, i);
|
|
|
|
}
|
2015-10-15 13:46:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureDisp.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_DISPLACEMENT(0));
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_DISPLACEMENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureOpacity.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_OPACITY(0));
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_OPACITY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 != pCurrentMaterial->textureSpecularity.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
|
|
|
|
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_SHININESS(0));
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_SHININESS);
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2021-12-20 18:43:46 +00:00
|
|
|
if (0 != pCurrentMaterial->textureRoughness.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureRoughness, _AI_MATKEY_TEXTURE_BASE, aiTextureType_DIFFUSE_ROUGHNESS, 0);
|
|
|
|
mat->AddProperty(&uvwIndex, 1, _AI_MATKEY_UVWSRC_BASE, aiTextureType_DIFFUSE_ROUGHNESS, 0 );
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureRoughnessType]) {
|
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_DIFFUSE_ROUGHNESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != pCurrentMaterial->textureMetallic.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureMetallic, _AI_MATKEY_TEXTURE_BASE, aiTextureType_METALNESS, 0);
|
|
|
|
mat->AddProperty(&uvwIndex, 1, _AI_MATKEY_UVWSRC_BASE, aiTextureType_METALNESS, 0 );
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureMetallicType]) {
|
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_METALNESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != pCurrentMaterial->textureSheen.length) {
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureSheen, _AI_MATKEY_TEXTURE_BASE, aiTextureType_SHEEN, 0);
|
|
|
|
mat->AddProperty(&uvwIndex, 1, _AI_MATKEY_UVWSRC_BASE, aiTextureType_SHEEN, 0 );
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSheenType]) {
|
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_SHEEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != pCurrentMaterial->textureRMA.length) {
|
|
|
|
// NOTE: glTF importer places Rough/Metal/AO texture in Unknown so doing the same here for consistency.
|
|
|
|
mat->AddProperty(&pCurrentMaterial->textureRMA, _AI_MATKEY_TEXTURE_BASE, aiTextureType_UNKNOWN, 0);
|
|
|
|
mat->AddProperty(&uvwIndex, 1, _AI_MATKEY_UVWSRC_BASE, aiTextureType_UNKNOWN, 0 );
|
|
|
|
if (pCurrentMaterial->clamp[ObjFile::Material::TextureRMAType]) {
|
|
|
|
addTextureMappingModeProperty(mat, aiTextureType_UNKNOWN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Store material property info in material array in scene
|
2020-03-15 09:21:08 +00:00
|
|
|
pScene->mMaterials[pScene->mNumMaterials] = mat;
|
2015-05-19 03:48:29 +00:00
|
|
|
pScene->mNumMaterials++;
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Test number of created materials.
|
2020-03-15 09:21:08 +00:00
|
|
|
ai_assert(pScene->mNumMaterials == numMaterials);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Appends this node to the parent node
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Checking preconditions
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(nullptr != pParent);
|
|
|
|
ai_assert(nullptr != pChild);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// Assign parent to child
|
|
|
|
pChild->mParent = pParent;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Copy node instances into parent node
|
|
|
|
pParent->mNumChildren++;
|
2020-03-15 09:21:08 +00:00
|
|
|
pParent->mChildren[pParent->mNumChildren - 1] = pChild;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
} // Namespace Assimp
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER
|