GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


A Paste by Anonymous
1
 
#include "C3dsLoader.h"

///////////////////////////////////////////////////////////////////////

C3dsLoader::TriMesh::~TriMesh()
{
    if( vertList )
        delete[] vertList;
    if( texCoordList )
        delete[] texCoordList;
    if( faceList )
        delete[] faceList;
}

///////////////////////////////////////////////////////////////////////

void C3dsLoader::GetChunk( Chunk* curChunk )
{
    curChunk->bytesRead += fread( &curChunk->id, 1, 2, m_InFile );
    curChunk->bytesRead += fread( &curChunk->length, 1, 4, m_InFile );
    return;
}

///////////////////////////////////////////////////////////////////////

bool C3dsLoader::ProcessChunk( Chunk* curChunk )
{
    Chunk newChunk;
    
    // End Recursion
    if( curChunk->bytesRead == curChunk->length )
        return false;

    GetChunk( &newChunk );

    switch( newChunk.id )
    {
    // Grouping Chunks
    case CHUNK_OBJECTINFO:
        while( ProcessChunk( &newChunk ) );
        break;

    case CHUNK_TRIMESH:
        while( ProcessChunk( &newChunk ) );
        break;

    case CHUNK_EDITOBJECT:
        {
            // Sets up the current Mesh
            TriMesh* tempTriMesh = new TriMesh;
            char save;

            while( ++newChunk.bytesRead, save = fgetc( m_InFile ) )
                tempTriMesh->meshName += save;

            m_MeshList.push_back( tempTriMesh );
            tempTriMesh = 0;

            ++m_TotalMeshes;

            while( ProcessChunk( &newChunk ) );
        }break;

    ///////////////////////////////////////////////////////////////////////
    // Data Chunks

    case CHUNK_POINT_ARRAY:        // Vertex Info
        {
            short points;
            newChunk.bytesRead += fread( &points, 1, 2, m_InFile );

            m_MeshList[m_MeshList.size() - 1]->totalVerts    = points;
            m_MeshList[m_MeshList.size() - 1]->vertList        = new Vertex[points];

            newChunk.bytesRead += fread( m_MeshList[m_MeshList.size()-1]->vertList, 1, points * sizeof(Vertex), m_InFile );
        }break;

    case CHUNK_TEX_VERTS:        // Texture Coord Info
        {
            short verts;
            newChunk.bytesRead += fread( &verts, 1, 2, m_InFile );

            m_MeshList[m_MeshList.size() - 1]->totalTexCoords    = verts;
            m_MeshList[m_MeshList.size() - 1]->texCoordList        = new TexCoord[verts];

            newChunk.bytesRead += fread( m_MeshList[m_MeshList.size() - 1]->texCoordList, 1, verts * sizeof(TexCoord), m_InFile );
        }break;

    case CHUNK_FACE_ARRAY:        // Vertex Index Info
        {
            short faces;
            newChunk.bytesRead += fread( &faces, 1, 2, m_InFile );

            m_MeshList[m_MeshList.size() - 1]->totalFaces    = faces;
            m_MeshList[m_MeshList.size() - 1]->faceList        = new Face[faces];
            
            newChunk.bytesRead += fread( m_MeshList[m_MeshList.size() - 1]->faceList, 1, faces * sizeof(Face), m_InFile );
        }break;

    ///////////////////////////////////////////////////////////////////////
    // Ignored Chunk, Skip past
    default:    
        fseek( m_InFile, newChunk.length - newChunk.bytesRead, SEEK_CUR );
        newChunk.bytesRead += newChunk.length - newChunk.bytesRead;
        break;
    }

    curChunk->bytesRead += newChunk.bytesRead;

    return true;
} // End C3dsLoader::ProcessChunk

///////////////////////////////////////////////////////////////////////

bool C3dsLoader::Load3ds( const char* fileName )
{
    if( !(m_InFile = fopen( fileName, "rb" )) )
        return false;

    Chunk mainChunk;
    GetChunk( &mainChunk );

    // Test if file is valid
    if( mainChunk.id == CHUNK_PRIMARY )
        while( ProcessChunk( &mainChunk ) );
    else
        return false;                // File not valid

    if( mainChunk.bytesRead != mainChunk.length )
        return false;                // Something went wrong

    fclose( m_InFile );
    return true;
}

///////////////////////////////////////////////////////////////////////

C3dsLoader::~C3dsLoader()
{
    for( int x = 0; x < m_MeshList.size(); ++x )
        delete m_MeshList[x];
}