#include "ShadowMapping.h"
#include "InputLayouts.h"

HRESULT ShadowMapping::init()
{
	HRESULT hr = S_OK;

    DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
	#if defined( DEBUG ) || defined( _DEBUG )
		dwShaderFlags |= D3D10_SHADER_DEBUG;
	#endif

	ID3D10Blob *pErrorMsg;
	hr = D3DX10CreateEffectFromFile( L"../../shaders/ShadowMapping.fx", NULL, NULL, "fx_4_0", dwShaderFlags, 0, DXUTGetD3D10Device(), NULL,
                                         NULL, &effect, &pErrorMsg, NULL );

	if(pErrorMsg)
	{
		 //_RPT1(_CRT_WARN, "compilation error : %s\n", (char*) pErrorMsg->GetBufferPointer()); 
		  SAFE_RELEASE(pErrorMsg);

	}
    if( FAILED( hr ) )
    {
        MessageBox( NULL,
                    L"The FX file cannot be located.  Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
		V_RETURN(hr);
    }
	technique = effect->GetTechniqueByName("Render");
	modelMatrixVariable = effect->GetVariableByName( "modelMatrix" )->AsMatrix();
	lightViewMatrixVariable = effect->GetVariableByName( "lightViewMatrix" )->AsMatrix();
    lightProjectionMatrixVariable = effect->GetVariableByName( "lightProjectionMatrix" )->AsMatrix();

	ID3D10Device* pd3dDevice = DXUTGetD3D10Device();
	
	
	D3D10_PASS_DESC PassDesc;
	technique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
    pd3dDevice->CreateInputLayout( layout, numElements, PassDesc.pIAInputSignature,
    PassDesc.IAInputSignatureSize, &vertexLayout);
	pd3dDevice->IASetInputLayout( vertexLayout );

	return S_OK;
}

ID3D10Effect* ShadowMapping::getEffectVariable()
{
	return effect;
}

ID3D10EffectTechnique* ShadowMapping::getTechniqueVariable()
{
	return technique;
}

void ShadowMapping::setModelMatrix(D3DXMATRIX* modelMatrix)
{
	modelMatrixVariable->SetMatrix((float*)modelMatrix);
}

void ShadowMapping::setViewMatrix(D3DXMATRIX* viewMatrix)
{
	lightViewMatrixVariable->SetMatrix((float*)viewMatrix);
}

void ShadowMapping::setProjectionMatrix(D3DXMATRIX* projMatrix)
{
	lightProjectionMatrixVariable->SetMatrix((float*)projMatrix);
}

ShadowMapping::~ShadowMapping()
{
	SAFE_RELEASE(effect);
	SAFE_RELEASE(vertexLayout);
}