/*
	Rogue Trader.
	Copyright (c) 2007, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef MD2MESH_H
#define MD2MESH_H

//##############################################################################
// C++ includes: can't forward reference these
//##############################################################################

#include "Mesh.h"

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   MD2Mesh
// Purpose:      Loads and stores MD2 model data according to the generalised
//               mesh class. If the model cannot be loaded for whatever reason
//               then a default model is generated instead (a cube).
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

class MD2Mesh : public Mesh
{

 public:

        //----------------------------------------------------------------------
        // Functions
        //----------------------------------------------------------------------

        // Constructor: loads the given model data and a skin to map onto it

        MD2Mesh::MD2Mesh( char * modelFile, char * skinFile );

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

 protected:

        //----------------------------------------------------------------------
        // MD2 file constants
        //----------------------------------------------------------------------

        static const unsigned MD2_FILE_ID           = 844121161;    // the file ID for MD2 or 'IDP2' when interpreted as 4 char ascii
        static const unsigned MD2_VERSION           = 8;            // must be this value, else an unknown type of md2
        static const unsigned MD2_MAX_TRIANGLES     = 4096;         // maximum number of triangles in an MD2
        static const unsigned MD2_MAX_VERTICES      = 2048;         // maximum number of vertices in an MD2
        static const unsigned MD2_MAX_FRAMES        = 512;          // maximum number of frames in an MD2
        static const unsigned MD2_MAX_SKINS         = 32;           // maximum number of frames in an MD2
        static const unsigned MD2_MAX_SKINNAME_LEN  = 16;           // maximum lenght of a skin name

        //----------------------------------------------------------------------
        // MD2 file structs
        //----------------------------------------------------------------------

        typedef struct MD2Header                    // The MD2 header file struct: goes at start of file
        {
            unsigned MD2FileID;
            unsigned MD2Version;
            unsigned skinWidth;
            unsigned skinHeight;
            unsigned frameSize;
            unsigned numSkins;
            unsigned numVertices;
            unsigned numTexCoords;
            unsigned numTriangles;
            unsigned numGlCommands;
            unsigned numFrames;
            unsigned offsetSkins;
            unsigned offsetTexCoords;
            unsigned offsetTriangles;
            unsigned offsetFrames;
            unsigned offsetGlCommands;
            unsigned offsetEnd;
        };

        typedef struct MD2TexCoord                  // MD2 Texture coordinates: tex coords are stored in short representation
        {
            unsigned short u;
            unsigned short v;
        };

        typedef struct MD2Triangle                  // MD2 Triangle: holds indexes into vertex and texture coordinate arrays, not actual data
        {
            unsigned short vertex1_index;
            unsigned short vertex2_index;
            unsigned short vertex3_index;
            unsigned short texCoord1_index;
            unsigned short texCoord2_index;
            unsigned short texCoord3_index;
        };

        typedef struct MD2FrameHeader               // Header for the begining of frame data: holds values needed to decompress frame and frame name
        {
            float frameScaling_x;
            float frameScaling_y;
            float frameScaling_z;

            float frameTranslation_x;
            float frameTranslation_y;
            float frameTranslation_z;

            unsigned char frameName[MD2_MAX_SKINNAME_LEN];
        };

        typedef struct MD2Vertex                    // Compressed MD2 vertex. Must use MD2 header info to decompress
        {
            unsigned char x;
            unsigned char y;
            unsigned char z;
            unsigned char normal_index;             // Q2 used this as a index into an array of predefined normals.
        };

}; // end of: class MD2Mesh{}

#endif // end of: ifndef MD2MESH_H
