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

	Torus Sample

	1997.10.20
*/
#include <Ninjawin.h>

#define	OBJECT	object_segalogo_s1_s1
extern NJS_OBJECT OBJECT[];

NJS_TEXNAME	tex1[1];
NJS_TEXLIST	tlist1 = {tex1,1};

#define	NUM		10000

NJS_MATRIX	matrix[128];
NJS_VERTEX_BUF	vbuf[NUM];

NJS_TEXMEMLIST	tex[10];

NJS_VIEW	view;
NJS_LIGHT	light;

Sint32	xx,yy,zz = 0;

NJS_ARGB	argb;

void	chnormal( NJS_OBJECT *obj )
{
	int	i;
	NJS_POINT3		*vertex;
	NJS_VECTOR		*normal;

	vertex = obj->model->points;
	normal = obj->model->normals;

	for(i=0;i<obj->model->nbPoint;i++) {
#if 0
		normal->x = vertex->x - obj->model->center.x;
		normal->y = vertex->y - obj->model->center.y;
		normal->z = vertex->z - obj->model->center.z;
#else
		normal->x = vertex->x;
		normal->y = vertex->y;
		normal->z = vertex->z;
#endif
		njUnitVector( normal ) ;
		normal++;
		vertex++;
	}
	if( !(obj->evalflags & NJD_EVAL_BREAK) ) chnormal( obj->child );
	if( obj->sibling != NULL ) {
		chnormal( obj->sibling );
	}
}

NJS_PERIPHERAL *per;

void njUserInit(void)
{

	njInitSystem( NJD_RESOLUTION_VGA, NJD_FRAMEBUFFER_MODE_RGB565, 1 );
	njInitVertexBuffer( 600000, 0, 10000, 0 );
	njInitMatrix( matrix, 128, 0 );
	njInit3D( vbuf, NUM );
	njInitView( &view );
	njCreateLight( &light, NJD_SPEC_DIR );
//	njCreateLight( &light, NJD_DIR_LIGHT );
	njSetLightIntensity( &light, 1.0f, 2.0f, 0.5f );
	njSetView( &view );
	njInitTexture( tex, 10 );
//	njSetTextureName(&tex1[0],"009.pvr",0,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
//	njSetTextureName(&tex1[0],"ref_deepbluea.pvr",0,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
//	njSetTextureName(&tex1[0],"tex_sky_l.pvr",0,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
	njSetTextureName(&tex1[0],"hq1_2.pvr",0,NJD_TEXATTR_TYPE_FILE|NJD_TEXATTR_GLOBALINDEX);
//	njSetTextureName(&tex1[0],NULL,0,NJD_TEXATTR_TYPE_FRAMEBUFFER|NJD_TEXATTR_GLOBALINDEX);
	njLoadTexture( &tlist1 );
	njSetTexture( &tlist1 );
	njSetTextureNum(0);
//	njClipZ( -2.0f, -100.f );

	njControl3D( 
		NJD_CONTROL_3D_CONSTANT_ATTR | 
//		NJD_CONTROL_3D_NO_CLIP_CHECK |
		NJD_CONTROL_3D_CONSTANT_TEXTURE_MATERIAL |
		NJD_CONTROL_3D_CONSTANT_MATERIAL |
		0
		);
//	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FILTER_BILINEAR | NJD_FLAG_IGNORE_LIGHT);
//	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FILTER_BILINEAR );
	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_USE_FLAT | NJD_FILTER_BILINEAR );
//	njSetConstantAttr( 0xffffffff, NJD_FLAG_USE_TEXTURE | NJD_FLAG_USE_ENV | NJD_FLAG_IGNORE_LIGHT);

//	njSetBackColor(0x00000000,0x00000000,0x000000FF);

	chnormal( OBJECT );

//	njSetRenderWidth(640);


}
Sint32	r,g,b=0;

Sint32 njUserMain(void)
{
	NJS_COLOR	col;

	njClearMatrix();
//	njTranslate( NULL, 0.f, 0.f, -7.f );
	njTranslate( NULL, 0.f, njSin(zz), -7.f );
	njRotateXYZ( NULL, xx,yy,0 );
//	njRotateY( NULL, yy );
	
	argb.a = 1.0f;
	argb.r = (float)(njSin(r) * 0.5f + 0.5);
	argb.g = (float)(njSin(g) * 0.5f + 0.5);
	argb.b = (float)(njSin(b) * 0.5f + 0.5);
//	njSetLightColor( &light, argb.r, argb.g, argb.b );
	njSetConstantMaterial( &argb );

	col.argb.a = 0xff;
	col.argb.r = (unsigned char)(argb.r * 255.f/2);
	col.argb.g = (unsigned char)(argb.g * 255.f/2);
	col.argb.b = (unsigned char)(argb.b * 255.f/2);
	njSetBackColor(col.color,col.color,col.color);

	njDrawObject( OBJECT );
	xx += 257;
	yy += 179;
	zz += 193;

	r += 219;
	g += 329;
	b += 523;

	return NJD_USER_CONTINUE;
}

void njUserExit(void)
{
	njExitTexture();
	njExitSystem();
}
