2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2016-01-01 20:07:24 +00:00
|
|
|
Copyright (c) 2006-2016, assimp team
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-05-19 03:48:29 +00:00
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
|
|
|
|
|
|
|
|
#include "ObjFileParser.h"
|
|
|
|
#include "ObjFileMtlImporter.h"
|
|
|
|
#include "ObjTools.h"
|
|
|
|
#include "ObjFileData.h"
|
|
|
|
#include "ParsingUtils.h"
|
|
|
|
#include "DefaultIOSystem.h"
|
|
|
|
#include "BaseImporter.h"
|
2015-07-09 23:21:10 +00:00
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
#include <assimp/material.h>
|
|
|
|
#include <assimp/Importer.hpp>
|
2015-05-19 03:48:29 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
|
2016-11-09 19:18:26 +00:00
|
|
|
const std::string ObjFileParser::DEFAULT_MATERIAL = AI_DEFAULT_MATERIAL_NAME;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Constructor with loaded data and directories.
|
2016-10-22 21:34:56 +00:00
|
|
|
ObjFileParser::ObjFileParser( IOStreamBuffer<char> &streamBuffer, const std::string &modelName,
|
|
|
|
IOSystem *io, ProgressHandler* progress,
|
|
|
|
const std::string &originalObjFileName) :
|
2016-10-11 10:29:40 +00:00
|
|
|
m_DataIt(),
|
|
|
|
m_DataItEnd(),
|
2015-05-19 03:48:29 +00:00
|
|
|
m_pModel(NULL),
|
|
|
|
m_uiLine(0),
|
2016-01-11 15:25:36 +00:00
|
|
|
m_pIO( io ),
|
2016-03-22 17:38:28 +00:00
|
|
|
m_progress(progress),
|
|
|
|
m_originalObjFileName(originalObjFileName)
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
std::fill_n(m_buffer,Buffersize,0);
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Create the model instance to store all the data
|
|
|
|
m_pModel = new ObjFile::Model();
|
2015-08-30 13:21:53 +00:00
|
|
|
m_pModel->m_ModelName = modelName;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// create default material and store it
|
2015-08-30 13:21:53 +00:00
|
|
|
m_pModel->m_pDefaultMaterial = new ObjFile::Material;
|
2015-05-19 03:48:29 +00:00
|
|
|
m_pModel->m_pDefaultMaterial->MaterialName.Set( DEFAULT_MATERIAL );
|
|
|
|
m_pModel->m_MaterialLib.push_back( DEFAULT_MATERIAL );
|
|
|
|
m_pModel->m_MaterialMap[ DEFAULT_MATERIAL ] = m_pModel->m_pDefaultMaterial;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Start parsing the file
|
2016-10-11 10:29:40 +00:00
|
|
|
parseFile( streamBuffer );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Destructor
|
2016-11-09 19:16:45 +00:00
|
|
|
ObjFileParser::~ObjFileParser() {
|
2015-05-19 03:48:29 +00:00
|
|
|
delete m_pModel;
|
|
|
|
m_pModel = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Returns a pointer to the model instance.
|
2016-11-09 19:16:45 +00:00
|
|
|
ObjFile::Model *ObjFileParser::GetModel() const {
|
2015-05-19 03:48:29 +00:00
|
|
|
return m_pModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// File parsing method.
|
2016-10-11 10:29:40 +00:00
|
|
|
void ObjFileParser::parseFile( IOStreamBuffer<char> &streamBuffer ) {
|
2016-01-11 15:25:36 +00:00
|
|
|
// only update every 100KB or it'll be too slow
|
|
|
|
const unsigned int updateProgressEveryBytes = 100 * 1024;
|
|
|
|
unsigned int progressCounter = 0;
|
2016-10-11 10:29:40 +00:00
|
|
|
const unsigned int bytesToProcess = streamBuffer.size();
|
2016-02-07 19:03:24 +00:00
|
|
|
const unsigned int progressTotal = 3 * bytesToProcess;
|
|
|
|
const unsigned int progressOffset = bytesToProcess;
|
2016-01-11 15:25:36 +00:00
|
|
|
unsigned int processed = 0;
|
2016-10-29 19:41:24 +00:00
|
|
|
size_t lastFilePos( 0 );
|
2016-10-11 10:29:40 +00:00
|
|
|
|
|
|
|
std::vector<char> buffer;
|
|
|
|
while ( streamBuffer.getNextLine( buffer ) ) {
|
|
|
|
m_DataIt = buffer.begin();
|
|
|
|
m_DataItEnd = buffer.end();
|
2016-01-11 15:25:36 +00:00
|
|
|
|
2016-10-29 19:41:24 +00:00
|
|
|
// Handle progress reporting
|
|
|
|
const size_t filePos( streamBuffer.getFilePos() );
|
|
|
|
if ( lastFilePos < filePos ) {
|
|
|
|
processed += filePos;
|
|
|
|
lastFilePos = filePos;
|
2016-01-11 15:25:36 +00:00
|
|
|
progressCounter++;
|
2016-10-29 19:41:24 +00:00
|
|
|
m_progress->UpdateFileRead( progressOffset + processed * 2, progressTotal );
|
|
|
|
}
|
2016-01-11 15:25:36 +00:00
|
|
|
|
|
|
|
// parse line
|
2016-11-10 20:39:32 +00:00
|
|
|
switch (*m_DataIt) {
|
2015-05-19 03:48:29 +00:00
|
|
|
case 'v': // Parse a vertex texture coordinate
|
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
++m_DataIt;
|
|
|
|
if (*m_DataIt == ' ' || *m_DataIt == '\t') {
|
2016-06-28 00:08:22 +00:00
|
|
|
size_t numComponents = getNumComponentsInLine();
|
|
|
|
if (numComponents == 3) {
|
|
|
|
// read in vertex definition
|
|
|
|
getVector3(m_pModel->m_Vertices);
|
2016-10-04 21:06:31 +00:00
|
|
|
} else if (numComponents == 4) {
|
|
|
|
// read in vertex definition (homogeneous coords)
|
|
|
|
getHomogeneousVector3(m_pModel->m_Vertices);
|
2016-06-28 00:08:22 +00:00
|
|
|
} else if (numComponents == 6) {
|
|
|
|
// read vertex and vertex-color
|
|
|
|
getTwoVectors3(m_pModel->m_Vertices, m_pModel->m_VertexColors);
|
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
} else if (*m_DataIt == 't') {
|
|
|
|
// read in texture coordinate ( 2D or 3D )
|
2016-10-11 10:29:40 +00:00
|
|
|
++m_DataIt;
|
|
|
|
getVector( m_pModel->m_TextureCoord );
|
2016-02-07 19:03:24 +00:00
|
|
|
} else if (*m_DataIt == 'n') {
|
|
|
|
// Read in normal vector definition
|
|
|
|
++m_DataIt;
|
|
|
|
getVector3( m_pModel->m_Normals );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p': // Parse a face, line or point statement
|
|
|
|
case 'l':
|
|
|
|
case 'f':
|
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
getFace(*m_DataIt == 'f' ? aiPrimitiveType_POLYGON : (*m_DataIt == 'l'
|
|
|
|
? aiPrimitiveType_LINE : aiPrimitiveType_POINT));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '#': // Parse a comment
|
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
getComment();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u': // Parse a material desc. setter
|
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
getMaterialDesc();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm': // Parse a material library or merging group ('mg')
|
|
|
|
{
|
2016-10-17 22:48:04 +00:00
|
|
|
std::string name;
|
2016-10-07 13:40:51 +00:00
|
|
|
|
2016-10-17 22:48:04 +00:00
|
|
|
getName(m_DataIt, m_DataItEnd, name);
|
2016-10-18 14:48:47 +00:00
|
|
|
|
|
|
|
size_t nextSpace = name.find(" ");
|
|
|
|
if (nextSpace != std::string::npos)
|
|
|
|
name = name.substr(0, nextSpace);
|
2016-10-07 13:40:51 +00:00
|
|
|
|
|
|
|
if (name == "mg")
|
2015-05-19 03:48:29 +00:00
|
|
|
getGroupNumberAndResolution();
|
2016-10-07 13:40:51 +00:00
|
|
|
else if(name == "mtllib")
|
2016-02-07 19:03:24 +00:00
|
|
|
getMaterialLib();
|
2016-10-07 13:40:51 +00:00
|
|
|
else
|
|
|
|
goto pf_skip_line;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g': // Parse group name
|
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
getGroupName();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's': // Parse group number
|
|
|
|
{
|
|
|
|
getGroupNumber();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o': // Parse object name
|
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
getObjectName();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-02-07 19:03:24 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
default:
|
|
|
|
{
|
2016-10-07 13:40:51 +00:00
|
|
|
pf_skip_line:
|
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Copy the next word in a temporary buffer
|
2016-11-10 20:39:32 +00:00
|
|
|
void ObjFileParser::copyNextWord(char *pBuffer, size_t length) {
|
2016-02-07 19:03:24 +00:00
|
|
|
size_t index = 0;
|
|
|
|
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
while( m_DataIt != m_DataItEnd && !IsSpaceOrNewLine( *m_DataIt ) ) {
|
|
|
|
pBuffer[index] = *m_DataIt;
|
|
|
|
index++;
|
|
|
|
if( index == length - 1 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++m_DataIt;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
ai_assert(index < length);
|
|
|
|
pBuffer[index] = '\0';
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 00:08:22 +00:00
|
|
|
size_t ObjFileParser::getNumComponentsInLine() {
|
2015-05-19 03:48:29 +00:00
|
|
|
size_t numComponents( 0 );
|
2016-02-07 19:03:24 +00:00
|
|
|
const char* tmp( &m_DataIt[0] );
|
|
|
|
while( !IsLineEnd( *tmp ) ) {
|
|
|
|
if ( !SkipSpaces( &tmp ) ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
SkipToken( tmp );
|
|
|
|
++numComponents;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2016-06-28 00:08:22 +00:00
|
|
|
return numComponents;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFileParser::getVector( std::vector<aiVector3D> &point3d_array ) {
|
|
|
|
size_t numComponents = getNumComponentsInLine();
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real x, y, z;
|
2016-01-23 23:40:47 +00:00
|
|
|
if( 2 == numComponents ) {
|
2016-02-07 19:03:24 +00:00
|
|
|
copyNextWord( m_buffer, Buffersize );
|
2016-07-16 02:14:36 +00:00
|
|
|
x = ( ai_real ) fast_atof( m_buffer );
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
copyNextWord( m_buffer, Buffersize );
|
2016-07-16 02:14:36 +00:00
|
|
|
y = ( ai_real ) fast_atof( m_buffer );
|
2016-02-07 19:03:24 +00:00
|
|
|
z = 0.0;
|
|
|
|
} else if( 3 == numComponents ) {
|
|
|
|
copyNextWord( m_buffer, Buffersize );
|
2016-07-16 02:14:36 +00:00
|
|
|
x = ( ai_real ) fast_atof( m_buffer );
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
copyNextWord( m_buffer, Buffersize );
|
2016-07-16 02:14:36 +00:00
|
|
|
y = ( ai_real ) fast_atof( m_buffer );
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
copyNextWord( m_buffer, Buffersize );
|
2016-07-16 02:14:36 +00:00
|
|
|
z = ( ai_real ) fast_atof( m_buffer );
|
2016-02-07 19:03:24 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
throw DeadlyImportError( "OBJ: Invalid number of components" );
|
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
point3d_array.push_back( aiVector3D( x, y, z ) );
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Get values for a new 3D vector instance
|
2016-02-08 17:07:00 +00:00
|
|
|
void ObjFileParser::getVector3( std::vector<aiVector3D> &point3d_array ) {
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real x, y, z;
|
2016-02-07 19:03:24 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2016-07-16 02:14:36 +00:00
|
|
|
x = (ai_real) fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2016-07-16 02:14:36 +00:00
|
|
|
y = (ai_real) fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
copyNextWord( m_buffer, Buffersize );
|
2016-07-16 02:14:36 +00:00
|
|
|
z = ( ai_real ) fast_atof( m_buffer );
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
point3d_array.push_back( aiVector3D( x, y, z ) );
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2016-10-04 21:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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 );
|
|
|
|
w = ( ai_real ) fast_atof( m_buffer );
|
|
|
|
|
|
|
|
ai_assert( w != 0 );
|
|
|
|
|
|
|
|
point3d_array.push_back( aiVector3D( x/w, y/w, z/w ) );
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 00:08:22 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// Get values for two 3D vectors on the same line
|
|
|
|
void ObjFileParser::getTwoVectors3( std::vector<aiVector3D> &point3d_array_a, std::vector<aiVector3D> &point3d_array_b ) {
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real x, y, z;
|
2016-06-28 00:08:22 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2016-07-16 02:14:36 +00:00
|
|
|
x = (ai_real) fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2016-07-16 02:14:36 +00:00
|
|
|
y = (ai_real) fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
copyNextWord( m_buffer, Buffersize );
|
2016-07-16 02:14:36 +00:00
|
|
|
z = ( ai_real ) fast_atof( m_buffer );
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
point3d_array_a.push_back( aiVector3D( x, y, z ) );
|
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2016-07-16 02:14:36 +00:00
|
|
|
x = (ai_real) fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2016-07-16 02:14:36 +00:00
|
|
|
y = (ai_real) fast_atof(m_buffer);
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
copyNextWord( m_buffer, Buffersize );
|
2016-07-16 02:14:36 +00:00
|
|
|
z = ( ai_real ) fast_atof( m_buffer );
|
2016-06-28 00:08:22 +00:00
|
|
|
|
|
|
|
point3d_array_b.push_back( aiVector3D( x, y, z ) );
|
|
|
|
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Get values for a new 2D vector instance
|
2016-02-07 19:03:24 +00:00
|
|
|
void ObjFileParser::getVector2( std::vector<aiVector2D> &point2d_array ) {
|
2016-07-16 02:14:36 +00:00
|
|
|
ai_real x, y;
|
2016-02-07 19:03:24 +00:00
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2016-07-16 02:14:36 +00:00
|
|
|
x = (ai_real) fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
copyNextWord(m_buffer, Buffersize);
|
2016-07-16 02:14:36 +00:00
|
|
|
y = (ai_real) fast_atof(m_buffer);
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
point2d_array.push_back(aiVector2D(x, y));
|
|
|
|
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 16:50:08 +00:00
|
|
|
static const std::string DefaultObjName = "defaultobject";
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Get values for a new face instance
|
2016-11-09 19:09:45 +00:00
|
|
|
void ObjFileParser::getFace( aiPrimitiveType type ) {
|
2016-11-10 20:39:32 +00:00
|
|
|
//copyNextLine(m_buffer, Buffersize);
|
|
|
|
//char *pPtr = m_DataIt;
|
|
|
|
//char *pPtr = m_buffer;
|
|
|
|
//char *pEnd = &pPtr[Buffersize];
|
|
|
|
m_DataIt = getNextToken<DataArrayIt>( m_DataIt, m_DataItEnd );
|
|
|
|
if ( m_DataIt == m_DataItEnd || *m_DataIt == '\0' ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
2016-02-09 16:50:08 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-11-09 19:09:45 +00:00
|
|
|
ObjFile::Face *face = new ObjFile::Face( type );
|
2015-05-19 03:48:29 +00:00
|
|
|
bool hasNormal = false;
|
|
|
|
|
|
|
|
const int vSize = m_pModel->m_Vertices.size();
|
|
|
|
const int vtSize = m_pModel->m_TextureCoord.size();
|
|
|
|
const int vnSize = m_pModel->m_Normals.size();
|
|
|
|
|
|
|
|
const bool vt = (!m_pModel->m_TextureCoord.empty());
|
|
|
|
const bool vn = (!m_pModel->m_Normals.empty());
|
|
|
|
int iStep = 0, iPos = 0;
|
2016-11-10 20:39:32 +00:00
|
|
|
while ( m_DataIt != m_DataItEnd ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
iStep = 1;
|
|
|
|
|
2016-11-10 20:39:32 +00:00
|
|
|
if ( IsLineEnd( *m_DataIt ) ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
break;
|
2016-02-09 16:50:08 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-11-10 20:39:32 +00:00
|
|
|
if ( *m_DataIt =='/' ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
if (type == aiPrimitiveType_POINT) {
|
|
|
|
DefaultLogger::get()->error("Obj: Separator unexpected in point statement");
|
|
|
|
}
|
2016-02-09 16:50:08 +00:00
|
|
|
if (iPos == 0) {
|
2015-05-19 03:48:29 +00:00
|
|
|
//if there are no texture coordinates in the file, but normals
|
|
|
|
if (!vt && vn) {
|
|
|
|
iPos = 1;
|
|
|
|
iStep++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iPos++;
|
2016-11-10 20:39:32 +00:00
|
|
|
} else if( IsSpaceOrNewLine( *m_DataIt ) ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
iPos = 0;
|
2016-02-09 16:50:08 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
//OBJ USES 1 Base ARRAYS!!!!
|
2016-11-10 20:39:32 +00:00
|
|
|
const int iVal( ::atoi( & ( *m_DataIt ) ) );
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// increment iStep position based off of the sign and # of digits
|
|
|
|
int tmp = iVal;
|
2016-02-09 16:50:08 +00:00
|
|
|
if ( iVal < 0 ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
++iStep;
|
2016-02-09 16:50:08 +00:00
|
|
|
}
|
|
|
|
while ( ( tmp = tmp / 10 ) != 0 ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
++iStep;
|
2016-02-09 16:50:08 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-11-09 19:16:45 +00:00
|
|
|
if ( iVal > 0 ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Store parsed index
|
2016-11-09 19:16:45 +00:00
|
|
|
if ( 0 == iPos ) {
|
2016-11-09 19:09:45 +00:00
|
|
|
face->m_vertices.push_back( iVal - 1 );
|
2016-11-09 19:16:45 +00:00
|
|
|
} else if ( 1 == iPos ) {
|
2016-11-09 19:09:45 +00:00
|
|
|
face->m_texturCoords.push_back( iVal - 1 );
|
2016-11-09 19:16:45 +00:00
|
|
|
} else if ( 2 == iPos ) {
|
2016-11-09 19:09:45 +00:00
|
|
|
face->m_normals.push_back( iVal - 1 );
|
2015-05-19 03:48:29 +00:00
|
|
|
hasNormal = true;
|
2016-11-09 19:16:45 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
reportErrorTokenInFace();
|
|
|
|
}
|
2016-11-09 19:16:45 +00:00
|
|
|
} else if ( iVal < 0 ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Store relatively index
|
2016-11-09 19:16:45 +00:00
|
|
|
if ( 0 == iPos ) {
|
2016-11-09 19:09:45 +00:00
|
|
|
face->m_vertices.push_back( vSize + iVal );
|
2016-11-09 19:16:45 +00:00
|
|
|
} else if ( 1 == iPos ) {
|
2016-11-09 19:09:45 +00:00
|
|
|
face->m_texturCoords.push_back( vtSize + iVal );
|
2016-11-09 19:16:45 +00:00
|
|
|
} else if ( 2 == iPos ) {
|
2016-11-09 19:09:45 +00:00
|
|
|
face->m_normals.push_back( vnSize + iVal );
|
2015-05-19 03:48:29 +00:00
|
|
|
hasNormal = true;
|
2016-11-09 19:16:45 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
reportErrorTokenInFace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 20:39:32 +00:00
|
|
|
m_DataIt += iStep;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 19:09:45 +00:00
|
|
|
if ( face->m_vertices.empty() ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
DefaultLogger::get()->error("Obj: Ignoring empty face");
|
2016-02-07 19:03:24 +00:00
|
|
|
// skip line and clean up
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set active material, if one set
|
2015-07-06 18:47:45 +00:00
|
|
|
if( NULL != m_pModel->m_pCurrentMaterial ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
face->m_pMaterial = m_pModel->m_pCurrentMaterial;
|
2015-07-06 18:47:45 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
face->m_pMaterial = m_pModel->m_pDefaultMaterial;
|
2015-07-06 18:47:45 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// Create a default object, if nothing is there
|
2015-07-06 18:47:45 +00:00
|
|
|
if( NULL == m_pModel->m_pCurrent ) {
|
2016-02-09 16:50:08 +00:00
|
|
|
createObject( DefaultObjName );
|
2015-07-06 18:47:45 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Assign face to mesh
|
2015-07-06 18:47:45 +00:00
|
|
|
if ( NULL == m_pModel->m_pCurrentMesh ) {
|
2016-02-09 16:50:08 +00:00
|
|
|
createMesh( DefaultObjName );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Store the face
|
|
|
|
m_pModel->m_pCurrentMesh->m_Faces.push_back( face );
|
2016-11-09 19:09:45 +00:00
|
|
|
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();
|
2015-07-06 18:47:45 +00:00
|
|
|
if( !m_pModel->m_pCurrentMesh->m_hasNormals && hasNormal ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
m_pModel->m_pCurrentMesh->m_hasNormals = true;
|
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
// Skip the rest of the line
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Get values for a new material description
|
2016-11-09 19:16:45 +00:00
|
|
|
void ObjFileParser::getMaterialDesc() {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Get next data for material data
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
if (m_DataIt == m_DataItEnd) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
2015-08-30 13:21:53 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
char *pStart = &(*m_DataIt);
|
|
|
|
while( m_DataIt != m_DataItEnd && !IsLineEnd( *m_DataIt ) ) {
|
|
|
|
++m_DataIt;
|
|
|
|
}
|
|
|
|
|
2015-12-13 09:54:50 +00:00
|
|
|
// In some cases we should ignore this 'usemtl' command, this variable helps us to do so
|
|
|
|
bool skip = false;
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Get name
|
2016-02-07 19:03:24 +00:00
|
|
|
std::string strName(pStart, &(*m_DataIt));
|
2015-12-13 19:32:09 +00:00
|
|
|
strName = trim_whitespaces(strName);
|
2015-12-13 09:54:50 +00:00
|
|
|
if (strName.empty())
|
|
|
|
skip = true;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-12-13 09:54:50 +00:00
|
|
|
// If the current mesh has the same material, we simply ignore that 'usemtl' command
|
|
|
|
// There is no need to create another object or even mesh here
|
2016-11-09 19:16:45 +00:00
|
|
|
if ( m_pModel->m_pCurrentMaterial && m_pModel->m_pCurrentMaterial->MaterialName == aiString( strName ) ) {
|
2015-12-13 09:54:50 +00:00
|
|
|
skip = true;
|
2016-11-09 19:16:45 +00:00
|
|
|
}
|
2015-12-13 09:54:50 +00:00
|
|
|
|
2016-11-09 19:16:45 +00:00
|
|
|
if (!skip) {
|
2015-12-13 09:54:50 +00:00
|
|
|
// Search for material
|
|
|
|
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find(strName);
|
2016-11-09 19:16:45 +00:00
|
|
|
if (it == m_pModel->m_MaterialMap.end()) {
|
2015-12-13 09:54:50 +00:00
|
|
|
// Not found, use default material
|
|
|
|
m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
|
|
|
|
DefaultLogger::get()->error("OBJ: failed to locate material " + strName + ", skipping");
|
|
|
|
strName = m_pModel->m_pDefaultMaterial->MaterialName.C_Str();
|
2016-11-09 19:16:45 +00:00
|
|
|
} else {
|
2015-12-13 09:54:50 +00:00
|
|
|
// Found, using detected material
|
|
|
|
m_pModel->m_pCurrentMaterial = (*it).second;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2015-12-13 09:54:50 +00:00
|
|
|
|
2016-11-09 19:16:45 +00:00
|
|
|
if ( needsNewMesh( strName ) ) {
|
|
|
|
createMesh( strName );
|
|
|
|
}
|
2015-12-13 09:54:50 +00:00
|
|
|
|
|
|
|
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex(strName);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
// Skip rest of line
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// Get a comment, values will be skipped
|
2016-11-09 19:16:45 +00:00
|
|
|
void ObjFileParser::getComment() {
|
|
|
|
while (m_DataIt != m_DataItEnd) {
|
|
|
|
if ( '\n' == (*m_DataIt)) {
|
2016-02-07 19:03:24 +00:00
|
|
|
++m_DataIt;
|
|
|
|
break;
|
2016-11-09 19:16:45 +00:00
|
|
|
} else {
|
2016-02-07 19:03:24 +00:00
|
|
|
++m_DataIt;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Get material library from file.
|
2016-11-09 19:16:45 +00:00
|
|
|
void ObjFileParser::getMaterialLib() {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Translate tuple
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
if( m_DataIt == m_DataItEnd ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
char *pStart = &(*m_DataIt);
|
|
|
|
while( m_DataIt != m_DataItEnd && !IsLineEnd( *m_DataIt ) ) {
|
|
|
|
++m_DataIt;
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Check for existence
|
2016-02-07 19:03:24 +00:00
|
|
|
const std::string strMatName(pStart, &(*m_DataIt));
|
2015-08-30 13:21:53 +00:00
|
|
|
std::string absName;
|
2016-10-07 13:40:51 +00:00
|
|
|
|
|
|
|
// Check if directive is valid.
|
|
|
|
if(!strMatName.length()) throw DeadlyImportError("File name of the material is absent.");
|
|
|
|
|
2015-08-30 13:21:53 +00:00
|
|
|
if ( m_pIO->StackSize() > 0 ) {
|
2015-10-19 20:07:32 +00:00
|
|
|
std::string path = m_pIO->CurrentDirectory();
|
|
|
|
if ( '/' != *path.rbegin() ) {
|
|
|
|
path += '/';
|
|
|
|
}
|
2015-08-30 13:21:53 +00:00
|
|
|
absName = path + strMatName;
|
|
|
|
} else {
|
|
|
|
absName = strMatName;
|
|
|
|
}
|
|
|
|
IOStream *pFile = m_pIO->Open( absName );
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-08-30 13:37:56 +00:00
|
|
|
if (!pFile ) {
|
2016-03-22 17:38:28 +00:00
|
|
|
DefaultLogger::get()->error("OBJ: Unable to locate material file " + strMatName);
|
|
|
|
std::string strMatFallbackName = m_originalObjFileName.substr(0, m_originalObjFileName.length() - 3) + "mtl";
|
2016-03-22 17:39:52 +00:00
|
|
|
DefaultLogger::get()->info("OBJ: Opening fallback material file " + strMatFallbackName);
|
2016-03-22 17:38:28 +00:00
|
|
|
pFile = m_pIO->Open(strMatFallbackName);
|
|
|
|
if (!pFile) {
|
2016-03-22 17:39:52 +00:00
|
|
|
DefaultLogger::get()->error("OBJ: Unable to locate fallback material file " + strMatName);
|
2016-03-22 17:38:28 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
|
|
|
|
return;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 06:13:25 +00:00
|
|
|
// Import material library data from file.
|
|
|
|
// Some exporters (e.g. Silo) will happily write out empty
|
|
|
|
// material files if the model doesn't use any materials, so we
|
|
|
|
// allow that.
|
2015-05-19 03:48:29 +00:00
|
|
|
std::vector<char> buffer;
|
2015-12-14 06:13:25 +00:00
|
|
|
BaseImporter::TextFileToBuffer( pFile, buffer, BaseImporter::ALLOW_EMPTY );
|
2015-05-19 03:48:29 +00:00
|
|
|
m_pIO->Close( pFile );
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// Importing the material library
|
|
|
|
ObjFileMtlImporter mtlImporter( buffer, strMatName, m_pModel );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Set a new material definition as the current material.
|
2016-11-10 20:39:32 +00:00
|
|
|
void ObjFileParser::getNewMaterial() {
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
m_DataIt = getNextWord<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
if( m_DataIt == m_DataItEnd ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
char *pStart = &(*m_DataIt);
|
|
|
|
std::string strMat( pStart, *m_DataIt );
|
|
|
|
while( m_DataIt != m_DataItEnd && IsSpaceOrNewLine( *m_DataIt ) ) {
|
|
|
|
++m_DataIt;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strMat );
|
2016-11-10 20:39:32 +00:00
|
|
|
if ( it == m_pModel->m_MaterialMap.end() ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Show a warning, if material was not found
|
|
|
|
DefaultLogger::get()->warn("OBJ: Unsupported material requested: " + strMat);
|
|
|
|
m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
|
2016-11-10 20:39:32 +00:00
|
|
|
} else {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Set new material
|
2016-11-10 20:39:32 +00:00
|
|
|
if ( needsNewMesh( strMat ) ) {
|
2015-07-09 18:15:44 +00:00
|
|
|
createMesh( strMat );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strMat );
|
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
int ObjFileParser::getMaterialIndex( const std::string &strMaterialName )
|
|
|
|
{
|
|
|
|
int mat_index = -1;
|
|
|
|
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 ])
|
|
|
|
{
|
|
|
|
mat_index = (int)index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mat_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Getter for a group name.
|
2016-09-06 13:41:37 +00:00
|
|
|
void ObjFileParser::getGroupName() {
|
|
|
|
std::string groupName;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2016-07-22 13:10:06 +00:00
|
|
|
// here we skip 'g ' from line
|
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
2016-09-06 13:41:37 +00:00
|
|
|
m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, groupName);
|
2016-02-07 19:03:24 +00:00
|
|
|
if( isEndOfBuffer( m_DataIt, m_DataItEnd ) ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change active group, if necessary
|
2016-09-06 13:41:37 +00:00
|
|
|
if ( m_pModel->m_strActiveGroup != groupName ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
// Search for already existing entry
|
2016-09-06 13:41:37 +00:00
|
|
|
ObjFile::Model::ConstGroupMapIt it = m_pModel->m_Groups.find(groupName);
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// We are mapping groups into the object structure
|
2016-09-06 13:41:37 +00:00
|
|
|
createObject( groupName );
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// New group name, creating a new entry
|
|
|
|
if (it == m_pModel->m_Groups.end())
|
|
|
|
{
|
|
|
|
std::vector<unsigned int> *pFaceIDArray = new std::vector<unsigned int>;
|
2016-09-06 13:41:37 +00:00
|
|
|
m_pModel->m_Groups[ groupName ] = pFaceIDArray;
|
2015-05-19 03:48:29 +00:00
|
|
|
m_pModel->m_pGroupFaceIDs = (pFaceIDArray);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pModel->m_pGroupFaceIDs = (*it).second;
|
|
|
|
}
|
2016-09-06 13:41:37 +00:00
|
|
|
m_pModel->m_strActiveGroup = groupName;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Not supported
|
2015-05-19 03:48:29 +00:00
|
|
|
void ObjFileParser::getGroupNumber()
|
|
|
|
{
|
|
|
|
// Not used
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Not supported
|
2015-05-19 03:48:29 +00:00
|
|
|
void ObjFileParser::getGroupNumberAndResolution()
|
|
|
|
{
|
|
|
|
// Not used
|
2016-02-07 19:03:24 +00:00
|
|
|
|
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Stores values for a new object instance, name will be used to
|
|
|
|
// identify it.
|
2016-02-07 19:03:24 +00:00
|
|
|
void ObjFileParser::getObjectName()
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
|
|
|
if( m_DataIt == m_DataItEnd ) {
|
2015-05-19 03:48:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
char *pStart = &(*m_DataIt);
|
|
|
|
while( m_DataIt != m_DataItEnd && !IsSpaceOrNewLine( *m_DataIt ) ) {
|
|
|
|
++m_DataIt;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-02-07 19:03:24 +00:00
|
|
|
std::string strObjectName(pStart, &(*m_DataIt));
|
2015-05-19 03:52:10 +00:00
|
|
|
if (!strObjectName.empty())
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
|
|
|
// Reset current object
|
|
|
|
m_pModel->m_pCurrent = NULL;
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// Search for actual object
|
|
|
|
for (std::vector<ObjFile::Object*>::const_iterator it = m_pModel->m_Objects.begin();
|
|
|
|
it != m_pModel->m_Objects.end();
|
|
|
|
++it)
|
|
|
|
{
|
|
|
|
if ((*it)->m_strObjName == strObjectName)
|
|
|
|
{
|
|
|
|
m_pModel->m_pCurrent = *it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate a new object, if current one was not found before
|
|
|
|
if( NULL == m_pModel->m_pCurrent ) {
|
|
|
|
createObject( strObjectName );
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Creates a new object instance
|
2015-07-09 17:59:47 +00:00
|
|
|
void ObjFileParser::createObject(const std::string &objName)
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
|
|
|
ai_assert( NULL != m_pModel );
|
|
|
|
|
|
|
|
m_pModel->m_pCurrent = new ObjFile::Object;
|
2015-07-09 17:59:47 +00:00
|
|
|
m_pModel->m_pCurrent->m_strObjName = objName;
|
2015-05-19 03:48:29 +00:00
|
|
|
m_pModel->m_Objects.push_back( m_pModel->m_pCurrent );
|
2015-05-19 03:52:10 +00:00
|
|
|
|
2015-07-09 18:15:44 +00:00
|
|
|
createMesh( objName );
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
if( m_pModel->m_pCurrentMaterial )
|
|
|
|
{
|
2015-05-19 03:52:10 +00:00
|
|
|
m_pModel->m_pCurrentMesh->m_uiMaterialIndex =
|
2015-05-19 03:48:29 +00:00
|
|
|
getMaterialIndex( m_pModel->m_pCurrentMaterial->MaterialName.data );
|
|
|
|
m_pModel->m_pCurrentMesh->m_pMaterial = m_pModel->m_pCurrentMaterial;
|
2015-05-19 03:52:10 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Creates a new mesh
|
2015-07-09 18:15:44 +00:00
|
|
|
void ObjFileParser::createMesh( const std::string &meshName )
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
|
|
|
ai_assert( NULL != m_pModel );
|
2015-07-09 18:15:44 +00:00
|
|
|
m_pModel->m_pCurrentMesh = new ObjFile::Mesh( meshName );
|
2015-05-19 03:48:29 +00:00
|
|
|
m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
|
|
|
|
unsigned int meshId = m_pModel->m_Meshes.size()-1;
|
|
|
|
if ( NULL != m_pModel->m_pCurrent )
|
|
|
|
{
|
|
|
|
m_pModel->m_pCurrent->m_Meshes.push_back( meshId );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->error("OBJ: No object detected to attach a new mesh instance.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Returns true, if a new mesh must be created.
|
2016-02-10 09:40:39 +00:00
|
|
|
bool ObjFileParser::needsNewMesh( const std::string &materialName )
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2016-02-09 16:50:08 +00:00
|
|
|
// If no mesh data yet
|
2015-05-19 03:48:29 +00:00
|
|
|
if(m_pModel->m_pCurrentMesh == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool newMat = false;
|
2016-02-10 09:40:39 +00:00
|
|
|
int matIdx = getMaterialIndex( materialName );
|
2015-05-19 03:48:29 +00:00
|
|
|
int curMatIdx = m_pModel->m_pCurrentMesh->m_uiMaterialIndex;
|
2016-07-22 13:10:06 +00:00
|
|
|
if ( curMatIdx != int(ObjFile::Mesh::NoMaterial)
|
|
|
|
&& curMatIdx != matIdx
|
|
|
|
// no need create a new mesh if no faces in current
|
|
|
|
// lets say 'usemtl' goes straight after 'g'
|
|
|
|
&& m_pModel->m_pCurrentMesh->m_Faces.size() > 0 )
|
2015-05-19 03:48:29 +00:00
|
|
|
{
|
2015-05-19 03:52:10 +00:00
|
|
|
// New material -> only one material per mesh, so we need to create a new
|
2015-05-19 03:48:29 +00:00
|
|
|
// material
|
|
|
|
newMat = true;
|
|
|
|
}
|
|
|
|
return newMat;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
2015-05-19 03:57:13 +00:00
|
|
|
// Shows an error in parsing process.
|
2015-05-19 03:48:29 +00:00
|
|
|
void ObjFileParser::reportErrorTokenInFace()
|
2015-05-19 03:52:10 +00:00
|
|
|
{
|
2016-02-07 19:03:24 +00:00
|
|
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
2015-05-19 03:48:29 +00:00
|
|
|
DefaultLogger::get()->error("OBJ: Not supported token in face description detected");
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
} // Namespace Assimp
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER
|