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

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


#define	OBJECT	 object_ver_viewer_null1_null1
#define TEXLIST texlist_ver_viewer_null1

static	void *model;

extern	NJS_CNK_OBJECT	OBJECT[];
extern NJS_TEXLIST TEXLIST[];

void ResetAllParam(void);
/*---------------------------------------------------------------------------
		初期化
		initialize
---------------------------------------------------------------------------*/
Sint32	UserInit(void)
{
	if(nuLoadTexture( TEXLIST ) < 0){
		return USER_EXIT;
	}
	njSetTexture( TEXLIST );
	
	njSetDirectLight( 0.f,0.f, -1.f );
	njSetDirectLightColor( 1.f, 1.f, 1.f );
	njSetDirectLightIntensity( 1.0f, 0.1f );
	
	if((model = syMalloc( 6*1024*1024 )) == NULL){
		return USER_EXIT;
	}
	njCnkDirectObjectCompile( OBJECT, model );
	njSetDirectClip();
	
	ResetAllParam();
	
	return USER_CONTINUE;
}
/*---------------------------------------------------------------------------
		終了処理
		exit
---------------------------------------------------------------------------*/
Sint32	UserExit(void)
{
	return USER_CONTINUE;
}


static	Float	zzz;
static	Float	xxx;
static	Float	yyy;

static	Sint32	xx;
static	Sint32	yy;
static	Sint32	zz;

static	Float	inten;
static	Float	ambi;

static	NJS_VECTOR	light = { 1.f, -1.f, -1.f };

static Float fFilterRate;
static Float fBrightness;

static Uint32 FilterMode;

/*---------------------------------------------------------------------------
		メイン
		main
---------------------------------------------------------------------------*/
Sint32	UserMain(void)
{
	NJS_VECTOR	l;

	njPushMatrixEx();
	
	njPushMatrixEx();
	njRotateXYZ( NULL, Per->y1*120,-Per->x1*120,0 );
	njCalcVector( NULL, &light, &l );
	njUnitVector( &l );
	njPopMatrixEx();
	
	njSetDirectLight( l.x, l.y, l.z );
	njSetDirectLightIntensity( inten, ambi );
	
	njTranslate( NULL, xxx, yyy, zzz );
	njRotateXYZ( NULL, xx,yy,zz );
	yy+=100;
	
	njDirectDrawObject( model );
	
	if (Per->on & PDD_DGT_TA ) {
		if (Per->on & PDD_DGT_KU) 	yyy += 1.01f;
		if (Per->on & PDD_DGT_KD) 	yyy -= 1.01f;
		if (Per->on & PDD_DGT_KR) 	xxx += 1.01f;
		if (Per->on & PDD_DGT_KL) 	xxx -= 1.01f;
		if (Per->on & PDD_DGT_TR) 	zzz += 1.01f;
		if (Per->on & PDD_DGT_TL) 	zzz -= 1.01f;
	}
	else if (Per->on & PDD_DGT_TB ) {
		if (Per->on & PDD_DGT_KU) 	inten += 0.01f;
		if (Per->on & PDD_DGT_KD) 	inten -= 0.01f;
		if (Per->on & PDD_DGT_KR) 	ambi  += 0.01f;
		if (Per->on & PDD_DGT_KL) 	ambi  -= 0.01f;
	}
	else if (Per->on & PDD_DGT_TX ) {
		if (Per->on & PDD_DGT_KU) 	fFilterRate += 0.01f;
		if (Per->on & PDD_DGT_KD) 	fFilterRate -= 0.01f;
		if (Per->on & PDD_DGT_KR) 	fBrightness += 0.01f;
		if (Per->on & PDD_DGT_KL) 	fBrightness -= 0.01f;
		
		if (fFilterRate>1.f) fFilterRate = 1.f;
		if (fFilterRate<0.f) fFilterRate = 0.f;
		if (fBrightness>1.f) fBrightness = 1.f;
		if (fBrightness<0.f) fBrightness = 0.f;
		
		njSetYCoefficientEx( fFilterRate, fBrightness );
	}
	else {
		if (Per->on & PDD_DGT_KU) 	xx += 200;
		if (Per->on & PDD_DGT_KD) 	xx -= 200;
		if (Per->on & PDD_DGT_KR) 	yy += 200;
		if (Per->on & PDD_DGT_KL) 	yy -= 200;
		if (Per->on & PDD_DGT_TR) 	zz += 200;
		if (Per->on & PDD_DGT_TL) 	zz -= 200;
	}
	
	if (Per->press & PDD_DGT_TY ) {
		FilterMode++;
		if (FilterMode>3) FilterMode = 0;
		njSetVirticalFilterMode( FilterMode );
	}
	
	if (Per->press & PDD_DGT_ST) ResetAllParam();
	
	njPopMatrixEx();
#if DEBUG	
	switch (FilterMode) {
	case 0: njPrintC( NJM_LOCATION( 2,2 ), "VIRTICAL FILTER [NO FILTER]   " ); break;
	case 1: njPrintC( NJM_LOCATION( 2,2 ), "VIRTICAL FILTER [NORMAL]      " ); break;
	case 2: njPrintC( NJM_LOCATION( 2,2 ), "VIRTICAL FILTER [SQUARE PIXEL]" ); break;
	case 3: njPrintC( NJM_LOCATION( 2,2 ), "VIRTICAL FILTER [VGA]         " ); break;
	}
	
	njPrint( NJM_LOCATION( 2,4 ), "FILTER RATE = %f", fFilterRate );
	njPrint( NJM_LOCATION( 2,5 ), "BRIGHT NESS = %f", fBrightness );
#endif	
	return USER_CONTINUE;
}
/*-------------------------------------------------------------------------*/
void ResetAllParam(void)
{
	zzz   = -200.f;
	xxx   = 0.f;
	yyy   = 15.f;
	xx    = 2000;
	yy    = 0;
	zz    = 0;
	inten = 1.0f;
	ambi  = 0.2f;
	
	fFilterRate = 0.5f;
	fBrightness = 1.0f;
	
	FilterMode  = NJD_VIRTICAL_FILTER_NORMAL;
	
	
	njSetVirticalFilterMode( FilterMode );
	njSetYCoefficientEx( fFilterRate, fBrightness );
}
/*-------------------------------------------------------------------------*/
