Danbias/Code/OysterGraphics/FileLoader/ObjReader.cpp

152 lines
3.2 KiB
C++
Raw Normal View History

2013-11-19 13:30:46 +01:00
#include "OBJReader.h"
2013-11-21 18:31:16 +01:00
#include "..\Definitions\GraphicalDefinition.h"
2013-11-19 13:30:46 +01:00
#include <sstream>
#include <fstream>
#include "GeneralLoader.h"
using namespace std;
2013-11-19 13:30:46 +01:00
OBJReader::OBJReader()
{
2013-11-19 13:30:46 +01:00
_mPos = 0;
_mNormal = 0;
_mTexel = 0;
}
2013-11-19 13:30:46 +01:00
OBJReader::~OBJReader()
{
}
2013-11-21 12:47:47 +01:00
void OBJReader::readOBJFile( std::wstring fileName )
{
2013-11-21 12:47:47 +01:00
std::fstream inStream;
std::string typeOfData = " ";
2013-11-19 13:30:46 +01:00
float vertexData;
2013-11-21 12:47:47 +01:00
std::string face1, face2, face3;
inStream.open( fileName + L".obj", std::fstream::in );
2013-11-19 13:30:46 +01:00
if( inStream.is_open() )
{
2013-11-19 13:30:46 +01:00
while( !inStream.eof() )
{
2013-11-19 13:30:46 +01:00
inStream >> typeOfData;
2013-11-19 13:30:46 +01:00
if( typeOfData == "v" )
{
2013-11-19 13:30:46 +01:00
Oyster::Math::Float3 position;
2013-11-19 13:30:46 +01:00
inStream >> vertexData;
position.x = vertexData;
inStream >> vertexData;
position.y = vertexData;
2013-11-19 13:30:46 +01:00
inStream >> vertexData;
position.z = vertexData;
2013-11-19 13:30:46 +01:00
_mVertexCoord.push_back( position );
}
2013-11-19 13:30:46 +01:00
else if( typeOfData == "vt" )
{
2013-11-19 13:30:46 +01:00
Oyster::Math::Float2 texel;
inStream >> vertexData;
texel.x = vertexData;
2013-11-19 13:30:46 +01:00
inStream >> vertexData;
texel.y = 1 - vertexData;
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-19 13:30:46 +01:00
inStream >> vertexData;
normal.y = vertexData;
2013-11-19 13:30:46 +01:00
inStream >> vertexData;
normal.z = vertexData;
2013-11-19 13:30:46 +01:00
_mVertexNormal.push_back( normal );
}
else if( typeOfData == "f" )
{
inStream >> face1;
stringSplit( face1 );
2013-11-19 13:30:46 +01:00
addToOBJarray();
2013-11-19 13:30:46 +01:00
inStream >> face2;
stringSplit( face2 );
2013-11-19 13:30:46 +01:00
addToOBJarray();
2013-11-19 13:30:46 +01:00
inStream >> face3;
stringSplit( face3 );
2013-11-19 13:30:46 +01:00
addToOBJarray();
}
}
}
inStream.close();
Mat = Oyster::Resource::OysterResource::LoadResource((fileName + L".jpg").c_str(),Oyster::Graphics::Loading::LoadTexture);
}
Oyster::Graphics::Model::ModelInfo* OBJReader::toModel()
2013-11-21 13:45:11 +01:00
{
Oyster::Graphics::Core::Buffer* b = new Oyster::Graphics::Core::Buffer();
Oyster::Graphics::Core::Buffer::BUFFER_INIT_DESC desc;
Oyster::Graphics::Model::ModelInfo* modelInfo = new Oyster::Graphics::Model::ModelInfo();
2013-11-21 13:45:11 +01:00
2013-11-21 18:31:16 +01:00
desc.ElementSize = 32;
2013-11-21 13:45:11 +01:00
desc.InitData = &this->_myOBJ[0];
2013-11-21 18:31:16 +01:00
desc.NumElements = this->_myOBJ.size();
desc.Type = Oyster::Graphics::Core::Buffer::BUFFER_TYPE::VERTEX_BUFFER;
desc.Usage = Oyster::Graphics::Core::Buffer::BUFFER_DEFAULT;
2013-11-21 13:45:11 +01:00
HRESULT hr = S_OK;
2013-11-21 18:31:16 +01:00
hr = b->Init(desc);
2013-11-21 13:45:11 +01:00
if(FAILED(hr))
{
//Something isn't okay here
}
2013-11-21 18:31:16 +01:00
modelInfo->Indexed = false;
modelInfo->VertexCount = (int)desc.NumElements;
2013-11-21 23:54:12 +01:00
modelInfo->Vertices = b;
modelInfo->Material.push_back((ID3D11ShaderResourceView*)Mat);
2013-11-21 13:45:11 +01:00
return modelInfo;
}
2013-11-19 13:30:46 +01:00
//Private functions
2013-11-21 12:47:47 +01:00
void OBJReader::stringSplit( std::string strToSplit )
{
2013-11-19 13:30:46 +01:00
char delim = '/';
2013-11-21 12:47:47 +01:00
std::string vPos, vNormal, vTexel;
std::stringstream aStream(strToSplit);
2013-11-19 13:30:46 +01:00
getline( aStream, vPos, delim );
getline( aStream, vTexel, delim );
getline( aStream, vNormal );
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-19 13:30:46 +01:00
void OBJReader::addToOBJarray()
{
2013-11-19 13:30:46 +01:00
OBJFormat temp;
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-19 13:30:46 +01:00
_myOBJ.push_back( temp );
}