/********************************************************************/
/* Kamui function test program										*/
/* Test of Twiddled Mipmap and VQ Texture							*/
/********************************************************************/
/*   COPYRIGHT (C) NEC Corporation 1997 - 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.                                                       */
/********************************************************************/

#include	<stdio.h>

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

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

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

/* Textures */
extern unsigned char m1024twmm[];
extern unsigned char v128_1[];
extern unsigned char v128_2[];


/****************************************************************************/
/**	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 */
	KMSURFACEDESC		TexSurfaceDesc2;		/* texture surface */
	KMSURFACEDESC		TexSurfaceDesc3;		/* texture surface */
	KMVERTEXBUFFDESC	VertexBufferDesc;		/* Vertex-buffer */
	KMVERTEXCONTEXT		VertexContext01;		/* Vertex context 1 : Object 1 : Opaque */
	KMVERTEXCONTEXT		VertexContext02;		/* Vertex context 2 : Object 2 : Translucent */
	KMVERTEXCONTEXT		VertexContext03;		/* Vertex context 3 : target plane of "RenderTexture"  */
	KMVERTEX_05			V1[4];					/* Vertexs 1 : Object 1 : Twiddled */
	KMVERTEX_05			V2[4];					/* Vertexs 2 : Object 2 : VQ */
	KMVERTEX_05			V3[4];					/* Vertexs 3 : Object 3 : VQ */
	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;
	}
}

/****************************************************************************/
/**	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_FLOATINGCOLOR;
	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_POINT_SAMPLE;
	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_POINT_SAMPLE;
	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				= 260.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				= 150.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				= 560.0f;
	BV[2].fY				= 260.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");
}

#if defined(_X86_)
/****************************************************************************/
/**	Load texture ************************************************************/
/****************************************************************************/
PKMDWORD LoadTextureFile(char *filename)
{
	int			hFile;
	long		filelen;
	PKMDWORD	pTexture;

	/* Open input file */
	hFile = _open(filename, _O_RDONLY | _O_BINARY);
	if(hFile != -1) {
		filelen = _filelength(hFile);			/* length of texture file */
		pTexture = (PKMDWORD)malloc(filelen);	/* allocate mainmemory */
		_read(hFile, pTexture, filelen);		/* read in texture file */
		_close(hFile);							/* file close */
		printf("LoadTextureFile: Texture file %s readed.\n",filename);
	}
	else {
		printf("LoadTextureFile: ERROR Can not read file %s.\n",filename);
	}
	return pTexture;
}
#endif

