//
// Asura_PS3_AABBRenderer.cpp
//
// Created by KJFL - 17 January 2006
//
// Copyright (C) 2006 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#if 0
// This file's header.
#include "Rendering/Asura_PS3_AABBRenderer.h"

#include <string.h>
#include "Rendering/Shaders/Asura_PS3_GCM_Shader.h"
#include "Rendering/Asura_PS3_GCM_VertexBuffer.h"
#include "Rendering/Asura_PS3_GCM_IndexBuffer.h"


// To render AABBs, we use a static vertex buffer of a 'unit AABB', which can be
// scaled and positioned via vertex shaders constants. Each vertex for the AABB is 
// just a 3-space co-ord.
class Asura_PS3_AABB_Vertex
{
	public:

		float afPosition[3];
};

// Co-ords for a 'unit AABB'.
static const Asura_PS3_AABB_Vertex g_axUnitAABBVertices[] = 
{
	{ { 0.0f, 0.0f, 0.0f } },
	{ { 0.0f, 0.0f, 1.0f } },
	{ { 0.0f, 1.0f, 0.0f } },
	{ { 0.0f, 1.0f, 1.0f } },
	{ { 1.0f, 0.0f, 0.0f } },
	{ { 1.0f, 0.0f, 1.0f } },
	{ { 1.0f, 1.0f, 0.0f } },
	{ { 1.0f, 1.0f, 1.0f } }
};
/*
// vertex definition for PS3 
static Asura_PS3_GCM_VertexShaderElement g_axAABBVertexDeclaration[] = 
{
	"IN.Position",	CELL_GCM_VERTEX_F,	3,
};
*/

// cube face definitions, listing vertices counter-clockwise, using quads
// since PS3 can draw them as basic primitives.
static const u_short g_ausUnitAABBFaceIndices[] = 
{
	0,1,3,2,
	0,4,5,1,
	4,6,7,5,
	6,2,3,7,
	1,5,7,3,
	2,6,4,0
};

static Asura_PS3_GCM_VertexBuffer		g_xVertexBuffer;
static Asura_PS3_GCM_IndexBuffer		g_xIndexBuffer;

#if 0
static Asura_PS3_GCM_VertexShader*		g_pxAABB_VertexShader;
static CGparameter					g_xCG_AABB_ViewProjectionMatrix;
static CGparameter					g_xCG_AABB_Scale;
static CGparameter					g_xCG_AABB_Offset; 

static Asura_PS3_FragmentShader*	g_pxXRay_FragmentShader;
#endif // #if 0


void Asura_PS3_AABBRenderer::Initialise( void )
{
#if 0 //TODO:
	// setup vertex desc
	g_xVertexDesc.Initialise( g_axAABBVertexDeclaration, sizeof(g_axAABBVertexDeclaration) / sizeof(g_axAABBVertexDeclaration[0] ) );

	// make a static vertex buffer, and load it with our vertex data
	{
		const u_int uNumberOfVertexDeclarations	= sizeof( g_axAABBVertexDeclaration ) / sizeof( g_axAABBVertexDeclaration[0] );
		
		// generate the vertex buffer
		const u_int uVertSize = g_xVertexDesc.GetVertexSize();
		const u_int uNumVerts = sizeof( g_axUnitAABBVertices ) / sizeof( g_axUnitAABBVertices[0]);
		g_xVertexBuffer.Initialise(uVertSize, uNumVerts);
		g_xVertexBuffer.CreateStatic();

		// lock the buffer
		GLvoid*	pBuffer = g_xVertexBuffer.LockForWriting();

		// copy in data
		memcpy( pBuffer, g_axUnitAABBVertices, g_xVertexBuffer.GetVertexBufferSize() );

		// unlock the buffer
		g_xVertexBuffer.Unlock();
	}

	// make a static index buffer, and load it with our vertex data
	{
		// generate the index buffer
		g_xIndexBuffer.Initialise();
		g_xIndexBuffer.CreateStatic( sizeof( g_ausUnitAABBFaceIndices ) / sizeof( g_ausUnitAABBFaceIndices[0] ) );

		// lock the buffer
		GLvoid*	pBuffer = g_xIndexBuffer.LockForWriting();

		// copy in data
		memcpy( pBuffer, g_ausUnitAABBFaceIndices, g_xIndexBuffer.GetIndexBufferSize() );
		
		// unlock the buffer
		g_xIndexBuffer.Unlock();
	}

	// setup the shaders
	g_pxAABB_VertexShader = Asura_PS3_GCM_ShadersLibrary::GetVertexShader( Asura_PS3_GCM_ShadersLibrary::VP_ENV_AABB );
	g_xCG_AABB_ViewProjectionMatrix = g_pxAABB_VertexShader->GetParameter( "ViewProjectionMatrix" );

	g_xCG_AABB_Scale = g_pxAABB_VertexShader->GetParameter( "Scale" );
	g_xCG_AABB_Offset = g_pxAABB_VertexShader->GetParameter( "Offset" );

	g_pxXRay_FragmentShader = Asura_PS3_GCM_ShadersLibrary::GetFragmentShader( Asura_PS3_GCM_ShadersLibrary::FP_ENV_XRAY );
#endif
}

void Asura_PS3_AABBRenderer::Shutdown( void )
{
	// release our buffers and any memory we've allocated
	g_xVertexBuffer.Destroy();
	g_xIndexBuffer.Shutdown();

#if 0 //TODO:
	g_xVertexDesc.Shutdown();
#endif
	// TODO - shader release? 
}

void Asura_PS3_AABBRenderer::Render( const Asura_Bounding_Box& xBB )
{
#if 0 //TODO:
	glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );

	Asura_PS3_RenderingStates::RequestTranslucencyMode( ASURA_TRANSLUCENCY_OFF );
	Asura_PS3_RenderingStates::RequestCullMode( ASURA_CULLMODE_NONE );
	Asura_PS3_RenderingStates::RequestZBufferWriteEnabled	( false );
	Asura_PS3_RenderingStates::RequestAlphaTest( false );

	g_pxAABB_VertexShader->Bind(g_xVertexDesc);
	g_pxXRay_FragmentShader->Bind();

	cgGLSetStateMatrixParameter( g_xCG_AABB_ViewProjectionMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY );

	Asura_PS3_RenderingStates::RequestZBufferCompareMode( ASURA_ZBUFFERCMP_LESSEQUAL );	// EQUALS);

	Asura_PS3_Graphics::EnableDeferredRenderTargets();

	g_xIndexBuffer.Bind();
	g_xVertexBuffer.BindForVertexShader();

	cgGLSetParameter4f( g_xCG_AABB_Scale, xBB.MaxX-xBB.MinX, xBB.MaxY-xBB.MinY, xBB.MaxZ-xBB.MinZ, 1.0f );
	cgGLSetParameter4f( g_xCG_AABB_Offset,  xBB.MinX, xBB.MinY, xBB.MinZ, 0.0f );

	g_xIndexBuffer.RenderQuads( sizeof( g_ausUnitAABBFaceIndices ) / sizeof( g_ausUnitAABBFaceIndices[0] ) );

	Asura_PS3_Graphics::DisableDeferredRenderTargets();

	glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );

	Asura_PS3_GCM_ShadersLibrary::UnbindAll();
#endif
}

void Asura_PS3_AABBRenderer::BeginBatchRender( void )
{
#if 0 //TODO:
	glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );

	Asura_PS3_RenderingStates::RequestTranslucencyMode( ASURA_TRANSLUCENCY_OFF );
	Asura_PS3_RenderingStates::RequestCullMode( ASURA_CULLMODE_NONE );
	Asura_PS3_RenderingStates::RequestZBufferWriteEnabled	( false );
	Asura_PS3_RenderingStates::RequestAlphaTest( false );

	g_pxAABB_VertexShader->Bind(g_xVertexDesc);
	g_pxXRay_FragmentShader->Bind();

	cgGLSetStateMatrixParameter( g_xCG_AABB_ViewProjectionMatrix, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY );

	Asura_PS3_RenderingStates::RequestZBufferCompareMode( ASURA_ZBUFFERCMP_LESSEQUAL );	// EQUALS);

	Asura_PS3_Graphics::EnableDeferredRenderTargets();

	g_xIndexBuffer.Bind();
	g_xVertexBuffer.BindForVertexShader();
#endif
}

void Asura_PS3_AABBRenderer::BatchRender( const Asura_Bounding_Box& xBB )
{
#if 0 //TODO:
	cgGLSetParameter4f( g_xCG_AABB_Scale, xBB.MaxX-xBB.MinX, xBB.MaxY-xBB.MinY, xBB.MaxZ-xBB.MinZ, 1.0f );
	cgGLSetParameter4f( g_xCG_AABB_Offset,  xBB.MinX, xBB.MinY, xBB.MinZ, 0.0f );

	g_xIndexBuffer.RenderQuads( sizeof( g_ausUnitAABBFaceIndices ) / sizeof( g_ausUnitAABBFaceIndices[0] ) );
#endif
}

void Asura_PS3_AABBRenderer::EndBatchRender( void )
{
#if 0 //TODO:
	Asura_PS3_Graphics::DisableDeferredRenderTargets();

	glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );

	Asura_PS3_GCM_ShadersLibrary::UnbindAll();
#endif
}

#endif
