Torque3D/Engine/lib/assimp/code/AssetLib/Obj/ObjFileParser.cpp

886 lines
30 KiB
C++
Raw Normal View History

2022-04-26 17:11:02 -05:00
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2026-06-09 12:46:56 -05:00
Copyright (c) 2006-2026, assimp team
2022-04-26 17:11:02 -05:00
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
#ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
#include "ObjFileParser.h"
#include "ObjFileData.h"
#include "ObjFileMtlImporter.h"
#include "ObjTools.h"
#include <assimp/BaseImporter.h>
#include <assimp/DefaultIOSystem.h>
#include <assimp/ParsingUtils.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/Importer.hpp>
#include <cstdlib>
#include <memory>
#include <utility>
namespace Assimp {
2022-10-02 19:02:49 +01:00
constexpr const char ObjFileParser::DEFAULT_MATERIAL[];
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
static bool isDataDefinitionEnd(const char *tmp) {
ai_assert(tmp != nullptr);
if (*tmp == '\\') {
++tmp;
if (IsLineEnd(*tmp)) {
return true;
}
}
return false;
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
static bool isNanOrInf(const char *in) {
ai_assert(in != nullptr);
// Look for "nan" or "inf", case insensitive
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);
}
// -------------------------------------------------------------------
ObjFileParser::ObjFileParser() : mEnd(&mBuffer[Buffersize-1]+1) {
std::fill_n(mBuffer, Buffersize, '\0');
}
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
ObjFileParser::ObjFileParser(IOStreamBuffer<char> &streamBuffer, const std::string &modelName,
2026-06-09 12:46:56 -05:00
IOSystem *io, ProgressHandler *progress, const std::string &originalObjFileName) :
mIO(io),
mProgress(progress),
mOriginalObjFileName(originalObjFileName) {
std::fill_n(mBuffer, Buffersize, '\0');
2022-04-26 17:11:02 -05:00
// Create the model instance to store all the data
2026-06-09 12:46:56 -05:00
mModel.reset(new ObjFile::Model());
mModel->mModelName = modelName;
2022-04-26 17:11:02 -05:00
// create default material and store it
2026-06-09 12:46:56 -05:00
mModel->mDefaultMaterial = new ObjFile::Material;
mModel->mDefaultMaterial->MaterialName.Set(DEFAULT_MATERIAL);
mModel->mMaterialLib.emplace_back(DEFAULT_MATERIAL);
mModel->mMaterialMap[DEFAULT_MATERIAL] = mModel->mDefaultMaterial;
2022-04-26 17:11:02 -05:00
// Start parsing the file
parseFile(streamBuffer);
}
void ObjFileParser::setBuffer(std::vector<char> &buffer) {
2026-06-09 12:46:56 -05:00
mDataIt = buffer.begin();
mDataItEnd = buffer.end();
ai_assert(mDataIt < mDataItEnd);
if (!buffer.empty()) {
mEnd = &buffer[buffer.size() - 1] + 1;
}
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
ObjFile::Model *ObjFileParser::GetModel() const {
2026-06-09 12:46:56 -05:00
return mModel.get();
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
void ObjFileParser::parseFile(IOStreamBuffer<char> &streamBuffer) {
// only update every 100KB or it'll be too slow
2026-06-09 12:46:56 -05:00
// const unsigned int updateProgressEveryBytes = 100 * 1024;
2022-04-26 17:11:02 -05:00
const unsigned int bytesToProcess = static_cast<unsigned int>(streamBuffer.size());
const unsigned int progressTotal = bytesToProcess;
2026-06-09 12:46:56 -05:00
unsigned int processed = 0u;
size_t lastFilePos = 0u;
2022-04-26 17:11:02 -05:00
bool insideCstype = false;
std::vector<char> buffer;
while (streamBuffer.getNextDataLine(buffer, '\\')) {
2026-06-09 12:46:56 -05:00
mDataIt = buffer.begin();
mDataItEnd = buffer.end();
2024-12-09 20:22:47 +00:00
mEnd = &buffer[buffer.size() - 1] + 1;
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
if (processed == 0 && std::distance(mDataIt, mDataItEnd) >= 3 &&
static_cast<unsigned char>(*mDataIt) == 0xEF &&
static_cast<unsigned char>(*(mDataIt + 1)) == 0xBB &&
static_cast<unsigned char>(*(mDataIt + 2)) == 0xBF) {
mDataIt += 3; // skip BOM
}
2022-04-26 17:11:02 -05:00
// Handle progress reporting
2026-06-09 12:46:56 -05:00
const size_t filePos = streamBuffer.getFilePos();
2022-04-26 17:11:02 -05:00
if (lastFilePos < filePos) {
processed = static_cast<unsigned int>(filePos);
lastFilePos = filePos;
2026-06-09 12:46:56 -05:00
if (mProgress != nullptr) {
mProgress->UpdateFileRead(processed, progressTotal);
}
2022-04-26 17:11:02 -05:00
}
2024-12-09 20:22:47 +00:00
// handle c-stype section end (http://paulbourke.net/dataformats/obj/)
2022-04-26 17:11:02 -05:00
if (insideCstype) {
2026-06-09 12:46:56 -05:00
switch (*mDataIt) {
case 'e': {
std::string name;
getNameNoSpace(mDataIt, mDataItEnd, name);
insideCstype = name != "end";
} break;
default:
break;
2022-04-26 17:11:02 -05:00
}
goto pf_skip_line;
}
// parse line
2026-06-09 12:46:56 -05:00
switch (*mDataIt) {
2022-04-26 17:11:02 -05:00
case 'v': // Parse a vertex texture coordinate
{
2026-06-09 12:46:56 -05:00
++mDataIt;
if (*mDataIt == ' ' || *mDataIt == '\t') {
2022-04-26 17:11:02 -05:00
size_t numComponents = getNumComponentsInDataDefinition();
if (numComponents == 3) {
// read in vertex definition
2026-06-09 12:46:56 -05:00
getVector3(mModel->mVertices);
2022-04-26 17:11:02 -05:00
} else if (numComponents == 4) {
// read in vertex definition (homogeneous coords)
2026-06-09 12:46:56 -05:00
getHomogeneousVector3(mModel->mVertices);
2022-04-26 17:11:02 -05:00
} else if (numComponents == 6) {
2024-12-09 20:22:47 +00:00
// fill previous omitted vertex-colors by default
2026-06-09 12:46:56 -05:00
if (mModel->mVertexColors.size() < mModel->mVertices.size()) {
mModel->mVertexColors.resize(mModel->mVertices.size(), aiVector3D(0, 0, 0));
2024-12-09 20:22:47 +00:00
}
2022-04-26 17:11:02 -05:00
// read vertex and vertex-color
2026-06-09 12:46:56 -05:00
getTwoVectors3(mModel->mVertices, mModel->mVertexColors);
2022-04-26 17:11:02 -05:00
}
2024-12-09 20:22:47 +00:00
// append omitted vertex-colors as default for the end if any vertex-color exists
2026-06-09 12:46:56 -05:00
if (!mModel->mVertexColors.empty() && mModel->mVertexColors.size() < mModel->mVertices.size()) {
mModel->mVertexColors.resize(mModel->mVertices.size(), aiVector3D(0, 0, 0));
2024-12-09 20:22:47 +00:00
}
2026-06-09 12:46:56 -05:00
} else if (*mDataIt == 't') {
2022-04-26 17:11:02 -05:00
// read in texture coordinate ( 2D or 3D )
2026-06-09 12:46:56 -05:00
++mDataIt;
size_t dim = getTexCoordVector(mModel->mTextureCoord);
mModel->mTextureCoordDim = std::max(mModel->mTextureCoordDim, (unsigned int)dim);
} else if (*mDataIt == 'n') {
2022-04-26 17:11:02 -05:00
// Read in normal vector definition
2026-06-09 12:46:56 -05:00
++mDataIt;
getVector3(mModel->mNormals);
2022-04-26 17:11:02 -05:00
}
} break;
case 'p': // Parse a face, line or point statement
case 'l':
case 'f': {
2026-06-09 12:46:56 -05:00
getFace(*mDataIt == 'f' ? aiPrimitiveType_POLYGON : (*mDataIt == 'l' ? aiPrimitiveType_LINE : aiPrimitiveType_POINT));
2022-04-26 17:11:02 -05:00
} break;
case '#': // Parse a comment
{
2026-06-09 12:46:56 -05:00
skipComment();
2022-04-26 17:11:02 -05:00
} break;
case 'u': // Parse a material desc. setter
{
std::string name;
2026-06-09 12:46:56 -05:00
getNameNoSpace(mDataIt, mDataItEnd, name);
2022-04-26 17:11:02 -05:00
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;
2026-06-09 12:46:56 -05:00
getNameNoSpace(mDataIt, mDataItEnd, name);
2022-04-26 17:11:02 -05:00
size_t nextSpace = name.find(' ');
if (nextSpace != std::string::npos)
name = name.substr(0, nextSpace);
if (name == "mg")
2026-06-09 12:46:56 -05:00
skipGroupNumberAndResolution();
2022-04-26 17:11:02 -05:00
else if (name == "mtllib")
getMaterialLib();
else
goto pf_skip_line;
} break;
case 'g': // Parse group name
{
getGroupName();
} break;
case 's': // Parse group number
{
2026-06-09 12:46:56 -05:00
skipGroupNumber();
2022-04-26 17:11:02 -05:00
} break;
case 'o': // Parse object name
{
getObjectName();
} break;
case 'c': // handle cstype section start
{
std::string name;
2026-06-09 12:46:56 -05:00
getNameNoSpace(mDataIt, mDataItEnd, name);
2022-04-26 17:11:02 -05:00
insideCstype = name == "cstype";
goto pf_skip_line;
2024-12-09 20:22:47 +00:00
}
2022-04-26 17:11:02 -05:00
default: {
pf_skip_line:
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
} break;
}
}
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
void ObjFileParser::copyNextWord(char *pBuffer, size_t length) {
size_t index = 0;
2026-06-09 12:46:56 -05:00
mDataIt = getNextWord<DataArrayIt>(mDataIt, mDataItEnd);
if (*mDataIt == '\\') {
++mDataIt;
++mDataIt;
mDataIt = getNextWord<DataArrayIt>(mDataIt, mDataItEnd);
}
while (mDataIt != mDataItEnd && !IsSpaceOrNewLine(*mDataIt)) {
pBuffer[index] = *mDataIt;
2022-04-26 17:11:02 -05:00
index++;
if (index == length - 1) {
break;
}
2026-06-09 12:46:56 -05:00
++mDataIt;
2022-04-26 17:11:02 -05:00
}
ai_assert(index < length);
pBuffer[index] = '\0';
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
size_t ObjFileParser::getNumComponentsInDataDefinition() {
size_t numComponents(0);
2026-06-09 12:46:56 -05:00
const char *tmp = &mDataIt[0];
2022-04-26 17:11:02 -05:00
bool end_of_definition = false;
while (!end_of_definition) {
if (isDataDefinitionEnd(tmp)) {
tmp += 2;
} else if (IsLineEnd(*tmp)) {
end_of_definition = true;
}
2026-06-09 12:46:56 -05:00
if (!SkipSpaces(&tmp, mEnd) || *tmp == '#') {
2022-04-26 17:11:02 -05:00
break;
}
const bool isNum(IsNumeric(*tmp) || isNanOrInf(tmp));
2024-12-09 20:22:47 +00:00
SkipToken(tmp, mEnd);
2022-04-26 17:11:02 -05:00
if (isNum) {
++numComponents;
}
2026-06-09 12:46:56 -05:00
if (!SkipSpaces(&tmp, mEnd) || *tmp == '#') {
2022-04-26 17:11:02 -05:00
break;
}
}
2026-06-09 12:46:56 -05:00
2022-04-26 17:11:02 -05:00
return numComponents;
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
size_t ObjFileParser::getTexCoordVector(std::vector<aiVector3D> &point3d_array) {
size_t numComponents = getNumComponentsInDataDefinition();
ai_real x, y, z;
if (2 == numComponents) {
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
x = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
y = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
z = 0.0;
} else if (3 == numComponents) {
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
x = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
y = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
z = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
} else {
throw DeadlyImportError("OBJ: Invalid number of components");
}
// Coerce nan and inf to 0 as is the OBJ default value
if (!std::isfinite(x))
x = 0;
if (!std::isfinite(y))
y = 0;
if (!std::isfinite(z))
z = 0;
point3d_array.emplace_back(x, y, z);
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
return numComponents;
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
void ObjFileParser::getVector3(std::vector<aiVector3D> &point3d_array) {
ai_real x, y, z;
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
x = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
y = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
z = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
point3d_array.emplace_back(x, y, z);
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
void ObjFileParser::getHomogeneousVector3(std::vector<aiVector3D> &point3d_array) {
ai_real x, y, z, w;
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
x = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
y = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
z = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
w = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
if (w == 0)
throw DeadlyImportError("OBJ: Invalid component in homogeneous vector (Division by zero)");
point3d_array.emplace_back(x / w, y / w, z / w);
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
void ObjFileParser::getTwoVectors3(std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b) {
ai_real x, y, z;
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
x = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
y = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
z = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
point3d_array_a.emplace_back(x, y, z);
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
x = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
y = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
z = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
point3d_array_b.emplace_back(x, y, z);
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
void ObjFileParser::getVector2(std::vector<aiVector2D> &point2d_array) {
ai_real x, y;
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
x = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
copyNextWord(mBuffer, Buffersize);
y = fast_atof(mBuffer);
2022-04-26 17:11:02 -05:00
point2d_array.emplace_back(x, y);
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
2022-10-02 19:02:49 +01:00
static constexpr char DefaultObjName[] = "defaultobject";
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
void ObjFileParser::getFace(aiPrimitiveType type) {
2026-06-09 12:46:56 -05:00
mDataIt = getNextToken<DataArrayIt>(mDataIt, mDataItEnd);
if (mDataIt == mDataItEnd || *mDataIt == '\0') {
2022-04-26 17:11:02 -05:00
return;
}
ObjFile::Face *face = new ObjFile::Face(type);
bool hasNormal = false;
2026-06-09 12:46:56 -05:00
const int vSize = static_cast<unsigned int>(mModel->mVertices.size());
const int vtSize = static_cast<unsigned int>(mModel->mTextureCoord.size());
const int vnSize = static_cast<unsigned int>(mModel->mNormals.size());
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
const bool vt = (!mModel->mTextureCoord.empty());
const bool vn = (!mModel->mNormals.empty());
2022-04-26 17:11:02 -05:00
int iPos = 0;
2026-06-09 12:46:56 -05:00
while (mDataIt < mDataItEnd) {
2022-04-26 17:11:02 -05:00
int iStep = 1;
2026-06-09 12:46:56 -05:00
if (IsLineEnd(*mDataIt) || *mDataIt == '#') {
2022-04-26 17:11:02 -05:00
break;
}
2026-06-09 12:46:56 -05:00
if (*mDataIt == '/') {
2022-04-26 17:11:02 -05:00
if (type == aiPrimitiveType_POINT) {
ASSIMP_LOG_ERROR("Obj: Separator unexpected in point statement");
}
2026-06-09 12:46:56 -05:00
++iPos;
} else if (IsSpaceOrNewLine(*mDataIt) || *mDataIt == '\v') {
2022-04-26 17:11:02 -05:00
iPos = 0;
} else {
//OBJ USES 1 Base ARRAYS!!!!
2026-06-09 12:46:56 -05:00
const int iVal = ::atoi(&(*mDataIt));
2024-12-09 20:22:47 +00:00
2022-04-26 17:11:02 -05:00
// increment iStep position based off of the sign and # of digits
int tmp = iVal;
if (iVal < 0) {
++iStep;
}
while ((tmp = tmp / 10) != 0) {
++iStep;
}
2024-12-09 20:22:47 +00:00
if (iPos == 1 && !vt && vn) {
2022-04-26 17:11:02 -05:00
iPos = 2; // skip texture coords for normals if there are no tex coords
2024-12-09 20:22:47 +00:00
}
2022-04-26 17:11:02 -05:00
if (iVal > 0) {
// Store parsed index
if (0 == iPos) {
face->m_vertices.push_back(iVal - 1);
} else if (1 == iPos) {
face->m_texturCoords.push_back(iVal - 1);
} else if (2 == iPos) {
face->m_normals.push_back(iVal - 1);
hasNormal = true;
} else {
reportErrorTokenInFace();
}
} else if (iVal < 0) {
// Store relatively index
if (0 == iPos) {
face->m_vertices.push_back(vSize + iVal);
} else if (1 == iPos) {
face->m_texturCoords.push_back(vtSize + iVal);
} else if (2 == iPos) {
face->m_normals.push_back(vnSize + iVal);
hasNormal = true;
} else {
reportErrorTokenInFace();
}
} else {
//On error, std::atoi will return 0 which is not a valid value
delete face;
2022-10-02 19:02:49 +01:00
throw DeadlyImportError("OBJ: Invalid face index.");
2022-04-26 17:11:02 -05:00
}
}
2026-06-09 12:46:56 -05:00
mDataIt += iStep;
2022-04-26 17:11:02 -05:00
}
if (face->m_vertices.empty()) {
ASSIMP_LOG_ERROR("Obj: Ignoring empty face");
// skip line and clean up
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
delete face;
return;
}
// Set active material, if one set
2026-06-09 12:46:56 -05:00
if (nullptr != mModel->mCurrentMaterial) {
face->m_pMaterial = mModel->mCurrentMaterial;
2022-04-26 17:11:02 -05:00
} else {
2026-06-09 12:46:56 -05:00
face->m_pMaterial = mModel->mDefaultMaterial;
2022-04-26 17:11:02 -05:00
}
// Create a default object, if nothing is there
2026-06-09 12:46:56 -05:00
if (nullptr == mModel->mCurrentObject) {
2022-04-26 17:11:02 -05:00
createObject(DefaultObjName);
}
// Assign face to mesh
2026-06-09 12:46:56 -05:00
if (nullptr == mModel->mCurrentMesh) {
2022-04-26 17:11:02 -05:00
createMesh(DefaultObjName);
}
// Store the face
2026-06-09 12:46:56 -05:00
mModel->mCurrentMesh->m_Faces.emplace_back(face);
mModel->mCurrentMesh->m_uiNumIndices += static_cast<unsigned int>(face->m_vertices.size());
mModel->mCurrentMesh->m_uiUVCoordinates[0] += static_cast<unsigned int>(face->m_texturCoords.size());
if (!mModel->mCurrentMesh->m_hasNormals && hasNormal) {
mModel->mCurrentMesh->m_hasNormals = true;
2022-04-26 17:11:02 -05:00
}
// Skip the rest of the line
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
// -------------------------------------------------------------------
2022-04-26 17:11:02 -05:00
void ObjFileParser::getMaterialDesc() {
// Get next data for material data
2026-06-09 12:46:56 -05:00
mDataIt = getNextToken<DataArrayIt>(mDataIt, mDataItEnd);
if (mDataIt == mDataItEnd) {
2022-04-26 17:11:02 -05:00
return;
}
2026-06-09 12:46:56 -05:00
char *pStart = &(*mDataIt);
while (mDataIt != mDataItEnd && !IsLineEnd(*mDataIt)) {
++mDataIt;
2022-04-26 17:11:02 -05:00
}
// In some cases we should ignore this 'usemtl' command, this variable helps us to do so
bool skip = false;
// Get name
2026-06-09 12:46:56 -05:00
std::string strName(pStart, &(*mDataIt));
2024-12-09 20:22:47 +00:00
strName = ai_trim(strName);
if (strName.empty()) {
2022-04-26 17:11:02 -05:00
skip = true;
2024-12-09 20:22:47 +00:00
}
2022-04-26 17:11:02 -05:00
2024-12-09 20:22:47 +00:00
// If the current mesh has the same material, we will ignore that 'usemtl' command
2022-04-26 17:11:02 -05:00
// There is no need to create another object or even mesh here
2024-12-09 20:22:47 +00:00
if (!skip) {
2026-06-09 12:46:56 -05:00
if (mModel->mCurrentMaterial && mModel->mCurrentMaterial->MaterialName == aiString(strName)) {
2024-12-09 20:22:47 +00:00
skip = true;
}
2022-04-26 17:11:02 -05:00
}
if (!skip) {
// Search for material
2026-06-09 12:46:56 -05:00
std::map<std::string, ObjFile::Material *>::iterator it = mModel->mMaterialMap.find(strName);
if (it == mModel->mMaterialMap.end()) {
2022-04-26 17:11:02 -05: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.
ASSIMP_LOG_ERROR("OBJ: failed to locate material ", strName, ", creating new material");
2026-06-09 12:46:56 -05:00
mModel->mCurrentMaterial = new ObjFile::Material();
mModel->mCurrentMaterial->MaterialName.Set(strName);
mModel->mMaterialLib.push_back(strName);
mModel->mMaterialMap[strName] = mModel->mCurrentMaterial;
2022-04-26 17:11:02 -05:00
} else {
// Found, using detected material
2026-06-09 12:46:56 -05:00
mModel->mCurrentMaterial = it->second;
2022-04-26 17:11:02 -05:00
}
if (needsNewMesh(strName)) {
2026-06-09 12:46:56 -05:00
auto newMeshName = mModel->mActiveGroup.empty() ? strName : mModel->mActiveGroup;
2024-12-09 20:22:47 +00:00
createMesh(newMeshName);
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
mModel->mCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strName);
2022-04-26 17:11:02 -05:00
}
// Skip rest of line
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
// -------------------------------------------------------------------
// Get a comment, values will be skipped
2026-06-09 12:46:56 -05:00
void ObjFileParser::skipComment() {
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
// -------------------------------------------------------------------
void ObjFileParser::getMaterialLib() {
// Translate tuple
2026-06-09 12:46:56 -05:00
mDataIt = getNextToken<DataArrayIt>(mDataIt, mDataItEnd);
if (mDataIt == mDataItEnd) {
2022-04-26 17:11:02 -05:00
return;
}
2026-06-09 12:46:56 -05:00
char *pStart = &(*mDataIt);
while (mDataIt != mDataItEnd && !IsLineEnd(*mDataIt)) {
++mDataIt;
2022-04-26 17:11:02 -05:00
}
// Check for existence
2026-06-09 12:46:56 -05:00
const std::string strMatName(pStart, &(*mDataIt));
2022-04-26 17:11:02 -05:00
std::string absName;
// Check if directive is valid.
if (0 == strMatName.length()) {
ASSIMP_LOG_WARN("OBJ: no name for material library specified.");
return;
}
2026-06-09 12:46:56 -05:00
if (mIO->StackSize() > 0) {
std::string path = mIO->CurrentDirectory();
2022-04-26 17:11:02 -05:00
if ('/' != *path.rbegin()) {
path += '/';
}
absName += path;
absName += strMatName;
} else {
absName = strMatName;
}
2026-06-09 12:46:56 -05:00
std::unique_ptr<IOStream> pFile(mIO->Open(absName));
2022-04-26 17:11:02 -05:00
if (nullptr == pFile) {
ASSIMP_LOG_ERROR("OBJ: Unable to locate material file ", strMatName);
2026-06-09 12:46:56 -05:00
std::string strMatFallbackName = mOriginalObjFileName.substr(0, mOriginalObjFileName.length() - 3) + "mtl";
2022-04-26 17:11:02 -05:00
ASSIMP_LOG_INFO("OBJ: Opening fallback material file ", strMatFallbackName);
2026-06-09 12:46:56 -05:00
pFile.reset(mIO->Open(strMatFallbackName));
2022-04-26 17:11:02 -05:00
if (!pFile) {
ASSIMP_LOG_ERROR("OBJ: Unable to locate fallback material file ", strMatFallbackName);
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
return;
}
}
// 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.
std::vector<char> buffer;
2026-06-09 12:46:56 -05:00
BaseImporter::TextFileToBuffer(pFile.get(), buffer, BaseImporter::ALLOW_EMPTY);
//m_pIO->Close(pFile);
2022-04-26 17:11:02 -05:00
// Importing the material library
2026-06-09 12:46:56 -05:00
ObjFileMtlImporter mtlImporter(buffer, strMatName, mModel.get());
2022-04-26 17:11:02 -05:00
}
// -------------------------------------------------------------------
void ObjFileParser::getNewMaterial() {
2026-06-09 12:46:56 -05:00
mDataIt = getNextToken<DataArrayIt>(mDataIt, mDataItEnd);
mDataIt = getNextWord<DataArrayIt>(mDataIt, mDataItEnd);
if (mDataIt == mDataItEnd) {
2022-04-26 17:11:02 -05:00
return;
}
2026-06-09 12:46:56 -05:00
char *pStart = &(*mDataIt);
std::string strMat(pStart, *mDataIt);
while (mDataIt != mDataItEnd && IsSpaceOrNewLine(*mDataIt)) {
++mDataIt;
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
auto it = mModel->mMaterialMap.find(strMat);
if (it == mModel->mMaterialMap.end()) {
2022-04-26 17:11:02 -05:00
// Show a warning, if material was not found
ASSIMP_LOG_WARN("OBJ: Unsupported material requested: ", strMat);
2026-06-09 12:46:56 -05:00
mModel->mCurrentMaterial = mModel->mDefaultMaterial;
2022-04-26 17:11:02 -05:00
} else {
// Set new material
if (needsNewMesh(strMat)) {
createMesh(strMat);
}
2026-06-09 12:46:56 -05:00
mModel->mCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strMat);
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
static constexpr int InvalidMaterialIndex = -1;
2022-04-26 17:11:02 -05:00
// -------------------------------------------------------------------
int ObjFileParser::getMaterialIndex(const std::string &strMaterialName) {
2026-06-09 12:46:56 -05:00
int mat_index = InvalidMaterialIndex;
2022-04-26 17:11:02 -05:00
if (strMaterialName.empty()) {
return mat_index;
}
2026-06-09 12:46:56 -05:00
for (size_t index = 0; index < mModel->mMaterialLib.size(); ++index) {
if (strMaterialName == mModel->mMaterialLib[index]) {
2022-04-26 17:11:02 -05:00
mat_index = (int)index;
break;
}
}
return mat_index;
}
// -------------------------------------------------------------------
// Getter for a group name.
void ObjFileParser::getGroupName() {
std::string groupName;
// here we skip 'g ' from line
2026-06-09 12:46:56 -05:00
mDataIt = getNextToken<DataArrayIt>(mDataIt, mDataItEnd);
mDataIt = getName<DataArrayIt>(mDataIt, mDataItEnd, groupName);
if (isEndOfBuffer(mDataIt, mDataItEnd)) {
2022-04-26 17:11:02 -05:00
return;
}
// Change active group, if necessary
2026-06-09 12:46:56 -05:00
if (mModel->mActiveGroup != groupName) {
2022-04-26 17:11:02 -05:00
// Search for already existing entry
2026-06-09 12:46:56 -05:00
ObjFile::Model::ConstGroupMapIt it = mModel->mGroups.find(groupName);
2022-04-26 17:11:02 -05:00
// We are mapping groups into the object structure
createObject(groupName);
// New group name, creating a new entry
2026-06-09 12:46:56 -05:00
if (it == mModel->mGroups.end()) {
2022-04-26 17:11:02 -05:00
std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
2026-06-09 12:46:56 -05:00
mModel->mGroups[groupName] = pFaceIDArray;
mModel->mGroupFaceIDs = (pFaceIDArray);
2022-04-26 17:11:02 -05:00
} else {
2026-06-09 12:46:56 -05:00
mModel->mGroupFaceIDs = (*it).second;
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
mModel->mActiveGroup = groupName;
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
// -------------------------------------------------------------------
2026-06-09 12:46:56 -05:00
void ObjFileParser::skipGroupNumber() {
2022-04-26 17:11:02 -05:00
// Not used
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
// -------------------------------------------------------------------
2026-06-09 12:46:56 -05:00
void ObjFileParser::skipGroupNumberAndResolution() {
2022-04-26 17:11:02 -05:00
// Not used
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
// -------------------------------------------------------------------
// Stores values for a new object instance, name will be used to
// identify it.
void ObjFileParser::getObjectName() {
2026-06-09 12:46:56 -05:00
mDataIt = getNextToken<DataArrayIt>(mDataIt, mDataItEnd);
if (mDataIt == mDataItEnd) {
2022-04-26 17:11:02 -05:00
return;
}
2026-06-09 12:46:56 -05:00
char *pStart = &(*mDataIt);
while (mDataIt != mDataItEnd && !IsSpaceOrNewLine(*mDataIt)) {
++mDataIt;
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
std::string strObjectName(pStart, &(*mDataIt));
2022-04-26 17:11:02 -05:00
if (!strObjectName.empty()) {
// Reset current object
2026-06-09 12:46:56 -05:00
mModel->mCurrentObject = nullptr;
2022-04-26 17:11:02 -05:00
// Search for actual object
2026-06-09 12:46:56 -05:00
for (auto it = mModel->mObjects.begin(); it != mModel->mObjects.end(); ++it) {
2022-04-26 17:11:02 -05:00
if ((*it)->m_strObjName == strObjectName) {
2026-06-09 12:46:56 -05:00
mModel->mCurrentObject = *it;
2022-04-26 17:11:02 -05:00
break;
}
}
// Allocate a new object, if current one was not found before
2026-06-09 12:46:56 -05:00
if (mModel->mCurrentObject == nullptr) {
2022-04-26 17:11:02 -05:00
createObject(strObjectName);
}
}
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
}
2026-06-09 12:46:56 -05:00
2022-04-26 17:11:02 -05:00
// -------------------------------------------------------------------
// Creates a new object instance
void ObjFileParser::createObject(const std::string &objName) {
2026-06-09 12:46:56 -05:00
ai_assert(nullptr != mModel);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
mModel->mCurrentObject = new ObjFile::Object;
mModel->mCurrentObject->m_strObjName = objName;
mModel->mObjects.push_back(mModel->mCurrentObject);
2022-04-26 17:11:02 -05:00
createMesh(objName);
2026-06-09 12:46:56 -05:00
if (mModel->mCurrentMaterial) {
mModel->mCurrentMesh->m_uiMaterialIndex =
getMaterialIndex(mModel->mCurrentMaterial->MaterialName.data);
mModel->mCurrentMesh->m_pMaterial = mModel->mCurrentMaterial;
2022-04-26 17:11:02 -05:00
}
}
// -------------------------------------------------------------------
// Creates a new mesh
void ObjFileParser::createMesh(const std::string &meshName) {
2026-06-09 12:46:56 -05:00
ai_assert(nullptr != mModel);
2022-04-26 17:11:02 -05:00
2026-06-09 12:46:56 -05:00
mModel->mCurrentMesh = new ObjFile::Mesh(meshName);
mModel->mMeshes.push_back(mModel->mCurrentMesh);
auto meshId = static_cast<unsigned int>(mModel->mMeshes.size() - 1);
if (mModel->mCurrentObject != nullptr) {
mModel->mCurrentObject->m_Meshes.push_back(meshId);
2022-04-26 17:11:02 -05:00
} else {
ASSIMP_LOG_ERROR("OBJ: No object detected to attach a new mesh instance.");
}
}
// -------------------------------------------------------------------
// Returns true, if a new mesh must be created.
bool ObjFileParser::needsNewMesh(const std::string &materialName) {
// If no mesh data yet
2026-06-09 12:46:56 -05:00
if (mModel->mCurrentMesh == nullptr) {
2022-04-26 17:11:02 -05:00
return true;
}
bool newMat = false;
int matIdx = getMaterialIndex(materialName);
2026-06-09 12:46:56 -05:00
int curMatIdx = mModel->mCurrentMesh->m_uiMaterialIndex;
2022-04-26 17:11:02 -05: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'
2026-06-09 12:46:56 -05:00
&& !mModel->mCurrentMesh->m_Faces.empty()) {
2022-04-26 17:11:02 -05:00
// New material -> only one material per mesh, so we need to create a new
// material
newMat = true;
}
return newMat;
}
// -------------------------------------------------------------------
// Shows an error in parsing process.
void ObjFileParser::reportErrorTokenInFace() {
2026-06-09 12:46:56 -05:00
mDataIt = skipLine<DataArrayIt>(mDataIt, mDataItEnd, mLine);
2022-04-26 17:11:02 -05:00
ASSIMP_LOG_ERROR("OBJ: Not supported token in face description detected");
}
// -------------------------------------------------------------------
} // Namespace Assimp
#endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER