/********************************************************************/
/* Kamui function test program										*/
/* Test of Tri-linear filter										*/
/********************************************************************/
/*   COPYRIGHT (C) NEC Corporation 1998                             */
/*   All rights reserved by NEC Corporation. This program must be   */
/*   used solely for the purpose for which it was furnished by NEC  */
/*   Corporation. No part of this program may be reproduced or      */
/*   disclosed to others, in any form, without the prior written    */
/*   permission of NEC Corporation.                                 */
/*   Use of copyright notice does not evidence publication of this  */
/*   program.                                                       */
/*                                                                  */
/*   Author   : Takeshi Muto / NEC Software, Ltd.                   */
/*   Created  : May 21 1998 for SET5 by NEC Software, Ltd.          */
/*   Modified :                                                     */
/********************************************************************/

#include	<math.h>
#include	"kamui.h"				/* for Kamui Call */
#include	"dispstr.h"				/* for String Screen */

#define		_DISPLAY_STRING_
/* if defined then display strings on screen */

/* Size of vertex buffer. */
#define VBUFFER_SIZE    8*3*2000

/* Textures */
extern  unsigned char segata[];   /* 256x256 RGB565 Twiddled Mipmap */

#define	PI					(3.141592f)
#define	RGBColour(r, g, b)	((KMUINT32) (((r) << 16) | ((g) << 8) | (b)))
#define	RGBA(r, g, b, a)	((KMUINT32) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b)))

typedef float		KMVECTOR[3];	/* type of KMVECTOR */
typedef struct		tagKMVERTICES
{
	/* Projected screen coordinates: */
	float		x;
	float		y;
	float		z;
	float		u;
	float		v;
} KMVERTICES, *PKMVERTICES;

/****************************************************************************/
/**	Valuables for KAMUI *****************************************************/
/****************************************************************************/
	KMSTATUS			status;
	KMVERSIONINFO		VersionInfo;			/* KAMUI Version information */
	KMSURFACEDESC		PrimarySurfaceDesc;		/* Primary frame buffer */
	KMSURFACEDESC		BackSurfaceDesc;		/* Back-buffer frame buffer */
	KMSURFACEDESC		TexSurfaceDesc1;		/* texture surface */
	KMVERTEXBUFFDESC	VertexBufferDesc;		/* Vertex-buffer */
	KMVERTEXCONTEXT		VertexContext01;		/* Vertex context 1 : Object 1 : Opaque */
	PKMDWORD			pTexture01;				/* Pointer to texture */
	KMTEXTURETYPE		nTextureType;			/* texture type definition */

/* System Configuration */

PKMSURFACEDESC          psurface[2];            /* array of pointers to surfaces */
KMPACKEDARGB            border;

KMSYSTEMCONFIGSTRUCT kmsc = {
		sizeof(KMSYSTEMCONFIGSTRUCT),
		KM_CONFIGFLAG_ENABLE_CLEAR_FRAMEBUFFER,	/* set any special flags = clear frame buffer */
        psurface,                               /* pointer to array of surface pointers */
        2,                                      /* number of buffers = 2 */
        640,480,                                /* width and height of surface */
        KM_DSPBPP_RGB565,                       /* bits per pixel */
        0x100000,                               /* allocate amount of VRAM for texture = 4MB */
        &VertexBufferDesc,                      /* pointer to vertex buffer descriptor */
        0,                                      /* pointer to vertex buffer - ** malloc now on application side */
		2*VBUFFER_SIZE,							/* size in DWORDS of vertex buffer - must be multiple of 32 */
        {50.f,0.f,50.f,0.f,0.f},                /* percentage of vbuf to use for each type of vertex */
        0,0,0,0,0,0,0,                          /* reserved */
        KM_VERTEXBUFMODEL_NORMAL                /* latency model = 3VB */
	};

/****************************************************************************/
/**	Check KAMUI function status *********************************************/
/****************************************************************************/
void	Check_Status(KMSTATUS status, char * func)
{
	switch(status) {
	case KMSTATUS_SUCCESS:
		/* No message is displayed when SUCCESS */
		break;
	case KMSTATUS_INVALID_DISPLAY_MODE:
		PRINTOUT(("%s:KMSTATUS_INVALID_DISPLAY_MODE\n",func)); break;
	case KMSTATUS_INVALID_VIDEO_MODE:
		PRINTOUT(("%s:KMSTATUS_INVALID_VIDEO_MODE\n",func)); break;
	case KMSTATUS_NOT_ENOUGH_MEMORY:
		PRINTOUT(("%s:KMSTATUS_NOT_ENOUGH_MEMORY\n",func)); break;
	case KMSTATUS_INVALID_TEXTURE_TYPE:
		PRINTOUT(("%s:KMSTATUS_INVALID_TEXTURE_TYPE\n",func)); break;
	case KMSTATUS_INVALID_MIPMAPED_TEXTURE:
		PRINTOUT(("%s:KMSTATUS_INVALID_MIPMAPED_TEXTURE\n",func)); break;
	case KMSTATUS_CANT_FLIP_SURFACE:
		PRINTOUT(("%s:KMSTATUS_CANT_FLIP_SURFACE\n",func)); break;
	case KMSTATUS_INVALID_ADDRESS:
		PRINTOUT(("%s:KMSTATUS_INVALID_ADDRESS\n",func)); break;
	case KMSTATUS_HARDWARE_NOT_PRESENTED:
		PRINTOUT(("%s:KMSTATUS_HARDWARE_NOT_PRESENTED\n",func)); break;
	case KMSTATUS_INVALID_SETTING:
		PRINTOUT(("%s:KMSTATUS_INVALID_SETTING\n",func)); break;
	case KMSTATUS_INVALID_VERTEX_TYPE:
		PRINTOUT(("%s:KMSTATUS_INVALID_VERTEX_TYPE\n",func)); break;
	case KMSTATUS_NOT_IMPLEMENTED:
		PRINTOUT(("%s:KMSTATUS_NOT_IMPREMENTED\n",func)); break;
	case KMSTATUS_INVALID_PARAMETER:
		PRINTOUT(("%s:KMSTATUS_INVALID_PARAMETER\n",func)); break;
	default:
		PRINTOUT(("%s:???Undefined status : %d\n", func, status));
		break;
	}
}

/****************************************************************************/
/**	Transformation **********************************************************/
/****************************************************************************/
void	Transform(
				KMVECTOR	translate,		/* translate vector */
				KMVECTOR	rotate,			/* rotation angle (degree) */
				int			vertnum,		/* vertex number */
				KMVERTICES	*inputvector,	/* input vertices data */
				KMVERTICES	*outputvector	/* output vertices data */
				)
{
	int			i;
	KMVECTOR	rr;
	KMVECTOR	rot[3];
	float		xx, yy, zz;

	/* degree to radian */
	rr[0] = rotate[0] * (PI / 180);	/* x */
	rr[1] = rotate[1] * (PI / 180);	/* y */
	rr[2] = rotate[2] * (PI / 180);	/* z */

	/* Calc. rotation matrix */
	rot[0][0] =  cos(rr[1]) * cos(rr[2]);
	rot[0][1] = -sin(rr[1]);
	rot[0][2] =  cos(rr[1]) * sin(rr[2]);

	rot[1][0] = cos(rr[0]) * sin(rr[1]) * cos(rr[2]) + sin(rr[0]) * sin(rr[2]);
	rot[1][1] = cos(rr[0]) * cos(rr[1]);
	rot[1][2] = cos(rr[0]) * sin(rr[1]) * sin(rr[2]) - sin(rr[0]) * cos(rr[2]);

	rot[2][0] = sin(rr[0]) * sin(rr[1]) * cos(rr[2]) - cos(rr[0]) * sin(rr[2]);
	rot[2][1] = sin(rr[0]) * cos(rr[1]);
	rot[2][2] = sin(rr[0]) * sin(rr[1]) * sin(rr[2]) + cos(rr[0]) * cos(rr[2]);

	for(i = 0; i < vertnum; i++) {
		xx =  (inputvector[i].x * rot[0][0] + inputvector[i].y * rot[0][1] + inputvector[i].z * rot[0][2]) + translate[0];
		yy =  (inputvector[i].x * rot[1][0] + inputvector[i].y * rot[1][1] + inputvector[i].z * rot[1][2]) + translate[1];
		zz =  500 / ((inputvector[i].x * rot[2][0] + inputvector[i].y * rot[2][1] + inputvector[i].z * rot[2][2]) + translate[2] + 1000);
		outputvector[i].x	= xx * zz + 320;		/* screen coodination x */
		outputvector[i].y	= yy * zz + 240;		/* screen coodination y */
		outputvector[i].z	= zz;					/* screen coodination z */
		outputvector[i].u	= inputvector[i].u;		/* copy u coodination */
		outputvector[i].v	= inputvector[i].v;		/* copy v coodination */
	}
}

