/*
---------------------------------------------------------------------------
Direct3D AssimpRenderer by ponx
---------------------------------------------------------------------------

This code is heavily based on the "AssimpView" project of the Open Asset Import Library (ASSIMP).
http://assimp.sourceforge.net

/*
---------------------------------------------------------------------------
Open Asset Import Library (ASSIMP)
---------------------------------------------------------------------------

Copyright (c) 2006-2008, ASSIMP Development Team

All rights reserved.

Redistribution and use of this software 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 and/or other
materials provided with the distribution.

* Neither the name of the ASSIMP team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the ASSIMP Development Team.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"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 THE COPYRIGHT 
OWNER OR CONTRIBUTORS 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.
---------------------------------------------------------------------------
*/


#pragma once

#include "RenderOptions.h"
#include "AssetHelper.h"
#include "Camera.h"
#include "aiVector3D.h"
#include "aiScene.h"
#include <LogStream.h>


//#define DEBUG_VS                            // vertex shader debuggen ...macht nur Sinn, wenn eine externe Datei .fx benutzt wird, weil man sich sonst im PIX Debugger nur den Asm code angucken kann
//#define DEBUG_PS                            // pixel shader debuggen ... s.o.


#define USE_EXTERNAL_SHADER_FILE                // auskommentieren, falls als Source für die Shader die externe .fx Datei (FILENAME_EXTERNAL_EFFECT) benutzt werden soll 
   
#define FILENAME_EXTERNAL_EFFECT                    L"MaterialShader.fx"				 //wird nur benutzt, wenn USE_EXTERNAL_SHADER_FILE #definiert ist


namespace AssimpRendererD3d
{
	/// This class provides the main interface for importing, rendering, storing and saving ASSIMP assets using Direct3D.
	/** 
	*
	*/
	class AssimpRenderer
	{
		friend class AssetHelper;
		friend class CMaterialManager;
		friend class CMeshRenderer;

	public:

		static AssimpRenderer* Instance();
		static AssetHelper* ImportAsset(const char* pFile, unsigned int aiImportFlags);		
		static AssetHelper* ImportAsset(const char* pFile, unsigned int aiImportFlags, const D3DXMATRIX& matTransform);		

		AssetHelper* LoadAssetFromFile(aiString filename);							//PNX
		int SaveAssetToFile(const AssetHelper& asset, aiString filename);										//PNX

		LPDIRECT3DDEVICE9 CreateD3dDevice(	LPDIRECT3D9 d3d, 
												D3DPRESENT_PARAMETERS* d3dpp, 
												const RenderOptions& renderOptions);

		void RenderAsset(const AssetHelper& assetHelper, aiMatrix4x4 argMatrix);	// PNX
		void RenderAsset(const AssetHelper& assetHelper, const aiVector3D& vPos, const aiVector3D& vRot, const aiVector3D& vScale);		//PNX
		void SetCamera(const Camera& sourceCamera);
		int AttachLogStream(Assimp::LogStream* logstr, unsigned int severity);							//PNX
		int DetachLogStream( Assimp::LogStream* logstr, unsigned int severity );						//PNX
		static D3DXMATRIX CreateTransformMatrix(float rotX, float rotY, float rotZ, float scaleFactor);						//PNX
		

	private: 
		AssimpRenderer(void);
		~AssimpRenderer(void);

		static AssimpRenderer* sInstance;		
		static void ConvertMatrixDxToAi(aiMatrix4x4& aiDest, const D3DXMATRIX& dxSource);	//PNX
		static void ConvertMatrixAiToDx(D3DXMATRIX& dxDest, const aiMatrix4x4& aiSource);	//PNX
		static AssetHelper* ImportAssetInternal(const char* pFile, unsigned int aiImportFlags, const D3DXMATRIX* matTransform);
		static int CreateAssetData(AssetHelper* g_pcAsset, const aiScene* scene);				
		static void TransformVector(aiVector3D* aiVec, const D3DXMATRIX& matrix);		//PNX
		static int SendMeshDataToDx(AssetHelper::MeshHelper* meshHelper);			//PNX
		int RenderNode(const AssetHelper& g_pcAsset, aiNode* piNode,const aiMatrix4x4& piMatrix, bool bAlpha);
		int CreateD3dDevice(bool p_bMultiSample, bool bHW = true);
		int GetProjectionMatrix(aiMatrix4x4& p_mOut);
		aiVector3D GetCameraMatrix(aiMatrix4x4& p_mOut);								// achtung, davon gibts zwei versionen. Fehlt bei meiner was ? andere erstmal auskommentiert
		std::string GetCurrentDir();

		// Direct 3D
		LPDIRECT3D9 d3d;
		LPDIRECT3DDEVICE9 d3ddev;
		D3DPRESENT_PARAMETERS* d3dpp;
		IDirect3DVertexDeclaration9* gDefaultVertexDecl;

		TCHAR* applicationBaseDirectory;			// this will be set to the current active directory at the point of time when the Singleton is created
		RenderOptions renderOptions;

		ID3DXEffect* g_piDefaultEffect;
		ID3DXEffect* g_piPassThroughEffect;
		ID3DXEffect* g_piPatternEffect;

		D3DCAPS9 g_sCaps;

		Camera m_Camera;			//das sollte lieber noch nen setter oder sowas kriegen;
	};

}  // namespace