//
// Asura_Platform_Shard_Effect.cpp
//
// Created by KOR - 10 October 2006
//
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

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

// Asura headers
#include "Camera/Asura_Camera.h"
#include "Rendering/Asura_Render.h"

// Platform headers
#include "Rendering/Asura_GX.h"
#include "Rendering/Asura_GX_TextureManagement.h"

// Library headers
#include <revolution/gx.h>

void Asura_Shard_System::Platform_BeginRender(void)
{
	Asura_Render::CurrentStates.RequestTranslucencyMode(ASURA_TRANSLUCENCY_NORMAL);
	Asura_Render::CurrentStates.RequestZBufferEnabled(true);
	Asura_Render::CurrentStates.RequestZBufferWriteEnabled(false);
	Asura_Render::CurrentStates.RequestCullMode(ASURA_CULLMODE_NONE);

	// No lighting
	GXSetChanCtrl(GX_COLOR0A0, GX_DISABLE, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT_NULL, GX_DF_CLAMP, GX_AF_NONE);

	GXClearVtxDesc();
	GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
	GXSetVtxDesc(GX_VA_CLR0, GX_DIRECT);
	GXSetVtxDesc(GX_VA_TEX0, GX_DIRECT);
	// Initialize lighting, texgen, and tev parameters
	GXSetNumChans(1);

	GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
	GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
	GXSetNumTexGens(1);
	GXSetNumChans(1);
	GXSetNumTevStages(1);
}

void Asura_Shard_System::Platform_EndRender(void)
{
	Asura_Render::CurrentStates.RequestZBufferWriteEnabled(true);
}

void Asura_Shard_Effect::Platform_Render(unsigned uCamVisFlags)
{
	Asura_Render::CurrentStates.RequestTextureIndex(0,m_iTextureIndex);
	if(Asura_GX_Texture_Management::IsColourTextureLoaded())
	{
		GXSetTevOp(GX_TEVSTAGE0, GX_MODULATE);
	}
	else
	{
		GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
	}

	extern void RenderShard(Asura_Shard *shardPtr, int TextureIndex, int colour);

	unsigned uColour = (0x00ffffff & m_uColour) << 8;
	unsigned uAlpha = (m_uColour >> 24);
	uAlpha = unsigned(Asura_Maths::FloatToInt(float(uAlpha) * (1.0f - m_fAlpha)));
	uColour |= uAlpha;
	for(int i=0; i<Asura_Camera::GetNumberOfCameras(); i++)
	{
		if (uCamVisFlags & (1 << i))
		{
			Asura_Camera::SetCameraIndex(i);

			Asura_Render::ApplyCameraSettings();
	
			for(int s=0; s<m_iNumberOfActiveShards; s++ )
			{
				GXBegin(GX_TRIANGLES, GX_VTXFMT3, 3);
					GXPosition3f32(m_apxShards[s]->m_axVertices[0].x, m_apxShards[s]->m_axVertices[0].y, m_apxShards[s]->m_axVertices[0].z);
					GXColor1u32(uColour);
					GXTexCoord2f32(m_apxShards[s]->m_afU[0], m_apxShards[s]->m_afV[0]);

					GXPosition3f32(m_apxShards[s]->m_axVertices[1].x, m_apxShards[s]->m_axVertices[1].y, m_apxShards[s]->m_axVertices[1].z);
					GXColor1u32(uColour);
					GXTexCoord2f32(m_apxShards[s]->m_afU[1], m_apxShards[s]->m_afV[1]);

					GXPosition3f32(m_apxShards[s]->m_axVertices[2].x, m_apxShards[s]->m_axVertices[2].y, m_apxShards[s]->m_axVertices[2].z);
					GXColor1u32(uColour);
					GXTexCoord2f32(m_apxShards[s]->m_afU[2], m_apxShards[s]->m_afV[2]);
				GXEnd();
			}
		}
	}
}