/****************************************************************************/
/**	Send Triangle Strip *****************************************************/
/****************************************************************************/
void	SendTriangleStrip(
				KMVERTEXBUFFDESC	BufferDesc,		/* vertex buufer desc. */
				KMVERTEXCONTEXT		VertexContext,	/* vertex context */
				int					nNumVertex,		/* vertex number */
				KMVERTICES			*Vertices,		/* input vertex data */
				int					bTrilinear		/* tri-linear flag */
				)
{
	int					i, j, pass;
	KMSTATUS			status;
	KMVERTEX_03			Ver03;					/* for TEXTURE-GOURAUD */
	KMVERTEXCONTEXT		WorkVertexContext[2];	/* work context */

	/* copy vertex context */
	memcpy(&WorkVertexContext[0], &VertexContext, sizeof(KMVERTEXCONTEXT));
	memcpy(&WorkVertexContext[1], &VertexContext, sizeof(KMVERTEXCONTEXT));

	/* set vertex context for trilinear */
	if(bTrilinear == TRUE) {
		pass = 2;								/* pass number */
		/**************/
		/* for Pass A */
		/**************/
		WorkVertexContext[0].RenderState	= 0;
		/* Set Filter Mode */
		WorkVertexContext[0].RenderState		|= KM_FILTERMODE;
		WorkVertexContext[0].FilterMode		= KM_TRILINEAR_A;
		/* Set Blending Mode */
		WorkVertexContext[0].RenderState		|= KM_SRCBLENDINGMODE;
		WorkVertexContext[0].SRCBlendingMode	= KM_ONE;
		WorkVertexContext[0].RenderState		|= KM_DSTBLENDINGMODE;
		WorkVertexContext[0].DSTBlendingMode	= KM_ZERO;
		/* Set Filter Mode */
		WorkVertexContext[0].RenderState		|= KM_SRCSELECT;
		WorkVertexContext[0].bSRCSel			= FALSE;
		WorkVertexContext[0].RenderState		|= KM_DSTSELECT;
		WorkVertexContext[0].bDSTSel			= FALSE;
		/* Process & Set */
		status = kmProcessVertexRenderState(&WorkVertexContext[0]);
		Check_Status(status, "kmProcessVertexRenderState");

		/**************/
		/* for Pass B */
		/**************/
		WorkVertexContext[1].RenderState	= 0;
		/* Set List Type */
		WorkVertexContext[1].RenderState	|= KM_LISTTYPE;
		WorkVertexContext[1].ListType		= KM_TRANS_POLYGON;
		/* Set Filter Mode */
		WorkVertexContext[1].RenderState		|= KM_FILTERMODE;
		WorkVertexContext[1].FilterMode		= KM_TRILINEAR_B;
		/* Set Blending Mode */
		WorkVertexContext[1].RenderState		|= KM_SRCBLENDINGMODE;
		WorkVertexContext[1].SRCBlendingMode	= KM_ONE;
		WorkVertexContext[1].RenderState		|= KM_DSTBLENDINGMODE;
		WorkVertexContext[1].DSTBlendingMode	= KM_ONE;
		/* Set Filter Mode */
		WorkVertexContext[1].RenderState		|= KM_SRCSELECT;
		WorkVertexContext[1].bSRCSel			= FALSE;
		WorkVertexContext[1].RenderState		|= KM_DSTSELECT;
		WorkVertexContext[1].bDSTSel			= FALSE;
		/* Process & Set */
		status = kmProcessVertexRenderState(&WorkVertexContext[1]);
		Check_Status(status, "kmProcessVertexRenderState");
	}
	else {
		pass = 1;		/* Non-trilinear */
	}

	for (j = 0; j < pass; j++) {

	/*----------------------------------------------------------------------------------- */
	/* Set Vertex context */
	status = kmSetVertexRenderState(&WorkVertexContext[j]);
	Check_Status(status, "kmSetVertexRenderState");

	/*----------------------------------------------------------------------------------- */
	/* Start Triangle strip */
	status = kmStartVertexStrip(&BufferDesc);
	Check_Status(status, "kmStartVertexStrip");

	/*----------------------------------------------------------------------------------- */
	/* set vertex data */
	for(i = 0; i < nNumVertex; i++) {
		if(i == (nNumVertex - 1))
			{ Ver03.ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP; }
		else
			{ Ver03.ParamControlWord	= KM_VERTEXPARAM_NORMAL; }
		Ver03.fX					= Vertices[i].x;
		Ver03.fY					= Vertices[i].y;
#if defined(_STRICT_UNION_)
		Ver03.u.fZ					= Vertices[i].z;
#else
		Ver03.fZ					= Vertices[i].z;
#endif
		Ver03.fU					= Vertices[i].u;
		Ver03.fV					= Vertices[i].v;
		Ver03.uBaseRGB.dwPacked		= RGBA(255, 255, 255, 255);
		Ver03.uOffsetRGB.dwPacked	= RGBA(0, 0, 0, 0);
		status = kmSetVertex(	&BufferDesc,			/* pBufferDesc */
								&Ver03,					/* pVertex */
								KM_VERTEXTYPE_03,		/* VertexType */
								sizeof(KMVERTEX_03)		/* StructSize */
								);
		Check_Status(status, "kmSetVertex");
	}	/* end of for i */
	}	/* end of for j */
}

