Obj: apply clang format.

pull/3012/head
Kim Kulling 2020-03-15 10:21:08 +01:00
parent 0357333c81
commit 8b6f95ec30
8 changed files with 816 additions and 1009 deletions

View File

@ -4,7 +4,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2020, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@ -44,10 +43,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef OBJ_FILEDATA_H_INC
#define OBJ_FILEDATA_H_INC
#include <vector>
#include <map>
#include <assimp/types.h>
#include <assimp/mesh.h>
#include <assimp/types.h>
#include <map>
#include <vector>
namespace Assimp {
namespace ObjFile {
@ -75,12 +74,8 @@ struct Face {
Material *m_pMaterial;
//! \brief Default constructor
Face( aiPrimitiveType pt = aiPrimitiveType_POLYGON)
: m_PrimitiveType( pt )
, m_vertices()
, m_normals()
, m_texturCoords()
, m_pMaterial( 0L ) {
Face(aiPrimitiveType pt = aiPrimitiveType_POLYGON) :
m_PrimitiveType(pt), m_vertices(), m_normals(), m_texturCoords(), m_pMaterial(0L) {
// empty
}
@ -105,19 +100,19 @@ struct Object {
//! Transformation matrix, stored in OpenGL format
aiMatrix4x4 m_Transformation;
//! All sub-objects referenced by this object
std::vector<Object*> m_SubObjects;
std::vector<Object *> m_SubObjects;
/// Assigned meshes
std::vector<unsigned int> m_Meshes;
//! \brief Default constructor
Object()
: m_strObjName("") {
Object() :
m_strObjName("") {
// empty
}
//! \brief Destructor
~Object() {
for ( std::vector<Object*>::iterator it = m_SubObjects.begin(); it != m_SubObjects.end(); ++it) {
for (std::vector<Object *>::iterator it = m_SubObjects.begin(); it != m_SubObjects.end(); ++it) {
delete *it;
}
}
@ -184,16 +179,11 @@ struct Material {
aiColor3D transparent;
//! Constructor
Material()
: diffuse ( ai_real( 0.6 ), ai_real( 0.6 ), ai_real( 0.6 ) )
, alpha (ai_real( 1.0 ) )
, shineness ( ai_real( 0.0) )
, illumination_model (1)
, ior ( ai_real( 1.0 ) )
, transparent( ai_real( 1.0), ai_real (1.0), ai_real(1.0)) {
Material() :
diffuse(ai_real(0.6), ai_real(0.6), ai_real(0.6)), alpha(ai_real(1.0)), shineness(ai_real(0.0)), illumination_model(1), ior(ai_real(1.0)), transparent(ai_real(1.0), ai_real(1.0), ai_real(1.0)) {
// empty
for (size_t i = 0; i < TextureTypeCount; ++i) {
clamp[ i ] = false;
clamp[i] = false;
}
}
@ -212,13 +202,13 @@ struct Mesh {
/// The name for the mesh
std::string m_name;
/// Array with pointer to all stored faces
std::vector<Face*> m_Faces;
std::vector<Face *> m_Faces;
/// Assigned material
Material *m_pMaterial;
/// Number of stored indices.
unsigned int m_uiNumIndices;
/// Number of UV
unsigned int m_uiUVCoordinates[ AI_MAX_NUMBER_OF_TEXTURECOORDS ];
unsigned int m_uiUVCoordinates[AI_MAX_NUMBER_OF_TEXTURECOORDS];
/// Material index.
unsigned int m_uiMaterialIndex;
/// True, if normals are stored.
@ -227,20 +217,15 @@ struct Mesh {
bool m_hasVertexColors;
/// Constructor
explicit Mesh( const std::string &name )
: m_name( name )
, m_pMaterial(NULL)
, m_uiNumIndices(0)
, m_uiMaterialIndex( NoMaterial )
, m_hasNormals(false) {
memset(m_uiUVCoordinates, 0, sizeof( unsigned int ) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
explicit Mesh(const std::string &name) :
m_name(name), m_pMaterial(NULL), m_uiNumIndices(0), m_uiMaterialIndex(NoMaterial), m_hasNormals(false) {
memset(m_uiUVCoordinates, 0, sizeof(unsigned int) * AI_MAX_NUMBER_OF_TEXTURECOORDS);
}
/// Destructor
~Mesh() {
for (std::vector<Face*>::iterator it = m_Faces.begin();
it != m_Faces.end(); ++it)
{
for (std::vector<Face *>::iterator it = m_Faces.begin();
it != m_Faces.end(); ++it) {
delete *it;
}
}
@ -251,14 +236,14 @@ struct Mesh {
//! \brief Data structure to store all obj-specific model datas
// ------------------------------------------------------------------------------------------------
struct Model {
typedef std::map<std::string, std::vector<unsigned int>* > GroupMap;
typedef std::map<std::string, std::vector<unsigned int>* >::iterator GroupMapIt;
typedef std::map<std::string, std::vector<unsigned int>* >::const_iterator ConstGroupMapIt;
typedef std::map<std::string, std::vector<unsigned int> *> GroupMap;
typedef std::map<std::string, std::vector<unsigned int> *>::iterator GroupMapIt;
typedef std::map<std::string, std::vector<unsigned int> *>::const_iterator ConstGroupMapIt;
//! Model name
std::string m_ModelName;
//! List ob assigned objects
std::vector<Object*> m_Objects;
std::vector<Object *> m_Objects;
//! Pointer to current object
ObjFile::Object *m_pCurrent;
//! Pointer to current material
@ -286,9 +271,9 @@ struct Model {
//! Current mesh instance
Mesh *m_pCurrentMesh;
//! Vector with stored meshes
std::vector<Mesh*> m_Meshes;
std::vector<Mesh *> m_Meshes;
//! Material map
std::map<std::string, Material*> m_MaterialMap;
std::map<std::string, Material *> m_MaterialMap;
//! \brief The default class constructor
Model() :
@ -299,33 +284,32 @@ struct Model {
m_pGroupFaceIDs(NULL),
m_strActiveGroup(""),
m_TextureCoordDim(0),
m_pCurrentMesh(NULL)
{
m_pCurrentMesh(NULL) {
// empty
}
//! \brief The class destructor
~Model() {
// Clear all stored object instances
for (std::vector<Object*>::iterator it = m_Objects.begin();
for (std::vector<Object *>::iterator it = m_Objects.begin();
it != m_Objects.end(); ++it) {
delete *it;
}
m_Objects.clear();
// Clear all stored mesh instances
for (std::vector<Mesh*>::iterator it = m_Meshes.begin();
for (std::vector<Mesh *>::iterator it = m_Meshes.begin();
it != m_Meshes.end(); ++it) {
delete *it;
}
m_Meshes.clear();
for(GroupMapIt it = m_Groups.begin(); it != m_Groups.end(); ++it) {
for (GroupMapIt it = m_Groups.begin(); it != m_Groups.end(); ++it) {
delete it->second;
}
m_Groups.clear();
for ( std::map<std::string, Material*>::iterator it = m_MaterialMap.begin(); it != m_MaterialMap.end(); ++it ) {
for (std::map<std::string, Material *>::iterator it = m_MaterialMap.begin(); it != m_MaterialMap.end(); ++it) {
delete it->second;
}
}

View File

@ -5,8 +5,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2020, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@ -44,16 +42,16 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
#include "ObjFileImporter.h"
#include "ObjFileParser.h"
#include "ObjFileData.h"
#include <assimp/IOStreamBuffer.h>
#include <memory>
#include "ObjFileParser.h"
#include <assimp/DefaultIOSystem.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/IOStreamBuffer.h>
#include <assimp/ai_assert.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/importerdesc.h>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/Importer.hpp>
#include <memory>
static const aiImporterDesc desc = {
"Wavefront Object Importer",
@ -76,10 +74,8 @@ using namespace std;
// ------------------------------------------------------------------------------------------------
// Default constructor
ObjFileImporter::ObjFileImporter()
: m_Buffer()
, m_pRootObject( nullptr )
, m_strAbsPath( std::string(1, DefaultIOSystem().getOsSeparator()) ) {}
ObjFileImporter::ObjFileImporter() :
m_Buffer(), m_pRootObject(nullptr), m_strAbsPath(std::string(1, DefaultIOSystem().getOsSeparator())) {}
// ------------------------------------------------------------------------------------------------
// Destructor.
@ -90,59 +86,59 @@ ObjFileImporter::~ObjFileImporter() {
// ------------------------------------------------------------------------------------------------
// Returns true, if file is an obj file.
bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler , bool checkSig ) const {
if(!checkSig) {
bool ObjFileImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
if (!checkSig) {
//Check File Extension
return SimpleExtensionCheck(pFile,"obj");
return SimpleExtensionCheck(pFile, "obj");
} else {
// Check file Header
static const char *pTokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " };
return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9, 200, false, true );
return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9, 200, false, true);
}
}
// ------------------------------------------------------------------------------------------------
const aiImporterDesc* ObjFileImporter::GetInfo() const {
const aiImporterDesc *ObjFileImporter::GetInfo() const {
return &desc;
}
// ------------------------------------------------------------------------------------------------
// Obj-file import implementation
void ObjFileImporter::InternReadFile( const std::string &file, aiScene* pScene, IOSystem* pIOHandler) {
void ObjFileImporter::InternReadFile(const std::string &file, aiScene *pScene, IOSystem *pIOHandler) {
// Read file into memory
static const std::string mode = "rb";
std::unique_ptr<IOStream> fileStream( pIOHandler->Open( file, mode));
if( !fileStream.get() ) {
throw DeadlyImportError( "Failed to open file " + file + "." );
std::unique_ptr<IOStream> fileStream(pIOHandler->Open(file, mode));
if (!fileStream.get()) {
throw DeadlyImportError("Failed to open file " + file + ".");
}
// Get the file-size and validate it, throwing an exception when fails
size_t fileSize = fileStream->FileSize();
if( fileSize < ObjMinSize ) {
throw DeadlyImportError( "OBJ-file is too small.");
if (fileSize < ObjMinSize) {
throw DeadlyImportError("OBJ-file is too small.");
}
IOStreamBuffer<char> streamedBuffer;
streamedBuffer.open( fileStream.get() );
streamedBuffer.open(fileStream.get());
// Allocate buffer and read file into it
//TextFileToBuffer( fileStream.get(),m_Buffer);
// Get the model name
std::string modelName, folderName;
std::string::size_type pos = file.find_last_of( "\\/" );
if ( pos != std::string::npos ) {
modelName = file.substr(pos+1, file.size() - pos - 1);
folderName = file.substr( 0, pos );
if ( !folderName.empty() ) {
pIOHandler->PushDirectory( folderName );
std::string::size_type pos = file.find_last_of("\\/");
if (pos != std::string::npos) {
modelName = file.substr(pos + 1, file.size() - pos - 1);
folderName = file.substr(0, pos);
if (!folderName.empty()) {
pIOHandler->PushDirectory(folderName);
}
} else {
modelName = file;
}
// parse the file into a temporary representation
ObjFileParser parser( streamedBuffer, modelName, pIOHandler, m_progress, file);
ObjFileParser parser(streamedBuffer, modelName, pIOHandler, m_progress, file);
// And create the proper return structures out of it
CreateDataFromImport(parser.GetModel(), pScene);
@ -153,21 +149,21 @@ void ObjFileImporter::InternReadFile( const std::string &file, aiScene* pScene,
m_Buffer.clear();
// Pop directory stack
if ( pIOHandler->StackSize() > 0 ) {
if (pIOHandler->StackSize() > 0) {
pIOHandler->PopDirectory();
}
}
// ------------------------------------------------------------------------------------------------
// Create the data from parsed obj-file
void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene) {
if( 0L == pModel ) {
void ObjFileImporter::CreateDataFromImport(const ObjFile::Model *pModel, aiScene *pScene) {
if (0L == pModel) {
return;
}
// Create the root node of the scene
pScene->mRootNode = new aiNode;
if ( !pModel->m_ModelName.empty() ) {
if (!pModel->m_ModelName.empty()) {
// Set the name of the scene
pScene->mRootNode->mName.Set(pModel->m_ModelName);
} else {
@ -181,17 +177,17 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
unsigned int childCount = 0;
for (auto object : pModel->m_Objects) {
if(object) {
if (object) {
++childCount;
meshCount += (unsigned int)object->m_Meshes.size();
}
}
// Allocate space for the child nodes on the root node
pScene->mRootNode->mChildren = new aiNode*[ childCount ];
pScene->mRootNode->mChildren = new aiNode *[childCount];
// Create nodes for the whole scene
std::vector<aiMesh*> MeshArray;
std::vector<aiMesh *> MeshArray;
MeshArray.reserve(meshCount);
for (size_t index = 0; index < pModel->m_Objects.size(); ++index) {
createNodes(pModel, pModel->m_Objects[index], pScene->mRootNode, pScene, MeshArray);
@ -201,7 +197,7 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
// Create mesh pointer buffer for this scene
if (pScene->mNumMeshes > 0) {
pScene->mMeshes = new aiMesh*[MeshArray.size()];
pScene->mMeshes = new aiMesh *[MeshArray.size()];
for (size_t index = 0; index < MeshArray.size(); ++index) {
pScene->mMeshes[index] = MeshArray[index];
}
@ -209,34 +205,34 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
// Create all materials
createMaterials(pModel, pScene);
}else {
if (pModel->m_Vertices.empty()){
} else {
if (pModel->m_Vertices.empty()) {
return;
}
std::unique_ptr<aiMesh> mesh( new aiMesh );
std::unique_ptr<aiMesh> mesh(new aiMesh);
mesh->mPrimitiveTypes = aiPrimitiveType_POINT;
unsigned int n = (unsigned int)pModel->m_Vertices.size();
mesh->mNumVertices = n;
mesh->mVertices = new aiVector3D[n];
memcpy(mesh->mVertices, pModel->m_Vertices.data(), n*sizeof(aiVector3D) );
memcpy(mesh->mVertices, pModel->m_Vertices.data(), n * sizeof(aiVector3D));
if ( !pModel->m_Normals.empty() ) {
if (!pModel->m_Normals.empty()) {
mesh->mNormals = new aiVector3D[n];
if (pModel->m_Normals.size() < n) {
throw DeadlyImportError("OBJ: vertex normal index out of range");
}
memcpy(mesh->mNormals, pModel->m_Normals.data(), n*sizeof(aiVector3D));
memcpy(mesh->mNormals, pModel->m_Normals.data(), n * sizeof(aiVector3D));
}
if ( !pModel->m_VertexColors.empty() ){
if (!pModel->m_VertexColors.empty()) {
mesh->mColors[0] = new aiColor4D[mesh->mNumVertices];
for (unsigned int i = 0; i < n; ++i) {
if (i < pModel->m_VertexColors.size() ) {
const aiVector3D& color = pModel->m_VertexColors[i];
if (i < pModel->m_VertexColors.size()) {
const aiVector3D &color = pModel->m_VertexColors[i];
mesh->mColors[0][i] = aiColor4D(color.x, color.y, color.z, 1.0);
}else {
} else {
throw DeadlyImportError("OBJ: vertex color index out of range");
}
}
@ -245,7 +241,7 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
pScene->mRootNode->mNumMeshes = 1;
pScene->mRootNode->mMeshes = new unsigned int[1];
pScene->mRootNode->mMeshes[0] = 0;
pScene->mMeshes = new aiMesh*[1];
pScene->mMeshes = new aiMesh *[1];
pScene->mNumMeshes = 1;
pScene->mMeshes[0] = mesh.release();
}
@ -253,12 +249,11 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
// ------------------------------------------------------------------------------------------------
// Creates all nodes of the model
aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pObject,
aiNode *pParent, aiScene* pScene,
std::vector<aiMesh*> &MeshArray )
{
ai_assert( NULL != pModel );
if( NULL == pObject ) {
aiNode *ObjFileImporter::createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pObject,
aiNode *pParent, aiScene *pScene,
std::vector<aiMesh *> &MeshArray) {
ai_assert(NULL != pModel);
if (NULL == pObject) {
return NULL;
}
@ -269,15 +264,15 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
pNode->mName = pObject->m_strObjName;
// If we have a parent node, store it
ai_assert( NULL != pParent );
appendChildToParentNode( pParent, pNode );
ai_assert(NULL != pParent);
appendChildToParentNode(pParent, pNode);
for ( size_t i=0; i< pObject->m_Meshes.size(); ++i ) {
unsigned int meshId = pObject->m_Meshes[ i ];
aiMesh *pMesh = createTopology( pModel, pObject, meshId );
if( pMesh ) {
for (size_t i = 0; i < pObject->m_Meshes.size(); ++i) {
unsigned int meshId = pObject->m_Meshes[i];
aiMesh *pMesh = createTopology(pModel, pObject, meshId);
if (pMesh) {
if (pMesh->mNumFaces > 0) {
MeshArray.push_back( pMesh );
MeshArray.push_back(pMesh);
} else {
delete pMesh;
}
@ -285,22 +280,22 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
}
// Create all nodes from the sub-objects stored in the current object
if ( !pObject->m_SubObjects.empty() ) {
if (!pObject->m_SubObjects.empty()) {
size_t numChilds = pObject->m_SubObjects.size();
pNode->mNumChildren = static_cast<unsigned int>( numChilds );
pNode->mChildren = new aiNode*[ numChilds ];
pNode->mNumChildren = static_cast<unsigned int>(numChilds);
pNode->mChildren = new aiNode *[numChilds];
pNode->mNumMeshes = 1;
pNode->mMeshes = new unsigned int[ 1 ];
pNode->mMeshes = new unsigned int[1];
}
// Set mesh instances into scene- and node-instances
const size_t meshSizeDiff = MeshArray.size()- oldMeshSize;
if ( meshSizeDiff > 0 ) {
pNode->mMeshes = new unsigned int[ meshSizeDiff ];
pNode->mNumMeshes = static_cast<unsigned int>( meshSizeDiff );
const size_t meshSizeDiff = MeshArray.size() - oldMeshSize;
if (meshSizeDiff > 0) {
pNode->mMeshes = new unsigned int[meshSizeDiff];
pNode->mNumMeshes = static_cast<unsigned int>(meshSizeDiff);
size_t index = 0;
for (size_t i = oldMeshSize; i < MeshArray.size(); ++i ) {
pNode->mMeshes[ index ] = pScene->mNumMeshes;
for (size_t i = oldMeshSize; i < MeshArray.size(); ++i) {
pNode->mMeshes[index] = pScene->mNumMeshes;
pScene->mNumMeshes++;
++index;
}
@ -311,33 +306,32 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
// ------------------------------------------------------------------------------------------------
// Create topology data
aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const ObjFile::Object* pData, unsigned int meshIndex ) {
aiMesh *ObjFileImporter::createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData, unsigned int meshIndex) {
// Checking preconditions
ai_assert( NULL != pModel );
ai_assert(NULL != pModel);
if( NULL == pData ) {
if (NULL == pData) {
return NULL;
}
// Create faces
ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ meshIndex ];
if( !pObjMesh ) {
ObjFile::Mesh *pObjMesh = pModel->m_Meshes[meshIndex];
if (!pObjMesh) {
return NULL;
}
if( pObjMesh->m_Faces.empty() ) {
if (pObjMesh->m_Faces.empty()) {
return NULL;
}
std::unique_ptr<aiMesh> pMesh(new aiMesh);
if( !pObjMesh->m_name.empty() ) {
pMesh->mName.Set( pObjMesh->m_name );
if (!pObjMesh->m_name.empty()) {
pMesh->mName.Set(pObjMesh->m_name);
}
for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
{
ObjFile::Face *const inp = pObjMesh->m_Faces[ index ];
ai_assert( NULL != inp );
for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++) {
ObjFile::Face *const inp = pObjMesh->m_Faces[index];
ai_assert(NULL != inp);
if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
pMesh->mNumFaces += static_cast<unsigned int>(inp->m_vertices.size() - 1);
@ -355,40 +349,39 @@ aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const Obj
}
}
unsigned int uiIdxCount( 0u );
if ( pMesh->mNumFaces > 0 ) {
pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
if ( pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial ) {
unsigned int uiIdxCount(0u);
if (pMesh->mNumFaces > 0) {
pMesh->mFaces = new aiFace[pMesh->mNumFaces];
if (pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial) {
pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
}
unsigned int outIndex( 0 );
unsigned int outIndex(0);
// Copy all data from all stored meshes
for (auto& face : pObjMesh->m_Faces) {
ObjFile::Face* const inp = face;
for (auto &face : pObjMesh->m_Faces) {
ObjFile::Face *const inp = face;
if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
for(size_t i = 0; i < inp->m_vertices.size() - 1; ++i) {
aiFace& f = pMesh->mFaces[ outIndex++ ];
for (size_t i = 0; i < inp->m_vertices.size() - 1; ++i) {
aiFace &f = pMesh->mFaces[outIndex++];
uiIdxCount += f.mNumIndices = 2;
f.mIndices = new unsigned int[2];
}
continue;
}
else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
for(size_t i = 0; i < inp->m_vertices.size(); ++i) {
aiFace& f = pMesh->mFaces[ outIndex++ ];
} else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
for (size_t i = 0; i < inp->m_vertices.size(); ++i) {
aiFace &f = pMesh->mFaces[outIndex++];
uiIdxCount += f.mNumIndices = 1;
f.mIndices = new unsigned int[1];
}
continue;
}
aiFace *pFace = &pMesh->mFaces[ outIndex++ ];
const unsigned int uiNumIndices = (unsigned int) face->m_vertices.size();
uiIdxCount += pFace->mNumIndices = (unsigned int) uiNumIndices;
aiFace *pFace = &pMesh->mFaces[outIndex++];
const unsigned int uiNumIndices = (unsigned int)face->m_vertices.size();
uiIdxCount += pFace->mNumIndices = (unsigned int)uiNumIndices;
if (pFace->mNumIndices > 0) {
pFace->mIndices = new unsigned int[ uiNumIndices ];
pFace->mIndices = new unsigned int[uiNumIndices];
}
}
}
@ -401,46 +394,45 @@ aiMesh *ObjFileImporter::createTopology( const ObjFile::Model* pModel, const Obj
// ------------------------------------------------------------------------------------------------
// Creates a vertex array
void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
const ObjFile::Object* pCurrentObject,
void ObjFileImporter::createVertexArray(const ObjFile::Model *pModel,
const ObjFile::Object *pCurrentObject,
unsigned int uiMeshIndex,
aiMesh* pMesh,
aiMesh *pMesh,
unsigned int numIndices) {
// Checking preconditions
ai_assert( NULL != pCurrentObject );
ai_assert(NULL != pCurrentObject);
// Break, if no faces are stored in object
if ( pCurrentObject->m_Meshes.empty() )
if (pCurrentObject->m_Meshes.empty())
return;
// Get current mesh
ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
if ( NULL == pObjMesh || pObjMesh->m_uiNumIndices < 1 ) {
ObjFile::Mesh *pObjMesh = pModel->m_Meshes[uiMeshIndex];
if (NULL == pObjMesh || pObjMesh->m_uiNumIndices < 1) {
return;
}
// Copy vertices of this mesh instance
pMesh->mNumVertices = numIndices;
if (pMesh->mNumVertices == 0) {
throw DeadlyImportError( "OBJ: no vertices" );
throw DeadlyImportError("OBJ: no vertices");
} else if (pMesh->mNumVertices > AI_MAX_VERTICES) {
throw DeadlyImportError( "OBJ: Too many vertices" );
throw DeadlyImportError("OBJ: Too many vertices");
}
pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
// Allocate buffer for normal vectors
if ( !pModel->m_Normals.empty() && pObjMesh->m_hasNormals )
pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
if (!pModel->m_Normals.empty() && pObjMesh->m_hasNormals)
pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
// Allocate buffer for vertex-color vectors
if ( !pModel->m_VertexColors.empty() )
pMesh->mColors[0] = new aiColor4D[ pMesh->mNumVertices ];
if (!pModel->m_VertexColors.empty())
pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
// Allocate buffer for texture coordinates
if ( !pModel->m_TextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0] )
{
pMesh->mNumUVComponents[ 0 ] = pModel->m_TextureCoordDim;
pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ];
if (!pModel->m_TextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0]) {
pMesh->mNumUVComponents[0] = pModel->m_TextureCoordDim;
pMesh->mTextureCoords[0] = new aiVector3D[pMesh->mNumVertices];
}
// Copy vertices, normals and textures into aiMesh instance
@ -448,60 +440,52 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
unsigned int newIndex = 0, outIndex = 0;
for (auto sourceFace : pObjMesh->m_Faces) {
// Copy all index arrays
for (size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < sourceFace->m_vertices.size(); vertexIndex++ ) {
const unsigned int vertex = sourceFace->m_vertices.at(vertexIndex );
if ( vertex >= pModel->m_Vertices.size() ) {
throw DeadlyImportError( "OBJ: vertex index out of range" );
for (size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < sourceFace->m_vertices.size(); vertexIndex++) {
const unsigned int vertex = sourceFace->m_vertices.at(vertexIndex);
if (vertex >= pModel->m_Vertices.size()) {
throw DeadlyImportError("OBJ: vertex index out of range");
}
if ( pMesh->mNumVertices <= newIndex ) {
if (pMesh->mNumVertices <= newIndex) {
throw DeadlyImportError("OBJ: bad vertex index");
}
pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
pMesh->mVertices[newIndex] = pModel->m_Vertices[vertex];
// Copy all normals
if ( normalsok && !pModel->m_Normals.empty() && vertexIndex < sourceFace->m_normals.size()) {
const unsigned int normal = sourceFace->m_normals.at(vertexIndex );
if ( normal >= pModel->m_Normals.size() )
{
if (normalsok && !pModel->m_Normals.empty() && vertexIndex < sourceFace->m_normals.size()) {
const unsigned int normal = sourceFace->m_normals.at(vertexIndex);
if (normal >= pModel->m_Normals.size()) {
normalsok = false;
}
else
{
pMesh->mNormals[ newIndex ] = pModel->m_Normals[ normal ];
} else {
pMesh->mNormals[newIndex] = pModel->m_Normals[normal];
}
}
// Copy all vertex colors
if ( !pModel->m_VertexColors.empty())
{
const aiVector3D& color = pModel->m_VertexColors[ vertex ];
pMesh->mColors[0][ newIndex ] = aiColor4D(color.x, color.y, color.z, 1.0);
if (!pModel->m_VertexColors.empty()) {
const aiVector3D &color = pModel->m_VertexColors[vertex];
pMesh->mColors[0][newIndex] = aiColor4D(color.x, color.y, color.z, 1.0);
}
// Copy all texture coordinates
if ( uvok && !pModel->m_TextureCoord.empty() && vertexIndex < sourceFace->m_texturCoords.size())
{
const unsigned int tex = sourceFace->m_texturCoords.at(vertexIndex );
if (uvok && !pModel->m_TextureCoord.empty() && vertexIndex < sourceFace->m_texturCoords.size()) {
const unsigned int tex = sourceFace->m_texturCoords.at(vertexIndex);
if ( tex >= pModel->m_TextureCoord.size() )
{
if (tex >= pModel->m_TextureCoord.size()) {
uvok = false;
}
else
{
const aiVector3D &coord3d = pModel->m_TextureCoord[ tex ];
pMesh->mTextureCoords[ 0 ][ newIndex ] = aiVector3D( coord3d.x, coord3d.y, coord3d.z );
} else {
const aiVector3D &coord3d = pModel->m_TextureCoord[tex];
pMesh->mTextureCoords[0][newIndex] = aiVector3D(coord3d.x, coord3d.y, coord3d.z);
}
}
// Get destination face
aiFace *pDestFace = &pMesh->mFaces[ outIndex ];
aiFace *pDestFace = &pMesh->mFaces[outIndex];
const bool last = (vertexIndex == sourceFace->m_vertices.size() - 1 );
const bool last = (vertexIndex == sourceFace->m_vertices.size() - 1);
if (sourceFace->m_PrimitiveType != aiPrimitiveType_LINE || !last) {
pDestFace->mIndices[ outVertexIndex ] = newIndex;
pDestFace->mIndices[outVertexIndex] = newIndex;
outVertexIndex++;
}
@ -511,18 +495,18 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
} else if (sourceFace->m_PrimitiveType == aiPrimitiveType_LINE) {
outVertexIndex = 0;
if(!last)
if (!last)
outIndex++;
if (vertexIndex) {
if(!last) {
pMesh->mVertices[ newIndex+1 ] = pMesh->mVertices[ newIndex ];
if (!last) {
pMesh->mVertices[newIndex + 1] = pMesh->mVertices[newIndex];
if (!sourceFace->m_normals.empty() && !pModel->m_Normals.empty()) {
pMesh->mNormals[ newIndex+1 ] = pMesh->mNormals[newIndex ];
pMesh->mNormals[newIndex + 1] = pMesh->mNormals[newIndex];
}
if ( !pModel->m_TextureCoord.empty() ) {
for ( size_t i=0; i < pMesh->GetNumUVChannels(); i++ ) {
pMesh->mTextureCoords[ i ][ newIndex+1 ] = pMesh->mTextureCoords[ i ][ newIndex ];
if (!pModel->m_TextureCoord.empty()) {
for (size_t i = 0; i < pMesh->GetNumUVChannels(); i++) {
pMesh->mTextureCoords[i][newIndex + 1] = pMesh->mTextureCoords[i][newIndex];
}
}
++newIndex;
@ -530,40 +514,34 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
pDestFace[-1].mIndices[1] = newIndex;
}
}
else if (last) {
} else if (last) {
outIndex++;
}
++newIndex;
}
}
if (!normalsok)
{
delete [] pMesh->mNormals;
if (!normalsok) {
delete[] pMesh->mNormals;
pMesh->mNormals = nullptr;
}
if (!uvok)
{
delete [] pMesh->mTextureCoords[0];
if (!uvok) {
delete[] pMesh->mTextureCoords[0];
pMesh->mTextureCoords[0] = nullptr;
}
}
// ------------------------------------------------------------------------------------------------
// Counts all stored meshes
void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes)
{
void ObjFileImporter::countObjects(const std::vector<ObjFile::Object *> &rObjects, int &iNumMeshes) {
iNumMeshes = 0;
if ( rObjects.empty() )
if (rObjects.empty())
return;
iNumMeshes += static_cast<unsigned int>( rObjects.size() );
for (auto object: rObjects)
{
if (!object->m_SubObjects.empty())
{
iNumMeshes += static_cast<unsigned int>(rObjects.size());
for (auto object : rObjects) {
if (!object->m_SubObjects.empty()) {
countObjects(object->m_SubObjects, iNumMeshes);
}
}
@ -571,48 +549,46 @@ void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects
// ------------------------------------------------------------------------------------------------
// Add clamp mode property to material if necessary
void ObjFileImporter::addTextureMappingModeProperty( aiMaterial* mat, aiTextureType type, int clampMode, int index) {
if ( nullptr == mat ) {
void ObjFileImporter::addTextureMappingModeProperty(aiMaterial *mat, aiTextureType type, int clampMode, int index) {
if (nullptr == mat) {
return;
}
mat->AddProperty<int>( &clampMode, 1, AI_MATKEY_MAPPINGMODE_U( type, index ) );
mat->AddProperty<int>( &clampMode, 1, AI_MATKEY_MAPPINGMODE_V( type, index ) );
mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_U(type, index));
mat->AddProperty<int>(&clampMode, 1, AI_MATKEY_MAPPINGMODE_V(type, index));
}
// ------------------------------------------------------------------------------------------------
// Creates the material
void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pScene ) {
if ( NULL == pScene ) {
void ObjFileImporter::createMaterials(const ObjFile::Model *pModel, aiScene *pScene) {
if (NULL == pScene) {
return;
}
const unsigned int numMaterials = (unsigned int) pModel->m_MaterialLib.size();
const unsigned int numMaterials = (unsigned int)pModel->m_MaterialLib.size();
pScene->mNumMaterials = 0;
if ( pModel->m_MaterialLib.empty() ) {
if (pModel->m_MaterialLib.empty()) {
ASSIMP_LOG_DEBUG("OBJ: no materials specified");
return;
}
pScene->mMaterials = new aiMaterial*[ numMaterials ];
for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ )
{
pScene->mMaterials = new aiMaterial *[numMaterials];
for (unsigned int matIndex = 0; matIndex < numMaterials; matIndex++) {
// Store material name
std::map<std::string, ObjFile::Material*>::const_iterator it;
it = pModel->m_MaterialMap.find( pModel->m_MaterialLib[ matIndex ] );
std::map<std::string, ObjFile::Material *>::const_iterator it;
it = pModel->m_MaterialMap.find(pModel->m_MaterialLib[matIndex]);
// No material found, use the default material
if ( pModel->m_MaterialMap.end() == it )
if (pModel->m_MaterialMap.end() == it)
continue;
aiMaterial* mat = new aiMaterial;
aiMaterial *mat = new aiMaterial;
ObjFile::Material *pCurrentMaterial = (*it).second;
mat->AddProperty( &pCurrentMaterial->MaterialName, AI_MATKEY_NAME );
mat->AddProperty(&pCurrentMaterial->MaterialName, AI_MATKEY_NAME);
// convert illumination model
int sm = 0;
switch (pCurrentMaterial->illumination_model)
{
switch (pCurrentMaterial->illumination_model) {
case 0:
sm = aiShadingMode_NoShading;
break;
@ -627,149 +603,129 @@ void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pSc
ASSIMP_LOG_ERROR("OBJ: unexpected illumination model (0-2 recognized)");
}
mat->AddProperty<int>( &sm, 1, AI_MATKEY_SHADING_MODEL);
mat->AddProperty<int>(&sm, 1, AI_MATKEY_SHADING_MODEL);
// Adding material colors
mat->AddProperty( &pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT );
mat->AddProperty( &pCurrentMaterial->diffuse, 1, AI_MATKEY_COLOR_DIFFUSE );
mat->AddProperty( &pCurrentMaterial->specular, 1, AI_MATKEY_COLOR_SPECULAR );
mat->AddProperty( &pCurrentMaterial->emissive, 1, AI_MATKEY_COLOR_EMISSIVE );
mat->AddProperty( &pCurrentMaterial->shineness, 1, AI_MATKEY_SHININESS );
mat->AddProperty( &pCurrentMaterial->alpha, 1, AI_MATKEY_OPACITY );
mat->AddProperty( &pCurrentMaterial->transparent,1,AI_MATKEY_COLOR_TRANSPARENT);
mat->AddProperty(&pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT);
mat->AddProperty(&pCurrentMaterial->diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
mat->AddProperty(&pCurrentMaterial->specular, 1, AI_MATKEY_COLOR_SPECULAR);
mat->AddProperty(&pCurrentMaterial->emissive, 1, AI_MATKEY_COLOR_EMISSIVE);
mat->AddProperty(&pCurrentMaterial->shineness, 1, AI_MATKEY_SHININESS);
mat->AddProperty(&pCurrentMaterial->alpha, 1, AI_MATKEY_OPACITY);
mat->AddProperty(&pCurrentMaterial->transparent, 1, AI_MATKEY_COLOR_TRANSPARENT);
// Adding refraction index
mat->AddProperty( &pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI );
mat->AddProperty(&pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI);
// Adding textures
const int uvwIndex = 0;
if ( 0 != pCurrentMaterial->texture.length )
{
mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_DIFFUSE(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType])
{
if (0 != pCurrentMaterial->texture.length) {
mat->AddProperty(&pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_DIFFUSE(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDiffuseType]) {
addTextureMappingModeProperty(mat, aiTextureType_DIFFUSE);
}
}
if ( 0 != pCurrentMaterial->textureAmbient.length )
{
mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_AMBIENT(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType])
{
if (0 != pCurrentMaterial->textureAmbient.length) {
mat->AddProperty(&pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_AMBIENT(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureAmbientType]) {
addTextureMappingModeProperty(mat, aiTextureType_AMBIENT);
}
}
if ( 0 != pCurrentMaterial->textureEmissive.length )
{
mat->AddProperty( &pCurrentMaterial->textureEmissive, AI_MATKEY_TEXTURE_EMISSIVE(0));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_EMISSIVE(0) );
if (0 != pCurrentMaterial->textureEmissive.length) {
mat->AddProperty(&pCurrentMaterial->textureEmissive, AI_MATKEY_TEXTURE_EMISSIVE(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_EMISSIVE(0));
}
if ( 0 != pCurrentMaterial->textureSpecular.length )
{
mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_SPECULAR(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType])
{
if (0 != pCurrentMaterial->textureSpecular.length) {
mat->AddProperty(&pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_SPECULAR(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularType]) {
addTextureMappingModeProperty(mat, aiTextureType_SPECULAR);
}
}
if ( 0 != pCurrentMaterial->textureBump.length )
{
mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_HEIGHT(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType])
{
if (0 != pCurrentMaterial->textureBump.length) {
mat->AddProperty(&pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_HEIGHT(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureBumpType]) {
addTextureMappingModeProperty(mat, aiTextureType_HEIGHT);
}
}
if ( 0 != pCurrentMaterial->textureNormal.length )
{
mat->AddProperty( &pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_NORMALS(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType])
{
if (0 != pCurrentMaterial->textureNormal.length) {
mat->AddProperty(&pCurrentMaterial->textureNormal, AI_MATKEY_TEXTURE_NORMALS(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_NORMALS(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureNormalType]) {
addTextureMappingModeProperty(mat, aiTextureType_NORMALS);
}
}
if( 0 != pCurrentMaterial->textureReflection[0].length )
{
if (0 != pCurrentMaterial->textureReflection[0].length) {
ObjFile::Material::TextureType type = 0 != pCurrentMaterial->textureReflection[1].length ?
ObjFile::Material::TextureReflectionCubeTopType :
ObjFile::Material::TextureReflectionSphereType;
unsigned count = type == ObjFile::Material::TextureReflectionSphereType ? 1 : 6;
for( unsigned i = 0; i < count; i++ )
{
for (unsigned i = 0; i < count; i++) {
mat->AddProperty(&pCurrentMaterial->textureReflection[i], AI_MATKEY_TEXTURE_REFLECTION(i));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_REFLECTION(i) );
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_REFLECTION(i));
if(pCurrentMaterial->clamp[type])
if (pCurrentMaterial->clamp[type])
addTextureMappingModeProperty(mat, aiTextureType_REFLECTION, 1, i);
}
}
if ( 0 != pCurrentMaterial->textureDisp.length )
{
mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_DISPLACEMENT(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType])
{
if (0 != pCurrentMaterial->textureDisp.length) {
mat->AddProperty(&pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_DISPLACEMENT(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureDispType]) {
addTextureMappingModeProperty(mat, aiTextureType_DISPLACEMENT);
}
}
if ( 0 != pCurrentMaterial->textureOpacity.length )
{
mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_OPACITY(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType])
{
if (0 != pCurrentMaterial->textureOpacity.length) {
mat->AddProperty(&pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_OPACITY(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureOpacityType]) {
addTextureMappingModeProperty(mat, aiTextureType_OPACITY);
}
}
if ( 0 != pCurrentMaterial->textureSpecularity.length )
{
mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
mat->AddProperty( &uvwIndex, 1, AI_MATKEY_UVWSRC_SHININESS(0) );
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType])
{
if (0 != pCurrentMaterial->textureSpecularity.length) {
mat->AddProperty(&pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
mat->AddProperty(&uvwIndex, 1, AI_MATKEY_UVWSRC_SHININESS(0));
if (pCurrentMaterial->clamp[ObjFile::Material::TextureSpecularityType]) {
addTextureMappingModeProperty(mat, aiTextureType_SHININESS);
}
}
// Store material property info in material array in scene
pScene->mMaterials[ pScene->mNumMaterials ] = mat;
pScene->mMaterials[pScene->mNumMaterials] = mat;
pScene->mNumMaterials++;
}
// Test number of created materials.
ai_assert( pScene->mNumMaterials == numMaterials );
ai_assert(pScene->mNumMaterials == numMaterials);
}
// ------------------------------------------------------------------------------------------------
// Appends this node to the parent node
void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild)
{
void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild) {
// Checking preconditions
ai_assert( NULL != pParent );
ai_assert( NULL != pChild );
ai_assert(NULL != pParent);
ai_assert(NULL != pChild);
// Assign parent to child
pChild->mParent = pParent;
// Copy node instances into parent node
pParent->mNumChildren++;
pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
pParent->mChildren[pParent->mNumChildren - 1] = pChild;
}
// ------------------------------------------------------------------------------------------------

View File

@ -4,7 +4,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2020, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@ -52,9 +51,9 @@ struct aiNode;
namespace Assimp {
namespace ObjFile {
struct Object;
struct Model;
}
struct Object;
struct Model;
} // namespace ObjFile
// ------------------------------------------------------------------------------------------------
/// \class ObjFileImporter
@ -71,38 +70,38 @@ public:
public:
/// \brief Returns whether the class can handle the format of the given file.
/// \remark See BaseImporter::CanRead() for details.
bool CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const;
bool CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const;
private:
//! \brief Appends the supported extension.
const aiImporterDesc* GetInfo () const;
const aiImporterDesc *GetInfo() const;
//! \brief File import implementation.
void InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
void InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler);
//! \brief Create the data from imported content.
void CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene);
void CreateDataFromImport(const ObjFile::Model *pModel, aiScene *pScene);
//! \brief Creates all nodes stored in imported content.
aiNode *createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pData,
aiNode *pParent, aiScene* pScene, std::vector<aiMesh*> &MeshArray);
aiNode *createNodes(const ObjFile::Model *pModel, const ObjFile::Object *pData,
aiNode *pParent, aiScene *pScene, std::vector<aiMesh *> &MeshArray);
//! \brief Creates topology data like faces and meshes for the geometry.
aiMesh *createTopology( const ObjFile::Model* pModel, const ObjFile::Object* pData,
unsigned int uiMeshIndex );
aiMesh *createTopology(const ObjFile::Model *pModel, const ObjFile::Object *pData,
unsigned int uiMeshIndex);
//! \brief Creates vertices from model.
void createVertexArray(const ObjFile::Model* pModel, const ObjFile::Object* pCurrentObject,
unsigned int uiMeshIndex, aiMesh* pMesh, unsigned int numIndices );
void createVertexArray(const ObjFile::Model *pModel, const ObjFile::Object *pCurrentObject,
unsigned int uiMeshIndex, aiMesh *pMesh, unsigned int numIndices);
//! \brief Object counter helper method.
void countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes);
void countObjects(const std::vector<ObjFile::Object *> &rObjects, int &iNumMeshes);
//! \brief Material creation.
void createMaterials(const ObjFile::Model* pModel, aiScene* pScene);
void createMaterials(const ObjFile::Model *pModel, aiScene *pScene);
/// @brief Adds special property for the used texture mapping mode of the model.
void addTextureMappingModeProperty(aiMaterial* mat, aiTextureType type, int clampMode = 1, int index = 0);
void addTextureMappingModeProperty(aiMaterial *mat, aiTextureType type, int clampMode = 1, int index = 0);
//! \brief Appends a child node to a parent node and updates the data structures.
void appendChildToParentNode(aiNode *pParent, aiNode *pChild);

View File

@ -5,8 +5,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2020, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@ -41,16 +39,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
#ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
#include <stdlib.h>
#include "ObjFileMtlImporter.h"
#include "ObjTools.h"
#include "ObjFileData.h"
#include <assimp/fast_atof.h>
#include "ObjTools.h"
#include <assimp/ParsingUtils.h>
#include <assimp/fast_atof.h>
#include <assimp/material.h>
#include <stdlib.h>
#include <assimp/DefaultLogger.hpp>
namespace Assimp {
@ -86,113 +83,92 @@ static const std::string TypeOption = "-type";
// -------------------------------------------------------------------
// Constructor
ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
ObjFileMtlImporter::ObjFileMtlImporter(std::vector<char> &buffer,
const std::string &,
ObjFile::Model *pModel ) :
m_DataIt( buffer.begin() ),
m_DataItEnd( buffer.end() ),
m_pModel( pModel ),
m_uiLine( 0 )
{
ai_assert( NULL != m_pModel );
if ( NULL == m_pModel->m_pDefaultMaterial )
{
ObjFile::Model *pModel) :
m_DataIt(buffer.begin()),
m_DataItEnd(buffer.end()),
m_pModel(pModel),
m_uiLine(0) {
ai_assert(NULL != m_pModel);
if (NULL == m_pModel->m_pDefaultMaterial) {
m_pModel->m_pDefaultMaterial = new ObjFile::Material;
m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" );
m_pModel->m_pDefaultMaterial->MaterialName.Set("default");
}
load();
}
// -------------------------------------------------------------------
// Destructor
ObjFileMtlImporter::~ObjFileMtlImporter()
{
ObjFileMtlImporter::~ObjFileMtlImporter() {
// empty
}
// -------------------------------------------------------------------
// Private copy constructor
ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter & )
{
ObjFileMtlImporter::ObjFileMtlImporter(const ObjFileMtlImporter &) {
// empty
}
// -------------------------------------------------------------------
// Private copy constructor
ObjFileMtlImporter &ObjFileMtlImporter::operator = ( const ObjFileMtlImporter & )
{
ObjFileMtlImporter &ObjFileMtlImporter::operator=(const ObjFileMtlImporter &) {
return *this;
}
// -------------------------------------------------------------------
// Loads the material description
void ObjFileMtlImporter::load()
{
if ( m_DataIt == m_DataItEnd )
void ObjFileMtlImporter::load() {
if (m_DataIt == m_DataItEnd)
return;
while ( m_DataIt != m_DataItEnd )
{
switch (*m_DataIt)
{
while (m_DataIt != m_DataItEnd) {
switch (*m_DataIt) {
case 'k':
case 'K':
{
case 'K': {
++m_DataIt;
if (*m_DataIt == 'a') // Ambient color
{
++m_DataIt;
getColorRGBA( &m_pModel->m_pCurrentMaterial->ambient );
}
else if (*m_DataIt == 'd') // Diffuse color
getColorRGBA(&m_pModel->m_pCurrentMaterial->ambient);
} else if (*m_DataIt == 'd') // Diffuse color
{
++m_DataIt;
getColorRGBA( &m_pModel->m_pCurrentMaterial->diffuse );
}
else if (*m_DataIt == 's')
{
getColorRGBA(&m_pModel->m_pCurrentMaterial->diffuse);
} else if (*m_DataIt == 's') {
++m_DataIt;
getColorRGBA( &m_pModel->m_pCurrentMaterial->specular );
}
else if (*m_DataIt == 'e')
{
getColorRGBA(&m_pModel->m_pCurrentMaterial->specular);
} else if (*m_DataIt == 'e') {
++m_DataIt;
getColorRGBA( &m_pModel->m_pCurrentMaterial->emissive );
getColorRGBA(&m_pModel->m_pCurrentMaterial->emissive);
}
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
}
break;
case 'T':
{
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
} break;
case 'T': {
++m_DataIt;
if (*m_DataIt == 'f') // Material transmission
{
++m_DataIt;
getColorRGBA( &m_pModel->m_pCurrentMaterial->transparent);
getColorRGBA(&m_pModel->m_pCurrentMaterial->transparent);
}
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
}
break;
case 'd':
{
if( *(m_DataIt+1) == 'i' && *( m_DataIt + 2 ) == 's' && *( m_DataIt + 3 ) == 'p' ) {
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
} break;
case 'd': {
if (*(m_DataIt + 1) == 'i' && *(m_DataIt + 2) == 's' && *(m_DataIt + 3) == 'p') {
// A displacement map
getTexture();
} else {
// Alpha value
++m_DataIt;
getFloatValue( m_pModel->m_pCurrentMaterial->alpha );
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
getFloatValue(m_pModel->m_pCurrentMaterial->alpha);
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
}
break;
} break;
case 'N':
case 'n':
{
case 'n': {
++m_DataIt;
switch(*m_DataIt)
{
switch (*m_DataIt) {
case 's': // Specular exponent
++m_DataIt;
getFloatValue(m_pModel->m_pCurrentMaterial->shineness);
@ -205,50 +181,44 @@ void ObjFileMtlImporter::load()
createMaterial();
break;
}
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
}
break;
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
} break;
case 'm': // Texture
case 'b': // quick'n'dirty - for 'bump' sections
case 'r': // quick'n'dirty - for 'refl' sections
{
getTexture();
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
}
break;
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
} break;
case 'i': // Illumination model
{
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
getIlluminationModel( m_pModel->m_pCurrentMaterial->illumination_model );
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
}
break;
getIlluminationModel(m_pModel->m_pCurrentMaterial->illumination_model);
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
} break;
default:
{
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
}
break;
default: {
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
} break;
}
}
}
// -------------------------------------------------------------------
// Loads a color definition
void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
{
ai_assert( NULL != pColor );
void ObjFileMtlImporter::getColorRGBA(aiColor3D *pColor) {
ai_assert(NULL != pColor);
ai_real r( 0.0 ), g( 0.0 ), b( 0.0 );
m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, r );
ai_real r(0.0), g(0.0), b(0.0);
m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, r);
pColor->r = r;
// we have to check if color is default 0 with only one token
if( !IsLineEnd( *m_DataIt ) ) {
m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, g );
m_DataIt = getFloat<DataArrayIt>( m_DataIt, m_DataItEnd, b );
if (!IsLineEnd(*m_DataIt)) {
m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, g);
m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, b);
}
pColor->g = g;
pColor->b = b;
@ -256,34 +226,31 @@ void ObjFileMtlImporter::getColorRGBA( aiColor3D *pColor )
// -------------------------------------------------------------------
// Loads the kind of illumination model.
void ObjFileMtlImporter::getIlluminationModel( int &illum_model )
{
m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
void ObjFileMtlImporter::getIlluminationModel(int &illum_model) {
m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE);
illum_model = atoi(m_buffer);
}
// -------------------------------------------------------------------
// Loads a single float value.
void ObjFileMtlImporter::getFloatValue( ai_real &value )
{
m_DataIt = CopyNextWord<DataArrayIt>( m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE );
value = (ai_real) fast_atof(m_buffer);
void ObjFileMtlImporter::getFloatValue(ai_real &value) {
m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, m_buffer, BUFFERSIZE);
value = (ai_real)fast_atof(m_buffer);
}
// -------------------------------------------------------------------
// Creates a material from loaded data.
void ObjFileMtlImporter::createMaterial()
{
void ObjFileMtlImporter::createMaterial() {
std::string line;
while( !IsLineEnd( *m_DataIt ) ) {
while (!IsLineEnd(*m_DataIt)) {
line += *m_DataIt;
++m_DataIt;
}
std::vector<std::string> token;
const unsigned int numToken = tokenize<std::string>( line, token, " \t" );
const unsigned int numToken = tokenize<std::string>(line, token, " \t");
std::string name;
if ( numToken == 1 ) {
if (numToken == 1) {
name = AI_DEFAULT_MATERIAL_NAME;
} else {
// skip newmtl and all following white spaces
@ -296,13 +263,13 @@ void ObjFileMtlImporter::createMaterial()
name = trim_whitespaces(name);
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( name );
if ( m_pModel->m_MaterialMap.end() == it) {
std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->m_MaterialMap.find(name);
if (m_pModel->m_MaterialMap.end() == it) {
// New Material created
m_pModel->m_pCurrentMaterial = new ObjFile::Material();
m_pModel->m_pCurrentMaterial->MaterialName.Set( name );
m_pModel->m_MaterialLib.push_back( name );
m_pModel->m_MaterialMap[ name ] = m_pModel->m_pCurrentMaterial;
m_pModel->m_pCurrentMaterial->MaterialName.Set(name);
m_pModel->m_MaterialLib.push_back(name);
m_pModel->m_MaterialMap[name] = m_pModel->m_pCurrentMaterial;
if (m_pModel->m_pCurrentMesh) {
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = static_cast<unsigned int>(m_pModel->m_MaterialLib.size() - 1);
@ -316,52 +283,52 @@ void ObjFileMtlImporter::createMaterial()
// -------------------------------------------------------------------
// Gets a texture name from data.
void ObjFileMtlImporter::getTexture() {
aiString *out( NULL );
aiString *out(NULL);
int clampIndex = -1;
const char *pPtr( &(*m_DataIt) );
if ( !ASSIMP_strincmp( pPtr, DiffuseTexture.c_str(), static_cast<unsigned int>(DiffuseTexture.size()) ) ) {
const char *pPtr(&(*m_DataIt));
if (!ASSIMP_strincmp(pPtr, DiffuseTexture.c_str(), static_cast<unsigned int>(DiffuseTexture.size()))) {
// Diffuse texture
out = & m_pModel->m_pCurrentMaterial->texture;
out = &m_pModel->m_pCurrentMaterial->texture;
clampIndex = ObjFile::Material::TextureDiffuseType;
} else if ( !ASSIMP_strincmp( pPtr,AmbientTexture.c_str(), static_cast<unsigned int>(AmbientTexture.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, AmbientTexture.c_str(), static_cast<unsigned int>(AmbientTexture.size()))) {
// Ambient texture
out = & m_pModel->m_pCurrentMaterial->textureAmbient;
out = &m_pModel->m_pCurrentMaterial->textureAmbient;
clampIndex = ObjFile::Material::TextureAmbientType;
} else if ( !ASSIMP_strincmp( pPtr, SpecularTexture.c_str(), static_cast<unsigned int>(SpecularTexture.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, SpecularTexture.c_str(), static_cast<unsigned int>(SpecularTexture.size()))) {
// Specular texture
out = & m_pModel->m_pCurrentMaterial->textureSpecular;
out = &m_pModel->m_pCurrentMaterial->textureSpecular;
clampIndex = ObjFile::Material::TextureSpecularType;
} else if ( !ASSIMP_strincmp( pPtr, DisplacementTexture1.c_str(), static_cast<unsigned int>(DisplacementTexture1.size()) ) ||
!ASSIMP_strincmp( pPtr, DisplacementTexture2.c_str(), static_cast<unsigned int>(DisplacementTexture2.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, DisplacementTexture1.c_str(), static_cast<unsigned int>(DisplacementTexture1.size())) ||
!ASSIMP_strincmp(pPtr, DisplacementTexture2.c_str(), static_cast<unsigned int>(DisplacementTexture2.size()))) {
// Displacement texture
out = &m_pModel->m_pCurrentMaterial->textureDisp;
clampIndex = ObjFile::Material::TextureDispType;
} else if ( !ASSIMP_strincmp( pPtr, OpacityTexture.c_str(), static_cast<unsigned int>(OpacityTexture.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, OpacityTexture.c_str(), static_cast<unsigned int>(OpacityTexture.size()))) {
// Opacity texture
out = & m_pModel->m_pCurrentMaterial->textureOpacity;
out = &m_pModel->m_pCurrentMaterial->textureOpacity;
clampIndex = ObjFile::Material::TextureOpacityType;
} else if ( !ASSIMP_strincmp( pPtr, EmissiveTexture1.c_str(), static_cast<unsigned int>(EmissiveTexture1.size()) ) ||
!ASSIMP_strincmp( pPtr, EmissiveTexture2.c_str(), static_cast<unsigned int>(EmissiveTexture2.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, EmissiveTexture1.c_str(), static_cast<unsigned int>(EmissiveTexture1.size())) ||
!ASSIMP_strincmp(pPtr, EmissiveTexture2.c_str(), static_cast<unsigned int>(EmissiveTexture2.size()))) {
// Emissive texture
out = & m_pModel->m_pCurrentMaterial->textureEmissive;
out = &m_pModel->m_pCurrentMaterial->textureEmissive;
clampIndex = ObjFile::Material::TextureEmissiveType;
} else if ( !ASSIMP_strincmp( pPtr, BumpTexture1.c_str(), static_cast<unsigned int>(BumpTexture1.size()) ) ||
!ASSIMP_strincmp( pPtr, BumpTexture2.c_str(), static_cast<unsigned int>(BumpTexture2.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, BumpTexture1.c_str(), static_cast<unsigned int>(BumpTexture1.size())) ||
!ASSIMP_strincmp(pPtr, BumpTexture2.c_str(), static_cast<unsigned int>(BumpTexture2.size()))) {
// Bump texture
out = & m_pModel->m_pCurrentMaterial->textureBump;
out = &m_pModel->m_pCurrentMaterial->textureBump;
clampIndex = ObjFile::Material::TextureBumpType;
} else if ( !ASSIMP_strincmp( pPtr,NormalTexture.c_str(), static_cast<unsigned int>(NormalTexture.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, NormalTexture.c_str(), static_cast<unsigned int>(NormalTexture.size()))) {
// Normal map
out = & m_pModel->m_pCurrentMaterial->textureNormal;
out = &m_pModel->m_pCurrentMaterial->textureNormal;
clampIndex = ObjFile::Material::TextureNormalType;
} else if( !ASSIMP_strincmp( pPtr, ReflectionTexture.c_str(), static_cast<unsigned int>(ReflectionTexture.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, ReflectionTexture.c_str(), static_cast<unsigned int>(ReflectionTexture.size()))) {
// Reflection texture(s)
//Do nothing here
return;
} else if ( !ASSIMP_strincmp( pPtr, SpecularityTexture.c_str(), static_cast<unsigned int>(SpecularityTexture.size()) ) ) {
} else if (!ASSIMP_strincmp(pPtr, SpecularityTexture.c_str(), static_cast<unsigned int>(SpecularityTexture.size()))) {
// Specularity scaling (glossiness)
out = & m_pModel->m_pCurrentMaterial->textureSpecularity;
out = &m_pModel->m_pCurrentMaterial->textureSpecularity;
clampIndex = ObjFile::Material::TextureSpecularityType;
} else {
ASSIMP_LOG_ERROR("OBJ/MTL: Encountered unknown texture type");
@ -373,9 +340,9 @@ void ObjFileMtlImporter::getTexture() {
m_pModel->m_pCurrentMaterial->clamp[clampIndex] = clamp;
std::string texture;
m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, texture );
if ( NULL!=out ) {
out->Set( texture );
m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, texture);
if (NULL != out) {
out->Set(texture);
}
}
@ -398,90 +365,57 @@ void ObjFileMtlImporter::getTextureOption(bool &clamp, int &clampIndex, aiString
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
// If there is any more texture option
while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-')
{
const char *pPtr( &(*m_DataIt) );
while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-') {
const char *pPtr(&(*m_DataIt));
//skip option key and value
int skipToken = 1;
if (!ASSIMP_strincmp(pPtr, ClampOption.c_str(), static_cast<unsigned int>(ClampOption.size())))
{
if (!ASSIMP_strincmp(pPtr, ClampOption.c_str(), static_cast<unsigned int>(ClampOption.size()))) {
DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
char value[3];
CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
if (!ASSIMP_strincmp(value, "on", 2))
{
if (!ASSIMP_strincmp(value, "on", 2)) {
clamp = true;
}
skipToken = 2;
}
else if( !ASSIMP_strincmp( pPtr, TypeOption.c_str(), static_cast<unsigned int>(TypeOption.size()) ) )
{
DataArrayIt it = getNextToken<DataArrayIt>( m_DataIt, m_DataItEnd );
char value[ 12 ];
CopyNextWord( it, m_DataItEnd, value, sizeof( value ) / sizeof( *value ) );
if( !ASSIMP_strincmp( value, "cube_top", 8 ) )
{
} else if (!ASSIMP_strincmp(pPtr, TypeOption.c_str(), static_cast<unsigned int>(TypeOption.size()))) {
DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
char value[12];
CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
if (!ASSIMP_strincmp(value, "cube_top", 8)) {
clampIndex = ObjFile::Material::TextureReflectionCubeTopType;
out = &m_pModel->m_pCurrentMaterial->textureReflection[0];
}
else if( !ASSIMP_strincmp( value, "cube_bottom", 11 ) )
{
} else if (!ASSIMP_strincmp(value, "cube_bottom", 11)) {
clampIndex = ObjFile::Material::TextureReflectionCubeBottomType;
out = &m_pModel->m_pCurrentMaterial->textureReflection[1];
}
else if( !ASSIMP_strincmp( value, "cube_front", 10 ) )
{
} else if (!ASSIMP_strincmp(value, "cube_front", 10)) {
clampIndex = ObjFile::Material::TextureReflectionCubeFrontType;
out = &m_pModel->m_pCurrentMaterial->textureReflection[2];
}
else if( !ASSIMP_strincmp( value, "cube_back", 9 ) )
{
} else if (!ASSIMP_strincmp(value, "cube_back", 9)) {
clampIndex = ObjFile::Material::TextureReflectionCubeBackType;
out = &m_pModel->m_pCurrentMaterial->textureReflection[3];
}
else if( !ASSIMP_strincmp( value, "cube_left", 9 ) )
{
} else if (!ASSIMP_strincmp(value, "cube_left", 9)) {
clampIndex = ObjFile::Material::TextureReflectionCubeLeftType;
out = &m_pModel->m_pCurrentMaterial->textureReflection[4];
}
else if( !ASSIMP_strincmp( value, "cube_right", 10 ) )
{
} else if (!ASSIMP_strincmp(value, "cube_right", 10)) {
clampIndex = ObjFile::Material::TextureReflectionCubeRightType;
out = &m_pModel->m_pCurrentMaterial->textureReflection[5];
}
else if( !ASSIMP_strincmp( value, "sphere", 6 ) )
{
} else if (!ASSIMP_strincmp(value, "sphere", 6)) {
clampIndex = ObjFile::Material::TextureReflectionSphereType;
out = &m_pModel->m_pCurrentMaterial->textureReflection[0];
}
skipToken = 2;
}
else if (!ASSIMP_strincmp(pPtr, BlendUOption.c_str(), static_cast<unsigned int>(BlendUOption.size()))
|| !ASSIMP_strincmp(pPtr, BlendVOption.c_str(), static_cast<unsigned int>(BlendVOption.size()))
|| !ASSIMP_strincmp(pPtr, BoostOption.c_str(), static_cast<unsigned int>(BoostOption.size()))
|| !ASSIMP_strincmp(pPtr, ResolutionOption.c_str(), static_cast<unsigned int>(ResolutionOption.size()))
|| !ASSIMP_strincmp(pPtr, BumpOption.c_str(), static_cast<unsigned int>(BumpOption.size()))
|| !ASSIMP_strincmp(pPtr, ChannelOption.c_str(), static_cast<unsigned int>(ChannelOption.size())))
{
} else if (!ASSIMP_strincmp(pPtr, BlendUOption.c_str(), static_cast<unsigned int>(BlendUOption.size())) || !ASSIMP_strincmp(pPtr, BlendVOption.c_str(), static_cast<unsigned int>(BlendVOption.size())) || !ASSIMP_strincmp(pPtr, BoostOption.c_str(), static_cast<unsigned int>(BoostOption.size())) || !ASSIMP_strincmp(pPtr, ResolutionOption.c_str(), static_cast<unsigned int>(ResolutionOption.size())) || !ASSIMP_strincmp(pPtr, BumpOption.c_str(), static_cast<unsigned int>(BumpOption.size())) || !ASSIMP_strincmp(pPtr, ChannelOption.c_str(), static_cast<unsigned int>(ChannelOption.size()))) {
skipToken = 2;
}
else if (!ASSIMP_strincmp(pPtr, ModifyMapOption.c_str(), static_cast<unsigned int>(ModifyMapOption.size())))
{
} else if (!ASSIMP_strincmp(pPtr, ModifyMapOption.c_str(), static_cast<unsigned int>(ModifyMapOption.size()))) {
skipToken = 3;
}
else if ( !ASSIMP_strincmp(pPtr, OffsetOption.c_str(), static_cast<unsigned int>(OffsetOption.size()))
|| !ASSIMP_strincmp(pPtr, ScaleOption.c_str(), static_cast<unsigned int>(ScaleOption.size()))
|| !ASSIMP_strincmp(pPtr, TurbulenceOption.c_str(), static_cast<unsigned int>(TurbulenceOption.size()))
)
{
} else if (!ASSIMP_strincmp(pPtr, OffsetOption.c_str(), static_cast<unsigned int>(OffsetOption.size())) || !ASSIMP_strincmp(pPtr, ScaleOption.c_str(), static_cast<unsigned int>(ScaleOption.size())) || !ASSIMP_strincmp(pPtr, TurbulenceOption.c_str(), static_cast<unsigned int>(TurbulenceOption.size()))) {
skipToken = 4;
}
for (int i = 0; i < skipToken; ++i)
{
for (int i = 0; i < skipToken; ++i) {
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
}
}

View File

@ -4,7 +4,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2020, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@ -41,9 +40,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef OBJFILEMTLIMPORTER_H_INC
#define OBJFILEMTLIMPORTER_H_INC
#include <vector>
#include <string>
#include <assimp/defs.h>
#include <string>
#include <vector>
struct aiColor3D;
struct aiString;
@ -51,17 +50,15 @@ struct aiString;
namespace Assimp {
namespace ObjFile {
struct Model;
struct Material;
}
struct Model;
struct Material;
} // namespace ObjFile
/**
* @class ObjFileMtlImporter
* @brief Loads the material description from a mtl file.
*/
class ObjFileMtlImporter
{
class ObjFileMtlImporter {
public:
static const size_t BUFFERSIZE = 2048;
typedef std::vector<char> DataArray;
@ -70,8 +67,8 @@ public:
public:
//! \brief Default constructor
ObjFileMtlImporter( std::vector<char> &buffer, const std::string &strAbsPath,
ObjFile::Model *pModel );
ObjFileMtlImporter(std::vector<char> &buffer, const std::string &strAbsPath,
ObjFile::Model *pModel);
//! \brief DEstructor
~ObjFileMtlImporter();
@ -80,15 +77,15 @@ private:
/// Copy constructor, empty.
ObjFileMtlImporter(const ObjFileMtlImporter &rOther);
/// \brief Assignment operator, returns only a reference of this instance.
ObjFileMtlImporter &operator = (const ObjFileMtlImporter &rOther);
ObjFileMtlImporter &operator=(const ObjFileMtlImporter &rOther);
/// Load the whole material description
void load();
/// Get color data.
void getColorRGBA( aiColor3D *pColor);
void getColorRGBA(aiColor3D *pColor);
/// Get illumination model from loaded data
void getIlluminationModel( int &illum_model );
void getIlluminationModel(int &illum_model);
/// Gets a float value from data.
void getFloatValue( ai_real &value );
void getFloatValue(ai_real &value);
/// Creates a new material from loaded data.
void createMaterial();
/// Get texture name from loaded data.

View File

@ -5,8 +5,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2020, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@ -43,14 +41,14 @@ 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 "ObjFileData.h"
#include <assimp/ParsingUtils.h>
#include <assimp/BaseImporter.h>
#include <assimp/DefaultIOSystem.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/ParsingUtils.h>
#include <assimp/material.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/Importer.hpp>
#include <cstdlib>
@ -58,29 +56,22 @@ namespace Assimp {
const std::string ObjFileParser::DEFAULT_MATERIAL = AI_DEFAULT_MATERIAL_NAME;
ObjFileParser::ObjFileParser()
: m_DataIt()
, m_DataItEnd()
, m_pModel( nullptr )
, m_uiLine( 0 )
, m_pIO( nullptr )
, m_progress( nullptr )
, m_originalObjFileName() {
ObjFileParser::ObjFileParser() :
m_DataIt(), m_DataItEnd(), m_pModel(nullptr), m_uiLine(0), m_pIO(nullptr), m_progress(nullptr), m_originalObjFileName() {
// empty
}
ObjFileParser::ObjFileParser( IOStreamBuffer<char> &streamBuffer, const std::string &modelName,
IOSystem *io, ProgressHandler* progress,
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),
m_pIO( io ),
m_pIO(io),
m_progress(progress),
m_originalObjFileName(originalObjFileName)
{
std::fill_n(m_buffer,Buffersize,'\0');
m_originalObjFileName(originalObjFileName) {
std::fill_n(m_buffer, Buffersize, '\0');
// Create the model instance to store all the data
m_pModel.reset(new ObjFile::Model());
@ -88,18 +79,18 @@ ObjFileParser::ObjFileParser( IOStreamBuffer<char> &streamBuffer, const std::str
// create default material and store it
m_pModel->m_pDefaultMaterial = new ObjFile::Material;
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;
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;
// Start parsing the file
parseFile( streamBuffer );
parseFile(streamBuffer);
}
ObjFileParser::~ObjFileParser() {
}
void ObjFileParser::setBuffer( std::vector<char> &buffer ) {
void ObjFileParser::setBuffer(std::vector<char> &buffer) {
m_DataIt = buffer.begin();
m_DataItEnd = buffer.end();
}
@ -108,27 +99,27 @@ ObjFile::Model *ObjFileParser::GetModel() const {
return m_pModel.get();
}
void ObjFileParser::parseFile( IOStreamBuffer<char> &streamBuffer ) {
void ObjFileParser::parseFile(IOStreamBuffer<char> &streamBuffer) {
// only update every 100KB or it'll be too slow
//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;
size_t lastFilePos( 0 );
size_t lastFilePos(0);
std::vector<char> buffer;
while ( streamBuffer.getNextDataLine( buffer, '\\' ) ) {
while (streamBuffer.getNextDataLine(buffer, '\\')) {
m_DataIt = buffer.begin();
m_DataItEnd = buffer.end();
// Handle progress reporting
const size_t filePos( streamBuffer.getFilePos() );
if ( lastFilePos < filePos ) {
const size_t filePos(streamBuffer.getFilePos());
if (lastFilePos < filePos) {
processed = static_cast<unsigned int>(filePos);
lastFilePos = filePos;
progressCounter++;
m_progress->UpdateFileRead( processed, progressTotal );
m_progress->UpdateFileRead(processed, progressTotal);
}
// parse line
@ -156,25 +147,20 @@ void ObjFileParser::parseFile( IOStreamBuffer<char> &streamBuffer ) {
} else if (*m_DataIt == 'n') {
// Read in normal vector definition
++m_DataIt;
getVector3( m_pModel->m_Normals );
getVector3(m_pModel->m_Normals);
}
}
break;
} 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 'f': {
getFace(*m_DataIt == 'f' ? aiPrimitiveType_POLYGON : (*m_DataIt == 'l' ? aiPrimitiveType_LINE : aiPrimitiveType_POINT));
} break;
case '#': // Parse a comment
{
getComment();
}
break;
} break;
case 'u': // Parse a material desc. setter
{
@ -186,12 +172,10 @@ void ObjFileParser::parseFile( IOStreamBuffer<char> &streamBuffer ) {
if (nextSpace != std::string::npos)
name = name.substr(0, nextSpace);
if(name == "usemtl")
{
if (name == "usemtl") {
getMaterialDesc();
}
}
break;
} break;
case 'm': // Parse a material library or merging group ('mg')
{
@ -205,37 +189,31 @@ void ObjFileParser::parseFile( IOStreamBuffer<char> &streamBuffer ) {
if (name == "mg")
getGroupNumberAndResolution();
else if(name == "mtllib")
else if (name == "mtllib")
getMaterialLib();
else
goto pf_skip_line;
}
break;
} break;
case 'g': // Parse group name
{
getGroupName();
}
break;
} break;
case 's': // Parse group number
{
getGroupNumber();
}
break;
} break;
case 'o': // Parse object name
{
getObjectName();
}
break;
} break;
default:
{
pf_skip_line:
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
}
break;
default: {
pf_skip_line:
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
} break;
}
}
}
@ -243,15 +221,15 @@ pf_skip_line:
void ObjFileParser::copyNextWord(char *pBuffer, size_t length) {
size_t index = 0;
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
if ( *m_DataIt == '\\' ) {
if (*m_DataIt == '\\') {
++m_DataIt;
++m_DataIt;
m_DataIt = getNextWord<DataArrayIt>( m_DataIt, m_DataItEnd );
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
}
while( m_DataIt != m_DataItEnd && !IsSpaceOrNewLine( *m_DataIt ) ) {
while (m_DataIt != m_DataItEnd && !IsSpaceOrNewLine(*m_DataIt)) {
pBuffer[index] = *m_DataIt;
index++;
if( index == length - 1 ) {
if (index == length - 1) {
break;
}
++m_DataIt;
@ -261,10 +239,10 @@ void ObjFileParser::copyNextWord(char *pBuffer, size_t length) {
pBuffer[index] = '\0';
}
static bool isDataDefinitionEnd( const char *tmp ) {
if ( *tmp == '\\' ) {
static bool isDataDefinitionEnd(const char *tmp) {
if (*tmp == '\\') {
tmp++;
if ( IsLineEnd( *tmp ) ) {
if (IsLineEnd(*tmp)) {
tmp++;
return true;
}
@ -272,58 +250,58 @@ static bool isDataDefinitionEnd( const char *tmp ) {
return false;
}
static bool isNanOrInf(const char * in) {
static bool isNanOrInf(const char *in) {
// 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);
}
size_t ObjFileParser::getNumComponentsInDataDefinition() {
size_t numComponents( 0 );
const char* tmp( &m_DataIt[0] );
size_t numComponents(0);
const char *tmp(&m_DataIt[0]);
bool end_of_definition = false;
while ( !end_of_definition ) {
if ( isDataDefinitionEnd( tmp ) ) {
while (!end_of_definition) {
if (isDataDefinitionEnd(tmp)) {
tmp += 2;
} else if ( IsLineEnd( *tmp ) ) {
} else if (IsLineEnd(*tmp)) {
end_of_definition = true;
}
if ( !SkipSpaces( &tmp ) ) {
if (!SkipSpaces(&tmp)) {
break;
}
const bool isNum( IsNumeric( *tmp ) || isNanOrInf(tmp));
SkipToken( tmp );
if ( isNum ) {
const bool isNum(IsNumeric(*tmp) || isNanOrInf(tmp));
SkipToken(tmp);
if (isNum) {
++numComponents;
}
if ( !SkipSpaces( &tmp ) ) {
if (!SkipSpaces(&tmp)) {
break;
}
}
return numComponents;
}
size_t ObjFileParser::getTexCoordVector( std::vector<aiVector3D> &point3d_array ) {
size_t ObjFileParser::getTexCoordVector(std::vector<aiVector3D> &point3d_array) {
size_t numComponents = getNumComponentsInDataDefinition();
ai_real x, y, z;
if( 2 == numComponents ) {
copyNextWord( m_buffer, Buffersize );
x = ( ai_real ) fast_atof( m_buffer );
if (2 == numComponents) {
copyNextWord(m_buffer, Buffersize);
x = (ai_real)fast_atof(m_buffer);
copyNextWord( m_buffer, Buffersize );
y = ( ai_real ) fast_atof( m_buffer );
copyNextWord(m_buffer, Buffersize);
y = (ai_real)fast_atof(m_buffer);
z = 0.0;
} else if( 3 == numComponents ) {
copyNextWord( m_buffer, Buffersize );
x = ( ai_real ) fast_atof( m_buffer );
} else if (3 == numComponents) {
copyNextWord(m_buffer, Buffersize);
x = (ai_real)fast_atof(m_buffer);
copyNextWord( m_buffer, Buffersize );
y = ( ai_real ) fast_atof( m_buffer );
copyNextWord(m_buffer, Buffersize);
y = (ai_real)fast_atof(m_buffer);
copyNextWord( m_buffer, Buffersize );
z = ( ai_real ) fast_atof( m_buffer );
copyNextWord(m_buffer, Buffersize);
z = (ai_real)fast_atof(m_buffer);
} else {
throw DeadlyImportError( "OBJ: Invalid number of components" );
throw DeadlyImportError("OBJ: Invalid number of components");
}
// Coerce nan and inf to 0 as is the OBJ default value
@ -336,96 +314,96 @@ size_t ObjFileParser::getTexCoordVector( std::vector<aiVector3D> &point3d_array
if (!std::isfinite(z))
z = 0;
point3d_array.emplace_back( x, y, z );
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
point3d_array.emplace_back(x, y, z);
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
return numComponents;
}
void ObjFileParser::getVector3( std::vector<aiVector3D> &point3d_array ) {
void ObjFileParser::getVector3(std::vector<aiVector3D> &point3d_array) {
ai_real x, y, z;
copyNextWord(m_buffer, Buffersize);
x = (ai_real) fast_atof(m_buffer);
x = (ai_real)fast_atof(m_buffer);
copyNextWord(m_buffer, Buffersize);
y = (ai_real) fast_atof(m_buffer);
y = (ai_real)fast_atof(m_buffer);
copyNextWord( m_buffer, Buffersize );
z = ( ai_real ) fast_atof( m_buffer );
copyNextWord(m_buffer, Buffersize);
z = (ai_real)fast_atof(m_buffer);
point3d_array.emplace_back( x, y, z );
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
point3d_array.emplace_back(x, y, z);
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
void ObjFileParser::getHomogeneousVector3( std::vector<aiVector3D> &point3d_array ) {
void ObjFileParser::getHomogeneousVector3(std::vector<aiVector3D> &point3d_array) {
ai_real x, y, z, w;
copyNextWord(m_buffer, Buffersize);
x = (ai_real) fast_atof(m_buffer);
x = (ai_real)fast_atof(m_buffer);
copyNextWord(m_buffer, Buffersize);
y = (ai_real) fast_atof(m_buffer);
y = (ai_real)fast_atof(m_buffer);
copyNextWord( m_buffer, Buffersize );
z = ( ai_real ) fast_atof( m_buffer );
copyNextWord(m_buffer, Buffersize);
z = (ai_real)fast_atof(m_buffer);
copyNextWord( m_buffer, Buffersize );
w = ( ai_real ) fast_atof( m_buffer );
copyNextWord(m_buffer, Buffersize);
w = (ai_real)fast_atof(m_buffer);
if (w == 0)
throw DeadlyImportError("OBJ: Invalid component in homogeneous vector (Division by zero)");
point3d_array.emplace_back( x/w, y/w, z/w );
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
point3d_array.emplace_back(x / w, y / w, z / w);
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
void ObjFileParser::getTwoVectors3( std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b ) {
void ObjFileParser::getTwoVectors3(std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b) {
ai_real x, y, z;
copyNextWord(m_buffer, Buffersize);
x = (ai_real) fast_atof(m_buffer);
x = (ai_real)fast_atof(m_buffer);
copyNextWord(m_buffer, Buffersize);
y = (ai_real) fast_atof(m_buffer);
copyNextWord( m_buffer, Buffersize );
z = ( ai_real ) fast_atof( m_buffer );
point3d_array_a.emplace_back( x, y, z );
y = (ai_real)fast_atof(m_buffer);
copyNextWord(m_buffer, Buffersize);
x = (ai_real) fast_atof(m_buffer);
z = (ai_real)fast_atof(m_buffer);
point3d_array_a.emplace_back(x, y, z);
copyNextWord(m_buffer, Buffersize);
y = (ai_real) fast_atof(m_buffer);
x = (ai_real)fast_atof(m_buffer);
copyNextWord( m_buffer, Buffersize );
z = ( ai_real ) fast_atof( m_buffer );
copyNextWord(m_buffer, Buffersize);
y = (ai_real)fast_atof(m_buffer);
point3d_array_b.emplace_back( x, y, z );
copyNextWord(m_buffer, Buffersize);
z = (ai_real)fast_atof(m_buffer);
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
point3d_array_b.emplace_back(x, y, z);
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
void ObjFileParser::getVector2( std::vector<aiVector2D> &point2d_array ) {
void ObjFileParser::getVector2(std::vector<aiVector2D> &point2d_array) {
ai_real x, y;
copyNextWord(m_buffer, Buffersize);
x = (ai_real) fast_atof(m_buffer);
x = (ai_real)fast_atof(m_buffer);
copyNextWord(m_buffer, Buffersize);
y = (ai_real) fast_atof(m_buffer);
y = (ai_real)fast_atof(m_buffer);
point2d_array.emplace_back(x, y);
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
static const std::string DefaultObjName = "defaultobject";
void ObjFileParser::getFace( aiPrimitiveType type ) {
m_DataIt = getNextToken<DataArrayIt>( m_DataIt, m_DataItEnd );
if ( m_DataIt == m_DataItEnd || *m_DataIt == '\0' ) {
void ObjFileParser::getFace(aiPrimitiveType type) {
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
if (m_DataIt == m_DataItEnd || *m_DataIt == '\0') {
return;
}
ObjFile::Face *face = new ObjFile::Face( type );
ObjFile::Face *face = new ObjFile::Face(type);
bool hasNormal = false;
const int vSize = static_cast<unsigned int>(m_pModel->m_Vertices.size());
@ -435,56 +413,56 @@ void ObjFileParser::getFace( aiPrimitiveType type ) {
const bool vt = (!m_pModel->m_TextureCoord.empty());
const bool vn = (!m_pModel->m_Normals.empty());
int iPos = 0;
while ( m_DataIt != m_DataItEnd ) {
while (m_DataIt != m_DataItEnd) {
int iStep = 1;
if ( IsLineEnd( *m_DataIt ) ) {
if (IsLineEnd(*m_DataIt)) {
break;
}
if ( *m_DataIt =='/' ) {
if (*m_DataIt == '/') {
if (type == aiPrimitiveType_POINT) {
ASSIMP_LOG_ERROR("Obj: Separator unexpected in point statement");
}
iPos++;
} else if( IsSpaceOrNewLine( *m_DataIt ) ) {
} else if (IsSpaceOrNewLine(*m_DataIt)) {
iPos = 0;
} else {
//OBJ USES 1 Base ARRAYS!!!!
const int iVal( ::atoi( & ( *m_DataIt ) ) );
const int iVal(::atoi(&(*m_DataIt)));
// increment iStep position based off of the sign and # of digits
int tmp = iVal;
if ( iVal < 0 ) {
if (iVal < 0) {
++iStep;
}
while ( ( tmp = tmp / 10 ) != 0 ) {
while ((tmp = tmp / 10) != 0) {
++iStep;
}
if (iPos == 1 && !vt && vn)
iPos = 2; // skip texture coords for normals if there are no tex coords
if ( iVal > 0 ) {
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 );
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 ) {
} 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 );
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();
@ -494,45 +472,44 @@ void ObjFileParser::getFace( aiPrimitiveType type ) {
delete face;
throw DeadlyImportError("OBJ: Invalid face indice");
}
}
m_DataIt += iStep;
}
if ( face->m_vertices.empty() ) {
if (face->m_vertices.empty()) {
ASSIMP_LOG_ERROR("Obj: Ignoring empty face");
// skip line and clean up
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
delete face;
return;
}
// Set active material, if one set
if( NULL != m_pModel->m_pCurrentMaterial ) {
if (NULL != 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( NULL == m_pModel->m_pCurrent ) {
createObject( DefaultObjName );
if (NULL == m_pModel->m_pCurrent) {
createObject(DefaultObjName);
}
// Assign face to mesh
if ( NULL == m_pModel->m_pCurrentMesh ) {
createMesh( DefaultObjName );
if (NULL == m_pModel->m_pCurrentMesh) {
createMesh(DefaultObjName);
}
// Store the face
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_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
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
void ObjFileParser::getMaterialDesc() {
@ -543,7 +520,7 @@ void ObjFileParser::getMaterialDesc() {
}
char *pStart = &(*m_DataIt);
while( m_DataIt != m_DataItEnd && !IsLineEnd( *m_DataIt ) ) {
while (m_DataIt != m_DataItEnd && !IsLineEnd(*m_DataIt)) {
++m_DataIt;
}
@ -558,13 +535,13 @@ void ObjFileParser::getMaterialDesc() {
// 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
if ( m_pModel->m_pCurrentMaterial && m_pModel->m_pCurrentMaterial->MaterialName == aiString( strName ) ) {
if (m_pModel->m_pCurrentMaterial && m_pModel->m_pCurrentMaterial->MaterialName == aiString(strName)) {
skip = true;
}
if (!skip) {
// Search for material
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find(strName);
std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->m_MaterialMap.find(strName);
if (it == m_pModel->m_MaterialMap.end()) {
// 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
@ -580,21 +557,21 @@ void ObjFileParser::getMaterialDesc() {
m_pModel->m_pCurrentMaterial = (*it).second;
}
if ( needsNewMesh( strName ) ) {
createMesh( strName );
if (needsNewMesh(strName)) {
createMesh(strName);
}
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strName);
}
// Skip rest of line
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
// Get a comment, values will be skipped
void ObjFileParser::getComment() {
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
@ -602,12 +579,12 @@ void ObjFileParser::getComment() {
void ObjFileParser::getMaterialLib() {
// Translate tuple
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
if( m_DataIt == m_DataItEnd ) {
if (m_DataIt == m_DataItEnd) {
return;
}
char *pStart = &(*m_DataIt);
while( m_DataIt != m_DataItEnd && !IsLineEnd( *m_DataIt ) ) {
while (m_DataIt != m_DataItEnd && !IsLineEnd(*m_DataIt)) {
++m_DataIt;
}
@ -616,14 +593,14 @@ void ObjFileParser::getMaterialLib() {
std::string absName;
// Check if directive is valid.
if ( 0 == strMatName.length() ) {
ASSIMP_LOG_WARN( "OBJ: no name for material library specified." );
if (0 == strMatName.length()) {
ASSIMP_LOG_WARN("OBJ: no name for material library specified.");
return;
}
if ( m_pIO->StackSize() > 0 ) {
if (m_pIO->StackSize() > 0) {
std::string path = m_pIO->CurrentDirectory();
if ( '/' != *path.rbegin() ) {
if ('/' != *path.rbegin()) {
path += '/';
}
absName += path;
@ -632,8 +609,8 @@ void ObjFileParser::getMaterialLib() {
absName = strMatName;
}
IOStream *pFile = m_pIO->Open( absName );
if ( nullptr == pFile ) {
IOStream *pFile = m_pIO->Open(absName);
if (nullptr == pFile) {
ASSIMP_LOG_ERROR("OBJ: Unable to locate material file " + strMatName);
std::string strMatFallbackName = m_originalObjFileName.substr(0, m_originalObjFileName.length() - 3) + "mtl";
ASSIMP_LOG_INFO("OBJ: Opening fallback material file " + strMatFallbackName);
@ -650,11 +627,11 @@ void ObjFileParser::getMaterialLib() {
// material files if the model doesn't use any materials, so we
// allow that.
std::vector<char> buffer;
BaseImporter::TextFileToBuffer( pFile, buffer, BaseImporter::ALLOW_EMPTY );
m_pIO->Close( pFile );
BaseImporter::TextFileToBuffer(pFile, buffer, BaseImporter::ALLOW_EMPTY);
m_pIO->Close(pFile);
// Importing the material library
ObjFileMtlImporter mtlImporter( buffer, strMatName, m_pModel.get() );
ObjFileMtlImporter mtlImporter(buffer, strMatName, m_pModel.get());
}
// -------------------------------------------------------------------
@ -662,42 +639,39 @@ void ObjFileParser::getMaterialLib() {
void ObjFileParser::getNewMaterial() {
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
if( m_DataIt == m_DataItEnd ) {
if (m_DataIt == m_DataItEnd) {
return;
}
char *pStart = &(*m_DataIt);
std::string strMat( pStart, *m_DataIt );
while( m_DataIt != m_DataItEnd && IsSpaceOrNewLine( *m_DataIt ) ) {
std::string strMat(pStart, *m_DataIt);
while (m_DataIt != m_DataItEnd && IsSpaceOrNewLine(*m_DataIt)) {
++m_DataIt;
}
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strMat );
if ( it == m_pModel->m_MaterialMap.end() ) {
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
ASSIMP_LOG_WARN("OBJ: Unsupported material requested: " + strMat);
m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
} else {
// Set new material
if ( needsNewMesh( strMat ) ) {
createMesh( strMat );
if (needsNewMesh(strMat)) {
createMesh(strMat);
}
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strMat );
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strMat);
}
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
int ObjFileParser::getMaterialIndex( const std::string &strMaterialName )
{
int ObjFileParser::getMaterialIndex(const std::string &strMaterialName) {
int mat_index = -1;
if( strMaterialName.empty() ) {
if (strMaterialName.empty()) {
return mat_index;
}
for (size_t index = 0; index < m_pModel->m_MaterialLib.size(); ++index)
{
if ( strMaterialName == m_pModel->m_MaterialLib[ index ])
{
for (size_t index = 0; index < m_pModel->m_MaterialLib.size(); ++index) {
if (strMaterialName == m_pModel->m_MaterialLib[index]) {
mat_index = (int)index;
break;
}
@ -713,146 +687,127 @@ void ObjFileParser::getGroupName() {
// here we skip 'g ' from line
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, groupName);
if( isEndOfBuffer( m_DataIt, m_DataItEnd ) ) {
if (isEndOfBuffer(m_DataIt, m_DataItEnd)) {
return;
}
// Change active group, if necessary
if ( m_pModel->m_strActiveGroup != groupName ) {
if (m_pModel->m_strActiveGroup != groupName) {
// Search for already existing entry
ObjFile::Model::ConstGroupMapIt it = m_pModel->m_Groups.find(groupName);
// We are mapping groups into the object structure
createObject( groupName );
createObject(groupName);
// New group name, creating a new entry
if (it == m_pModel->m_Groups.end())
{
if (it == m_pModel->m_Groups.end()) {
std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
m_pModel->m_Groups[ groupName ] = pFaceIDArray;
m_pModel->m_Groups[groupName] = pFaceIDArray;
m_pModel->m_pGroupFaceIDs = (pFaceIDArray);
}
else
{
} else {
m_pModel->m_pGroupFaceIDs = (*it).second;
}
m_pModel->m_strActiveGroup = groupName;
}
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
// Not supported
void ObjFileParser::getGroupNumber()
{
void ObjFileParser::getGroupNumber() {
// Not used
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
// Not supported
void ObjFileParser::getGroupNumberAndResolution()
{
void ObjFileParser::getGroupNumberAndResolution() {
// Not used
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
// Stores values for a new object instance, name will be used to
// identify it.
void ObjFileParser::getObjectName()
{
void ObjFileParser::getObjectName() {
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
if( m_DataIt == m_DataItEnd ) {
if (m_DataIt == m_DataItEnd) {
return;
}
char *pStart = &(*m_DataIt);
while( m_DataIt != m_DataItEnd && !IsSpaceOrNewLine( *m_DataIt ) ) {
while (m_DataIt != m_DataItEnd && !IsSpaceOrNewLine(*m_DataIt)) {
++m_DataIt;
}
std::string strObjectName(pStart, &(*m_DataIt));
if (!strObjectName.empty())
{
if (!strObjectName.empty()) {
// Reset current object
m_pModel->m_pCurrent = NULL;
// Search for actual object
for (std::vector<ObjFile::Object*>::const_iterator it = m_pModel->m_Objects.begin();
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)
{
++it) {
if ((*it)->m_strObjName == strObjectName) {
m_pModel->m_pCurrent = *it;
break;
}
}
// Allocate a new object, if current one was not found before
if( NULL == m_pModel->m_pCurrent ) {
createObject( strObjectName );
if (NULL == m_pModel->m_pCurrent) {
createObject(strObjectName);
}
}
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
}
// -------------------------------------------------------------------
// Creates a new object instance
void ObjFileParser::createObject(const std::string &objName)
{
ai_assert( NULL != m_pModel );
void ObjFileParser::createObject(const std::string &objName) {
ai_assert(NULL != m_pModel);
m_pModel->m_pCurrent = new ObjFile::Object;
m_pModel->m_pCurrent->m_strObjName = objName;
m_pModel->m_Objects.push_back( m_pModel->m_pCurrent );
m_pModel->m_Objects.push_back(m_pModel->m_pCurrent);
createMesh( objName );
createMesh(objName);
if( m_pModel->m_pCurrentMaterial )
{
if (m_pModel->m_pCurrentMaterial) {
m_pModel->m_pCurrentMesh->m_uiMaterialIndex =
getMaterialIndex( m_pModel->m_pCurrentMaterial->MaterialName.data );
getMaterialIndex(m_pModel->m_pCurrentMaterial->MaterialName.data);
m_pModel->m_pCurrentMesh->m_pMaterial = m_pModel->m_pCurrentMaterial;
}
}
// -------------------------------------------------------------------
// Creates a new mesh
void ObjFileParser::createMesh( const std::string &meshName )
{
ai_assert( NULL != m_pModel );
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 ( NULL != m_pModel->m_pCurrent )
{
m_pModel->m_pCurrent->m_Meshes.push_back( meshId );
}
else
{
void ObjFileParser::createMesh(const std::string &meshName) {
ai_assert(NULL != m_pModel);
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 (NULL != m_pModel->m_pCurrent) {
m_pModel->m_pCurrent->m_Meshes.push_back(meshId);
} 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 )
{
bool ObjFileParser::needsNewMesh(const std::string &materialName) {
// If no mesh data yet
if (m_pModel->m_pCurrentMesh == nullptr)
{
if (m_pModel->m_pCurrentMesh == nullptr) {
return true;
}
bool newMat = false;
int matIdx = getMaterialIndex( materialName );
int matIdx = getMaterialIndex(materialName);
int curMatIdx = m_pModel->m_pCurrentMesh->m_uiMaterialIndex;
if ( curMatIdx != int(ObjFile::Mesh::NoMaterial)
&& curMatIdx != matIdx
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() )
{
&& !m_pModel->m_pCurrentMesh->m_Faces.empty()) {
// New material -> only one material per mesh, so we need to create a new
// material
newMat = true;
@ -862,9 +817,8 @@ bool ObjFileParser::needsNewMesh( const std::string &materialName )
// -------------------------------------------------------------------
// Shows an error in parsing process.
void ObjFileParser::reportErrorTokenInFace()
{
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
void ObjFileParser::reportErrorTokenInFace() {
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
ASSIMP_LOG_ERROR("OBJ: Not supported token in face description detected");
}

View File

@ -4,7 +4,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2020, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@ -42,24 +41,24 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef OBJ_FILEPARSER_H_INC
#define OBJ_FILEPARSER_H_INC
#include <vector>
#include <string>
#include <map>
#include <memory>
#include <assimp/IOStreamBuffer.h>
#include <assimp/mesh.h>
#include <assimp/vector2.h>
#include <assimp/vector3.h>
#include <assimp/mesh.h>
#include <assimp/IOStreamBuffer.h>
#include <map>
#include <memory>
#include <string>
#include <vector>
namespace Assimp {
namespace ObjFile {
struct Model;
struct Object;
struct Material;
struct Point3;
struct Point2;
}
struct Model;
struct Object;
struct Material;
struct Point3;
struct Point2;
} // namespace ObjFile
class ObjFileImporter;
class IOSystem;
@ -78,31 +77,31 @@ public:
/// @brief The default constructor.
ObjFileParser();
/// @brief Constructor with data array.
ObjFileParser( IOStreamBuffer<char> &streamBuffer, const std::string &modelName, IOSystem* io, ProgressHandler* progress, const std::string &originalObjFileName);
ObjFileParser(IOStreamBuffer<char> &streamBuffer, const std::string &modelName, IOSystem *io, ProgressHandler *progress, const std::string &originalObjFileName);
/// @brief Destructor
~ObjFileParser();
/// @brief If you want to load in-core data.
void setBuffer( std::vector<char> &buffer );
void setBuffer(std::vector<char> &buffer);
/// @brief Model getter.
ObjFile::Model *GetModel() const;
protected:
/// Parse the loaded file
void parseFile( IOStreamBuffer<char> &streamBuffer );
void parseFile(IOStreamBuffer<char> &streamBuffer);
/// Method to copy the new delimited word in the current line.
void copyNextWord(char *pBuffer, size_t length);
/// Method to copy the new line.
// void copyNextLine(char *pBuffer, size_t length);
// void copyNextLine(char *pBuffer, size_t length);
/// Get the number of components in a line.
size_t getNumComponentsInDataDefinition();
/// Stores the vector
size_t getTexCoordVector( std::vector<aiVector3D> &point3d_array );
size_t getTexCoordVector(std::vector<aiVector3D> &point3d_array);
/// Stores the following 3d vector.
void getVector3( std::vector<aiVector3D> &point3d_array );
void getVector3(std::vector<aiVector3D> &point3d_array);
/// Stores the following homogeneous vector as a 3D vector
void getHomogeneousVector3( std::vector<aiVector3D> &point3d_array );
void getHomogeneousVector3(std::vector<aiVector3D> &point3d_array);
/// Stores the following two 3d vectors on the line.
void getTwoVectors3( std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b );
void getTwoVectors3(std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b);
/// Stores the following 3d vector.
void getVector2(std::vector<aiVector2D> &point2d_array);
/// Stores the following face.
@ -122,23 +121,23 @@ protected:
/// Gets the group number and resolution from file.
void getGroupNumberAndResolution();
/// Returns the index of the material. Is -1 if not material was found.
int getMaterialIndex( const std::string &strMaterialName );
int getMaterialIndex(const std::string &strMaterialName);
/// Parse object name
void getObjectName();
/// Creates a new object.
void createObject( const std::string &strObjectName );
void createObject(const std::string &strObjectName);
/// Creates a new mesh.
void createMesh( const std::string &meshName );
void createMesh(const std::string &meshName);
/// Returns true, if a new mesh instance must be created.
bool needsNewMesh( const std::string &rMaterialName );
bool needsNewMesh(const std::string &rMaterialName);
/// Error report in token
void reportErrorTokenInFace();
private:
// Copy and assignment constructor should be private
// because the class contains pointer to allocated memory
ObjFileParser(const ObjFileParser& rhs);
ObjFileParser& operator=(const ObjFileParser& rhs);
ObjFileParser(const ObjFileParser &rhs);
ObjFileParser &operator=(const ObjFileParser &rhs);
/// Default material name
static const std::string DEFAULT_MATERIAL;
@ -155,7 +154,7 @@ private:
/// Pointer to IO system instance.
IOSystem *m_pIO;
//! Pointer to progress handler
ProgressHandler* m_progress;
ProgressHandler *m_progress;
/// Path to the current model, name of the obj file where the buffer comes from
const std::string m_originalObjFileName;
};

View File

@ -4,7 +4,6 @@ Open Asset Import Library (assimp)
Copyright (c) 2006-2020, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
@ -46,8 +45,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef OBJ_TOOLS_H_INC
#define OBJ_TOOLS_H_INC
#include <assimp/fast_atof.h>
#include <assimp/ParsingUtils.h>
#include <assimp/fast_atof.h>
#include <vector>
namespace Assimp {
@ -57,17 +56,14 @@ namespace Assimp {
* @param end Iterator with end of buffer.
* @return true, if the end of the buffer is reached.
*/
template<class char_t>
inline bool isEndOfBuffer( char_t it, char_t end ) {
if ( it == end )
{
template <class char_t>
inline bool isEndOfBuffer(char_t it, char_t end) {
if (it == end) {
return true;
}
else
{
} else {
--end;
}
return ( it == end );
return (it == end);
}
/** @brief Returns next word separated by a space
@ -75,12 +71,10 @@ inline bool isEndOfBuffer( char_t it, char_t end ) {
* @param pEnd Pointer to end of buffer
* @return Pointer to next space
*/
template<class Char_T>
inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd )
{
while ( !isEndOfBuffer( pBuffer, pEnd ) )
{
if ( !IsSpaceOrNewLine( *pBuffer ) || IsLineEnd( *pBuffer ) ) {
template <class Char_T>
inline Char_T getNextWord(Char_T pBuffer, Char_T pEnd) {
while (!isEndOfBuffer(pBuffer, pEnd)) {
if (!IsSpaceOrNewLine(*pBuffer) || IsLineEnd(*pBuffer)) {
//if ( *pBuffer != '\\' )
break;
}
@ -94,16 +88,14 @@ inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd )
* @param pEnd Pointer to end of buffer
* @return Pointer to next token
*/
template<class Char_T>
inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd )
{
while ( !isEndOfBuffer( pBuffer, pEnd ) )
{
if( IsSpaceOrNewLine( *pBuffer ) )
template <class Char_T>
inline Char_T getNextToken(Char_T pBuffer, Char_T pEnd) {
while (!isEndOfBuffer(pBuffer, pEnd)) {
if (IsSpaceOrNewLine(*pBuffer))
break;
pBuffer++;
}
return getNextWord( pBuffer, pEnd );
return getNextWord(pBuffer, pEnd);
}
/** @brief Skips a line
@ -112,18 +104,18 @@ inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd )
* @param uiLine Current line number in format
* @return Current-iterator with new position
*/
template<class char_t>
inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine ) {
while( !isEndOfBuffer( it, end ) && !IsLineEnd( *it ) ) {
template <class char_t>
inline char_t skipLine(char_t it, char_t end, unsigned int &uiLine) {
while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
++it;
}
if ( it != end ) {
if (it != end) {
++it;
++uiLine;
}
// fix .. from time to time there are spaces at the beginning of a material line
while ( it != end && ( *it == '\t' || *it == ' ' ) ) {
while (it != end && (*it == '\t' || *it == ' ')) {
++it;
}
@ -137,20 +129,19 @@ inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine ) {
* @param name Separated name
* @return Current-iterator with new position
*/
template<class char_t>
inline char_t getName( char_t it, char_t end, std::string &name )
{
template <class char_t>
inline char_t getName(char_t it, char_t end, std::string &name) {
name = "";
if( isEndOfBuffer( it, end ) ) {
if (isEndOfBuffer(it, end)) {
return end;
}
char *pStart = &( *it );
while( !isEndOfBuffer( it, end ) && !IsLineEnd( *it )) {
char *pStart = &(*it);
while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
++it;
}
while(IsSpace( *it ) ) {
while (IsSpace(*it)) {
--it;
}
// Get name
@ -158,8 +149,8 @@ inline char_t getName( char_t it, char_t end, std::string &name )
while (&(*it) < pStart) {
++it;
}
std::string strName( pStart, &(*it) );
if ( strName.empty() )
std::string strName(pStart, &(*it));
if (strName.empty())
return it;
else
name = strName;
@ -174,22 +165,19 @@ inline char_t getName( char_t it, char_t end, std::string &name )
* @param name Separated name
* @return Current-iterator with new position
*/
template<class char_t>
inline char_t getNameNoSpace( char_t it, char_t end, std::string &name )
{
template <class char_t>
inline char_t getNameNoSpace(char_t it, char_t end, std::string &name) {
name = "";
if( isEndOfBuffer( it, end ) ) {
if (isEndOfBuffer(it, end)) {
return end;
}
char *pStart = &( *it );
while( !isEndOfBuffer( it, end ) && !IsLineEnd( *it )
&& !IsSpaceOrNewLine( *it ) ) {
char *pStart = &(*it);
while (!isEndOfBuffer(it, end) && !IsLineEnd(*it) && !IsSpaceOrNewLine(*it)) {
++it;
}
while( isEndOfBuffer( it, end ) || IsLineEnd( *it )
|| IsSpaceOrNewLine( *it ) ) {
while (isEndOfBuffer(it, end) || IsLineEnd(*it) || IsSpaceOrNewLine(*it)) {
--it;
}
++it;
@ -199,8 +187,8 @@ inline char_t getNameNoSpace( char_t it, char_t end, std::string &name )
while (&(*it) < pStart) {
++it;
}
std::string strName( pStart, &(*it) );
if ( strName.empty() )
std::string strName(pStart, &(*it));
if (strName.empty())
return it;
else
name = strName;
@ -215,20 +203,18 @@ inline char_t getNameNoSpace( char_t it, char_t end, std::string &name )
* @param length Buffer length
* @return Current-iterator with new position
*/
template<class char_t>
inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length )
{
template <class char_t>
inline char_t CopyNextWord(char_t it, char_t end, char *pBuffer, size_t length) {
size_t index = 0;
it = getNextWord<char_t>( it, end );
while( !IsSpaceOrNewLine( *it ) && !isEndOfBuffer( it, end ) )
{
pBuffer[index] = *it ;
it = getNextWord<char_t>(it, end);
while (!IsSpaceOrNewLine(*it) && !isEndOfBuffer(it, end)) {
pBuffer[index] = *it;
index++;
if (index == length-1)
if (index == length - 1)
break;
++it;
}
pBuffer[ index ] = '\0';
pBuffer[index] = '\0';
return it;
}
@ -238,13 +224,12 @@ inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length
* @param value Separated float value.
* @return Current-iterator with new position
*/
template<class char_t>
inline char_t getFloat( char_t it, char_t end, ai_real &value )
{
template <class char_t>
inline char_t getFloat(char_t it, char_t end, ai_real &value) {
static const size_t BUFFERSIZE = 1024;
char buffer[ BUFFERSIZE ];
it = CopyNextWord<char_t>( it, end, buffer, BUFFERSIZE );
value = (ai_real) fast_atof( buffer );
char buffer[BUFFERSIZE];
it = CopyNextWord<char_t>(it, end, buffer, BUFFERSIZE);
value = (ai_real)fast_atof(buffer);
return it;
}
@ -255,46 +240,45 @@ inline char_t getFloat( char_t it, char_t end, ai_real &value )
* @param delimiters Delimiter for tokenize.
* @return Number of found token.
*/
template<class string_type>
unsigned int tokenize( const string_type& str, std::vector<string_type>& tokens,
const string_type& delimiters )
{
template <class string_type>
unsigned int tokenize(const string_type &str, std::vector<string_type> &tokens,
const string_type &delimiters) {
// Skip delimiters at beginning.
typename string_type::size_type lastPos = str.find_first_not_of( delimiters, 0 );
typename string_type::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
typename string_type::size_type pos = str.find_first_of( delimiters, lastPos );
while ( string_type::npos != pos || string_type::npos != lastPos )
{
typename string_type::size_type pos = str.find_first_of(delimiters, lastPos);
while (string_type::npos != pos || string_type::npos != lastPos) {
// Found a token, add it to the vector.
string_type tmp = str.substr(lastPos, pos - lastPos);
if ( !tmp.empty() && ' ' != tmp[ 0 ] )
tokens.push_back( tmp );
if (!tmp.empty() && ' ' != tmp[0])
tokens.push_back(tmp);
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of( delimiters, pos );
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of( delimiters, lastPos );
pos = str.find_first_of(delimiters, lastPos);
}
return static_cast<unsigned int>( tokens.size() );
return static_cast<unsigned int>(tokens.size());
}
template <class string_type>
string_type trim_whitespaces(string_type str)
{
while (!str.empty() && IsSpace(str[0])) str.erase(0);
while (!str.empty() && IsSpace(str[str.length() - 1])) str.erase(str.length() - 1);
string_type trim_whitespaces(string_type str) {
while (!str.empty() && IsSpace(str[0]))
str.erase(0);
while (!str.empty() && IsSpace(str[str.length() - 1]))
str.erase(str.length() - 1);
return str;
}
template<class T>
bool hasLineEnd( T it, T end ) {
bool hasLineEnd( false );
while ( !isEndOfBuffer( it, end ) ) {
template <class T>
bool hasLineEnd(T it, T end) {
bool hasLineEnd(false);
while (!isEndOfBuffer(it, end)) {
it++;
if ( IsLineEnd( it ) ) {
if (IsLineEnd(it)) {
hasLineEnd = true;
break;
}