2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2022-07-30 10:58:09 +00:00
|
|
|
Copyright (c) 2006-2022, 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 "ObjFileParser.h"
|
2020-03-15 09:21:08 +00:00
|
|
|
#include "ObjFileData.h"
|
2015-05-19 03:48:29 +00:00
|
|
|
#include "ObjFileMtlImporter.h"
|
|
|
|
#include "ObjTools.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/BaseImporter.h>
|
2017-02-28 02:31:56 +00:00
|
|
|
#include <assimp/DefaultIOSystem.h>
|
2020-03-15 09:21:08 +00:00
|
|
|
#include <assimp/ParsingUtils.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2015-07-09 23:21:10 +00:00
|
|
|
#include <assimp/Importer.hpp>
|
2015-05-19 03:48:29 +00:00
|
|
|
#include <cstdlib>
|
2020-02-15 21:29:04 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2022-07-30 10:58:09 +00:00
|
|
|
#include <string_view>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
|
2022-03-31 06:58:31 +00:00
|
|
|
constexpr const char ObjFileParser::DEFAULT_MATERIAL[];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
ObjFileParser::ObjFileParser() :
|
2020-03-21 07:56:49 +00:00
|
|
|
m_DataIt(),
|
|
|
|
m_DataItEnd(),
|
|
|
|
m_pModel(nullptr),
|
|
|
|
m_uiLine(0),
|
|
|
|
m_buffer(),
|
|
|
|
m_pIO(nullptr),
|
|
|
|
m_progress(nullptr),
|
|
|
|
m_originalObjFileName() {
|
2020-03-21 08:00:29 +00:00
|
|
|
std::fill_n(m_buffer, Buffersize, '\0');
|
2017-05-25 20:40:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
ObjFileParser::ObjFileParser(IOStreamBuffer<char> &streamBuffer, const std::string &modelName,
|
|
|
|
IOSystem *io, ProgressHandler *progress,
|
|
|
|
const std::string &originalObjFileName) :
|
|
|
|
m_DataIt(),
|
|
|
|
m_DataItEnd(),
|
|
|
|
m_pModel(nullptr),
|
|
|
|
m_uiLine(0),
|
2020-03-21 07:56:49 +00:00
|
|
|
m_buffer(),
|
2020-03-15 09:21:08 +00:00
|
|
|
m_pIO(io),
|
|
|
|
m_progress(progress),
|
|
|
|
m_originalObjFileName(originalObjFileName) {
|
|
|
|
std::fill_n(m_buffer, Buffersize, '\0');
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Create the model instance to store all the data
|
2017-12-30 12:22:30 +00:00
|
|
|
m_pModel.reset(new ObjFile::Model());
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mModelName = modelName;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// create default material and store it
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mDefaultMaterial = new ObjFile::Material;
|
|
|
|
m_pModel->mDefaultMaterial->MaterialName.Set(DEFAULT_MATERIAL);
|
2022-08-23 15:41:49 +00:00
|
|
|
m_pModel->mMaterialLib.emplace_back(DEFAULT_MATERIAL);
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mMaterialMap[DEFAULT_MATERIAL] = m_pModel->mDefaultMaterial;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Start parsing the file
|
2020-03-15 09:21:08 +00:00
|
|
|
parseFile(streamBuffer);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 15:19:26 +00:00
|
|
|
ObjFileParser::~ObjFileParser() = default;
|
2020-02-12 22:38:56 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::setBuffer(std::vector<char> &buffer) {
|
2017-05-25 20:40:36 +00:00
|
|
|
m_DataIt = buffer.begin();
|
|
|
|
m_DataItEnd = buffer.end();
|
|
|
|
}
|
|
|
|
|
2016-11-09 19:16:45 +00:00
|
|
|
ObjFile::Model *ObjFileParser::GetModel() const {
|
2017-12-30 12:22:30 +00:00
|
|
|
return m_pModel.get();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2017-05-25 20:40:36 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::parseFile(IOStreamBuffer<char> &streamBuffer) {
|
2016-01-11 15:25:36 +00:00
|
|
|
// only update every 100KB or it'll be too slow
|
2016-11-24 14:15:17 +00:00
|
|
|
//const unsigned int updateProgressEveryBytes = 100 * 1024;
|
2016-11-20 01:29:26 +00:00
|
|
|
const unsigned int bytesToProcess = static_cast<unsigned int>(streamBuffer.size());
|
2019-01-08 20:56:20 +00:00
|
|
|
const unsigned int progressTotal = bytesToProcess;
|
2016-01-11 15:25:36 +00:00
|
|
|
unsigned int processed = 0;
|
2020-03-15 09:21:08 +00:00
|
|
|
size_t lastFilePos(0);
|
2016-10-11 10:29:40 +00:00
|
|
|
|
2022-02-22 00:32:41 +00:00
|
|
|
bool insideCstype = false;
|
2016-10-11 10:29:40 +00:00
|
|
|
std::vector<char> buffer;
|
2020-03-15 09:21:08 +00:00
|
|
|
while (streamBuffer.getNextDataLine(buffer, '\\')) {
|
2016-10-11 10:29:40 +00:00
|
|
|
m_DataIt = buffer.begin();
|
|
|
|
m_DataItEnd = buffer.end();
|
2016-01-11 15:25:36 +00:00
|
|
|
|
2017-02-28 02:31:56 +00:00
|
|
|
// Handle progress reporting
|
2020-03-15 09:21:08 +00:00
|
|
|
const size_t filePos(streamBuffer.getFilePos());
|
|
|
|
if (lastFilePos < filePos) {
|
2019-01-08 20:56:20 +00:00
|
|
|
processed = static_cast<unsigned int>(filePos);
|
2016-10-29 19:41:24 +00:00
|
|
|
lastFilePos = filePos;
|
2020-03-15 09:21:08 +00:00
|
|
|
m_progress->UpdateFileRead(processed, progressTotal);
|
2016-10-29 19:41:24 +00:00
|
|
|
}
|
2017-05-31 19:37:16 +00:00
|
|
|
|
2022-02-22 00:46:47 +00:00
|
|
|
// handle cstype section end (http://paulbourke.net/dataformats/obj/)
|
2022-02-22 00:32:41 +00:00
|
|
|
if (insideCstype) {
|
|
|
|
switch (*m_DataIt) {
|
|
|
|
case 'e': {
|
|
|
|
std::string name;
|
|
|
|
getNameNoSpace(m_DataIt, m_DataItEnd, name);
|
|
|
|
insideCstype = name != "end";
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
goto pf_skip_line;
|
|
|
|
}
|
|
|
|
|
2016-01-11 15:25:36 +00:00
|
|
|
// parse line
|
2016-11-10 20:39:32 +00:00
|
|
|
switch (*m_DataIt) {
|
2020-03-20 12:18:49 +00:00
|
|
|
case 'v': // Parse a vertex texture coordinate
|
|
|
|
{
|
|
|
|
++m_DataIt;
|
|
|
|
if (*m_DataIt == ' ' || *m_DataIt == '\t') {
|
|
|
|
size_t numComponents = getNumComponentsInDataDefinition();
|
|
|
|
if (numComponents == 3) {
|
|
|
|
// read in vertex definition
|
2022-07-31 18:54:58 +00:00
|
|
|
getVector3(m_pModel->mVertices);
|
2020-03-20 12:18:49 +00:00
|
|
|
} else if (numComponents == 4) {
|
|
|
|
// read in vertex definition (homogeneous coords)
|
2022-07-31 18:54:58 +00:00
|
|
|
getHomogeneousVector3(m_pModel->mVertices);
|
2020-03-20 12:18:49 +00:00
|
|
|
} else if (numComponents == 6) {
|
|
|
|
// read vertex and vertex-color
|
2022-07-31 18:54:58 +00:00
|
|
|
getTwoVectors3(m_pModel->mVertices, m_pModel->mVertexColors);
|
2017-06-27 10:52:43 +00:00
|
|
|
}
|
2020-03-20 12:18:49 +00:00
|
|
|
} else if (*m_DataIt == 't') {
|
|
|
|
// read in texture coordinate ( 2D or 3D )
|
|
|
|
++m_DataIt;
|
2022-07-31 18:54:58 +00:00
|
|
|
size_t dim = getTexCoordVector(m_pModel->mTextureCoord);
|
|
|
|
m_pModel->mTextureCoordDim = std::max(m_pModel->mTextureCoordDim, (unsigned int)dim);
|
2020-03-20 12:18:49 +00:00
|
|
|
} else if (*m_DataIt == 'n') {
|
|
|
|
// Read in normal vector definition
|
|
|
|
++m_DataIt;
|
2022-07-31 18:54:58 +00:00
|
|
|
getVector3(m_pModel->mNormals);
|
2020-03-20 12:18:49 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 'p': // Parse a face, line or point statement
|
|
|
|
case 'l':
|
|
|
|
case 'f': {
|
|
|
|
getFace(*m_DataIt == 'f' ? aiPrimitiveType_POLYGON : (*m_DataIt == 'l' ? aiPrimitiveType_LINE : aiPrimitiveType_POINT));
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case '#': // Parse a comment
|
|
|
|
{
|
|
|
|
getComment();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 'u': // Parse a material desc. setter
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
getNameNoSpace(m_DataIt, m_DataItEnd, name);
|
|
|
|
|
|
|
|
size_t nextSpace = name.find(' ');
|
|
|
|
if (nextSpace != std::string::npos)
|
|
|
|
name = name.substr(0, nextSpace);
|
|
|
|
|
|
|
|
if (name == "usemtl") {
|
|
|
|
getMaterialDesc();
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 'm': // Parse a material library or merging group ('mg')
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
|
|
|
|
getNameNoSpace(m_DataIt, m_DataItEnd, name);
|
|
|
|
|
|
|
|
size_t nextSpace = name.find(' ');
|
|
|
|
if (nextSpace != std::string::npos)
|
|
|
|
name = name.substr(0, nextSpace);
|
|
|
|
|
|
|
|
if (name == "mg")
|
|
|
|
getGroupNumberAndResolution();
|
|
|
|
else if (name == "mtllib")
|
|
|
|
getMaterialLib();
|
|
|
|
else
|
|
|
|
goto pf_skip_line;
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 'g': // Parse group name
|
|
|
|
{
|
|
|
|
getGroupName();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 's': // Parse group number
|
|
|
|
{
|
|
|
|
getGroupNumber();
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 'o': // Parse object name
|
|
|
|
{
|
|
|
|
getObjectName();
|
|
|
|
} break;
|
|
|
|
|
2022-02-22 00:46:47 +00:00
|
|
|
case 'c': // handle cstype section start
|
2022-02-22 00:32:41 +00:00
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
getNameNoSpace(m_DataIt, m_DataItEnd, name);
|
|
|
|
insideCstype = name == "cstype";
|
|
|
|
goto pf_skip_line;
|
2023-04-01 08:23:22 +00:00
|
|
|
}
|
2022-02-22 00:32:41 +00:00
|
|
|
|
2020-03-20 12:18:49 +00:00
|
|
|
default: {
|
|
|
|
pf_skip_line:
|
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
|
|
|
} break;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 20:39:32 +00:00
|
|
|
void ObjFileParser::copyNextWord(char *pBuffer, size_t length) {
|
2016-02-07 19:03:24 +00:00
|
|
|
size_t index = 0;
|
|
|
|
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
|
2020-03-15 09:21:08 +00:00
|
|
|
if (*m_DataIt == '\\') {
|
2019-08-29 08:55:09 +00:00
|
|
|
++m_DataIt;
|
|
|
|
++m_DataIt;
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
|
2017-05-25 20:40:36 +00:00
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
while (m_DataIt != m_DataItEnd && !IsSpaceOrNewLine(*m_DataIt)) {
|
2016-02-07 19:03:24 +00:00
|
|
|
pBuffer[index] = *m_DataIt;
|
|
|
|
index++;
|
2020-03-15 09:21:08 +00:00
|
|
|
if (index == length - 1) {
|
2016-02-07 19:03:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
++m_DataIt;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
ai_assert(index < length);
|
|
|
|
pBuffer[index] = '\0';
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
static bool isDataDefinitionEnd(const char *tmp) {
|
|
|
|
if (*tmp == '\\') {
|
2017-05-28 20:25:06 +00:00
|
|
|
tmp++;
|
2020-03-15 09:21:08 +00:00
|
|
|
if (IsLineEnd(*tmp)) {
|
2017-05-29 20:00:13 +00:00
|
|
|
return true;
|
2017-05-28 20:25:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
static bool isNanOrInf(const char *in) {
|
2019-04-17 12:55:08 +00:00
|
|
|
// Look for "nan" or "inf", case insensitive
|
2020-03-15 09:21:08 +00:00
|
|
|
return ((in[0] == 'N' || in[0] == 'n') && ASSIMP_strincmp(in, "nan", 3) == 0) ||
|
|
|
|
((in[0] == 'I' || in[0] == 'i') && ASSIMP_strincmp(in, "inf", 3) == 0);
|
2019-04-17 12:55:08 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 20:25:06 +00:00
|
|
|
size_t ObjFileParser::getNumComponentsInDataDefinition() {
|
2020-03-15 09:21:08 +00:00
|
|
|
size_t numComponents(0);
|
|
|
|
const char *tmp(&m_DataIt[0]);
|
2017-05-29 20:00:13 +00:00
|
|
|
bool end_of_definition = false;
|
2020-03-15 09:21:08 +00:00
|
|
|
while (!end_of_definition) {
|
|
|
|
if (isDataDefinitionEnd(tmp)) {
|
2017-05-29 20:00:13 +00:00
|
|
|
tmp += 2;
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (IsLineEnd(*tmp)) {
|
2017-05-29 20:00:13 +00:00
|
|
|
end_of_definition = true;
|
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!SkipSpaces(&tmp)) {
|
2015-05-19 03:48:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
const bool isNum(IsNumeric(*tmp) || isNanOrInf(tmp));
|
|
|
|
SkipToken(tmp);
|
|
|
|
if (isNum) {
|
2017-05-28 20:25:06 +00:00
|
|
|
++numComponents;
|
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!SkipSpaces(&tmp)) {
|
2017-05-28 20:25:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2016-06-28 00:08:22 +00:00
|
|
|
return numComponents;
|
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
size_t ObjFileParser::getTexCoordVector(std::vector<aiVector3D> &point3d_array) {
|
2017-05-28 20:25:06 +00:00
|
|
|
size_t numComponents = getNumComponentsInDataDefinition();
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real x, y, z;
|
2020-03-15 09:21:08 +00:00
|
|
|
if (2 == numComponents) {
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
x = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
y = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
z = 0.0;
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (3 == numComponents) {
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
x = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
y = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
z = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
} else {
|
2020-03-15 09:21:08 +00:00
|
|
|
throw DeadlyImportError("OBJ: Invalid number of components");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2019-04-17 12:55:08 +00:00
|
|
|
|
|
|
|
// Coerce nan and inf to 0 as is the OBJ default value
|
2019-04-17 13:30:40 +00:00
|
|
|
if (!std::isfinite(x))
|
2019-04-17 12:55:08 +00:00
|
|
|
x = 0;
|
|
|
|
|
2019-04-17 13:30:40 +00:00
|
|
|
if (!std::isfinite(y))
|
2019-04-17 12:55:08 +00:00
|
|
|
y = 0;
|
|
|
|
|
2019-04-17 13:30:40 +00:00
|
|
|
if (!std::isfinite(z))
|
2019-04-17 12:55:08 +00:00
|
|
|
z = 0;
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
point3d_array.emplace_back(x, y, z);
|
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2019-03-15 21:43:12 +00:00
|
|
|
return numComponents;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::getVector3(std::vector<aiVector3D> &point3d_array) {
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real x, y, z;
|
2016-02-07 19:03:24 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
x = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
y = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
z = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
point3d_array.emplace_back(x, y, z);
|
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2016-10-04 21:06:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::getHomogeneousVector3(std::vector<aiVector3D> &point3d_array) {
|
2016-10-04 21:06:31 +00:00
|
|
|
ai_real x, y, z, w;
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
x = (ai_real)fast_atof(m_buffer);
|
2016-10-04 21:06:31 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
y = (ai_real)fast_atof(m_buffer);
|
2016-10-04 21:06:31 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
z = (ai_real)fast_atof(m_buffer);
|
2016-10-04 21:06:31 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
w = (ai_real)fast_atof(m_buffer);
|
2016-10-04 21:06:31 +00:00
|
|
|
|
2017-12-29 17:07:41 +00:00
|
|
|
if (w == 0)
|
2020-03-15 09:21:08 +00:00
|
|
|
throw DeadlyImportError("OBJ: Invalid component in homogeneous vector (Division by zero)");
|
2016-10-04 21:06:31 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
point3d_array.emplace_back(x / w, y / w, z / w);
|
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::getTwoVectors3(std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b) {
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real x, y, z;
|
2016-06-28 00:08:22 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
x = (ai_real)fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
y = (ai_real)fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
z = (ai_real)fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
point3d_array_a.emplace_back(x, y, z);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
x = (ai_real)fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
y = (ai_real)fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
|
|
|
z = (ai_real)fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
point3d_array_b.emplace_back(x, y, z);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2016-06-28 00:08:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::getVector2(std::vector<aiVector2D> &point2d_array) {
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real x, y;
|
2016-02-07 19:03:24 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
x = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2020-03-15 09:21:08 +00:00
|
|
|
y = (ai_real)fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-02-12 22:10:07 +00:00
|
|
|
point2d_array.emplace_back(x, y);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 10:58:09 +00:00
|
|
|
static constexpr char DefaultObjName[] = "defaultobject";
|
2016-02-09 16:50:08 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::getFace(aiPrimitiveType type) {
|
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
if (m_DataIt == m_DataItEnd || *m_DataIt == '\0') {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
2016-02-09 16:50:08 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-07-30 19:52:33 +00:00
|
|
|
ObjFile::Face *face = new ObjFile::Face(type);
|
2015-05-19 03:48:29 +00:00
|
|
|
bool hasNormal = false;
|
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
const int vSize = static_cast<unsigned int>(m_pModel->mVertices.size());
|
|
|
|
const int vtSize = static_cast<unsigned int>(m_pModel->mTextureCoord.size());
|
|
|
|
const int vnSize = static_cast<unsigned int>(m_pModel->mNormals.size());
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
const bool vt = (!m_pModel->mTextureCoord.empty());
|
|
|
|
const bool vn = (!m_pModel->mNormals.empty());
|
2020-02-12 22:27:30 +00:00
|
|
|
int iPos = 0;
|
2022-12-11 00:54:57 +00:00
|
|
|
while (m_DataIt < m_DataItEnd) {
|
2020-02-12 22:27:30 +00:00
|
|
|
int iStep = 1;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (IsLineEnd(*m_DataIt)) {
|
2015-05-19 03:48:29 +00:00
|
|
|
break;
|
2016-02-09 16:50:08 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (*m_DataIt == '/') {
|
2015-05-19 03:48:29 +00:00
|
|
|
if (type == aiPrimitiveType_POINT) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("Obj: Separator unexpected in point statement");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
iPos++;
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (IsSpaceOrNewLine(*m_DataIt)) {
|
2015-05-19 03:48:29 +00:00
|
|
|
iPos = 0;
|
2016-02-09 16:50:08 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
//OBJ USES 1 Base ARRAYS!!!!
|
2023-05-31 14:10:57 +00:00
|
|
|
int iVal;
|
|
|
|
auto end = m_DataIt;
|
2023-06-02 16:57:53 +00:00
|
|
|
// find either the buffer end or the '\0'
|
2023-05-31 14:10:57 +00:00
|
|
|
while (end < m_DataItEnd && *end != '\0')
|
|
|
|
++end;
|
2023-06-02 16:57:53 +00:00
|
|
|
// avoid temporary string allocation if there is a zero
|
2023-05-31 14:10:57 +00:00
|
|
|
if (end != m_DataItEnd) {
|
|
|
|
iVal = ::atoi(&(*m_DataIt));
|
|
|
|
} else {
|
2023-06-02 16:57:53 +00:00
|
|
|
// otherwise make a zero terminated copy, which is safe to pass to atoi
|
2023-05-31 14:10:57 +00:00
|
|
|
std::string number(&(*m_DataIt), m_DataItEnd - m_DataIt);
|
|
|
|
iVal = ::atoi(number.c_str());
|
|
|
|
}
|
2023-01-16 08:12:35 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// increment iStep position based off of the sign and # of digits
|
|
|
|
int tmp = iVal;
|
2020-03-15 09:21:08 +00:00
|
|
|
if (iVal < 0) {
|
2015-05-19 03:48:29 +00:00
|
|
|
++iStep;
|
2016-02-09 16:50:08 +00:00
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
while ((tmp = tmp / 10) != 0) {
|
2015-05-19 03:48:29 +00:00
|
|
|
++iStep;
|
2016-02-09 16:50:08 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2019-04-26 10:13:29 +00:00
|
|
|
if (iPos == 1 && !vt && vn)
|
|
|
|
iPos = 2; // skip texture coords for normals if there are no tex coords
|
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (iVal > 0) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Store parsed index
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 == iPos) {
|
2022-07-30 19:52:33 +00:00
|
|
|
face->m_vertices.push_back(iVal - 1);
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (1 == iPos) {
|
2022-07-30 19:52:33 +00:00
|
|
|
face->m_texturCoords.push_back(iVal - 1);
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (2 == iPos) {
|
2022-07-30 19:52:33 +00:00
|
|
|
face->m_normals.push_back(iVal - 1);
|
2015-05-19 03:48:29 +00:00
|
|
|
hasNormal = true;
|
2016-11-09 19:16:45 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
reportErrorTokenInFace();
|
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (iVal < 0) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Store relatively index
|
2020-03-15 09:21:08 +00:00
|
|
|
if (0 == iPos) {
|
2022-07-30 19:52:33 +00:00
|
|
|
face->m_vertices.push_back(vSize + iVal);
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (1 == iPos) {
|
2022-07-30 19:52:33 +00:00
|
|
|
face->m_texturCoords.push_back(vtSize + iVal);
|
2020-03-15 09:21:08 +00:00
|
|
|
} else if (2 == iPos) {
|
2022-07-30 19:52:33 +00:00
|
|
|
face->m_normals.push_back(vnSize + iVal);
|
2015-05-19 03:48:29 +00:00
|
|
|
hasNormal = true;
|
2016-11-09 19:16:45 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
reportErrorTokenInFace();
|
|
|
|
}
|
2017-10-21 18:36:43 +00:00
|
|
|
} else {
|
|
|
|
//On error, std::atoi will return 0 which is not a valid value
|
2022-07-31 10:10:43 +00:00
|
|
|
delete face;
|
2022-07-30 10:58:09 +00:00
|
|
|
throw DeadlyImportError("OBJ: Invalid face index.");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 20:39:32 +00:00
|
|
|
m_DataIt += iStep;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 19:52:33 +00:00
|
|
|
if (face->m_vertices.empty()) {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("Obj: Ignoring empty face");
|
2016-02-07 19:03:24 +00:00
|
|
|
// skip line and clean up
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2022-07-31 10:10:43 +00:00
|
|
|
delete face;
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set active material, if one set
|
2022-07-31 18:54:58 +00:00
|
|
|
if (nullptr != m_pModel->mCurrentMaterial) {
|
|
|
|
face->m_pMaterial = m_pModel->mCurrentMaterial;
|
2015-07-06 18:47:45 +00:00
|
|
|
} else {
|
2022-07-31 18:54:58 +00:00
|
|
|
face->m_pMaterial = m_pModel->mDefaultMaterial;
|
2015-07-06 18:47:45 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// Create a default object, if nothing is there
|
2022-07-31 18:54:58 +00:00
|
|
|
if (nullptr == m_pModel->mCurrentObject) {
|
2020-03-15 09:21:08 +00:00
|
|
|
createObject(DefaultObjName);
|
2015-07-06 18:47:45 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Assign face to mesh
|
2022-07-31 18:54:58 +00:00
|
|
|
if (nullptr == m_pModel->mCurrentMesh) {
|
2020-03-15 09:21:08 +00:00
|
|
|
createMesh(DefaultObjName);
|
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 the face
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentMesh->m_Faces.emplace_back(face);
|
|
|
|
m_pModel->mCurrentMesh->m_uiNumIndices += static_cast<unsigned int>(face->m_vertices.size());
|
|
|
|
m_pModel->mCurrentMesh->m_uiUVCoordinates[0] += static_cast<unsigned int>(face->m_texturCoords.size());
|
|
|
|
if (!m_pModel->mCurrentMesh->m_hasNormals && hasNormal) {
|
|
|
|
m_pModel->mCurrentMesh->m_hasNormals = true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
// Skip the rest of the line
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 19:16:45 +00:00
|
|
|
void ObjFileParser::getMaterialDesc() {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Get next data for material data
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
if (m_DataIt == m_DataItEnd) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
2015-08-30 13:21:53 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
char *pStart = &(*m_DataIt);
|
2020-03-15 09:21:08 +00:00
|
|
|
while (m_DataIt != m_DataItEnd && !IsLineEnd(*m_DataIt)) {
|
2016-02-07 19:03:24 +00:00
|
|
|
++m_DataIt;
|
|
|
|
}
|
|
|
|
|
2015-12-13 09:54:50 +00:00
|
|
|
// In some cases we should ignore this 'usemtl' command, this variable helps us to do so
|
|
|
|
bool skip = false;
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Get name
|
2016-02-07 19:03:24 +00:00
|
|
|
std::string strName(pStart, &(*m_DataIt));
|
2015-12-13 19:32:09 +00:00
|
|
|
strName = trim_whitespaces(strName);
|
2015-12-13 09:54:50 +00:00
|
|
|
if (strName.empty())
|
|
|
|
skip = true;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-12-13 09:54:50 +00:00
|
|
|
// If the current mesh has the same material, we simply ignore that 'usemtl' command
|
|
|
|
// There is no need to create another object or even mesh here
|
2022-07-31 18:54:58 +00:00
|
|
|
if (m_pModel->mCurrentMaterial && m_pModel->mCurrentMaterial->MaterialName == aiString(strName)) {
|
2015-12-13 09:54:50 +00:00
|
|
|
skip = true;
|
2016-11-09 19:16:45 +00:00
|
|
|
}
|
2015-12-13 09:54:50 +00:00
|
|
|
|
2016-11-09 19:16:45 +00:00
|
|
|
if (!skip) {
|
2015-12-13 09:54:50 +00:00
|
|
|
// Search for material
|
2022-07-31 18:54:58 +00:00
|
|
|
std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->mMaterialMap.find(strName);
|
|
|
|
if (it == m_pModel->mMaterialMap.end()) {
|
2020-03-15 09:21:08 +00:00
|
|
|
// Not found, so we don't know anything about the material except for its name.
|
|
|
|
// This may be the case if the material library is missing. We don't want to lose all
|
|
|
|
// materials if that happens, so create a new named material instead of discarding it
|
|
|
|
// completely.
|
2021-05-13 11:05:31 +00:00
|
|
|
ASSIMP_LOG_ERROR("OBJ: failed to locate material ", strName, ", creating new material");
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentMaterial = new ObjFile::Material();
|
|
|
|
m_pModel->mCurrentMaterial->MaterialName.Set(strName);
|
|
|
|
m_pModel->mMaterialLib.push_back(strName);
|
|
|
|
m_pModel->mMaterialMap[strName] = m_pModel->mCurrentMaterial;
|
2016-11-09 19:16:45 +00:00
|
|
|
} else {
|
2015-12-13 09:54:50 +00:00
|
|
|
// Found, using detected material
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentMaterial = (*it).second;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-12-13 09:54:50 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (needsNewMesh(strName)) {
|
|
|
|
createMesh(strName);
|
2016-11-09 19:16:45 +00:00
|
|
|
}
|
2015-12-13 09:54:50 +00:00
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strName);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
// Skip rest of line
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2016-02-07 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// Get a comment, values will be skipped
|
2016-11-09 19:16:45 +00:00
|
|
|
void ObjFileParser::getComment() {
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Get material library from file.
|
2016-11-09 19:16:45 +00:00
|
|
|
void ObjFileParser::getMaterialLib() {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Translate tuple
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
2020-03-15 09:21:08 +00:00
|
|
|
if (m_DataIt == m_DataItEnd) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
char *pStart = &(*m_DataIt);
|
2020-03-15 09:21:08 +00:00
|
|
|
while (m_DataIt != m_DataItEnd && !IsLineEnd(*m_DataIt)) {
|
2016-02-07 19:03:24 +00:00
|
|
|
++m_DataIt;
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Check for existence
|
2016-02-07 19:03:24 +00:00
|
|
|
const std::string strMatName(pStart, &(*m_DataIt));
|
2015-08-30 13:21:53 +00:00
|
|
|
std::string absName;
|
2016-10-07 13:40:51 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
// Check if directive is valid.
|
|
|
|
if (0 == strMatName.length()) {
|
|
|
|
ASSIMP_LOG_WARN("OBJ: no name for material library specified.");
|
2016-12-19 19:44:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-10-07 13:40:51 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
if (m_pIO->StackSize() > 0) {
|
2015-10-19 20:07:32 +00:00
|
|
|
std::string path = m_pIO->CurrentDirectory();
|
2020-03-15 09:21:08 +00:00
|
|
|
if ('/' != *path.rbegin()) {
|
|
|
|
path += '/';
|
2015-10-19 20:07:32 +00:00
|
|
|
}
|
2018-03-20 22:38:08 +00:00
|
|
|
absName += path;
|
|
|
|
absName += strMatName;
|
2015-08-30 13:21:53 +00:00
|
|
|
} else {
|
|
|
|
absName = strMatName;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
IOStream *pFile = m_pIO->Open(absName);
|
|
|
|
if (nullptr == pFile) {
|
2021-05-13 11:05:31 +00:00
|
|
|
ASSIMP_LOG_ERROR("OBJ: Unable to locate material file ", strMatName);
|
2016-03-22 17:38:28 +00:00
|
|
|
std::string strMatFallbackName = m_originalObjFileName.substr(0, m_originalObjFileName.length() - 3) + "mtl";
|
2021-05-13 11:05:31 +00:00
|
|
|
ASSIMP_LOG_INFO("OBJ: Opening fallback material file ", strMatFallbackName);
|
2016-03-22 17:38:28 +00:00
|
|
|
pFile = m_pIO->Open(strMatFallbackName);
|
|
|
|
if (!pFile) {
|
2021-05-13 11:05:31 +00:00
|
|
|
ASSIMP_LOG_ERROR("OBJ: Unable to locate fallback material file ", strMatFallbackName);
|
2016-03-22 17:38:28 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 06:13:25 +00:00
|
|
|
// Import material library data from file.
|
|
|
|
// Some exporters (e.g. Silo) will happily write out empty
|
|
|
|
// material files if the model doesn't use any materials, so we
|
|
|
|
// allow that.
|
2015-05-19 03:48:29 +00:00
|
|
|
std::vector<char> buffer;
|
2020-03-15 09:21:08 +00:00
|
|
|
BaseImporter::TextFileToBuffer(pFile, buffer, BaseImporter::ALLOW_EMPTY);
|
|
|
|
m_pIO->Close(pFile);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// Importing the material library
|
2020-03-15 09:21:08 +00:00
|
|
|
ObjFileMtlImporter mtlImporter(buffer, strMatName, m_pModel.get());
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Set a new material definition as the current material.
|
2016-11-10 20:39:32 +00:00
|
|
|
void ObjFileParser::getNewMaterial() {
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
|
2020-03-15 09:21:08 +00:00
|
|
|
if (m_DataIt == m_DataItEnd) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
char *pStart = &(*m_DataIt);
|
2020-03-15 09:21:08 +00:00
|
|
|
std::string strMat(pStart, *m_DataIt);
|
|
|
|
while (m_DataIt != m_DataItEnd && IsSpaceOrNewLine(*m_DataIt)) {
|
2016-02-07 19:03:24 +00:00
|
|
|
++m_DataIt;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2022-07-31 18:54:58 +00:00
|
|
|
std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->mMaterialMap.find(strMat);
|
|
|
|
if (it == m_pModel->mMaterialMap.end()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Show a warning, if material was not found
|
2021-05-13 11:05:31 +00:00
|
|
|
ASSIMP_LOG_WARN("OBJ: Unsupported material requested: ", strMat);
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentMaterial = m_pModel->mDefaultMaterial;
|
2016-11-10 20:39:32 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Set new material
|
2020-03-15 09:21:08 +00:00
|
|
|
if (needsNewMesh(strMat)) {
|
|
|
|
createMesh(strMat);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strMat);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2020-03-15 09:21:08 +00:00
|
|
|
int ObjFileParser::getMaterialIndex(const std::string &strMaterialName) {
|
2015-05-19 03:48:29 +00:00
|
|
|
int mat_index = -1;
|
2020-03-15 09:21:08 +00:00
|
|
|
if (strMaterialName.empty()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return mat_index;
|
|
|
|
}
|
2022-07-31 18:54:58 +00:00
|
|
|
for (size_t index = 0; index < m_pModel->mMaterialLib.size(); ++index) {
|
|
|
|
if (strMaterialName == m_pModel->mMaterialLib[index]) {
|
2015-05-19 03:48:29 +00:00
|
|
|
mat_index = (int)index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mat_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Getter for a group name.
|
2016-09-06 13:41:37 +00:00
|
|
|
void ObjFileParser::getGroupName() {
|
|
|
|
std::string groupName;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2016-07-22 13:10:06 +00:00
|
|
|
// here we skip 'g ' from line
|
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
2016-09-06 13:41:37 +00:00
|
|
|
m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, groupName);
|
2020-03-15 09:21:08 +00:00
|
|
|
if (isEndOfBuffer(m_DataIt, m_DataItEnd)) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change active group, if necessary
|
2022-07-31 18:54:58 +00:00
|
|
|
if (m_pModel->mActiveGroup != groupName) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Search for already existing entry
|
2022-07-31 18:54:58 +00:00
|
|
|
ObjFile::Model::ConstGroupMapIt it = m_pModel->mGroups.find(groupName);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// We are mapping groups into the object structure
|
2020-03-15 09:21:08 +00:00
|
|
|
createObject(groupName);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// New group name, creating a new entry
|
2022-07-31 18:54:58 +00:00
|
|
|
if (it == m_pModel->mGroups.end()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mGroups[groupName] = pFaceIDArray;
|
|
|
|
m_pModel->mGroupFaceIDs = (pFaceIDArray);
|
2020-03-15 09:21:08 +00:00
|
|
|
} else {
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mGroupFaceIDs = (*it).second;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mActiveGroup = groupName;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Not supported
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::getGroupNumber() {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Not used
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Not supported
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::getGroupNumberAndResolution() {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Not used
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Stores values for a new object instance, name will be used to
|
|
|
|
// identify it.
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::getObjectName() {
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
2020-03-15 09:21:08 +00:00
|
|
|
if (m_DataIt == m_DataItEnd) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
char *pStart = &(*m_DataIt);
|
2020-03-15 09:21:08 +00:00
|
|
|
while (m_DataIt != m_DataItEnd && !IsSpaceOrNewLine(*m_DataIt)) {
|
2016-02-07 19:03:24 +00:00
|
|
|
++m_DataIt;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
std::string strObjectName(pStart, &(*m_DataIt));
|
2020-03-15 09:21:08 +00:00
|
|
|
if (!strObjectName.empty()) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Reset current object
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentObject = nullptr;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Search for actual object
|
2022-07-31 18:54:58 +00:00
|
|
|
for (std::vector<ObjFile::Object *>::const_iterator it = m_pModel->mObjects.begin();
|
|
|
|
it != m_pModel->mObjects.end();
|
2020-03-15 09:21:08 +00:00
|
|
|
++it) {
|
|
|
|
if ((*it)->m_strObjName == strObjectName) {
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentObject = *it;
|
2015-05-19 03:48:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a new object, if current one was not found before
|
2022-07-31 18:54:58 +00:00
|
|
|
if (nullptr == m_pModel->mCurrentObject) {
|
2020-03-15 09:21:08 +00:00
|
|
|
createObject(strObjectName);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-15 09:21:08 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Creates a new object instance
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::createObject(const std::string &objName) {
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(nullptr != m_pModel);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentObject = new ObjFile::Object;
|
|
|
|
m_pModel->mCurrentObject->m_strObjName = objName;
|
|
|
|
m_pModel->mObjects.push_back(m_pModel->mCurrentObject);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2020-03-15 09:21:08 +00:00
|
|
|
createMesh(objName);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
if (m_pModel->mCurrentMaterial) {
|
|
|
|
m_pModel->mCurrentMesh->m_uiMaterialIndex =
|
|
|
|
getMaterialIndex(m_pModel->mCurrentMaterial->MaterialName.data);
|
|
|
|
m_pModel->mCurrentMesh->m_pMaterial = m_pModel->mCurrentMaterial;
|
2015-05-19 03:52:10 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Creates a new mesh
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::createMesh(const std::string &meshName) {
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(nullptr != m_pModel);
|
|
|
|
|
2022-07-31 18:54:58 +00:00
|
|
|
m_pModel->mCurrentMesh = new ObjFile::Mesh(meshName);
|
|
|
|
m_pModel->mMeshes.push_back(m_pModel->mCurrentMesh);
|
|
|
|
unsigned int meshId = static_cast<unsigned int>(m_pModel->mMeshes.size() - 1);
|
|
|
|
if (nullptr != m_pModel->mCurrentObject) {
|
|
|
|
m_pModel->mCurrentObject->m_Meshes.push_back(meshId);
|
2020-03-15 09:21:08 +00:00
|
|
|
} else {
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("OBJ: No object detected to attach a new mesh instance.");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Returns true, if a new mesh must be created.
|
2020-03-15 09:21:08 +00:00
|
|
|
bool ObjFileParser::needsNewMesh(const std::string &materialName) {
|
2016-02-09 16:50:08 +00:00
|
|
|
// If no mesh data yet
|
2022-07-31 18:54:58 +00:00
|
|
|
if (m_pModel->mCurrentMesh == nullptr) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool newMat = false;
|
2020-03-15 09:21:08 +00:00
|
|
|
int matIdx = getMaterialIndex(materialName);
|
2022-07-31 18:54:58 +00:00
|
|
|
int curMatIdx = m_pModel->mCurrentMesh->m_uiMaterialIndex;
|
2020-03-15 09:21:08 +00:00
|
|
|
if (curMatIdx != int(ObjFile::Mesh::NoMaterial) && curMatIdx != matIdx
|
|
|
|
// no need create a new mesh if no faces in current
|
|
|
|
// lets say 'usemtl' goes straight after 'g'
|
2022-07-31 18:54:58 +00:00
|
|
|
&& !m_pModel->mCurrentMesh->m_Faces.empty()) {
|
2015-05-19 03:52:10 +00:00
|
|
|
// New material -> only one material per mesh, so we need to create a new
|
2015-05-19 03:48:29 +00:00
|
|
|
// material
|
|
|
|
newMat = true;
|
|
|
|
}
|
|
|
|
return newMat;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Shows an error in parsing process.
|
2020-03-15 09:21:08 +00:00
|
|
|
void ObjFileParser::reportErrorTokenInFace() {
|
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
2018-04-26 12:10:18 +00:00
|
|
|
ASSIMP_LOG_ERROR("OBJ: Not supported token in face description detected");
|
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
|