/****************************************************************************/
/**	Initialize Vertex Context ***********************************************/
/****************************************************************************/
void	InitVertexContext(PKMVERTEXCONTEXT pVertexContext)
{
	/* 頂点CONTEXTの生成 */
	pVertexContext->RenderState	= KM_DEPTHMODE  		| KM_CULLINGMODE				/* ISP/TSP IW */
								| KM_SCREENCOORDINATION	| KM_SHADINGMODE
								| KM_MODIFIER 			| KM_ZWRITEDISABLE				/* TSP CW */
								| KM_SRCBLENDINGMODE	| KM_DSTBLENDINGMODE
								| KM_SRCSELECT			| KM_DSTSELECT
								| KM_FOGMODE			| KM_USESPECULAR
								| KM_USEALPHA			| KM_IGNORETEXTUREALPHA
								| KM_FLIPUV 			| KM_CLAMPUV
								| KM_FILTERMODE			| KM_SUPERSAMPLE
								| KM_MIPMAPDADJUST 		| KM_TEXTURESHADINGMODE
								| KM_PARAMTYPE 			| KM_LISTTYPE 					/* Param CW */
								| KM_COLORTYPE			| KM_USESPECULAR
								| KM_UVFORMAT;

	/* for ParameterControlWord  */
	pVertexContext->ParamType			= KM_POLYGON;
	pVertexContext->ListType			= KM_OPAQUE_POLYGON;
	pVertexContext->ColorType			= KM_PACKEDCOLOR;
	pVertexContext->bUseSpecular		= FALSE;
	pVertexContext->UVFormat			= KM_32BITUV;

	/* for ISP/TSP InstructionWord  */
	pVertexContext->DepthMode			= KM_GREATER;
	pVertexContext->CullingMode			= KM_NOCULLING;
	pVertexContext->ScreenCoordination	= KM_SCREEN;
	pVertexContext->ShadingMode			= KM_NOTEXTUREGOURAUD;
	pVertexContext->SelectModifier		= KM_NOMODIFIER;
	pVertexContext->bZWriteDisable		= FALSE;			/* ARC1にはない */

	/* for TSP ControlWord  */
	pVertexContext->SRCBlendingMode		= KM_ONE;
	pVertexContext->DSTBlendingMode		= KM_ZERO;
	pVertexContext->bSRCSel				= FALSE;			/* ARC1にはない */
	pVertexContext->bDSTSel				= FALSE;			/* ARC1にはない */
	pVertexContext->FogMode				= KM_NOFOG;			/* 11はリザーブ */
	pVertexContext->bUseSpecular		= FALSE;
	pVertexContext->bUseAlpha			= FALSE;
	pVertexContext->FlipUV				= KM_NOFLIP;
	pVertexContext->ClampUV				= KM_NOCLAMP;
	pVertexContext->FilterMode			= KM_BILINEAR;
	pVertexContext->bSuperSample		= FALSE;
	pVertexContext->MipMapAdjust		= 0x00000004;		/* D=1.00 */
	pVertexContext->TextureShadingMode	= KM_MODULATE;

	/* for Texture ControlBit */

	/* No effect on ARC1 */
	pVertexContext->bColorClamp		= FALSE;
	pVertexContext->PaletteBank		= 0;

	/* for GlobalColorData */
	pVertexContext->fFaceColorAlpha		= 1.0f;		/* Face Color Alpha */
	pVertexContext->fFaceColorRed		= 1.0f;		/* Face Color Red */
	pVertexContext->fFaceColorGreen		= 1.0f;		/* Face Color Green */
	pVertexContext->fFaceColorBlue		= 1.0f;		/* Face Color Blue */
	pVertexContext->fOffsetColorAlpha	= 1.0f;		/* Specular Color Alpha */
	pVertexContext->fOffsetColorRed		= 1.0f;		/* Specular Color Red */
	pVertexContext->fOffsetColorGreen	= 1.0f;		/* Specular Color Green */
	pVertexContext->fOffsetColorBlue	= 1.0f;		/* Specular Color Blue */

	kmProcessVertexRenderState(pVertexContext);
}