/****************************************************************************/
/**	Main ********************************************************************/
/****************************************************************************/
int	main(void)
{
	int		frame = 0;
	int		i, j;
	float	x1, y1, x2, y2;


/****************************************************************************/
/** Vertex Data *************************************************************/
/****************************************************************************/
	/* Object 1 : Opaque Polygon */
	/* Vertex #1 */
	V1[0].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V1[0].fX				=  0.0f;
	V1[0].fY				=  0.0f;
#if defined(_STRICT_UNION_)
	V1[0].u.fZ				=  0.5f;
#else
	V1[0].fZ				=  0.5f;
#endif
	V1[0].fU				=  0.0f;
	V1[0].fV				=  0.0f;
	V1[0].fBaseAlpha		=  1.0f;
	V1[0].fBaseRed			=  1.0f;
	V1[0].fBaseGreen		=  1.0f;
	V1[0].fBaseBlue			=  1.0f;
	V1[0].fOffsetAlpha		=  1.0f;
	V1[0].fOffsetRed		=  0.0f;
	V1[0].fOffsetGreen		=  0.0f;
	V1[0].fOffsetBlue		=  0.0f;

	/* Vertex #2 */
	V1[1].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V1[1].fX				=  0.0f;
	V1[1].fY				=  0.0f;
#if defined(_STRICT_UNION_)
	V1[1].u.fZ				=  0.5f;
#else
	V1[1].fZ				=  0.5f;
#endif
	V1[1].fU				=  0.0f;
	V1[1].fV				=  1.0f;
	V1[1].fBaseAlpha		=  1.0f;
	V1[1].fBaseRed			=  1.0f;
	V1[1].fBaseGreen		=  1.0f;
	V1[1].fBaseBlue			=  1.0f;
	V1[1].fOffsetAlpha		=  1.0f;
	V1[1].fOffsetRed		=  0.0f;
	V1[1].fOffsetGreen		=  0.0f;
	V1[1].fOffsetBlue		=  0.0f;

	/* Vertex #3 */
	V1[2].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V1[2].fX				=  0.0f;
	V1[2].fY				=  0.0f;
#if defined(_STRICT_UNION_)
	V1[2].u.fZ				=  0.5f;
#else
	V1[2].fZ				=  0.5f;
#endif
	V1[2].fU				=  1.0f;
	V1[2].fV				=  0.0f;
	V1[2].fBaseAlpha		=  1.0f;
	V1[2].fBaseRed			=  1.0f;
	V1[2].fBaseGreen		=  1.0f;
	V1[2].fBaseBlue			=  1.0f;
	V1[2].fOffsetAlpha		=  1.0f;
	V1[2].fOffsetRed		=  0.0f;
	V1[2].fOffsetGreen		=  0.0f;
	V1[2].fOffsetBlue		=  0.0f;

	/* Vertex #4 */
	V1[3].ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP;
	V1[3].fX				=  0.0f;
	V1[3].fY				=  0.0f;
#if defined(_STRICT_UNION_)
	V1[3].u.fZ				=  0.5f;
#else
	V1[3].fZ				=  0.5f;
#endif
	V1[3].fU				=  1.0f;
	V1[3].fV				=  1.0f;
	V1[3].fBaseAlpha		=  1.0f;
	V1[3].fBaseRed			=  1.0f;
	V1[3].fBaseGreen		=  1.0f;
	V1[3].fBaseBlue			=  1.0f;
	V1[3].fOffsetAlpha		=  1.0f;
	V1[3].fOffsetRed		=  0.0f;
	V1[3].fOffsetGreen		=  0.0f;
	V1[3].fOffsetBlue		=  0.0f;

	/* Object 2 : Translucent Polygon */
	/* Vertex #1 */
	V2[0].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V2[0].fX				=  0.0f;
	V2[0].fY				=  0.0f;
#if defined(_STRICT_UNION_)
	V2[0].u.fZ				=  0.6f;
#else
	V2[0].fZ				=  0.6f;
#endif
	V2[0].fU				=  0.0f;
	V2[0].fV				=  0.0f;
	V2[0].fBaseAlpha		=  1.0f;
	V2[0].fBaseRed			=  1.0f;
	V2[0].fBaseGreen		=  1.0f;
	V2[0].fBaseBlue			=  1.0f;
	V2[0].fOffsetAlpha		=  1.0f;
	V2[0].fOffsetRed		=  0.0f;
	V2[0].fOffsetGreen		=  0.0f;
	V2[0].fOffsetBlue		=  0.0f;

	/* Vertex #2 */
	V2[1].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V2[1].fX				=  0.0f;
	V2[1].fY				=  0.0f;
#if defined(_STRICT_UNION_)
	V2[1].u.fZ				=  0.6f;
#else
	V2[1].fZ				=  0.6f;
#endif
	V2[1].fU				=  0.0f;
	V2[1].fV				=  1.0f;
	V2[1].fBaseAlpha		=  1.0f;
	V2[1].fBaseRed			=  1.0f;
	V2[1].fBaseGreen		=  1.0f;
	V2[1].fBaseBlue			=  1.0f;
	V2[1].fOffsetAlpha		=  1.0f;
	V2[1].fOffsetRed		=  0.0f;
	V2[1].fOffsetGreen		=  0.0f;
	V2[1].fOffsetBlue		=  0.0f;

	/* Vertex #3 */
	V2[2].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V2[2].fX				=  0.0f;
	V2[2].fY				=  0.0f;
#if defined(_STRICT_UNION_)
	V2[2].u.fZ				=  0.6f;
#else
	V2[2].fZ				=  0.6f;
#endif
	V2[2].fU				=  1.0f;
	V2[2].fV				=  0.0f;
	V2[2].fBaseAlpha		=  1.0f;
	V2[2].fBaseRed			=  1.0f;
	V2[2].fBaseGreen		=  1.0f;
	V2[2].fBaseBlue			=  1.0f;
	V2[2].fOffsetAlpha		=  1.0f;
	V2[2].fOffsetRed		=  0.0f;
	V2[2].fOffsetGreen		=  0.0f;
	V2[2].fOffsetBlue		=  0.0f;

	/* Vertex #4 */
	V2[3].ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP;
	V2[3].fX				=  0.0f;
	V2[3].fY				=  0.0f;
#if defined(_STRICT_UNION_)
	V2[3].u.fZ				=  0.6f;
#else
	V2[3].fZ				=  0.6f;
#endif
	V2[3].fU				=  1.0f;
	V2[3].fV				=  1.0f;
	V2[3].fBaseAlpha		=  1.0f;
	V2[3].fBaseRed			=  1.0f;
	V2[3].fBaseGreen		=  1.0f;
	V2[3].fBaseBlue			=  1.0f;
	V2[3].fOffsetAlpha		=  1.0f;
	V2[3].fOffsetRed		=  0.0f;
	V2[3].fOffsetGreen		=  0.0f;
	V2[3].fOffsetBlue		=  0.0f;

	/* Object 3 : target plane of RenderTexture */
	/* Vertex #1 */
	V3[0].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V3[0].fX				=  460.0f;
	V3[0].fY				=  299.0f;
#if defined(_STRICT_UNION_)
	V3[0].u.fZ				=  1.0f;
#else
	V3[0].fZ				=  1.0f;
#endif
	V3[0].fU				=  0.0f;
/*	V3[0].fV				=  0.46875f;	// 1024x1024 */
	V3[0].fV				=  0.9375f;		/* 1024x512 */
/*	V3[0].fU				=  0.0f; */
/*	V3[0].fV				=  1.0f; */
	V3[0].fBaseAlpha		=  1.0f;
	V3[0].fBaseRed			=  1.0f;
	V3[0].fBaseGreen		=  1.0f;
	V3[0].fBaseBlue			=  1.0f;
	V3[0].fOffsetAlpha		=  1.0f;
	V3[0].fOffsetRed		=  0.0f;
	V3[0].fOffsetGreen		=  0.0f;
	V3[0].fOffsetBlue		=  0.0f;

	/* Vertex #2 */
	V3[1].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V3[1].fX				=  460.0f;
	V3[1].fY				=  180.0f;
#if defined(_STRICT_UNION_)
	V3[1].u.fZ				=  1.0f;
#else
	V3[1].fZ				=  1.0f;
#endif
	V3[1].fU				=  0.0f;
	V3[1].fV				=  0.0f;
	V3[1].fBaseAlpha		=  1.0f;
	V3[1].fBaseRed			=  1.0f;
	V3[1].fBaseGreen		=  1.0f;
	V3[1].fBaseBlue			=  1.0f;
	V3[1].fOffsetAlpha		=  1.0f;
	V3[1].fOffsetRed		=  0.0f;
	V3[1].fOffsetGreen		=  0.0f;
	V3[1].fOffsetBlue		=  0.0f;

	/* Vertex #3 */
	V3[2].ParamControlWord	= KM_VERTEXPARAM_NORMAL;
	V3[2].fX				=  619.0f;
	V3[2].fY				=  299.0f;
#if defined(_STRICT_UNION_)
	V3[2].u.fZ				=  1.0f;
#else
	V3[2].fZ				=  1.0f;
#endif
	V3[2].fU				=  0.625f;
/*	V3[2].fV				=  0.46875f;	// 1024x1024 */
	V3[2].fV				=  0.9375f;		/* 1024x512 */
/*	V3[2].fU				=  1.0f; */
/*	V3[2].fV				=  1.0f; */
	V3[2].fBaseAlpha		=  1.0f;
	V3[2].fBaseRed			=  1.0f;
	V3[2].fBaseGreen		=  1.0f;
	V3[2].fBaseBlue			=  1.0f;
	V3[2].fOffsetAlpha		=  1.0f;
	V3[2].fOffsetRed		=  0.0f;
	V3[2].fOffsetGreen		=  0.0f;
	V3[2].fOffsetBlue		=  0.0f;

	/* Vertex #4 */
	V3[3].ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP;
	V3[3].fX				=  619.0f;
	V3[3].fY				=  180.0f;
#if defined(_STRICT_UNION_)
	V3[3].u.fZ				=  1.0f;
#else
	V3[3].fZ				=  1.0f;
#endif
	V3[3].fU				=  0.625f;
	V3[3].fV				=  0.0f;
/*	V3[3].fU				=  1.0f; */
/*	V3[3].fV				=  0.0f; */
	V3[3].fBaseAlpha		=  1.0f;
	V3[3].fBaseRed			=  1.0f;
	V3[3].fBaseGreen		=  1.0f;
	V3[3].fBaseBlue			=  1.0f;
	V3[3].fOffsetAlpha		=  1.0f;
	V3[3].fOffsetRed		=  0.0f;
	V3[3].fOffsetGreen		=  0.0f;
	V3[3].fOffsetBlue		=  0.0f;

/****************************************************************************/
/** Kamu Test Main **********************************************************/
/****************************************************************************/
#ifdef __GNUC__
	shinobi_workaround();
#endif

	txt_ClearScreen();	/* Clear text screen */
	PRINTOUT(("** KAMUI Texture TEST 21 : Twiddled mipmap and VQ **\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 */

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

	nTextureType = KM_TEXTURE_VQ_MM | KM_TEXTURE_ARGB4444;	/* Set texture type */
	status = kmCreateTextureSurface(
		&TexSurfaceDesc2,		/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		128,					/*	IN	KMINT32			nWidth  */
		128,					/*	IN	KMINT32			nHeight  */
		nTextureType			/*	IN	KMTEXTURETYPE	nTextureType */
	);
	Check_Status(status, "kmCreateTextureSurface");

	nTextureType = KM_TEXTURE_VQ_MM | KM_TEXTURE_ARGB4444;	/* Set texture type */
/*	nTextureType = KM_TEXTURE_STRIDE | KM_TEXTURE_RGB565;	// Set texture type */
	status = kmCreateTextureSurface(
		&TexSurfaceDesc3,		/* OUT	PKMSURFACEDESC	pSurfaceDesc */
		128,					/*	IN	KMINT32			nWidth  */
		128,					/*	IN	KMINT32			nHeight  */
		nTextureType			/*	IN	KMTEXTURETYPE	nTextureType */
	);
	Check_Status(status, "kmCreateTextureSurface");

	/*----------------------------------------------------------------------------------- */
	/* Load and set texture */
	/* Twiddled mipmap RGB565 */
#if defined(_X86_)
	pTexture01 =  LoadTextureFile(".\\Sat1024.pvr");
#else
	pTexture01 = (unsigned long *)&m1024twmm;
#endif
	status = kmLoadTexture(&TexSurfaceDesc1,	/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),			/* PKMDWORD pTexture */
							FALSE,				/* KMBOOLEAN bAutoMipMap */
							FALSE				/* KMBOOLEAN bDither */
							);
	Check_Status(status, "kmLoadTexture #1");

	/* VQ ARGB4444 */
#if defined(_X86_)
	pTexture01 =  LoadTextureFile(".\\mwood1.pvr");
#else
	pTexture01 = (unsigned long *)&v128_1;
#endif
	status = kmLoadTexture(&TexSurfaceDesc2,	/* PKMSURFACEDESC pSurfaceDesc */
							pTexture01 + (16 / sizeof(KMDWORD)),			/* PKMDWORD pTexture */
							FALSE,				/* KMBOOLEAN bAutoMipMap */
							FALSE				/* KMBOOLEAN bDither */
							);
	Check_Status(status, "kmLoadTexture #2");

	/* VQ ARGB4444 */
#if defined(_X86_)
	pTexture01 =  LoadTextureFile(".\\marble3.pvr");
#else
	pTexture01 = (unsigned long *)&v128_2;
#endif
	status = kmLoadTexture(&TexSurfaceDesc3,
							pTexture01 + (16 / sizeof(KMDWORD)),
							FALSE,
							FALSE
							);
	Check_Status(status, "kmLoadTexture #3");

	/*----------------------------------------------------------------------------------- */
	/* 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");

	/*----------------------------------------------------------------------------------- */
	/* set VERTEXCONTEXT(2) : Object2 : Translucent */
	memset( &VertexContext02, 0, sizeof(VertexContext02));	/* Crear Context */
	InitVertexContext( &VertexContext02);

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

#if 0
	/* */
	VertexContext02.RenderState	|= KM_SRCBLENDINGMODE;
	VertexContext02.SRCBlendingMode	= KM_SRCALPHA;
	/* */
	VertexContext02.RenderState	|= KM_DSTBLENDINGMODE;
	VertexContext02.DSTBlendingMode	= KM_INVSRCALPHA;
	/* */
	VertexContext02.RenderState	|= KM_LISTTYPE;
	VertexContext02.ListType  = KM_TRANS_POLYGON;
	/* */
	VertexContext02.RenderState	|= KM_USEALPHA;
	VertexContext02.bUseAlpha = TRUE;
#endif

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

	/*----------------------------------------------------------------------------------- */
	/* set VERTEXCONTEXT(3) : Object3 : target of "RenderTexture" */
	memset( &VertexContext03, 0, sizeof(VertexContext03));	/* Crear Context */
	InitVertexContext( &VertexContext03);

	VertexContext03.RenderState	= 0;
	/* Set shading mode */
	VertexContext03.RenderState	|= KM_SHADINGMODE;
	VertexContext03.ShadingMode	= KM_TEXTUREGOURAUD;
	/* Set texture surface */
	VertexContext03.pTextureSurfaceDesc	= &TexSurfaceDesc3;
	/* Set Texture Clamp */
	VertexContext03.RenderState	|= KM_CLAMPUV;
	VertexContext03.ClampUV		 = KM_CLAMP_UV;
	
	/* Process & Set */
	status = kmProcessVertexRenderState(&VertexContext03);
	Check_Status(status, "kmProcessVertexRenderState");

	/*----------------------------------------------------------------------------------- */
	/* set background plane */
	InitBackGround();

	/*----------------------------------------------------------------------------------- */
	/* Start rendering loop */
	for(j = 0; j < 24; j++){
	for(i = 10; i < 200; i += 1) {
		x1 = (float)((200.0 - (float)i) / 2.0);
		y1 = (float)((200.0 - (float)i) / 2.0);
		x2 = x1 + (float)i;
		y2 = y1 + (float)i;

		/* No.1 */
		V1[0].fX	= x1 + 110;
		V1[0].fY	= y2 +  30;
		V1[1].fX	= x1 + 110;
		V1[1].fY	= y1 +  30;
		V1[2].fX	= x2 + 110;
		V1[2].fY	= y2 +  30;
		V1[3].fX	= x2 + 110;
		V1[3].fY	= y1 +  30;

		/* No.2 */
		V2[0].fX	= x1 + 330;
		V2[0].fY	= y2 +  30;
		V2[1].fX	= x1 + 330;
		V2[1].fY	= y1 +  30;
		V2[2].fX	= x2 + 330;
		V2[2].fY	= y2 +  30;
		V2[3].fX	= x2 + 330;
		V2[3].fY	= y1 +  30;

		/* No.3 */
		V3[0].fX	= x1 + 110;
		V3[0].fY	= y2 + 250;
		V3[1].fX	= x1 + 110;
		V3[1].fY	= y1 + 250;
		V3[2].fX	= x2 + 110;
		V3[2].fY	= y2 + 250;
		V3[3].fX	= x2 + 110;
		V3[3].fY	= y1 + 250;

	/***********/
	/* Object1 */
	/***********/
	/*----------------------------------------------------------------------------------- */
	/* Set Vertex context */
	status = kmSetVertexRenderState(&VertexContext01);
	Check_Status(status, "kmSetVertexRenderState");
	status = kmStartVertexStrip(&VertexBufferDesc);
	Check_Status(status, "kmStartVertexStrip");

	/*----------------------------------------------------------------------------------- */
	/* set vertex data */
	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V1[0],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#1)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V1[1],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#2)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V1[2],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#3)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V1[3],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#4)");

	/***********/
	/* Object2 */
	/***********/
	/*----------------------------------------------------------------------------------- */
	/* Set Vertex context */
	status = kmSetVertexRenderState(&VertexContext02);
	Check_Status(status, "kmSetVertexRenderState");
	status = kmStartVertexStrip(&VertexBufferDesc);
	Check_Status(status, "kmStartVertexStrip");

	/*----------------------------------------------------------------------------------- */
	/* set vertex data */
	/* Object2 : Translucent polygon */
	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V2[0],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#4)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V2[1],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#5)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V2[2],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#6)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V2[3],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#7)");

	/***********/
	/* Object3 */
	/***********/
	/*----------------------------------------------------------------------------------- */
	/* Set Vertex context */
	status = kmSetVertexRenderState(&VertexContext03);
	Check_Status(status, "kmSetVertexRenderState");
	status = kmStartVertexStrip(&VertexBufferDesc);
	Check_Status(status, "kmStartVertexStrip");

	/*----------------------------------------------------------------------------------- */
	/* set vertex data */
	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V3[0],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#8)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V3[1],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#9)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V3[2],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#A)");

	status = kmSetVertex(	&VertexBufferDesc,		/* pBufferDesc */
							&V3[3],					/* pVertex */
							KM_VERTEXTYPE_05,		/* VertexType */
							sizeof(KMVERTEX_05)		/* StructSize */
							);
	Check_Status(status, "kmSetVertex(#B)");

		/*----------------------------------------------------------------------------------- */
		/* 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++;
	}
	}

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

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

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

	return  0;
}
