/*
 Copyright (C) 2011 by Ivan Safrin
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */


#pragma once
#include "PolyLogger.h"
#include "PolyString.h"
#include "PolyGlobals.h"
#include "PolyCoreServices.h"
#include "PolySceneEntity.h"
#include "PolySceneRenderTexture.h"

namespace Polycode {

	/**
	* Camera in a 3D scene. Cameras can be added to a scene and changed between dynamically. You can also set a shader to a camera that will run as a screen shader for post-processing effects.
	*/	
	class _PolyExport Camera : public SceneEntity {
		public:
			
			/**
			* Constructor.
			* @param parentScene Scene to add the camera to.
			*/
			Camera(Scene *parentScene);
			~Camera();
			
			void buildFrustrumPlanes();
			
			/**
			* Checks if the camera can see a sphere.
			* @param pos Position of the sphere to check.
			* @param fRadius Radius of the sphere.
			* @return Returns true if the sphere is within the camera's frustrum, or false if it isn't.
			* @see canSee()
			*/								
			bool isSphereInFrustrum(Vector3 pos, Number fRadius);
		
			/**
			* Checks if the camera can see an entity based on its bounding radius.
			* @param entity Entity to check.
			* @return Returns true if the entity's bounding radius is within the camera's frustrum, or false if it isn't.
			* @see isSphereInFrustrum()
			*/					
			bool canSee(SceneEntity *entity);
			
			void setOrthoMode(bool mode);
			bool getOrthoMode();
			
			/**
			* Sets the field of view (FOV) for the camera. The larger the field of view, the more the camera can see, the smaller it is, the more zoomed in it is.
			* @param fov The new FOV value.
			*/			
			void setFOV(Number fov);
			
			/**
			* Returns the current FOV value for the camera.
			* @return Current FOV value for the camera.
			*/			
			Number getFOV();
			
			void setParentScene(Scene *parentScene);
			
			void doCameraTransform();
			void setLightDepthTexture(Texture *texture);			

			bool hasFilterShader();
			void drawFilter();
			
			/**
			* Sets the exposure for the camera. The exposure value can be passed to a shader for HDR rendering.
			* @param level The new exposure value.
			*/						
			void setExposureLevel(Number level);
			
			/**
			* Returns the camera's exposure value.
			* @return Current exposure value.
			*/									
			Number getExposureLevel();
			
			void createPostFilter(Material *shaderMaterial);
			
			/**
			* Sets the post-processing shader for the camera.
			* @param shaderName The shader name of the post-processing filter.
			*/												
			void setPostFilter(const String& shaderName);
			
			/**
			* Removes the currently assigned post filter.
			*/
			void removePostFilter();
			
			/**
			* Returns the local shader options for the camera post processing material.
			*/
			vector<ShaderBinding*> getLocalShaderOptions() { return localShaderOptions; }
			
			/**
			* Returns the shader material applied to the camera.
			*/			
			Material *getScreenShaderMaterial() { return filterShaderMaterial; }
			
			
		protected:
		
			Number exposureLevel;
			bool orthoMode;
			Number fov;
			Number frustumPlanes[6][4];
			Scene *parentScene;
		
			bool fovSet;

			Material *filterShaderMaterial;			
			Texture *originalSceneTexture;			
			Texture *zBufferSceneTexture;						
			vector<ShaderBinding*> localShaderOptions;
			bool _hasFilterShader;
	};	
}
