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

//##############################################################################
// C Includes
//##############################################################################

#include <cstring>
#include <cmath>
#include <Windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "MathLibrary.h"

//##############################################################################
// Forward references
//##############################################################################

class GLTexture;

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   Mesh
// Purpose:      Serves as a base class for all mesh objects. Defines the format
//               and layout for mesh data (vertices, texcoords and normals).
//               Also provides a handy function for generating surface normals
//               based on vertex data.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

class Mesh
{

 public:

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

        // Constructor:

        Mesh();

        // Destructor: deletes all data arrays and mesh skin

        virtual ~Mesh();

        // Draws the mesh orientated and positioned as specified. Orientation is given as a 3x3 rotation matrix.

        void draw
        (
            float      pos_x,
            float      pos_y,
            float      pos_z,
            float *    orientation

        ) const;

        // Debugging purposes: draws the meshe's normals

        void drawNormals
        (
            float      pos_x,
            float      pos_y,
            float      pos_z,
            float *    orientation

        ) const;

        // Debugging purposes: draws the meshe's bounding sphere

        void drawBoundingSphere
        (
            float      pos_x,
            float      pos_y,
            float      pos_z,
            float *    orientation

        ) const;

        // Accessors:

        inline const float * getVertexArray     ()                  const { return vertexArray;     }
        inline const float * getTexCoordArray   ()                  const { return texCoordArray;   }
        inline const float * getNormalArray     ()                  const { return normalArray;     }
        inline const float * getEdges           ()                  const { return edgeArray;       }
        inline const float * getEdgeNormalArray ()                  const { return edgeNormalArray; }

        inline unsigned getNumVertices  () const { return numVertices;    }
        inline unsigned getNumTriangles () const { return numTriangles;   }

        inline float getSphereRadius()  const { return sphereRadius;  }
        inline float getSphereCenterX() const { return sphereCenterX; }
        inline float getSphereCenterY() const { return sphereCenterY; }
        inline float getSphereCenterZ() const { return sphereCenterZ; }

        inline GLTexture * getTexture() const { return meshSkin; }

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

 protected:

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

        void defaultMesh();             // constructs a cube mesh. used when a mesh can't load or is invalid.
        void generateNormals();         // generates surface normals for the mesh. smoothed normals are not used for asthetic reasons.
        void generateEdges();           // generates mesh edge vectors. assumes vertex array is initialised.
        void generateEdgeNormals();     // generates mesh edge normal vectors. assumes normal and edge arrays are initialised.
        void generateBoundingSphere();  // creates a bounding sphere for the mesh

        //----------------------------------------------------------------------
        // Variables
        //----------------------------------------------------------------------

        GLTexture * meshSkin;       // the skin used for the mesh

        float sphereCenterX;        // bounding sphere center: x
        float sphereCenterY;        // bounding sphere center: y
        float sphereCenterZ;        // bounding sphere center: z
        float sphereRadius;         // bounding sphere radius

        unsigned numVertices;       // number of vertices in the model
        unsigned numTriangles;      // number of triangles in the model

        // the array of triangle vertices used for openGL rendering. elements are
        // ordered as follows:
        //
        // triangle 1 : vertex 1 : x
        // triangle 1 : vertex 1 : y
        // triangle 1 : vertex 1 : z
        // triangle 1 : vertex 2 : x
        // ..
        // triangle 1 : vertex 3 : x
        // ..
        // triangle 2 : vertex 1 : x
        // ..
        // .. and so on

        float * vertexArray;

        // triangle edge vectors:
        //
        // triangle 1 : edge 1 : x
        // triangle 1 : edge 1 : y
        // triangle 1 : edge 1 : z
        // triangle 1 : edge 2 : x
        // ..
        // triangle 1 : edge 3 : x
        // ..
        // triangle 2 : edge 1 : x
        // ..
        // .. and so on

        float * edgeArray;

        // triangle edge plane normal vectors:
        //
        // triangle 1 : edge normal 1 : x
        // triangle 1 : edge normal 1 : y
        // triangle 1 : edge normal 1 : z
        // triangle 1 : edge normal 2 : x
        // ..
        // triangle 1 : edge normal 3 : x
        // ..
        // triangle 2 : edge normal 1 : x
        // ..
        // .. and so on

        float * edgeNormalArray;

        // the array of texture coordinates used for openGL rendering. elements are
        // ordered as follows:
        //
        // triangle 1 : vertex 1 : x
        // triangle 1 : vertex 1 : y
        // triangle 1 : vertex 1 : z
        // triangle 1 : vertex 2 : x
        // ..
        // triangle 1 : vertex 3 : x
        // ..
        // triangle 2 : vertex 1 : x
        // ..
        // .. and so on

        float * texCoordArray;

        // the array of normals used for openGL rendering. elements are
        // ordered as follows:
        //
        // triangle 1 : vertex 1 normal : x
        // triangle 1 : vertex 1 normal : y
        // triangle 1 : vertex 1 normal : z
        // triangle 1 : vertex 2 normal : x
        // ..
        // triangle 1 : vertex 3 normal : x
        // ..
        // triangle 2 : vertex 1 normal : x
        // ..
        // .. and so on
        //
        // Also there are corresponding arrays of

        float * normalArray;

}; // end of: class Mesh{}

//##############################################################################
// C++ Includes
//##############################################################################

#include "GLTexture.h"

#endif // end of: ifndef MESH_H
