assimp/code/AssetLib/Obj/ObjFileParser.cpp

840 lines
28 KiB
C++
Raw Permalink Normal View History

/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2020, assimp team
2018-01-28 18:42:05 +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
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
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
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
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
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"
#include "ObjFileMtlImporter.h"
#include "ObjTools.h"
#include <assimp/BaseImporter.h>
#include <assimp/DefaultIOSystem.h>
2020-03-15 09:21:08 +00:00
#include <assimp/ParsingUtils.h>
2015-07-09 23:21:10 +00:00
#include <assimp/material.h>
2020-03-15 09:21:08 +00:00
#include <assimp/DefaultLogger.hpp>
2015-07-09 23:21:10 +00:00
#include <assimp/Importer.hpp>
#include <cstdlib>
#include <memory>
#include <utility>
namespace Assimp {
2016-11-09 19:18:26 +00:00
const std::string ObjFileParser::DEFAULT_MATERIAL = AI_DEFAULT_MATERIAL_NAME;
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');
}
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');
// Create the model instance to store all the data
2017-12-30 12:22:30 +00:00
m_pModel.reset(new ObjFile::Model());
m_pModel->m_ModelName = modelName;
2015-05-19 03:52:10 +00:00
// create default material and store it
m_pModel->m_pDefaultMaterial = new ObjFile::Material;
2020-03-15 09:21:08 +00:00
m_pModel->m_pDefaultMaterial->MaterialName.Set(DEFAULT_MATERIAL);
m_pModel->m_MaterialLib.push_back(DEFAULT_MATERIAL);
m_pModel->m_MaterialMap[DEFAULT_MATERIAL] = m_pModel->m_pDefaultMaterial;
2015-05-19 03:52:10 +00:00
// Start parsing the file
2020-03-15 09:21:08 +00:00
parseFile(streamBuffer);
}
2020-03-20 12:18:49 +00:00
ObjFileParser::~ObjFileParser() {
2020-02-12 22:38:56 +00:00
}
2020-03-15 09:21:08 +00:00
void ObjFileParser::setBuffer(std::vector<char> &buffer) {
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();
}
2020-03-15 09:21:08 +00:00
void ObjFileParser::parseFile(IOStreamBuffer<char> &streamBuffer) {
// only update every 100KB or it'll be too slow
2016-11-24 14:15:17 +00:00
//const unsigned int updateProgressEveryBytes = 100 * 1024;
unsigned int progressCounter = 0;
const unsigned int bytesToProcess = static_cast<unsigned int>(streamBuffer.size());
const unsigned int progressTotal = bytesToProcess;
unsigned int processed = 0;
2020-03-15 09:21:08 +00:00
size_t lastFilePos(0);
std::vector<char> buffer;
2020-03-15 09:21:08 +00:00
while (streamBuffer.getNextDataLine(buffer, '\\')) {
m_DataIt = buffer.begin();
m_DataItEnd = buffer.end();
// Handle progress reporting
2020-03-15 09:21:08 +00:00
const size_t filePos(streamBuffer.getFilePos());
if (lastFilePos < filePos) {
processed = static_cast<unsigned int>(filePos);
lastFilePos = filePos;
progressCounter++;
2020-03-15 09:21:08 +00:00
m_progress->UpdateFileRead(processed, progressTotal);
}
2017-05-31 19:37:16 +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
getVector3(m_pModel->m_Vertices);
} else if (numComponents == 4) {
// read in vertex definition (homogeneous coords)
getHomogeneousVector3(m_pModel->m_Vertices);
} else if (numComponents == 6) {
// read vertex and vertex-color
getTwoVectors3(m_pModel->m_Vertices, m_pModel->m_VertexColors);
}
2020-03-20 12:18:49 +00:00
} else if (*m_DataIt == 't') {
// read in texture coordinate ( 2D or 3D )
++m_DataIt;
size_t dim = getTexCoordVector(m_pModel->m_TextureCoord);
m_pModel->m_TextureCoordDim = std::max(m_pModel->m_TextureCoordDim, (unsigned int)dim);
} else if (*m_DataIt == 'n') {
// Read in normal vector definition
++m_DataIt;
getVector3(m_pModel->m_Normals);
}
} 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;
default: {
pf_skip_line:
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
} break;
}
}
}
2016-11-10 20:39:32 +00:00
void ObjFileParser::copyNextWord(char *pBuffer, size_t length) {
size_t index = 0;
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
2020-03-15 09:21:08 +00:00
if (*m_DataIt == '\\') {
++m_DataIt;
++m_DataIt;
2020-03-15 09:21:08 +00:00
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
}
2020-03-15 09:21:08 +00:00
while (m_DataIt != m_DataItEnd && !IsSpaceOrNewLine(*m_DataIt)) {
pBuffer[index] = *m_DataIt;
index++;
2020-03-15 09:21:08 +00:00
if (index == length - 1) {
break;
}
++m_DataIt;
}
ai_assert(index < length);
pBuffer[index] = '\0';
}
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)) {
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) {
// 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);
}
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]);
bool end_of_definition = false;
2020-03-15 09:21:08 +00:00
while (!end_of_definition) {
if (isDataDefinitionEnd(tmp)) {
tmp += 2;
2020-03-15 09:21:08 +00:00
} else if (IsLineEnd(*tmp)) {
end_of_definition = true;
}
2020-03-15 09:21:08 +00:00
if (!SkipSpaces(&tmp)) {
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;
}
}
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();
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);
2020-03-15 09:21:08 +00:00
copyNextWord(m_buffer, Buffersize);
y = (ai_real)fast_atof(m_buffer);
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);
2020-03-15 09:21:08 +00:00
copyNextWord(m_buffer, Buffersize);
y = (ai_real)fast_atof(m_buffer);
2020-03-15 09:21:08 +00:00
copyNextWord(m_buffer, Buffersize);
z = (ai_real)fast_atof(m_buffer);
} else {
2020-03-15 09:21:08 +00:00
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;
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;
}
2020-03-15 09:21:08 +00:00
void ObjFileParser::getVector3(std::vector<aiVector3D> &point3d_array) {
ai_real x, y, z;
copyNextWord(m_buffer, Buffersize);
2020-03-15 09:21:08 +00:00
x = (ai_real)fast_atof(m_buffer);
copyNextWord(m_buffer, Buffersize);
2020-03-15 09:21:08 +00:00
y = (ai_real)fast_atof(m_buffer);
2020-03-15 09:21:08 +00:00
copyNextWord(m_buffer, Buffersize);
z = (ai_real)fast_atof(m_buffer);
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);
}
2020-03-15 09:21:08 +00:00
void ObjFileParser::getHomogeneousVector3(std::vector<aiVector3D> &point3d_array) {
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);
copyNextWord(m_buffer, Buffersize);
2020-03-15 09:21:08 +00:00
y = (ai_real)fast_atof(m_buffer);
2020-03-15 09:21:08 +00:00
copyNextWord(m_buffer, Buffersize);
z = (ai_real)fast_atof(m_buffer);
2020-03-15 09:21:08 +00:00
copyNextWord(m_buffer, Buffersize);
w = (ai_real)fast_atof(m_buffer);
if (w == 0)
2020-03-15 09:21:08 +00:00
throw DeadlyImportError("OBJ: Invalid component in homogeneous vector (Division by zero)");
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);
}
2020-03-15 09:21:08 +00:00
void ObjFileParser::getTwoVectors3(std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b) {
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) {
ai_real x, y;
copyNextWord(m_buffer, Buffersize);
2020-03-15 09:21:08 +00:00
x = (ai_real)fast_atof(m_buffer);
copyNextWord(m_buffer, Buffersize);
2020-03-15 09:21:08 +00:00
y = (ai_real)fast_atof(m_buffer);
point2d_array.emplace_back(x, y);
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
2016-02-09 16:50:08 +00:00
static const std::string DefaultObjName = "defaultobject";
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') {
return;
2016-02-09 16:50:08 +00:00
}
2020-03-15 09:21:08 +00:00
ObjFile::Face *face = new ObjFile::Face(type);
bool hasNormal = false;
const int vSize = static_cast<unsigned int>(m_pModel->m_Vertices.size());
const int vtSize = static_cast<unsigned int>(m_pModel->m_TextureCoord.size());
const int vnSize = static_cast<unsigned int>(m_pModel->m_Normals.size());
const bool vt = (!m_pModel->m_TextureCoord.empty());
const bool vn = (!m_pModel->m_Normals.empty());
2020-02-12 22:27:30 +00:00
int iPos = 0;
2020-03-15 09:21:08 +00:00
while (m_DataIt != m_DataItEnd) {
2020-02-12 22:27:30 +00:00
int iStep = 1;
2020-03-15 09:21:08 +00:00
if (IsLineEnd(*m_DataIt)) {
break;
2016-02-09 16:50:08 +00:00
}
2020-03-15 09:21:08 +00:00
if (*m_DataIt == '/') {
if (type == aiPrimitiveType_POINT) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("Obj: Separator unexpected in point statement");
}
iPos++;
2020-03-15 09:21:08 +00:00
} else if (IsSpaceOrNewLine(*m_DataIt)) {
iPos = 0;
2016-02-09 16:50:08 +00:00
} else {
//OBJ USES 1 Base ARRAYS!!!!
2020-03-15 09:21:08 +00:00
const int iVal(::atoi(&(*m_DataIt)));
// 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) {
++iStep;
2016-02-09 16:50:08 +00:00
}
2020-03-15 09:21:08 +00:00
while ((tmp = tmp / 10) != 0) {
++iStep;
2016-02-09 16:50:08 +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) {
// Store parsed index
2020-03-15 09:21:08 +00:00
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;
2016-11-09 19:16:45 +00:00
} else {
reportErrorTokenInFace();
}
2020-03-15 09:21:08 +00:00
} else if (iVal < 0) {
// Store relatively index
2020-03-15 09:21:08 +00:00
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;
2016-11-09 19:16:45 +00:00
} else {
reportErrorTokenInFace();
}
} else {
//On error, std::atoi will return 0 which is not a valid value
delete face;
throw DeadlyImportError("OBJ: Invalid face indice");
}
}
2016-11-10 20:39:32 +00:00
m_DataIt += iStep;
}
2020-03-15 09:21:08 +00:00
if (face->m_vertices.empty()) {
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_ERROR("Obj: Ignoring empty face");
// skip line and clean up
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
2016-11-22 21:03:31 +00:00
delete face;
return;
}
// Set active material, if one set
if (nullptr != m_pModel->m_pCurrentMaterial) {
face->m_pMaterial = m_pModel->m_pCurrentMaterial;
} else {
face->m_pMaterial = m_pModel->m_pDefaultMaterial;
}
// Create a default object, if nothing is there
if (nullptr == m_pModel->m_pCurrent) {
2020-03-15 09:21:08 +00:00
createObject(DefaultObjName);
}
2015-05-19 03:52:10 +00:00
// Assign face to mesh
if (nullptr == m_pModel->m_pCurrentMesh) {
2020-03-15 09:21:08 +00:00
createMesh(DefaultObjName);
}
2015-05-19 03:52:10 +00:00
// Store the face
2020-03-15 09:21:08 +00:00
m_pModel->m_pCurrentMesh->m_Faces.push_back(face);
m_pModel->m_pCurrentMesh->m_uiNumIndices += (unsigned int)face->m_vertices.size();
m_pModel->m_pCurrentMesh->m_uiUVCoordinates[0] += (unsigned int)face->m_texturCoords.size();
if (!m_pModel->m_pCurrentMesh->m_hasNormals && hasNormal) {
m_pModel->m_pCurrentMesh->m_hasNormals = true;
}
// Skip the rest of the line
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
2016-11-09 19:16:45 +00:00
void ObjFileParser::getMaterialDesc() {
// Get next data for material data
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
if (m_DataIt == m_DataItEnd) {
return;
}
char *pStart = &(*m_DataIt);
2020-03-15 09:21:08 +00:00
while (m_DataIt != m_DataItEnd && !IsLineEnd(*m_DataIt)) {
++m_DataIt;
}
// In some cases we should ignore this 'usemtl' command, this variable helps us to do so
bool skip = false;
// Get name
std::string strName(pStart, &(*m_DataIt));
2015-12-13 19:32:09 +00:00
strName = trim_whitespaces(strName);
if (strName.empty())
skip = true;
// 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
2020-03-15 09:21:08 +00:00
if (m_pModel->m_pCurrentMaterial && m_pModel->m_pCurrentMaterial->MaterialName == aiString(strName)) {
skip = true;
2016-11-09 19:16:45 +00:00
}
2016-11-09 19:16:45 +00:00
if (!skip) {
// Search for material
2020-03-15 09:21:08 +00:00
std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->m_MaterialMap.find(strName);
2016-11-09 19:16:45 +00:00
if (it == m_pModel->m_MaterialMap.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");
2020-03-15 09:21:08 +00:00
m_pModel->m_pCurrentMaterial = new ObjFile::Material();
m_pModel->m_pCurrentMaterial->MaterialName.Set(strName);
m_pModel->m_MaterialLib.push_back(strName);
m_pModel->m_MaterialMap[strName] = m_pModel->m_pCurrentMaterial;
2016-11-09 19:16:45 +00:00
} else {
// Found, using detected material
m_pModel->m_pCurrentMaterial = (*it).second;
}
2020-03-15 09:21:08 +00:00
if (needsNewMesh(strName)) {
createMesh(strName);
2016-11-09 19:16:45 +00:00
}
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strName);
}
// Skip rest of line
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
// 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:57:13 +00:00
// Get material library from file.
2016-11-09 19:16:45 +00:00
void ObjFileParser::getMaterialLib() {
// Translate tuple
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
2020-03-15 09:21:08 +00:00
if (m_DataIt == m_DataItEnd) {
return;
}
2015-05-19 03:52:10 +00:00
char *pStart = &(*m_DataIt);
2020-03-15 09:21:08 +00:00
while (m_DataIt != m_DataItEnd && !IsLineEnd(*m_DataIt)) {
++m_DataIt;
}
// Check for existence
const std::string strMatName(pStart, &(*m_DataIt));
std::string absName;
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.");
return;
}
2020-03-15 09:21:08 +00:00
if (m_pIO->StackSize() > 0) {
std::string path = m_pIO->CurrentDirectory();
2020-03-15 09:21:08 +00:00
if ('/' != *path.rbegin()) {
path += '/';
}
2018-03-20 22:38:08 +00:00
absName += path;
absName += strMatName;
} else {
absName = strMatName;
}
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);
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);
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);
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
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;
2020-03-15 09:21:08 +00:00
BaseImporter::TextFileToBuffer(pFile, buffer, BaseImporter::ALLOW_EMPTY);
m_pIO->Close(pFile);
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:57:13 +00:00
// Set a new material definition as the current material.
2016-11-10 20:39:32 +00:00
void ObjFileParser::getNewMaterial() {
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) {
return;
}
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)) {
++m_DataIt;
}
2020-03-15 09:21:08 +00:00
std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->m_MaterialMap.find(strMat);
if (it == m_pModel->m_MaterialMap.end()) {
// Show a warning, if material was not found
2021-05-13 11:05:31 +00:00
ASSIMP_LOG_WARN("OBJ: Unsupported material requested: ", strMat);
m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
2016-11-10 20:39:32 +00:00
} else {
// Set new material
2020-03-15 09:21:08 +00:00
if (needsNewMesh(strMat)) {
createMesh(strMat);
}
2020-03-15 09:21:08 +00:00
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strMat);
}
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
2020-03-15 09:21:08 +00:00
int ObjFileParser::getMaterialIndex(const std::string &strMaterialName) {
int mat_index = -1;
2020-03-15 09:21:08 +00:00
if (strMaterialName.empty()) {
return mat_index;
}
2020-03-15 09:21:08 +00:00
for (size_t index = 0; index < m_pModel->m_MaterialLib.size(); ++index) {
if (strMaterialName == m_pModel->m_MaterialLib[index]) {
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
// 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)) {
return;
}
// Change active group, if necessary
2020-03-15 09:21:08 +00:00
if (m_pModel->m_strActiveGroup != groupName) {
// Search for already existing entry
2016-09-06 13:41:37 +00:00
ObjFile::Model::ConstGroupMapIt it = m_pModel->m_Groups.find(groupName);
2015-05-19 03:52:10 +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
// New group name, creating a new entry
2020-03-15 09:21:08 +00:00
if (it == m_pModel->m_Groups.end()) {
std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
2020-03-15 09:21:08 +00:00
m_pModel->m_Groups[groupName] = pFaceIDArray;
m_pModel->m_pGroupFaceIDs = (pFaceIDArray);
2020-03-15 09:21:08 +00:00
} else {
m_pModel->m_pGroupFaceIDs = (*it).second;
}
2016-09-06 13:41:37 +00:00
m_pModel->m_strActiveGroup = groupName;
}
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
2015-05-19 03:57:13 +00:00
// Not supported
2020-03-15 09:21:08 +00:00
void ObjFileParser::getGroupNumber() {
// Not used
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
2015-05-19 03:57:13 +00:00
// Not supported
2020-03-15 09:21:08 +00:00
void ObjFileParser::getGroupNumberAndResolution() {
// Not used
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
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() {
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
2020-03-15 09:21:08 +00:00
if (m_DataIt == m_DataItEnd) {
return;
}
char *pStart = &(*m_DataIt);
2020-03-15 09:21:08 +00:00
while (m_DataIt != m_DataItEnd && !IsSpaceOrNewLine(*m_DataIt)) {
++m_DataIt;
}
std::string strObjectName(pStart, &(*m_DataIt));
2020-03-15 09:21:08 +00:00
if (!strObjectName.empty()) {
// Reset current object
m_pModel->m_pCurrent = nullptr;
2015-05-19 03:52:10 +00:00
// Search for actual object
2020-03-15 09:21:08 +00:00
for (std::vector<ObjFile::Object *>::const_iterator it = m_pModel->m_Objects.begin();
it != m_pModel->m_Objects.end();
++it) {
if ((*it)->m_strObjName == strObjectName) {
m_pModel->m_pCurrent = *it;
break;
}
}
// Allocate a new object, if current one was not found before
if (nullptr == m_pModel->m_pCurrent) {
2020-03-15 09:21:08 +00:00
createObject(strObjectName);
}
}
2020-03-15 09:21:08 +00:00
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
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) {
ai_assert(nullptr != m_pModel);
m_pModel->m_pCurrent = new ObjFile::Object;
2015-07-09 17:59:47 +00:00
m_pModel->m_pCurrent->m_strObjName = objName;
2020-03-15 09:21:08 +00:00
m_pModel->m_Objects.push_back(m_pModel->m_pCurrent);
2015-05-19 03:52:10 +00:00
2020-03-15 09:21:08 +00:00
createMesh(objName);
2020-03-15 09:21:08 +00:00
if (m_pModel->m_pCurrentMaterial) {
2015-05-19 03:52:10 +00:00
m_pModel->m_pCurrentMesh->m_uiMaterialIndex =
2020-03-15 09:21:08 +00:00
getMaterialIndex(m_pModel->m_pCurrentMaterial->MaterialName.data);
m_pModel->m_pCurrentMesh->m_pMaterial = m_pModel->m_pCurrentMaterial;
2015-05-19 03:52:10 +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) {
ai_assert(nullptr != m_pModel);
2020-03-15 09:21:08 +00:00
m_pModel->m_pCurrentMesh = new ObjFile::Mesh(meshName);
m_pModel->m_Meshes.push_back(m_pModel->m_pCurrentMesh);
unsigned int meshId = static_cast<unsigned int>(m_pModel->m_Meshes.size() - 1);
if (nullptr != m_pModel->m_pCurrent) {
2020-03-15 09:21:08 +00:00
m_pModel->m_pCurrent->m_Meshes.push_back(meshId);
} 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: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
2020-03-15 09:21:08 +00:00
if (m_pModel->m_pCurrentMesh == nullptr) {
return true;
}
bool newMat = false;
2020-03-15 09:21:08 +00:00
int matIdx = getMaterialIndex(materialName);
2020-02-12 22:21:04 +00:00
int curMatIdx = m_pModel->m_pCurrentMesh->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'
&& !m_pModel->m_pCurrentMesh->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
// 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");
}
// -------------------------------------------------------------------
2020-03-15 09:21:08 +00:00
} // Namespace Assimp
#endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER