00001 #include "OBJModel.h"
00002 #include <stdio.h>
00003 #include <vector>
00004 #define STRUBUFFERSIZE 512
00005
00006 OBJModel::OBJModel()
00007 {
00008 iList = 0;
00009 iIndices = 0;
00010 fVertices = 0;
00011 iIndexCount = 0;
00012 iVertexCount = 0;
00013 }
00014
00015 OBJModel::~OBJModel()
00016 {
00017 Unload();
00018 }
00019
00020 void OBJModel::Unload()
00021 {
00022 if( iIndices ) delete iIndices;
00023 if( fVertices ) delete fVertices;
00024 if( iList ) glDeleteLists( iList, 1 );
00025
00026 iList = 0;
00027 iIndices = 0;
00028 fVertices = 0;
00029 iIndexCount = 0;
00030 iVertexCount = 0;
00031 }
00032
00033 bool OBJModel::LoadOBJ( const char * sFileToLoad )
00034 {
00035 Unload();
00036 char lbuffer[STRUBUFFERSIZE];
00037 std::vector< std::vector<float> > V;
00038 std::vector< std::vector<float> > VT;
00039 std::vector< std::vector<float> > VN;
00040 std::vector< float > fLine;
00041 std::vector< int > iFaceSet;
00042 fLine.resize( 3 );
00043
00044 int iFaces[9];
00045
00046 FILE * f = fopen( sFileToLoad, "r" );
00047 if( !f )
00048 {
00049 printf( "Could not open file \"%s\".", sFileToLoad );
00050 return false;
00051 }
00052
00053 iList = glGenLists( 1 );
00054 glNewList( iList, GL_COMPILE );
00055 glPushMatrix();
00056 glRotatef( 90.0, 1.0, 0.0, 0.0 );
00057 glBegin( GL_TRIANGLES );
00058
00059 while( fgets( lbuffer, STRUBUFFERSIZE-1, f ) )
00060 {
00061 switch( lbuffer[0] )
00062 {
00063 case 'v':
00064 switch( lbuffer[1] )
00065 {
00066 case 't':
00067 if( sscanf( &lbuffer[3], "%f %f %f", &fLine[0], &fLine[1], &fLine[2] ) )
00068 VT.push_back( fLine );
00069 break;
00070 case 'n':
00071 if( sscanf( &lbuffer[3], "%f %f %f", &fLine[0], &fLine[1], &fLine[2] ) )
00072 VN.push_back( fLine );
00073 break;
00074 case ' ':
00075 if( sscanf( &lbuffer[2], "%f %f %f", &fLine[0], &fLine[1], &fLine[2] ) )
00076 V.push_back( fLine );
00077 break;
00078 }
00079 break;
00080 case 'f':
00081 if( sscanf( &lbuffer[2], "%d/%d/%d %d/%d/%d %d/%d/%d",
00082 &iFaces[0], &iFaces[1], &iFaces[2],
00083 &iFaces[3], &iFaces[4], &iFaces[5],
00084 &iFaces[6], &iFaces[7], &iFaces[8] ) == 9 )
00085 {
00086 for( int i = 0; i < 9; ++i )
00087 iFaces[i]--;
00088 glNormal3f( VN[iFaces[8]][0], VN[iFaces[8]][1], VN[iFaces[8]][2] );
00089 glTexCoord3f( VT[iFaces[7]][0], VT[iFaces[7]][1], VT[iFaces[7]][2] );
00090 glVertex3f( V[iFaces[6]][0], V[iFaces[6]][1], V[iFaces[6]][2] );
00091 glNormal3f( VN[iFaces[5]][0], VN[iFaces[5]][1], VN[iFaces[5]][2] );
00092 glTexCoord3f( VT[iFaces[4]][0], VT[iFaces[4]][1], VT[iFaces[4]][2] );
00093 glVertex3f( V[iFaces[3]][0], V[iFaces[3]][1], V[iFaces[3]][2] );
00094 glNormal3f( VN[iFaces[2]][0], VN[iFaces[2]][1], VN[iFaces[2]][2] );
00095 glTexCoord3f( VT[iFaces[1]][0], VT[iFaces[1]][1], VT[iFaces[1]][2] );
00096 glVertex3f( V[iFaces[0]][0], V[iFaces[0]][1], V[iFaces[0]][2] );
00097
00098 iFaceSet.push_back( iFaces[0] );
00099 iFaceSet.push_back( iFaces[3] );
00100 iFaceSet.push_back( iFaces[6] );
00101 } else if( sscanf( &lbuffer[2], "%d %d %d",
00102 &iFaces[0],
00103 &iFaces[3],
00104 &iFaces[6] ) == 3 )
00105 {
00106 for( int i = 0; i < 9; ++i )
00107 iFaces[i]--;
00108 glVertex3f( V[iFaces[6]][0], V[iFaces[6]][1], V[iFaces[6]][2] );
00109 glVertex3f( V[iFaces[3]][0], V[iFaces[3]][1], V[iFaces[3]][2] );
00110 glVertex3f( V[iFaces[0]][0], V[iFaces[0]][1], V[iFaces[0]][2] );
00111
00112 iFaceSet.push_back( iFaces[0] );
00113 iFaceSet.push_back( iFaces[3] );
00114 iFaceSet.push_back( iFaces[6] );
00115 }
00116
00117
00118 default:
00119 break;
00120 }
00121 }
00122 glEnd();
00123 glPopMatrix();
00124 glEndList();
00125 fclose( f );
00126
00127 iIndexCount = (int)iFaceSet.size();
00128 iIndices = new int[iIndexCount];
00129 for( int i = 0; i < iIndexCount; ++i )
00130 iIndices[i] = iFaceSet[i];
00131
00132 iVertexCount = (int)V.size() * 3;
00133 fVertices = new float[iVertexCount];
00134 for( unsigned i = 0; i < V.size(); ++i )
00135 for( unsigned j = 0; j < V[i].size(); ++j )
00136 fVertices[i*3+j] = V[i][j];
00137
00138
00139 return true;
00140 }
00141
00142