/***************************************************************/
//
//
//		DirectX	[ renderingorderdx9.cpp ]
//
//											Author	kazuki tanaka
//											Date	2017 5/24
/*---------------------------------------------------------------
■Update : 2017/5/24
			renderingorderdx9.cppの作成

/*---------------------------------------------------------------
	Include File
---------------------------------------------------------------*/


#include "rendering_orderdx9.h"

#include "rendererdx9.h"

#include "vertexbufferdx9.h"
#include "indexbufferdx9.h"

#include "mesh3dobjectdx9.h"
#include "texturedx9_interface.h"

#include "castdx9.h"

/*---------------------------------------------------------------
	Macro Difinition
---------------------------------------------------------------*/


/*---------------------------------------------------------------
	Const Number Difinition
---------------------------------------------------------------*/


/*----------------------------------------------------------------
	static member variable Initialize
----------------------------------------------------------------*/



// RenderingOrderDX9 Constructor
RenderingOrderDX9::RenderingOrderDX9( )
{

	// Menber Clear Process

}

// RenderingOrderDX9 Destructor
RenderingOrderDX9::~RenderingOrderDX9( )
{

	// Do Nothing!!

}

// RenderingOrderDX9 Create Instance 
RenderingOrderDX9* RenderingOrderDX9::Create( void )
{

	// Create Class Instance
	return new RenderingOrderDX9;

}

// RenderingOrder DrawPrimitiveStrip
void RenderingOrderDX9::DrawPrimitiveStrip( VertexBuffer* pVtxBuf, TextureInterface* pTexture, Matrix* mtx )
{

	// Set < WorldMatrix >
	if( mtx ){
		GetDevice( ) ->SetTransform( D3DTS_WORLD, &CastMtx( *mtx ) );
	}

	// Set < VertexBuffer >
	pVtxBuf->SetVertex3D( );

	// Set < Texture > 
	pTexture->SetTexture( );

	// Render
	GetDevice( ) ->DrawPrimitive( D3DPT_TRIANGLESTRIP, pVtxBuf->GetVertexNumber( ), pVtxBuf->GetVertexNumber( )-2 );

}

// RenderingOrder DrawPrimitiveList
void RenderingOrderDX9::DrawPrimitiveList( VertexBuffer* pVtxBuf, TextureInterface* pTexture, Matrix* mtx )
{

	// Set < WorldMatrix >
	if( mtx ){
		GetDevice( ) ->SetTransform( D3DTS_WORLD, &CastMtx( *mtx ) );
	}

	// Set < VertexBuffer >
	pVtxBuf->SetVertex3D( );

	// Set < Texture > 
	pTexture->SetTexture( );

	// Render
	GetDevice( ) ->DrawPrimitive( D3DPT_TRIANGLELIST, pVtxBuf->GetVertexNumber( ), pVtxBuf->GetVertexNumber( )/3 );

}

// RenderingOrder DrawPrimitiveFan
void RenderingOrderDX9::DrawPrimitiveFan( VertexBuffer* pVtxBuf, TextureInterface* pTexture, Matrix* mtx )
{

	// Set < WorldMatrix >
	if( mtx ){
		GetDevice( ) ->SetTransform( D3DTS_WORLD, &CastMtx( *mtx ) );
	}

	// Set < VertexBuffer >
	pVtxBuf->SetVertex3D( );

	// Set < Texture > 
	pTexture->SetTexture( );

	// Render
	GetDevice( ) ->DrawPrimitive( D3DPT_TRIANGLEFAN, pVtxBuf->GetVertexNumber( ), pVtxBuf->GetVertexNumber( )-2 );

}

// RenderingOrder DrawSubset
void RenderingOrderDX9::DrawSubset( Mesh3DObject* pMeshObj, Matrix* mtx )
{

	// 現在デバイスに設定されているマテリアル情報の取得
	LPDIRECT3DDEVICE9 pDevice = GetDevice( );

	// デバイスにワールド変換行列を設定
	pDevice ->SetTransform( D3DTS_WORLD, &CastMtx( *mtx ) );

	D3DMATERIAL9 matDef;
	pDevice -> GetMaterial( &matDef );
	
	// マテリアル情報のポインタとしてアドレスを取得
	D3DXMATERIAL* pMat =( D3DXMATERIAL*)dynamic_cast<Mesh3DObjectDX9*>(pMeshObj)->GetMatBuffer( ) -> GetBufferPointer( );

	for( int index = 0; index < (int)dynamic_cast<Mesh3DObjectDX9*>(pMeshObj)->GetMatNumber( ); index++ ){

		// デバイスにマテリアルを設定
		pDevice -> SetMaterial( &pMat[ index ].MatD3D );

		// テクスチャ設定
		if( dynamic_cast<Mesh3DObjectDX9*>(pMeshObj)->GetTexture( index ) ){
			//pDevice -> SetTexture( 0, dynamic_cast<TextureDX9Interface*>(dynamic_cast<Mesh3DObjectDX9*>(pMeshObj)->GetTexture( index ))->data );
			dynamic_cast<Mesh3DObjectDX9*>(pMeshObj)->GetTexture( index )->SetTexture( );
		}else{
			pDevice -> SetTexture( 0, NULL );		
		}

		// MODEL描画
		dynamic_cast<Mesh3DObjectDX9*>(pMeshObj)->GetMesh( )->DrawSubset( index );
	}

	pDevice -> SetMaterial( &matDef );

}

// RenderingOrder DrawIndexPrimitive
void RenderingOrderDX9::DrawIndexPrimitive( VertexBuffer* pVtxBuf, IndexBuffer* pIdxBuf, TextureInterface* pTexture, Matrix* mtx )
{

	// Set < WorldMatrix >
	if( mtx ){
		GetDevice( ) ->SetTransform( D3DTS_WORLD, &CastMtx( *mtx ) );
	}

	// Set < Texture >
	pTexture->SetTexture( );

	// Set < VertexBuffer >
	pVtxBuf->SetVertex3D( );

	// Set < IndexBuffer >
	pIdxBuf->SetIndex( );

	// Render
	GetDevice( ) ->DrawIndexedPrimitive( D3DPT_TRIANGLESTRIP, 0, 0, pIdxBuf->GetIndexNumber( ), 0, (pIdxBuf->GetIndexNumber( )-2) );

}





