/*!
	@project Rune
	@file RuHelper_PostFX.cpp

	Copyright (c) 2004-2005 All rights reserved

	@author John Tang
	@date 2005/04/27
*/

#include "../Helper/RuHelper_PostFX.h"

#include "../Direct3D/Engine/RuEngineD3D.h"

#pragma unmanaged

// ************************************************************************************************************************************************************

/*!
	Applies a post process blur filter to the specified swap chain, using the two
	supplied render target textures as scratch space. Must be called outside of a
	Begin/End pair.

	@param affectedSwapChain Swap chain to apply the post process filter on.
	@param scratchTex0 Scratch texture 0.
	@param scratchTex1 Scratch texture 1.

	@return TRUE if successful, FALSE otherwise.
*/
BOOL g_woo = FALSE;

BOOL RuPostFX_Blur(IRuSwapChain *affectedSwapChain, IRuTexture *scratchTex0, IRuTexture *scratchTex1)
{
	// Copy contents of affected swap chain to scratch texture 0
	if(g_ruEngine->CopyTexture(affectedSwapChain, scratchTex0, 0) == FALSE)
		return FALSE;

	if(g_woo == FALSE)
	{
		g_woo = TRUE;
//		D3DXSaveTextureToFile("d:\\atlas.dds", D3DXIFF_DDS, ((CRuTextureD3D *) scratchTex0)->GetIDirect3DTexture(), NULL);
	}

	// Set rect sizes
	CRuRectangle srcRect, destRect;
	srcRect.Set(0.0f, 0.0f, static_cast<REAL>(scratchTex0->GetLevelWidth(0)), static_cast<REAL>(scratchTex0->GetLevelHeight(0)));
	destRect.Set(0.0f, 0.0f, static_cast<REAL>(scratchTex1->GetLevelWidth(0)), static_cast<REAL>(scratchTex1->GetLevelHeight(0)));

	// Disable 2D viewport scale
	g_ruUIRenderer->ViewportScale_Push(0.0f, 0.0f);

	for(INT32 i = 0; i < 1; ++i)
	{
		// Pick target texture (scratch texture 1) and begin 2D rendering
		g_ruUIRenderer->RenderTarget_Push(scratchTex1);

		// Set up Gaussian filter
		g_ruUIRenderer->SetEffectType(ru2DFXTYPE_GAUSSIAN);
		g_ruUIRenderer->SetEffectParameter(ru2DFXPARAM_COLOR, RUCOLOR_ARGB(255, 255, 255, 255));
		g_ruUIRenderer->SetTexture(scratchTex0);

		// Submit and flush
		g_ruUIRenderer->Submit(srcRect, destRect);
		g_ruUIRenderer->Flush();
		g_ruUIRenderer->RenderTarget_Pop();

		// Pick target texture (scratch texture 0) and begin 2D rendering
		g_ruUIRenderer->RenderTarget_Push(scratchTex0);

		// Set up Gaussian filter
		g_ruUIRenderer->SetEffectType(ru2DFXTYPE_GAUSSIAN);
		g_ruUIRenderer->SetEffectParameter(ru2DFXPARAM_COLOR, RUCOLOR_ARGB(255, 255, 255, 255));
		g_ruUIRenderer->SetTexture(scratchTex1);

		// Submit and flush
		g_ruUIRenderer->Submit(srcRect, destRect);
		g_ruUIRenderer->Flush();
		g_ruUIRenderer->RenderTarget_Pop();
	}

	// Select affected swap chain and begin 2D rendering
	g_ruUIRenderer->RenderTarget_Push(affectedSwapChain);

	// Setup alpha blit
	g_ruUIRenderer->SetEffectType(ru2DFXTYPE_BLIT_FILTER);
	g_ruUIRenderer->SetEffectParameter(ru2DFXPARAM_COLOR, RUCOLOR_ARGB(120, 255, 255, 255));
	g_ruUIRenderer->SetTexture(scratchTex0);

	destRect.m_left = 0.0f;
	destRect.m_top = 0.0f;
	destRect.m_right = static_cast<REAL>(affectedSwapChain->GetWidth());
	destRect.m_bottom = static_cast<REAL>(affectedSwapChain->GetHeight());

	// Submit and flush
	g_ruUIRenderer->Submit(srcRect, destRect);
	g_ruUIRenderer->Flush();
	g_ruUIRenderer->RenderTarget_Pop();

	// Restore 2D viewport scale
	g_ruUIRenderer->ViewportScale_Pop();

	return TRUE;
}