/****************************************************************************/
/**	Background surface setting **********************************************/
/****************************************************************************/
void	InitBackGround(void)
{
	KMSTATUS			status;
	KMVERTEXCONTEXT		BackVertexContext;		/* Vertex context */
	KMVERTEX_01			BV[3];					/* pointer to Vertex */
	PVOID				pBV[3];					/* pointer to Vertex */

	/* Clear VertexContext */
	memset(&BackVertexContext,0,sizeof(KMVERTEXCONTEXT));

	/* Setup VertexContext */
	BackVertexContext.RenderState	= KM_DEPTHMODE  		| KM_CULLINGMODE				/* ISP/TSP IW */
									| KM_SCREENCOORDINATION	| KM_SHADINGMODE
									| KM_MODIFIER 			| KM_ZWRITEDISABLE				/* TSP CW */
									| KM_SRCBLENDINGMODE
									| KM_SRCSELECT			| KM_DSTSELECT
									| KM_FOGMODE			| KM_USESPECULAR
									| KM_USEALPHA			| KM_IGNORETEXTUREALPHA
									| KM_FLIPUV 			| KM_CLAMPUV
									| KM_FILTERMODE			| KM_SUPERSAMPLE
									| KM_MIPMAPDADJUST 		| KM_TEXTURESHADINGMODE
									| KM_PARAMTYPE 			| KM_LISTTYPE 					/* Param CW */
									| KM_COLORTYPE			| KM_USESPECULAR
									| KM_UVFORMAT;

	/* for ParameterControlWord  */
	BackVertexContext.ParamType				= KM_POLYGON;
	BackVertexContext.ListType				= KM_OPAQUE_POLYGON;
	BackVertexContext.ColorType				= KM_FLOATINGCOLOR;
	BackVertexContext.bUseSpecular			= FALSE;
	BackVertexContext.UVFormat				= KM_32BITUV;

	/* for ISP/TSP InstructionWord  */
	BackVertexContext.DepthMode				= KM_GREATER;
	BackVertexContext.CullingMode			= KM_NOCULLING;
	BackVertexContext.ScreenCoordination	= KM_SCREEN;
	BackVertexContext.ShadingMode			= KM_NOTEXTUREGOURAUD;
	BackVertexContext.SelectModifier		= KM_NOMODIFIER;
	BackVertexContext.bZWriteDisable		= FALSE;			/* not effective on ARC1 */

	/* for TSP ControlWord  */
	BackVertexContext.SRCBlendingMode		= KM_ONE;
/*	BackVertexContext.DSTBlendingMode		= KM_ZERO;	// must not set this parameter on background plane */
	BackVertexContext.bSRCSel				= FALSE;			/* not effective on ARC1 */
	BackVertexContext.bDSTSel				= FALSE;			/* not effective on ARC1 */
	BackVertexContext.FogMode				= KM_NOFOG;			/* 11 is reserved */
	BackVertexContext.bUseSpecular			= FALSE;
	BackVertexContext.bUseAlpha				= FALSE;
	BackVertexContext.ClampUV				= KM_NOCLAMP;
	BackVertexContext.FilterMode			= KM_BILINEAR;
	BackVertexContext.bSuperSample			= FALSE;
	BackVertexContext.MipMapAdjust			= 0x00000004;		/* D=1.00 */
	BackVertexContext.TextureShadingMode	= KM_MODULATE;

	/* for Texture ControlBit */

	/* No effect on ARC1 */
	BackVertexContext.bColorClamp			= FALSE;
	BackVertexContext.PaletteBank			= 0;

	/* for GlobalColorData */
	BackVertexContext.fFaceColorAlpha		= 1.0f;		/* Face Color Alpha */
	BackVertexContext.fFaceColorRed			= 1.0f;		/* Face Color Red */
	BackVertexContext.fFaceColorGreen		= 1.0f;		/* Face Color Green */
	BackVertexContext.fFaceColorBlue		= 1.0f;		/* Face Color Blue */
	BackVertexContext.fOffsetColorAlpha		= 1.0f;		/* Specular Color Alpha */
	BackVertexContext.fOffsetColorRed		= 1.0f;		/* Specular Color Red */
	BackVertexContext.fOffsetColorGreen		= 1.0f;		/* Specular Color Green */
	BackVertexContext.fOffsetColorBlue		= 1.0f;		/* Specular Color Blue */

	status = kmProcessVertexRenderState(&BackVertexContext);
	Check_Status(status, "kmProcessVertexRenderState");
	status = kmSetBackGroundRenderState(&BackVertexContext);
	Check_Status(status, "kmSetBackGroundRenderState");

	/*----------------------------------------------------------------------------------- */
	/* set VERTEX DATA */

	/* Vertex #1 */
	BV[0].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	BV[0].fX				=  2.0f;
	BV[0].fY				= 460.0f;
#if defined(_STRICT_UNION_)
	BV[0].u.fZ				=  0.2f;
#else
	BV[0].fZ				=  0.2f;
#endif
	BV[0].fBaseAlpha		=  1.0f;
	BV[0].fBaseRed			=  0.0f;
	BV[0].fBaseGreen		=  0.0f;
	BV[0].fBaseBlue			=  1.0f;

	/* Vertex #2 */
	BV[1].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	BV[1].fX				= 320.0f;
	BV[1].fY				=  2.0f;
#if defined(_STRICT_UNION_)
	BV[1].u.fZ				=  0.2f;
#else
	BV[1].fZ				=  0.2f;
#endif
	BV[1].fBaseAlpha		=  1.0f;
	BV[1].fBaseRed			=  1.0f;
	BV[1].fBaseGreen		=  0.0f;
	BV[1].fBaseBlue			=  0.0f;

	/* Vertex #3 */
	BV[2].ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP;
	BV[2].fX				= 620.0f;
	BV[2].fY				= 460.0f;
#if defined(_STRICT_UNION_)
	BV[2].u.fZ				=  0.2f;
#else
	BV[2].fZ				=  0.2f;
#endif
	BV[2].fBaseAlpha		=  1.0f;
	BV[2].fBaseRed			=  0.0f;
	BV[2].fBaseGreen		=  1.0f;
	BV[2].fBaseBlue			=  0.0f;

	pBV[0] = &BV[0];
	pBV[1] = &BV[1];
	pBV[2] = &BV[2];

	/*----------------------------------------------------------------------------------- */
	/* set VERTEX */
	status = kmSetBackGroundPlane(	pBV,					/* pVertex */
									KM_VERTEXTYPE_01,		/* VertexType */
									sizeof(KMVERTEX_01)		/* StructSize */
									);
	Check_Status(status, "kmSetBackGroundPlane");
}

