///////////////////////////////////////////////////////////////////////////////
//
// File   : Asura_Platform_Shape.cpp
// Author : Kevin Floyer-Lea
// Date   : 29-Jun-2005
//
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

/******************************************************************************/
/* Included Files                                                             */
/******************************************************************************/

#include "Core/Asura.h"

#include "Shapes/Asura_Platform_Shape.h"

// Asura Platform-independent
#include "Camera/Asura_Camera.h"
#include "Core/Asura_Material_System.h"
#include "Core/Asura_MaterialResponse_System.h"
#include "Rendering/Asura_Render.h"

// Asura Xbox 360
#include "Rendering/Asura_Xbox360_ObjectRenderer.h"

/******************************************************************************/
/* Asura_Shape_Instance Class Functions                                       */
/******************************************************************************/

void Asura_Shape_Instance::Platform_SetAmbientARGB( const u_int uARGB )
{
	m_uAmbientARGB = uARGB;
	m_uFlags &= ~ASURA_SHAPE_INSTANCE_USES_DEFAULT_AMBIENT;
}

void Asura_Shape_Instance::Platform_SetAmbientRGB( const u_char ucR, const u_char ucG, const u_char ucB, const u_char ucA )
{
	m_uAmbientARGB = (ucA << 24) | (ucR << 16) | (ucG << 8) | ucB;
	m_uFlags &= ~ASURA_SHAPE_INSTANCE_USES_DEFAULT_AMBIENT;
}

void Asura_Shape_Instance::Platform_SetDefaultAmbientRGB()
{
	m_uFlags |= ASURA_SHAPE_INSTANCE_USES_DEFAULT_AMBIENT;
}

/******************************************************************************/
/* Asura_Platform_Shape_RenderData Class Functions                            */
/******************************************************************************/

Asura_Platform_Shape_RenderData::Asura_Platform_Shape_RenderData( const char*& pcChunkBuffer )
{
	#if defined _DEBUG
		m_szName = Asura_StringUtil::Duplicate( pcChunkBuffer );
	#endif

	pcChunkBuffer += Asura_Shape_RenderData::NAME_BUFFER_SIZE;
}

Asura_Platform_Shape_RenderData::~Asura_Platform_Shape_RenderData()
{
	#if defined _DEBUG
		delete[] m_szName;
	#endif
}

/******************************************************************************/
/* Asura_Shape Class Functions                                                */
/******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_Shape::Platform_GetMaterialIdx
// Author       : Kevin Floyer-Lea
// Date         : 26-Jul-2005
//
// Description  : Gets the material index for this shape.
//
// Return value : The index of the material used for this shape.
////////////////////////////////////////////////////////////////////////////////

int Asura_Shape::Platform_GetMaterialIdx() const
{
	return Asura_Xbox360_ObjectRenderer::GetMaterialIndex( *this );
}

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_Shape::Platform_Render
// Author       : Kevin Floyer-Lea
// Date         : 26-Jul-2005
//
// Description  : Attempts to render the specified shape.
//
// Parameters   : The shape, colour and camera flags to use for the render.
//
// Return value : None.
////////////////////////////////////////////////////////////////////////////////

void Asura_Shape::Platform_Render( const Asura_Shape_Instance* pxShapeInst, u_int uAmbientARGB, u_int uCamVisFlags ) const
{
	for( int i = 0; i < Asura_Camera::GetNumberOfCameras(); ++i )
	{
		Asura_Camera::SetCameraIndex(i);

		if( Asura_Camera::IsCameraActive()
		 && ((1 << i) & uCamVisFlags)
		 && ASURA_GUID_UNREGISTERED != Asura_Camera::GetEntityToIgnore() )
		{
			Asura_Render::ApplyCameraSettings();
			Asura_Xbox360_ObjectRenderer::RenderObject( *this, *pxShapeInst, uAmbientARGB );
		}
	}
}

void Asura_Shape::Platform_RenderShadow( const Asura_Shape_Instance* pxShapeInst, const Asura_Shadow& xShadow, u_int uCamVisFlags ) const
{
	Asura_Xbox360_ObjectRenderer::RenderShadow( *this, *pxShapeInst, xShadow );
}

////////////////////////////////////////////////////////////////////////////////
// Function     : Asura_Shape::Platform_IsOpaque
// Author       : Kevin Floyer-Lea
// Date         : 26-Jul-2005
//
// Description  : Returns whether the material for this shape is opaque.
//
// Return value : "true" if opaque, "false" if transparent.
////////////////////////////////////////////////////////////////////////////////

bool Asura_Shape::Platform_IsOpaque() const
{
	const Asura_Hash_ID uMaterialResponseHashID = Platform_GetMaterialIdx();
	const Asura_MaterialResponse* const pxMaterial
		= Asura_MaterialResponse_System::GetMaterialByHashID( uMaterialResponseHashID );

	return !pxMaterial || !pxMaterial->IsTransparent();
}
