/*
 * This source file is part of KEOS (Free 3D Engine)
 * For the latest info, see http://www.keosengine.org/
 * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
 * http://www.gnu.org/copyleft/lesser.txt.
 *
 */
#ifndef KEOS_MD2LOADER_H
#define KEOS_MD2LOADER_H

#include "KeosPrerequisites.h"
#include "KeosLoader.h"
#include "KeosModel.h"

namespace Keos
{
    //-----------------------------------------------------------------------
    /** MD2 Model loader.
     */
    class CMD2Loader : public ILoader<CModel>
    {
    public :

        /** Load a model from a file.
         *  @param strFilename Name of the file to be loaded.
         *  @return Pointer on the created model.
         */
        virtual CModel* LoadFromFile(const String& strFilename);

    private :
        
        //! Magic identifier "IDP2"
        static const int ms_nMagicId    = ('2' << 24) + ('P' << 16) + ('D' << 8) + 'I'; 
        //! Managed number of version
        static const int ms_nMD2Version = 8;  
        //! Table of the calculated normals
        static const TVector3F ms_NormalTable[];                                  

        #include <pshpack1.h>
        
        //! MD2 Header
        struct THeader
        {
            int Ident;            // This is used to identify the file
            int Version;          // The version number of the file (Must be 8)
            int TexWidth;         // The skin width in pixels
            int TexHeight;        // The skin height in pixels
            int FrameSize;        // The size in bytes the frames are
            int NbTextures;       // The number of skins associated with the model
            int NbVertices;       // The number of vertices (constant for each frame)
            int NbTexCoords;      // The number of texture coordinates
            int NbTriangles;      // The number of faces (polygons)
            int NbGLCommands;     // The number of gl commands
            int NbFrames;         // The number of animation frames
            int OffsetTextures;   // The offset in the file for the skin data
            int OffsetTexCoords;  // The offset in the file for the texture data
            int OffsetTriangles;  // The offset in the file for the face data
            int OffsetFrames;     // The offset in the file for the frames data
            int OffsetGLCommands; // The offset in the file for the gl commands data
            int OffsetEnd;        // The end of the file offset
        };

        //! Struct for the vertices that are read in for the current frame
        struct TPoint
        {
            unsigned char x, y, z; 
            unsigned char n; // light normal index    
        };

        //! This stores UV coordinates
        struct TTexCoord
        {
            short u, v; 
        };

        //! This stores the indices into the vertex and texture coordinate arrays
        struct TTriangle
        {
            unsigned short Vertices[3];
            unsigned short TexCoords[3];
        };

        // This stores a skin name
        struct TTexture
        {
            char Name[68];
        };

        // This stores the animation scale, translation and name information for a frame, plus verts
        struct TFrame
        {
            TVector3F           Scale;
            TVector3F           Translate;
            char                Name[16];
            std::vector<TPoint> Vertices;
        };
        #include <poppack.h>
    };
} // namespace Keos

#endif // KEOS_MD2LOADER_H
