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

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

#include <cassert>
#include <Windows.h>
#include <gl/gl.h>
#include "MathLibrary.h"

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

class GameObject;

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   Camera
// Purpose:      Class that describes a position and orientation of a camera.
//               Also contains functions for manipulating that view. The camera
//               rotation functions are completely 6 degrees of freedom and are
//               not constrained by the camera's current orientation. This sort
//               of movement is required for an outdoor space game like this.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

class Camera
{
public:

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

        Camera();   // Default constructor
        
        // Destructor
        
        ~Camera()
        { 
			// Debug only:
			
			#ifdef DEBUG
			
				assert( unsigned(followObject) != 0xffffffff );
			
				// Assign this for future checks:
				
				followObject = (GameObject*)(0xffffffff);
				
			#endif
		}

        // View scene from this camera. View must be setup in perspective mode first.

        void viewFrom();

        // Move camera's position by amount

        void move( float x , float y , float z );

        // Move camera relative to its own direction

        void moveLocal( float x , float y , float z );

        // Follow an object. Position and orientation of camera become relative to object. Set to null to stop following.
        // Camera does not continously track object, so follow must be called continuously to have camera follow objects
        // motion through space:

        void follow( GameObject * object , bool followObjectVisible );

        // Get the object being followed: returns null if no object is being followed

        inline GameObject * getFollowObject(){ return followObject; }

        // Get whether the camera is to draw the object being followed:

        inline bool showFollowObject(){ return followObjectVisible; }

        // Reset the camera's position and orientatation

        void reset();

        // Reset the camera's orientation

        void resetOrientation();

        // Set the cameras position

        void setPosition( float x , float y , float z );

        // Get the cameras position and store in 3 component vector: if following, object being followed position is taken into account

        void getPosition( float * position );

        // Rotate the camera's orientation about its own axes

        void rotateX( float angle );   // rotate about the x axis
        void rotateY( float angle );   // rotate about the y axis
        void rotateZ( float angle );   // rotate about the z axis

        // Matrix retrieval

        void getOrientationMatrix   ( float * matrix );    // returns the cameras 3x3 orientation matrix and saves to matrix: if following an object it is the combined object and camera orientation.
        void getHomogenousMatrix    ( float * matrix );    // gets the combined rotations and translations of the camera and saves to the 4x4 matrix given

private:

        //----------------------------------------------------------------------
        // Functions
        //----------------------------------------------------------------------
        
		// Copying is banned
		
		Camera( const Camera & other );

		// Copying is banned
		
		Camera & operator = ( const Camera & other );

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

        // Cameras own position and orientation: if following an object these are relative to the object being followed.

        float matrix[9];    // 3x3 matrix describing the cameras orientation
        float pos_x;        // camera position: x
        float pos_y;        // camera position: y
        float pos_z;        // camera position: z

        // Variables for object being followed

        bool            followObjectVisible;    // draw the object being followed ?
        GameObject *    followObject;           // pointer to the object being followed, may be null
        float           followMatrix[9];        // matrix describing the orientation of the object being followed
        float           followPos_x;            // position of object being followed: x
        float           followPos_y;            // position of object being followed: y
        float           followPos_z;            // position of object being followed: z

}; // end of: class Camera{}

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

#include "GameObject.h"

#endif // end of: ifndef CAMERA_H
