Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as C++ by a ( 16 years ago )
#pragma once
#include <d3d9.h>
#include <d3dx9math.h>
#include <d3dx9mesh.h>
#include "macros.h"
#include <fstream>
#include <sstream>
#include "Consts.h"
#include <Shlwapi.h>
class filepath
{
const string getpath()
{
char l[MAX_PATH];
GetCurrentDirectory( MAX_PATH, l );
return l;
}
void fill( const string &f )
{
size_t c = f.find_last_of( CONSTS::CHAR::COMMA );
size_t s = f.find_last_of( CONSTS::CHAR::SLASH );
fullpath = f;
format = fullpath.substr( c + 1 );
onlyname = fullpath.substr( s + 1, c - s - 1 );
onlypath = fullpath.substr( 0, s );
fullname = onlyname + CONSTS::CHAR::COMMA + format;
}
public :
bool exist;
string fullpath;
string format;
string onlypath;
string onlyname;
string fullname;
operator bool () const { return exist; }
filepath( const string & f ) :
exist( false ), fullpath( "" ), format( "" ), onlypath( "" ), onlyname( "" ), fullname( "" )
{
string str( f );
if ( exist = PathFileExists( str.c_str() ) )
{
fill( str );
return;
}
str = getpath() + string( CONSTS::CHAR::SLASH ) + f;
if ( exist = PathFileExists( str.c_str() ) )
{
fill( str );
return;
}
}
};
struct OBJVERTEX
{
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
D3DXVECTOR2 texcoord;
};
D3DVERTEXELEMENT9 OBJVERTEX_DECL[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{ 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
};
namespace CONSTS
{
namespace OBJ
{
const string COMMENT( "#" );
const string VECTOR( "v" );
const string TEXTURE( "vt" );
const string NORMAL( "vn" );
const string FACE( "f" );
const string MTLLIB( "mtllib" );
const string USEMTL( "usemtl" );
const int SLASH( '/' );
}
namespace MTL
{
const string COMMENT( "#" );
const string NEWMTL( "newmtl" );
const string AMBIENT( "Ka" );
const string DIFFUSE( "Kd" );
const string SPECULAR( "Ks" );
const string EMISSIVE( "Ke" );
const string TRANSPARENS( "Tr" );
const string DISSOLVE( "d" );
const string SPECULAREXPONENT( "Ns" );
const string ILLUMINATION( "illum" );
const string DIFFUSEMAP( "map_Kd" );
}
namespace FILE3DO
{
const string COMMENT( "#" );
const string VERTEXCOUNT( "vertexcount" );
const string VERTEX( "v" );
const string POSITION( "p" );
const string NORMAL( "n" );
const string TEXVERTEXCOUNT( "texturevertexcount" );
const string TEXVERTEX( "tv" );
const string FACECOUNT( "facecount" );
const string FACE( "f" );
const string TEXTUREFACE( "tf" );
const string BONECOUNT( "bonecount" );
const string BONENAME( "bonename" );
const string USEDVERTEX( "usedvertexcount" );
const string WEIGHT( "w" );
const string NAME( "name" );
const string MESH( "mesh" );
const string SKIN( "skin" );
}
}
string getMtlfromOBJfile( const string &f )
{
ifstream file( f.c_str() );
if ( !file.good() )
return "";
string line( "" );
string strMaterialFilename( "" );
while ( getline( file, line ) )
{
stringstream ss( line ); string st( "" );
ss >> st;
if ( st == CONSTS::OBJ::MTLLIB )
{
ss >> strMaterialFilename;
break;
}
}
file.close();
return strMaterialFilename;
}
struct MTLMaterial
{
string name;
D3DXVECTOR3 vAmbient;
D3DXVECTOR3 vDiffuse;
D3DXVECTOR3 vSpecular;
D3DXVECTOR3 vEmissive;
int nShininess;
float fAlpha;
bool bSpecular;
string texture;
MTLMaterial( const string & n ) : name( n )
{}
D3DMATERIAL9 ToD3DMATERIAL9 () const
{
D3DMATERIAL9 l =
{
D3DXCOLOR( vDiffuse.x, vDiffuse.y, vDiffuse.z, 1.0f ),
D3DXCOLOR( vAmbient.x, vAmbient.y, vAmbient.z, 1.0f ),
D3DXCOLOR( vSpecular.x, vSpecular.y, vSpecular.z, 1.0f ),
D3DXCOLOR( vEmissive.x, vEmissive.y, vEmissive.z, 1.0f ),
float(nShininess)
};
return l;
}
};
class MTLfileparser
{
public :
vector<MTLMaterial> materials;
MTLfileparser( const string & f )
{
filepath fp( f );
ifstream file( fp.fullpath.c_str() );
if ( file.good() )
{
string lastmtlname( "" );
string line( "" );
while ( getline( file, line ) )
{
stringstream ss( line );
string st( "" );
ss >> st;
if ( st == CONSTS::MTL::NEWMTL )
{
string name( "" );
ss >> name;
materials.push_back( MTLMaterial( name ) );
}
if ( materials.size() == 0 )
continue;
if ( st == CONSTS::MTL::COMMENT )
{
// just comment
}
else if ( st == CONSTS::MTL::AMBIENT )
{
float r, g, b;
ss >> r >> g >> b;
materials.back().vAmbient = D3DXVECTOR3( r, g, b );
}
else if ( st == CONSTS::MTL::DIFFUSE )
{
float r, g, b;
ss >> r >> g >> b;
materials.back().vDiffuse = D3DXVECTOR3( r, g, b );
}
else if ( st == CONSTS::MTL::SPECULAR )
{
float r, g, b;
ss >> r >> g >> b;
materials.back().vSpecular = D3DXVECTOR3( r, g, b );
}
else if ( st == CONSTS::MTL::EMISSIVE )
{
float r, g, b;
ss >> r >> g >> b;
materials.back().vEmissive = D3DXVECTOR3( r, g, b );
}
else if( st == CONSTS::MTL::DISSOLVE || st == CONSTS::MTL::TRANSPARENS )
{
ss >> materials.back().fAlpha;
}
else if( st == CONSTS::MTL::SPECULAREXPONENT )
{
ss >> materials.back().nShininess;
}
else if( st == CONSTS::MTL::ILLUMINATION )
{
int illumination;
ss >> illumination;
materials.back().bSpecular = ( illumination == 2 );
}
else if( st == CONSTS::MTL::DIFFUSEMAP )
{
ss >> materials.back().texture;
}
else
{
// Unimplemented or unrecognized command
}
}
}
file.close();
}
};
class OBJfileparser
{
public :
struct facevertex
{
DWORD ver;
DWORD tex;
DWORD nor;
};
vector<D3DXVECTOR3> vertices;
vector<D3DXVECTOR2> textures;
vector<D3DXVECTOR3> normals;
vector<facevertex> faces;
struct objpart
{
objpart( const DWORD d, const string & str ) : s( d ), mtrlname( str ) {}
DWORD s;
string mtrlname;
};
vector<objpart> parts;
OBJfileparser( const string & f )
{
ifstream file( f.c_str() );
if ( file.good() )
{
string line( "" );
while ( getline( file, line ) )
{
stringstream ss( line ); string st( "" );
ss >> st;
if ( st == CONSTS::OBJ::COMMENT )
{
//
}
else if ( st == CONSTS::OBJ::USEMTL )
{
string mtlname( "" );
ss >> mtlname;
parts.push_back( objpart( faces.size()/3, mtlname ) );
}
else if ( st == CONSTS::OBJ::VECTOR )
{
float x, y, z;
ss >> x >> y >> z;
vertices.push_back( D3DXVECTOR3( x, y, z ) );
}
else if ( st == CONSTS::OBJ::TEXTURE )
{
float u, v;
ss >> u >> v;
textures.push_back( D3DXVECTOR2( u, v ) );
}
else if ( st == CONSTS::OBJ::NORMAL )
{
float x, y, z;
ss >> x >> y >> z;
normals.push_back( D3DXVECTOR3( x, y, z ) );
}
else if ( st == CONSTS::OBJ::FACE )
{
facevertex fv = {0};
for( UINT iface = 0; iface < 3; iface++ )
{
ss >> fv.ver;
if ( ss.peek() == CONSTS::OBJ::SLASH )
{
ss.ignore();
if ( ss.peek() != CONSTS::OBJ::SLASH )
ss >> fv.tex;
if ( ss.peek() == CONSTS::OBJ::SLASH )
{
ss.ignore();
ss >> fv.nor;
}
}
faces.push_back( fv );
}
}
else
{
// Unimplemented or unrecognized command
}
}
parts.push_back( objpart( faces.size()/3, "" ) );
file.close();
}
}
};
D3DVERTEXELEMENT9 SMVERTEX_DECL[] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0},
{ 0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0},
{ 0, 36, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{ 0, 48, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END()
};
struct pseudoskinmesh
{
struct face
{
DWORD a;
DWORD b;
DWORD c;
};
struct SMVERTEX
{
D3DXVECTOR3 position;
float w1;
float w2;
float w3;
float bi1;
float bi2;
float bi3;
D3DXVECTOR3 normal;
D3DXVECTOR2 texcoord;
};
const static D3DVERTEXELEMENT9 * VERTEX_DECL = SMVERTEX_DECL;
vector<face> fb;
vector<SMVERTEX> vb;
vector<DWORD> ab;
vector<string> abmtrl;
};
class parser3DO
{
public :
const string filename;
string name;
string meshfilename;
string skinfilename;
parser3DO( const string & f ) : filename( f )
{
ifstream file( filename.c_str() );
if ( file.good() )
{
string line( "" );
while ( getline( file, line ) )
{
stringstream ss( line ); string st( "" );
ss >> st;
if ( st == CONSTS::FILE3DO::COMMENT )
{
//
}
else if ( st == CONSTS::FILE3DO::NAME )
{
ss >> name;
}
else if ( st == CONSTS::FILE3DO::MESH )
{
ss >> meshfilename;
}
else if ( st == CONSTS::FILE3DO::SKIN )
{
ss >> skinfilename;
}
}
file.close();
}
}
const pseudoskinmesh getpseudoskinmesh()
{
pseudoskinmesh p;
return p;
}
const pseudomesh getpseudomesh()
{
pseudomesh p;
parser3DOM m( meshfilename );
m.
return p;
}
};
class parser3DOM
{
public :
struct VERTEX3DOM
{
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
};
struct FACE3DOM
{
DWORD a;
DWORD b;
DWORD c;
DWORD uva;
DWORD uvb;
DWORD uvc;
};
const string filename;
vector<VERTEX3DOM> verts;
vector<D3DXVECTOR2> uvs;
vector<FACE3DOM> faces;
parser3DOM( const string &f ) : filename( f )
{
ifstream file( filename.c_str() );
if ( file.good() )
{
string line( "" );
while ( getline( file, line ) )
{
stringstream ss( line ); string st( "" );
ss >> st;
if ( st == CONSTS::FILE3DO::COMMENT )
{
//
}
else if( st == CONSTS::FILE3DO::VERTEXCOUNT )
{
DWORD count( 0 );
ss >> count;
verts.reserve( count );
}
else if ( st == CONSTS::FILE3DO::VERTEX )
{
VERTEX3DOM v; ZeroMemory( &v, sizeof(v) );
ss >> st;
if ( st == CONSTS::FILE3DO::POSITION )
{
ss >> v.position.x;
ss >> v.position.y;
ss >> v.position.z;
ss >> st;
if ( st == CONSTS::FILE3DO::NORMAL )
{
ss >> v.normal.x;
ss >> v.normal.y;
ss >> v.normal.z;
}
}
verts.push_back( v );
}
else if( st == CONSTS::FILE3DO::TEXVERTEXCOUNT )
{
DWORD count( 0 );
ss >> count;
uvs.reserve( count );
}
else if( st == CONSTS::FILE3DO::TEXVERTEX )
{
D3DXVECTOR2 uv( 0, 0 );
ss >> uv.x;
ss >> uv.y;
uvs.push_back( uv );
}
else if( st == CONSTS::FILE3DO::FACECOUNT )
{
DWORD count( 0 );
ss >> count;
faces.reserve( count );
}
else if( st == CONSTS::FILE3DO::FACE )
{
FACE3DOM f; ZeroMemory( &f, sizeof(f) );
ss >> f.a;
ss >> f.b;
ss >> f.c;
ss >> st;
if ( st == CONSTS::FILE3DO::TEXTUREFACE )
{
ss >> f.uva;
ss >> f.uvb;
ss >> f.uvc;
}
faces.push_back( f );
}
}
file.close();
}
}
};
class parser3DOS
{
public :
struct wv
{
DWORD id;
float weight;
};
const string filename;
vector<string> bones;
vector<vector<wv> > weights;
parser3DOS( const string &f ) : filename( f )
{
ifstream file( filename.c_str() );
if ( file.good() )
{
string line( "" );
while ( getline( file, line ) )
{
stringstream ss( line ); string st( "" );
ss >> st;
if ( st == CONSTS::FILE3DO::COMMENT )
{
//
}
else if ( st == CONSTS::FILE3DO::BONECOUNT )
{
DWORD count( 0 );
ss >> count;
bones.reserve( count );
}
else if ( st == CONSTS::FILE3DO::BONENAME )
{
ss >> st;
bones.push_back( st );
}
else if ( st == CONSTS::FILE3DO::USEDVERTEX )
{
DWORD count( 0 );
ss >> count;
weights.reserve( count );
}
else if ( st == CONSTS::FILE3DO::WEIGHT )
{
vector<wv> w;
DWORD count( 0 );
ss >> count;
w.reserve( count );
for ( DWORD i( 0 ); i < count; i++ )
{
wv h = {0};
ss >> h.id;
ss >> h.weight;
w.push_back( h );
}
weights.push_back( w );
}
}
file.close();
}
}
};
Revise this Paste
Children: 20735