//=============================================================================
//
// Copyright 1998 by Captivation Digital Laboratories, Inc.
// All Rights Reserved.
//
//=============================================================================
//
// "KRAD" -- Kamui Rendering/Animation Demonstration
//
// <<< INSERT MORE INFO HERE >>>
//
//=============================================================================
//-----------------------------------------------------------------------------
// $Revision: 1 $
// $NoKeywords: $
//=============================================================================

#include "krad.h"
#include "kppverts.h"

//=============================================================================
//=============================================================================	MODULE DEFINITIONS
//=============================================================================

//=============================================================================
//=============================================================================	MODULE VARIABLES
//=============================================================================
KMVERTEXCONTEXT*	lastVContext = NULL;

//=============================================================================
//=============================================================================	CLASS KVContext
//=============================================================================

//=============================================================================	KVContext::KVContext
KVContext::KVContext()
{
	// -- flash init of the data --
	mDirty		= true;
	mDirtyVtype = true;
	memset (&mVcontext, 0, sizeof (mVcontext));

	// -- set the structure to reasonable defaults --
	mVcontext.RenderState		|= KM_PARAMTYPE;
	mVcontext.ParamType			 = KM_POLYGON;

	mVcontext.RenderState		|= KM_LISTTYPE;
	mVcontext.ListType			 = KM_OPAQUE_POLYGON;

	mVcontext.RenderState		|= KM_COLORTYPE;
	mVcontext.ColorType			 = KM_PACKEDCOLOR;

	mVcontext.RenderState		|= KM_UVFORMAT;
	mVcontext.UVFormat			 = KM_32BITUV;

	mVcontext.RenderState		|= KM_DEPTHMODE;
	mVcontext.DepthMode			 = KM_GREATER;

	mVcontext.RenderState		|= KM_CULLINGMODE;
	mVcontext.CullingMode		 = KM_NOCULLING;

	mVcontext.RenderState		|= KM_SCREENCOORDINATION;
	mVcontext.ScreenCoordination = KM_SCREEN;

	mVcontext.RenderState		|= KM_SHADINGMODE;
	mVcontext.ShadingMode		 = KM_NOTEXTUREGOURAUD;

	mVcontext.RenderState		|= KM_MODIFIER;
	mVcontext.SelectModifier	 = KM_NOMODIFIER;

	mVcontext.RenderState		|= KM_ZWRITEDISABLE;
	mVcontext.bZWriteDisable	 = FALSE;

	mVcontext.RenderState		|= KM_SRCBLENDINGMODE;
	mVcontext.SRCBlendingMode	 = KM_ONE;

	mVcontext.RenderState		|= KM_DSTBLENDINGMODE;
	mVcontext.DSTBlendingMode	 = KM_ZERO;

	mVcontext.RenderState		|= KM_SRCSELECT;
	mVcontext.bSRCSel			 = FALSE;

	mVcontext.RenderState		|= KM_DSTSELECT;
	mVcontext.bDSTSel			 = FALSE;

	mVcontext.RenderState		|= KM_FOGMODE;
	mVcontext.FogMode			 = KM_NOFOG;

	mVcontext.RenderState		|= KM_USESPECULAR;
	mVcontext.bUseSpecular		 = FALSE;

	mVcontext.RenderState		|= KM_USEALPHA;
	mVcontext.bUseAlpha			 = FALSE;

	mVcontext.RenderState		|= KM_IGNORETEXTUREALPHA;
	mVcontext.bIgnoreTextureAlpha= FALSE;

	mVcontext.RenderState		|= KM_CLAMPUV;
	mVcontext.ClampUV			 = KM_NOCLAMP;

	mVcontext.RenderState		|= KM_FLIPUV;
	mVcontext.FlipUV			 = KM_NOFLIP;

	mVcontext.RenderState		|= KM_FILTERMODE;
	mVcontext.FilterMode		 = KM_POINT_SAMPLE;

	mVcontext.RenderState		|= KM_SUPERSAMPLE;
	mVcontext.bSuperSample		 = FALSE;

	mVcontext.RenderState		|= KM_MIPMAPDADJUST;
	mVcontext.MipMapAdjust		 = 0x00000004;

	mVcontext.RenderState		|= KM_TEXTURESHADINGMODE;
	mVcontext.TextureShadingMode = KM_MODULATE;

	mVcontext.RenderState		|= KM_COLORCLAMP;
	mVcontext.bColorClamp		 = FALSE;

	mVcontext.RenderState		|= KM_PALETTEBANK;
	mVcontext.PaletteBank		 = 0;

	mVcontext.pTextureSurfaceDesc= NULL;

	mVcontext.fFaceColorAlpha	 = 1.0f;
	mVcontext.fFaceColorRed		 = 1.0f;
	mVcontext.fFaceColorGreen	 = 1.0f;
	mVcontext.fFaceColorBlue	 = 1.0f;

	mVcontext.fOffsetColorAlpha	 = 1.0f;
	mVcontext.fOffsetColorRed	 = 1.0f;
	mVcontext.fOffsetColorGreen	 = 1.0f;
	mVcontext.fOffsetColorBlue	 = 1.0f;
}

//=============================================================================	KVContext::~KVContext
KVContext::~KVContext()
{
	// -- the extremely rare case of a new getting the just delete'd one --
	if (lastVContext == &mVcontext)
		lastVContext = NULL;
}

//=============================================================================	KVContext::Set()
void KVContext::Set()
{
	// -- we don't have to do anything, if the context is clean
	// -- and it was used last time
	if (!mDirty)
	{
		if (lastVContext == &mVcontext)
			return;
	}
	else
	{	// -- it's dirty
		KAMUI_TRY (kmProcessVertexRenderState(&mVcontext));
		mDirty = false;
	}

	// -- set the vertex state and initiate a strip --
	KAMUI_TRY (kmSetVertexRenderState (&mVcontext));
	KAMUI_TRY (KAMUI_START_VERTEX_STRIP());
}