// ************************************************************************************************************************************************************

BOOL RuPostFX_Blur(IRuTexture *sourceTexture, IRuTexture *scratchTex0)
{
	// Set rect sizes
	CRuRectangle srcRect, destRect;
	srcRect.Set(0.0f, 0.0f, static_cast<REAL>(sourceTexture->GetLevelWidth(0)), static_cast<REAL>(sourceTexture->GetLevelHeight(0)));
	destRect.Set(0.0f, 0.0f, static_cast<REAL>(sourceTexture->GetLevelWidth(0)), static_cast<REAL>(sourceTexture->GetLevelHeight(0)));
/*
	for(INT32 i = 0; i < 1; ++i)
	{
		// Pick target texture (scratch texture 1) and begin 2D rendering
		scratchTex0->SelectAsRenderTarget(0, 0);

		// Set up Gaussian filter
		g_ruUIRenderer->SetEffectType(ru2DFXTYPE_GAUSSIAN);
		g_ruUIRenderer->SetEffectParameter(ru2DFXPARAM_COLOR, RUCOLOR_ARGB(255, 255, 255, 255));
		g_ruUIRenderer->SetEffectParameter(ru2DFXPARAM_ROTATE, ru2DROTATE_NONE);
		g_ruUIRenderer->SetTexture(sourceTexture);

		// Submit and flush
		g_ruUIRenderer->Submit(srcRect, destRect);
		g_ruUIRenderer->End();

		// Pick target texture (scratch texture 0) and begin 2D rendering
		sourceTexture->SelectAsRenderTarget(0, 0);

		// Set up Gaussian filter
		g_ruUIRenderer->SetEffectType(ru2DFXTYPE_GAUSSIAN);
		g_ruUIRenderer->SetEffectParameter(ru2DFXPARAM_COLOR, RUCOLOR_ARGB(255, 255, 255, 255));
		g_ruUIRenderer->SetEffectParameter(ru2DFXPARAM_ROTATE, ru2DROTATE_NONE);
		g_ruUIRenderer->SetTexture(scratchTex0);

		// Submit and flush
		g_ruUIRenderer->Submit(srcRect, destRect);
		g_ruUIRenderer->End();
	}
*/
	return TRUE;
}

// ************************************************************************************************************************************************************

BOOL RuPostFX_Darken(IRuSwapChain *affectedSwapChain, IRuTexture *darkenTexture)
{
	// Set rect sizes
	CRuRectangle srcRect, destRect;
	srcRect.Set(0.0f, 0.0f, static_cast<REAL>(darkenTexture->GetLevelWidth(0)), static_cast<REAL>(darkenTexture->GetLevelHeight(0)));
	destRect.Set(0.0f, 0.0f, static_cast<REAL>(affectedSwapChain->GetWidth()), static_cast<REAL>(affectedSwapChain->GetHeight()));

	// Pick target texture and begin 2D rendering
	g_ruUIRenderer->RenderTarget_Push(affectedSwapChain);

	// Setup modulation blit
	g_ruUIRenderer->SetEffectType(ru2DFXTYPE_MODULATE);
	g_ruUIRenderer->SetEffectParameter(ru2DFXPARAM_COLOR, RUCOLOR_ARGB(255, 255, 255, 255));
	g_ruUIRenderer->SetTexture(darkenTexture);

	// Submit and flush
	g_ruUIRenderer->Submit(srcRect, destRect);

	// Pop off render target
	g_ruUIRenderer->RenderTarget_Pop();

	return TRUE;
}

// ************************************************************************************************************************************************************

#pragma managed
