/*
 * 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_MDLQ1LOADER_H
#define KEOS_MDLQ1LOADER_H

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

namespace Keos
{
    //-----------------------------------------------------------------------
    /** MDL (Quake 1) Model loader.
     */
    class CMDLQ1Loader : 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 :

		/** Load color map from a file.
         *  @param strFilename Name of the file to be loaded.
         *  @param ColorMap Array which will contain the color map
         */
        void LoadColorMap( const String& strFilename, uchar* ColorMap );
        
        //! Magic identifier "IDPO"
        static const int ms_nMagicId = (('O' << 24) + ('P' << 16) + ('D' << 8) + 'I');
        //! Managed number of version
        static const int ms_nMDLQ1Version = 6;  
        //! Table of the calculated normals
        static const TVector3F ms_NormalTable[]; 
		//! Table of the default colr map
        static const uchar ms_DefaultColorMap[];

        #include <pshpack1.h>

        typedef enum { ST_SYNC = 0, ST_RAND } TSyncType;
        typedef enum { ALIAS_SKIN_SINGLE = 0, ALIAS_SKIN_GROUP } TSkinType;

		//! MDL Header
        struct THeader
        {
	        int			Ident;			// This is used to identify the file
	        int			Version;		// The version number of the file (Must be 6)

	        TVector3F	Scale;			
	        TVector3F	Translate;		// Translation vector
	        float		BoundinGradius; // Bounding sphere radius
	        TVector3F	EyePosition;	// Eye position

	        int			NbTextures;		// The number of textures
	        int			TexWidth;		// largeur texture
	        int			TexHeight;		// hauteur texture

	        int			NbVertices;		// The number of vertices
	        int			NbTriangles;	// The number of triangles
	        int			NbFrames;		// The number of frames

	        TSyncType	SyncType;
	        int			Flags;
	        float		Size;

        };

        // Texture
        struct TTexture
        {
	        TSkinType			Group;	// Value = 0
	        std::vector<uchar>	Data;	// Texture data

        };

        //! This stores texture UV coordinates
        struct TTexCoord
        {
	        int		Onseam;			// 0 or 0x20
	        int		u;
	        int		v;

        };

        // Triangle
        struct TTriangle
        {
	        int		FacesFront;		// 0 = back face, 1 = front face
	        int		Vertices[3];	// Vertices index

        };

        // Vertice
        struct TPoint
        {
	        unsigned char x, y, z;
	        unsigned char n;	// light normal index    

        };

        // Frame
        struct TFrame
        {
	        long				Type;	
	        TPoint				BBoxMin;
	        TPoint				BBoxMax;	
	        char				Name[16];	
	        std::vector<TPoint>	Vertices;

        };

        #include <poppack.h>
    };
} // namespace Keos

#endif // KEOS_MDLQ1LOADER_H
