/*---------------------------------------------------------------------------
  Ninja Sample
  COPYRIGHT (C) SEGA ENTERPRISES, LTD.
---------------------------------------------------------------------------*/

#include	<shinobi.h>
#include	"nj.h"

extern NJS_CNK_OBJECT object_t_Teapot01_Teapot01[];

NJS_TEXINFO TexInfo;
NJS_TEXNAME TexName;
NJS_TEXLIST TexList = { &TexName, 1 };

NJS_TEXNAME TpName[2];
NJS_TEXLIST TpList = { &TpName[0], 2 };

/*---------------------------------------------------------------------------
		初期化
		initialize
---------------------------------------------------------------------------*/
Sint32	UserInit(void)
{
	void *p;
	if((p = syMalloc( 1024*1024 )) == NULL){
		return USER_EXIT;
	}
	njSetTextureInfo( &TexInfo , p, NJD_TEXFMT_RGB_565 | NJD_TEXFMT_RECTANGLE, 256,256 );
	njSetTextureName( &TexName , &TexInfo, 0x2000, NJD_TEXATTR_TYPE_MEMORY | NJD_TEXATTR_GLOBALINDEX );
	if(nuLoadTexture( &TexList ) < 0){
		return USER_EXIT;
	}
	
	njSetTextureName( &TpName[0] , "TP.PVR"  , 0, NJD_TEXATTR_TYPE_FILE | NJD_TEXATTR_GLOBALINDEX );
	njSetTextureName( &TpName[1] , "MASK.PVR", 1, NJD_TEXATTR_TYPE_FILE | NJD_TEXATTR_GLOBALINDEX );
	if(nuLoadTexture( &TpList ) < 0){
		return USER_EXIT;
	}
	
	njCnkSetSimpleLightIntensity( 1.0f,0.15f );
	njCnkSetSimpleLightColor( 1.f, 1.f, 1.f );
	
	njSetBackColor( 0xff0000,0x00ff00,0x0000ff );
	
	syFree( p );
	return USER_CONTINUE;
}
/*---------------------------------------------------------------------------
		終了処理
		exit
---------------------------------------------------------------------------*/
Sint32	UserExit(void)
{
	njReleaseTexture( &TexList );
	njReleaseTexture( &TpList  );
	
	return USER_CONTINUE;
}
/*---------------------------------------------------------------------------
		メイン
		main
---------------------------------------------------------------------------*/
Sint32	UserMain(void)
{
	static Uint32 xx,yy,zz,ty;
	NJS_TEXTURE_VTX v[4];
	
	njSetScreenCenter( 128, 128 );
	njCnkSetSimpleLight( 0.4f,0.4f,-0.8f );
	
	njPushMatrixEx();
	njTranslate( NULL, 0.f, 0.f, -16.f );
	njRotateXYZ( NULL, xx,yy,zz );
	njSetTexture( &TpList );
	njSetTextureNum( 0 );
	njCnkSimpleDrawObject( object_t_Teapot01_Teapot01 );
	njPopMatrixEx();
	
	xx += 257;
	yy += 179;
	zz += 193;
	
	njRenderTextureNumG( 0x2000 );
	njStartDraw();
	
	njSetScreenCenter( 320, 240 );
	
	v[0].x   = -1.f;
	v[0].y   = +1.f;
	v[0].z   =  0.f;
	v[0].u   =  0.f;
	v[0].v   =  0.f;
	v[0].col = 0xffffffff;
	v[1].x   = -1.f;
	v[1].y   = -1.f;
	v[1].z   =  0.f;
	v[1].u   =  0.f;
	v[1].v   =  1.f;
	v[1].col = 0xffffffff;
	v[2].x   = +1.f;
	v[2].y   = +1.f;
	v[2].z   =  0.f;
	v[2].u   =  1.f;
	v[2].v   =  0.f;
	v[2].col = 0xffffffff;
	v[3].x   = +1.f;
	v[3].y   = -1.f;
	v[3].z   =  0.f;
	v[3].u   =  1.f;
	v[3].v   =  1.f;
	v[3].col = 0xffffffff;
	
	njTextureFilterMode( NJD_TEXTUREFILTER_BILINEAR );
	
	njPushMatrixEx();
	njTranslate( NULL, 0.f, 0.f, -3.f );
	njRotateXYZ( NULL, 0,ty,0 );
	
	njColorBlendingMode( NJD_SOURCE_COLOR     , NJD_COLOR_BLENDING_ONE );
	njColorBlendingMode( NJD_DESTINATION_COLOR, NJD_COLOR_BLENDING_ZERO );
	
	njAccumlationBufferMode( OFF, ON );
	njSetTexture( &TexList );
	njSetTextureNum( 0 );
	njDrawTexture3DEx( v, 4, TRUE );
	
	njColorBlendingMode( NJD_SOURCE_COLOR     , NJD_COLOR_BLENDING_ZERO      );
	njColorBlendingMode( NJD_DESTINATION_COLOR, NJD_COLOR_BLENDING_SRCCOLOR  );
	
	njAccumlationBufferMode( OFF, ON );
	njSetTexture( &TpList );
	njSetTextureNum( 1 );
	njDrawTexture3DEx( v, 4, TRUE );
	
	njColorBlendingMode( NJD_SOURCE_COLOR     , NJD_COLOR_BLENDING_SRCALPHA    );
	njColorBlendingMode( NJD_DESTINATION_COLOR, NJD_COLOR_BLENDING_INVSRCALPHA );
	
	njAccumlationBufferMode( ON, OFF );
	njSetTexture( &TpList );
	njSetTextureNum( 0 );
	njDrawTexture3DEx( v, 4, TRUE );
	
	njPopMatrixEx();
	ty += 150;
	
	njAccumlationBufferMode( OFF, OFF );
#if DEBUG	
	njPrint( NJM_LOCATION( 2,2 ), "ACCUMLATION BUFFER" );
#endif	
	return USER_CONTINUE;
}
/*-------------------------------------------------------------------------*/
