//////////////////////////////////////////////////////////////////////////////
//
// Asura_PlatformSkybox.cpp
//
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//
//////////////////////////////////////////////////////////////////////////////
//
// Version History
//
// Created by KJFL - Tuesday, March 30, 2004
//
// Done first pass of base functionality except for tinting of skybox by
// 'skybox colour' and fog colour. The later will only be required once we
// have fog on the environment etc.
//
//////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

// This file's header.
#include "SpecialFX/Asura_Skybox.h"

// Platform headers
#include "Camera/Asura_Camera.h"
#include "Rendering/Asura_PSP_Graphics.h"
#include "Rendering/Asura_PSP_GU.h"
#include "Rendering/Asura_PSP_Stencil_Enums.h"
#include "Rendering/Asura_PSP_StencilBufferManager.h"
#include "SpecialFX/Asura_PSP_Skybox.h"

// GPU profile
#include "Debugging/Asura_PSP_Profiling.h"

void Asura_Skybox::Platform_DoRenderSetup(void)
{
	Asura_PSP_Skybox::DoRenderSetup();
}

void Asura_Skybox::Platform_Reset(void)
{
	Asura_PSP_Skybox::Reset();
}

void Asura_Skybox::Platform_Initialise(void)
{
	Asura_PSP_Skybox::Initialise();
}

void Asura_Skybox::Platform_Render(void)
{

#ifdef ASURA_PSP_PROFILING
	Asura_PSP_Profiling::Signal( Asura_PSP_Profiling::ASURA_PSP_SIGNAL_SKYBOX_START ); 
#endif // #ifdef ASURA_PSP_PROFILING

	// JG - Now we stencil the skybox to allow glows affecting only specific bits of the screen
	Asura_PSP_StencilBufferManager::SetStencilOperation(ASURA_PSP_STENCIL_OPERATION_KEEP, ASURA_PSP_STENCIL_OPERATION_KEEP, ASURA_PSP_STENCIL_OPERATION_REPLACE);
	Asura_PSP_StencilBufferManager::SetStencilFunction(ASURA_PSP_STENCIL_FUNCTION_ALWAYS, ASURA_PSP_STENCIL_SKYBOX, 0xFF);
	Asura_PSP_StencilBufferManager::SetStencilEnabled(true);
	GU_PixelMask(0x0);

	// If you adjust the near clip plane beyond 1M, it can end up clipping out part or all of the skybox;
	// it should be safe to assume that we never want the skybox clipped out, so adjust the projection
	// matrix with an offset to counter this.	
	Asura_PSP_Graphics::SetupPSPProjectionMatrix(-Asura_Camera::GetNearClip());

	Asura_PSP_Skybox::Render();

	Asura_PSP_Graphics::SetupPSPProjectionMatrix(0.0f);

	// JG - End of skybox drawing mask
	Asura_PSP_StencilBufferManager::SetStencilFunction(ASURA_PSP_STENCIL_FUNCTION_ALWAYS, ASURA_PSP_STENCIL_NONE, 0xFF);

#ifdef ASURA_PSP_PROFILING
	Asura_PSP_Profiling::Signal( Asura_PSP_Profiling::ASURA_PSP_SIGNAL_SKYBOX_END ); 
#endif // #ifdef ASURA_PSP_PROFILING

}

void Asura_Skybox::Platform_Shutdown(void)
{
	Asura_PSP_Skybox::Shutdown();
}

bool Asura_Skybox::Platform_ShouldModifyFogForSkyboxObjects()
{
	return true;
}
