2013-11-19 13:30:46 +01:00
|
|
|
#include "OBJReader.h"
|
|
|
|
#include <sstream>
|
2013-11-06 22:52:00 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace std;
|
2013-11-19 13:30:46 +01:00
|
|
|
OBJReader::OBJReader()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-19 13:30:46 +01:00
|
|
|
_mPos = 0;
|
|
|
|
_mNormal = 0;
|
|
|
|
_mTexel = 0;
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
OBJReader::~OBJReader()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
void OBJReader::readOBJFile( wstring fileName )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-19 13:30:46 +01:00
|
|
|
fstream inStream;
|
|
|
|
string typeOfData = " ";
|
|
|
|
float vertexData;
|
|
|
|
string face1, face2, face3;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream.open( fileName, fstream::in );
|
|
|
|
|
|
|
|
if( inStream.is_open() )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-19 13:30:46 +01:00
|
|
|
while( !inStream.eof() )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream >> typeOfData;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
if( typeOfData == "v" )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-19 13:30:46 +01:00
|
|
|
Oyster::Math::Float3 position;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream >> vertexData;
|
|
|
|
position.x = vertexData;
|
|
|
|
|
|
|
|
inStream >> vertexData;
|
|
|
|
position.y = vertexData;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream >> vertexData;
|
|
|
|
position.z = vertexData;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
_mVertexCoord.push_back( position );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
}
|
2013-11-19 13:30:46 +01:00
|
|
|
else if( typeOfData == "vt" )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-19 13:30:46 +01:00
|
|
|
Oyster::Math::Float2 texel;
|
|
|
|
inStream >> vertexData;
|
|
|
|
texel.x = vertexData;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream >> vertexData;
|
|
|
|
texel.y = 1 - vertexData;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
_mVertexTexture.push_back( texel );
|
|
|
|
}
|
|
|
|
else if( typeOfData == "vn" )
|
|
|
|
{
|
|
|
|
Oyster::Math::Float3 normal;
|
|
|
|
inStream >> vertexData;
|
|
|
|
normal.x = vertexData;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream >> vertexData;
|
|
|
|
normal.y = vertexData;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream >> vertexData;
|
|
|
|
normal.z = vertexData;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
_mVertexNormal.push_back( normal );
|
|
|
|
}
|
|
|
|
else if( typeOfData == "f" )
|
|
|
|
{
|
|
|
|
inStream >> face1;
|
|
|
|
stringSplit( face1 );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
addToOBJarray();
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream >> face2;
|
|
|
|
stringSplit( face2 );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
addToOBJarray();
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
inStream >> face3;
|
|
|
|
stringSplit( face3 );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
addToOBJarray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inStream.close();
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
//Private functions
|
|
|
|
void OBJReader::stringSplit( string strToSplit )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-19 13:30:46 +01:00
|
|
|
char delim = '/';
|
|
|
|
string vPos, vNormal, vTexel;
|
|
|
|
stringstream aStream(strToSplit);
|
|
|
|
getline( aStream, vPos, delim );
|
|
|
|
getline( aStream, vTexel, delim );
|
|
|
|
getline( aStream, vNormal );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
_mPos = atoi( vPos.c_str() );
|
|
|
|
_mNormal = atoi( vNormal.c_str() );
|
|
|
|
_mTexel = atoi( vTexel.c_str() );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
void OBJReader::addToOBJarray()
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-19 13:30:46 +01:00
|
|
|
OBJFormat temp;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
temp._d3VertexCoord = _mVertexCoord.at( _mPos - 1 );
|
|
|
|
temp._d3VertexNormal = _mVertexNormal.at( _mNormal - 1 );
|
|
|
|
temp._d3VertexTexture = _mVertexTexture.at( _mTexel - 1 );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-19 13:30:46 +01:00
|
|
|
_myOBJ.push_back( temp );
|
|
|
|
}
|