/****************************************************************************/
/**	Main ********************************************************************/
/****************************************************************************/
int	main(void)
{
#define	MAX_VERTEX	(4)

	int		frame = 0;
	int		i;

	/* parameter for object 1 */
	KMVERTICES	object01[MAX_VERTEX];
	KMVERTICES	object02[MAX_VERTEX];
	KMVERTICES	vertwork[MAX_VERTEX];
	KMVECTOR	translate01;
	KMVECTOR	translate02;
	KMVECTOR	rotate;
	KMVECTOR	rotate_d;

/****************************************************************************/
/** Vertex Data *************************************************************/
/****************************************************************************/
	object01[0].x = -300.0f; object01[0].y =  300.0f;  object01[0].z =  0.0f;  object01[0].u =  0.0f;  object01[0].v =  0.0f;
	object01[1].x = -300.0f; object01[1].y = -300.0f;  object01[1].z =  0.0f;  object01[1].u =  0.0f;  object01[1].v =  1.0f;
	object01[2].x =  300.0f; object01[2].y =  300.0f;  object01[2].z =  0.0f;  object01[2].u =  1.0f;  object01[2].v =  0.0f;
	object01[3].x =  300.0f; object01[3].y = -300.0f;  object01[3].z =  0.0f;  object01[3].u =  1.0f;  object01[3].v =  1.0f;

	object02[0].x = -300.0f; object02[0].y =  300.0f;  object02[0].z =  0.0f;  object02[0].u =  0.0f;  object02[0].v =  0.0f;
	object02[1].x = -300.0f; object02[1].y = -300.0f;  object02[1].z =  0.0f;  object02[1].u =  0.0f;  object02[1].v =  1.0f;
	object02[2].x =  300.0f; object02[2].y =  300.0f;  object02[2].z =  0.0f;  object02[2].u =  1.0f;  object02[2].v =  0.0f;
	object02[3].x =  300.0f; object02[3].y = -300.0f;  object02[3].z =  0.0f;  object02[3].u =  1.0f;  object02[3].v =  1.0f;

/****************************************************************************/
/** Kamu Test Main **********************************************************/
/****************************************************************************/

	txt_ClearScreen();	/* Clear text screen */
	PRINTOUT(("** Tri-linear filter test 01 **\n"));

	/*-----------------------------------------------------------------------------------*/
	/* Initialize Hardware */

    /* Replaced by sbInitSystem. */
    /* status = kmInitDevice(KM_NTSC); */
    /* Check_Status(status, "kmInitDevice"); */

	kmGetVersionInfo(&VersionInfo);
	PRINTOUT(("MajorVersion      0x0%08x\n",VersionInfo.kmMajorVersion));
	PRINTOUT(("LocalVersion      0x0%08x\n",VersionInfo.kmLocalVersion));
	PRINTOUT(("kmFrameBufferSize 0x0%08x\n",VersionInfo.kmFrameBufferSize));

    sbInitSystem (KM_DSPMODE_NTSCNI640x480, KM_DSPBPP_RGB565, 1);
    /* sbInitSystem (KM_DSPMODE_VGA, KM_DSPBPP_RGB565, 1); */

	border.dwPacked	=	0x00000000;
	kmSetBorderColor(border);
    Check_Status(status, "kmSetBorderColor");

    psurface[0]         =   &PrimarySurfaceDesc;
    psurface[1]         =   &BackSurfaceDesc;

    /* generate vertex buffer */
	kmsc.pVertexBuffer	=	(void *)((((int)syMalloc(2*sizeof(KMDWORD)*VBUFFER_SIZE)&0x3FFFFFE0)|0xA0000000)+32);

	/* Set system configuration */
    status  =   kmSetSystemConfiguration(&kmsc);
    Check_Status(status, "kmSetSystemConfiguration");

    /*-----------------------------------------------------------------------------------*/
	/* set display mode */
    /* status = kmSetDisplayMode(  KM_DSPMODE_NTSCNI640x480, */       /* nDisplayMode */
                                /* KM_DSPBPP_RGB565, */  /* nBpp */
                                /* TRUE, */              /* bDither */
                                /* FALSE */              /* bAntiAlias */
                                /* ); */
    /* Check_Status(status, "kmSetDisplayMode"); */

	/*-----------------------------------------------------------------------------------*/
	/* Create vertex buffer */
    /* status = kmCreateVertexBuffer(&VertexBufferDesc, */       /* pBufferDesc */
                                    /* 131072, */                /* Opaque-Polygon 500KB */
                                    /* 0, */                     /* Opaque-Modifier */
                                    /* 131072, */                /* Trans-Polygon 500KB */
                                    /* 0 */                      /* Trans-Modifier */
                                    /* ); */
    /* Check_Status(status, "kmCreateVertexBuffer"); */

	/*-----------------------------------------------------------------------------------*/
	/* Create primary frame buffer surface */
    /* status = kmCreateFrameBufferSurface(&PrimarySurfaceDesc, */   /* pSurfaceDesc */
                                /* 640, */   /* nWidth */
                                /* 480, */   /* nHeight */
                                /* FALSE, */ /* bStripBuffer */
                                /* FALSE */  /* bBufferClear */
                                /* ); */
    /* Check_Status(status, "kmCreateFrameBufferSurface(Primary)"); */

	/*-----------------------------------------------------------------------------------*/
	/* Create backbuffer frame buffer surface */
    /* status = kmCreateFrameBufferSurface(&BackSurfaceDesc, */  /* pSurfaceDesc */
                                /* 640, */   /* nWidth */
                                /* 480, */   /* nHeight */
                                /* FALSE, */ /* bStripBuffer */
                                /* FALSE */  /* bBufferClear */
                                /* ); */
    /* Check_Status(status, "kmCreateFrameBufferSurface(Back-buffer)"); */

	/*-----------------------------------------------------------------------------------*/
	/* set display surface */
    /* status = kmActivateFrameBuffer( &PrimarySurfaceDesc, */   /* pSurfaceDesc */
                                    /* &BackSurfaceDesc, */      /* pSurfaceDesc */
                                    /* FALSE, */                 /* bStripBuffer */
                                    /* TRUE */                   /* bWaitVSync */
                                    /* ); */
    /* Check_Status(status, "kmActivateFrameBuffer"); */

	/*-----------------------------------------------------------------------------------*/
	/* Other initializes */
	txt_InitDisplayString();	/* Initialize string data */
	InitBackGround();		/* set background plane */

	/*----------------------------------------------------------------------------------- */
	/* Create texture surface */
	nTextureType = KM_TEXTURE_TWIDDLED_MM | KM_TEXTURE_RGB565;	/* Set texture type */
	status = kmCreateTextureSurface(
		&TexSurfaceDesc1,		/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		256,					/*	IN	KMINT32			nWidth  */
		256,					/*	IN	KMINT32			nHeight  */
		nTextureType			/*	IN	KMTEXTURETYPE	nTextureType */
	);
	Check_Status(status, "kmCreateTextureSurface");

	/*----------------------------------------------------------------------------------- */
	/* Load and set texture */
	pTexture01 = (unsigned long *)&segata;
	status = kmLoadTexture(&TexSurfaceDesc1,	/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),			/* PKMDWORD pTexture */
							FALSE,				/* KMBOOLEAN bAutoMipMap */
							FALSE				/* KMBOOLEAN bDither */
							);
	Check_Status(status, "kmLoadTexture #1");

	/*----------------------------------------------------------------------------------- */
	/* set VERTEXCONTEXT(1) : Object1 : Opaque */
	memset( &VertexContext01, 0, sizeof(VertexContext01));	/* Crear Context */
	InitVertexContext( &VertexContext01);

	VertexContext01.RenderState	= 0;
	/* Set shading mode */
	VertexContext01.RenderState	|= KM_SHADINGMODE;
	VertexContext01.ShadingMode	= KM_TEXTUREGOURAUD;
	/* Set texture surface */
	VertexContext01.pTextureSurfaceDesc	= &TexSurfaceDesc1;
	/* Set Texture Clamp */
	VertexContext01.RenderState	|= KM_CLAMPUV;
	VertexContext01.ClampUV		 = KM_CLAMP_UV;

	/* Process & Set */
	status = kmProcessVertexRenderState(&VertexContext01);
	Check_Status(status, "kmProcessVertexRenderState");

	/*----------------------------------------------------------------------------------- */
	/* Init transform parameter */

	/* translate (object 1) */
	translate01[0] =  400.0f;
	translate01[1] =    0.0f;
	translate01[2] =  300.0f;

	/* translate (object 2) */
	translate02[0] = -400.0f;
	translate02[1] =    0.0f;
	translate02[2] =  300.0f;

	/* rotate */
	rotate[0] = 0.0f;
	rotate[1] = 0.0f;
	rotate[2] = 0.0f;
	/* rotate delta */
	rotate_d[0] = 0.5f;
	rotate_d[1] = 0.5f;
	rotate_d[2] = 0.5f;

	/*----------------------------------------------------------------------------------- */
	/* Start rendering loop */
	do{

		/* rotation */
		rotate[0] += rotate_d[0]; if( rotate[0] >= 360) rotate[0] -= 360;
		rotate[1] += rotate_d[1]; if( rotate[1] >= 360) rotate[1] -= 360;
		rotate[2] += rotate_d[2]; if( rotate[2] >= 360) rotate[2] -= 360;

		/* object 01 : Tri-linear OFF */
		Transform(
				translate01,	/* KMVECTOR	translate */
				rotate,			/* KMVECTOR	rotate */
				MAX_VERTEX,		/* int		vertnum */
				object01,		/* KMVERTICES	*inputvector */
				vertwork		/* KMVERTICES	*outputvector */
				);

		SendTriangleStrip(
				VertexBufferDesc,	/* KMVERTEXBUFFDESC BufferDesc */
				VertexContext01,	/* KMVERTEXCONTEXT  VertexContext */
				MAX_VERTEX,			/* int nNumVertex */
				vertwork,			/* KMVERTICES *Vertices */
				FALSE				/* int bTrilinear */
				);

		/* object 02 : Tri-linear ON */
		Transform(
				translate02,	/* KMVECTOR	translate */
				rotate,			/* KMVECTOR	rotate */
				MAX_VERTEX,		/* int		vertnum */
				object01,		/* KMVERTICES	*inputvector */
				vertwork		/* KMVERTICES	*outputvector */
				);

		SendTriangleStrip(
				VertexBufferDesc,	/* KMVERTEXBUFFDESC BufferDesc */
				VertexContext01,	/* KMVERTEXCONTEXT  VertexContext */
				MAX_VERTEX,			/* int nNumVertex */
				vertwork,			/* KMVERTICES *Vertices */
				TRUE				/* int bTrilinear */
				);

	/*----------------------------------------------------------------------------------- */
	/* Rendering start */
#if defined(_DISPLAY_STRING_)
	txt_DispScreen(VertexBufferDesc);		/* Display screen */
#endif
	status = kmRender();
	Check_Status(status, "kmRender");

	/*----------------------------------------------------------------------------------- */
	/* Flip screen */
	status = kmFlipFrameBuffer();
	Check_Status(status, "kmFlipFrameBuffer");

	/*----------------------------------------------------------------------------------- */
	/* Loop */
		frame++;
	}
	while(1);	/* end of "do" */

	/*----------------------------------------------------------------------------------- */
	/* Free vertex buffer */
	status = kmDiscardVertexBuffer(&VertexBufferDesc);
	Check_Status(status, "kmDiscadeVertexBuffer");

	/*----------------------------------------------------------------------------------- */
	/* Free texture Surface */
	status = kmFreeTexture(&TexSurfaceDesc1);
	Check_Status(status, "kmFreeTexture");

	/*----------------------------------------------------------------------------------- */
	/* End display */
	status = kmStopDisplayFrameBuffer();
	Check_Status(status, "kmStopDisplayFrameBuffer");

	PRINTOUT(("** End of test program **\n"));
	return  0;
}