//============================================================================= KVCONTEXT::ValidateVertexType
// This function calculates, based on the KMVERTEXCONTEXT parameters, the
// cheapest legal vertex type -- if it exists.
//	PRIORITY OF SELECTION:	LIST_TYPE
//							TEXTURED/bUseSpecular
//							COLOR_TYPE
//							UV_MODE
//-----------------------------------------------------------------------------
KMVERTEXTYPE KVContext::ValidateVertexType()
{
	KMVERTEXTYPE	vtype = KM_VERTEXTYPE_00;

	switch (mVcontext.ParamType)
	{
		//---------------------------------------------------------
		case KM_POLYGON:		if (mVcontext.SelectModifier == KM_NOMODIFIER)
								{	// unmodified (0-8)
									if ((mVcontext.ShadingMode == KM_NOTEXTUREGOURAUD) && !(mVcontext.bUseSpecular) )
									{	// unmod, nontex (0-2); specular requires textured vertex type
										switch (mVcontext.ColorType)
										{
											case KM_PACKEDCOLOR:
												vtype = KM_VERTEXTYPE_00;
												break;
											case KM_FLOATINGCOLOR:
												vtype = KM_VERTEXTYPE_01;
												break;
											case KM_INTENSITY:
												vtype = KM_VERTEXTYPE_02;
												break;
										}
									}
									else
									{	// unmod, textured (3-8)
										switch (mVcontext.ColorType)
										{
											case KM_PACKEDCOLOR:
												vtype = (mVcontext.UVFormat == KM_32BITUV) ? KM_VERTEXTYPE_03 : KM_VERTEXTYPE_04;
												break;
											case KM_FLOATINGCOLOR:
												vtype = (mVcontext.UVFormat == KM_32BITUV) ? KM_VERTEXTYPE_05 : KM_VERTEXTYPE_06;
												break;
											case KM_INTENSITY:
												vtype = (mVcontext.UVFormat == KM_32BITUV) ? KM_VERTEXTYPE_07 : KM_VERTEXTYPE_08;
												break;
										}

									}

								}
								else
								{	// modified (9-14)
									if ((mVcontext.ShadingMode == KM_NOTEXTUREGOURAUD) && !(mVcontext.bUseSpecular) )
									{	// modified, nontex (9-10); specular requires textured vertex type
										vtype = (mVcontext.ColorType != KM_INTENSITY) ? KM_VERTEXTYPE_09 : KM_VERTEXTYPE_10;
									}
									else
									{	// modified, textured (11-14)
										if (mVcontext.ColorType != KM_INTENSITY)
										{	//modified, textured, colored (11-12)
											vtype = (mVcontext.UVFormat == KM_32BITUV) ? KM_VERTEXTYPE_11 : KM_VERTEXTYPE_12;
										}
										else
										{	// modified, textured, intensity (13-14)
											vtype = (mVcontext.UVFormat == KM_32BITUV) ? KM_VERTEXTYPE_13 : KM_VERTEXTYPE_14;
										}
									}
								}
								break;
		//---------------------------------------------------------
		case KM_SPRITE:			vtype = (mVcontext.UVFormat == KM_32BITUV) ? KM_VERTEXTYPE_15 : KM_VERTEXTYPE_16;
								break;
		//---------------------------------------------------------
		case KM_MODIFIERVOLUME:	vtype = KM_VERTEXTYPE_17;
								break;
	}


	// -- Determine Vertex Size --
	if (vtype == KM_VERTEXTYPE_00) mVsize = sizeof (KMVERTEX_00);
	if (vtype == KM_VERTEXTYPE_01) mVsize = sizeof (KMVERTEX_01);
	if (vtype == KM_VERTEXTYPE_02) mVsize = sizeof (KMVERTEX_02);
	if (vtype == KM_VERTEXTYPE_03) mVsize = sizeof (KMVERTEX_03);
	if (vtype == KM_VERTEXTYPE_04) mVsize = sizeof (KMVERTEX_04);
	if (vtype == KM_VERTEXTYPE_05) mVsize = sizeof (KMVERTEX_05);
	if (vtype == KM_VERTEXTYPE_06) mVsize = sizeof (KMVERTEX_06);
	if (vtype == KM_VERTEXTYPE_07) mVsize = sizeof (KMVERTEX_07);
	if (vtype == KM_VERTEXTYPE_08) mVsize = sizeof (KMVERTEX_08);
	if (vtype == KM_VERTEXTYPE_09) mVsize = sizeof (KMVERTEX_09);
	if (vtype == KM_VERTEXTYPE_10) mVsize = sizeof (KMVERTEX_10);
	if (vtype == KM_VERTEXTYPE_11) mVsize = sizeof (KMVERTEX_11);
	if (vtype == KM_VERTEXTYPE_12) mVsize = sizeof (KMVERTEX_12);
	if (vtype == KM_VERTEXTYPE_13) mVsize = sizeof (KMVERTEX_13);
	if (vtype == KM_VERTEXTYPE_14) mVsize = sizeof (KMVERTEX_14);
	if (vtype == KM_VERTEXTYPE_15) mVsize = sizeof (KMVERTEX_15);
	if (vtype == KM_VERTEXTYPE_16) mVsize = sizeof (KMVERTEX_16);
	if (vtype == KM_VERTEXTYPE_17) mVsize = sizeof (KMVERTEX_17);

	// -- it's up to date, now --
	mDirtyVtype = false;

	// -- Save and return the result --
	mVtype = vtype;
	return(vtype);

}
//=============================================================================	END-OF-FILE