/**********************************************************************************************/
/* Name: ColorVtx.C*/
/* Goal : compute screen color for all vertices. These functions write the colors*/
/*		  directly to the device dependent vertices to avoid copies. That's why this*/
/*		  is in the drivers directory.*/
/* Author: Steve McCalla*/
/**********************************************************************************************/
 
 
/**/
/* TODO: Get rid of writes to the packed colors */
/*
Tested with LINT
*/
#include"gli_st.h"
#include "GLI_Defn.h"
#include "light_st.h"
#include "PvObj_st.h"
#include "dllcom.h"
 
#ifdef __cplusplus
extern "C"
{
#endif
    
 
#define GLI_C_fMagicNumber	(16384.0f + 8192.0f) 
 
/**********************************************************************************************/
/* Name: GLI_DRV_vColorVertexIndexedNoAlpha*/
/* Goal : compute screen color for all vertices*/
/* Code: Steve McCalla*/
/* OPTIMIZED : Yes - no more branches*/
/**********************************************************************************************/
void 
GLI_DRV_vColorVertexIndexedNoAlpha( GLI_tdstInternalGlobalValuesFor3dEngine *p_stGP,
									ACP_tdxIndex xNumber,
									ACP_tdxIndex *p_ListOfIndexes)
{
	unsigned long *p_Baseu32Colors;
	GLI_tdScreenVertx	*pScrVertex;
	unsigned long ulAlpha;
	unsigned long ulTmpCol1, ulTmpCol2, ulTmpCol3;
	unsigned long ulRed, ulGreen, ulBlue, ulTmpColNew;
	GEO_tdstColor stComputedColor,*p_SRCColors;
	GEO_tdstColor stTempColor;
 
	ACP_tdxIndex *p_ListOfIndexesLast;
 
	/* use a temp so we don't have to subtract at the end*/
	stTempColor.xR = p_stGP->GLI_stColorsAdd.xR + GLI_C_fMagicNumber;
	stTempColor.xG = p_stGP->GLI_stColorsAdd.xG + GLI_C_fMagicNumber;
	stTempColor.xB = p_stGP->GLI_stColorsAdd.xB + GLI_C_fMagicNumber;
	stTempColor.xA = p_stGP->GLI_stColorsAdd.xA + GLI_C_fMagicNumber;
 
	p_ListOfIndexesLast = p_ListOfIndexes + xNumber;
 
	ulTmpCol1 = (~(*((unsigned long *)&stTempColor.xA) & 0x80000000)) & 0x00000001;
	ulTmpCol2 = (*((unsigned long *)&stTempColor.xA) & 0x0000FF00) ? 0x000000ff: 0x00000000;
	ulTmpCol3 = (*((unsigned long *)&stTempColor.xA) & 0x000000FF);
	ulAlpha = ulTmpCol1 * (ulTmpCol2 | ulTmpCol3);
 
	while (p_ListOfIndexes < p_ListOfIndexesLast)
	{
		p_Baseu32Colors = &(p_stGP->GLI_ScreenPoint  + *p_ListOfIndexes)->ulPackedColor;
		pScrVertex      = GLI_ScreenVertices + *p_ListOfIndexes;
		p_SRCColors     = p_stGP -> GLI_aDEF_stColorsRLID + *(p_ListOfIndexes++);
		stComputedColor.xR = p_SRCColors->xR * p_stGP->GLI_stColorsMul.xR + stTempColor.xR ;
		stComputedColor.xG = p_SRCColors->xG * p_stGP->GLI_stColorsMul.xG + stTempColor.xG ;
		stComputedColor.xB = p_SRCColors->xB * p_stGP->GLI_stColorsMul.xB + stTempColor.xB ;
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xR) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xR) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xR) & 0x000000FF);
		ulRed = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xG) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xG) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xG) & 0x000000FF);
		ulGreen = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xB) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xB) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xB) & 0x000000FF);
		ulBlue = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpColNew = 0x000000ff; /*(ulAlpha<<24) | (ulRed<<16) | (ulGreen<<8) | ulBlue;*/
		PSetRGBA(pScrVertex, ulRed, ulGreen, ulBlue, ulAlpha);
		*p_Baseu32Colors = ulTmpColNew;
	}
}
    
 
/**********************************************************************************************/
/* Name: GLI_DRV_vColorVertexNoAlpha*/
/* Goal : compute screen color for all vertices*/
/* Code: Steve McCalla*/
/* OPTIMIZED : Yes*/
/**********************************************************************************************/
void 
GLI_DRV_vColorVertexNoAlpha(GLI_tdstInternalGlobalValuesFor3dEngine *p_stGP,
						    ACP_tdxIndex xNumber)
{
	GLI_tdstAligned2DVector *p_Baseu32Colors;
	GLI_tdScreenVertx	*pScrVertex;
	GEO_tdstColor stComputedColor,*p_SRCColors;
	GEO_tdstColor stTempColor;
	unsigned long ulTmpCol1, ulTmpCol2, ulTmpCol3;
	unsigned long ulRed, ulGreen, ulBlue, ulTmpColNew;
	unsigned long ulAlpha;
 
 
	stTempColor.xR = p_stGP->GLI_stColorsAdd.xR + GLI_C_fMagicNumber;
	stTempColor.xG = p_stGP->GLI_stColorsAdd.xG + GLI_C_fMagicNumber;
	stTempColor.xB = p_stGP->GLI_stColorsAdd.xB + GLI_C_fMagicNumber;
	stTempColor.xA = p_stGP->GLI_stColorsAdd.xA + GLI_C_fMagicNumber;
 
	p_Baseu32Colors = p_stGP->GLI_ScreenPoint;
	p_SRCColors		= p_stGP -> GLI_aDEF_stColorsRLID;
	pScrVertex		= GLI_ScreenVertices;
 
	ulAlpha = 0;
	ulTmpCol1 = (~(*((unsigned long *)&stTempColor.xA) & 0x80000000)) & 0x00000001;
	ulTmpCol2 = (*((unsigned long *)&stTempColor.xA) & 0x0000FF00) ? 0x000000ff: 0x00000000;
	ulTmpCol3 = (*((unsigned long *)&stTempColor.xA) & 0x000000FF);
	ulAlpha = ulTmpCol1 * (ulTmpCol2 | ulTmpCol3);
 
	while (xNumber--)
	{
		stComputedColor.xR = p_SRCColors->xR * p_stGP->GLI_stColorsMul.xR + stTempColor.xR ;
		stComputedColor.xG = p_SRCColors->xG * p_stGP->GLI_stColorsMul.xG + stTempColor.xG ;
		stComputedColor.xB = p_SRCColors->xB * p_stGP->GLI_stColorsMul.xB + stTempColor.xB ;
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xR) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xR) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xR) & 0x000000FF);
		ulRed = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xG) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xG) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xG) & 0x000000FF);
		ulGreen = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xB) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xB) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xB) & 0x000000FF);
		ulBlue = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpColNew =  0x000000ff; /*(ulAlpha<<24) | (ulRed<<16) | (ulGreen<<8) | ulBlue;*/
 
		p_Baseu32Colors->ulPackedColor = ulTmpColNew;
		PSetRGBA(pScrVertex, ulRed, ulGreen, ulBlue, ulAlpha);
		pScrVertex++;
		p_Baseu32Colors++;
		p_SRCColors++;
	}
}
 
 
/**********************************************************************************************/
/* Name: GLI_DRV_vColorVertexIndexedAlpha*/
/* Goal : compute screen color for all vertex when Light is alpha sensitive*/
/* Code: Steve McCalla*/
/* OPTIMIZED : Yes*/
/**********************************************************************************************/
 
void 
GLI_DRV_vColorVertexIndexedAlpha( GLI_tdstInternalGlobalValuesFor3dEngine *p_stGP,
								  ACP_tdxIndex xNumber,
								  ACP_tdxIndex *p_ListOfIndexes)
{
	GEO_tdstColor stComputedColor;
	GEO_tdstColor stTempColor;
	GLI_tdScreenVertx	*pScrVertex;
	unsigned long *p_Baseu32Colors;
	unsigned long ulTmpCol1, ulTmpCol2, ulTmpCol3;
	unsigned long ulAlpha, ulRed, ulGreen, ulBlue, ulTmpColNew;
 
	stTempColor.xR = p_stGP->GLI_stColorsAdd.xR + GLI_C_fMagicNumber;
	stTempColor.xG = p_stGP->GLI_stColorsAdd.xG + GLI_C_fMagicNumber;
	stTempColor.xB = p_stGP->GLI_stColorsAdd.xB + GLI_C_fMagicNumber;
	stTempColor.xA = p_stGP->GLI_stColorsAdd.xA + GLI_C_fMagicNumber;
     
	while (xNumber--)	{
		p_Baseu32Colors = &(p_stGP->GLI_ScreenPoint + *p_ListOfIndexes)->ulPackedColor;
		pScrVertex      = GLI_ScreenVertices + *p_ListOfIndexes;
 
		stComputedColor.xR = (p_stGP -> GLI_aDEF_stColorsRLID + (*p_ListOfIndexes)) -> xR * p_stGP -> GLI_stColorsMul . xR + stTempColor.xR;
		stComputedColor.xG = (p_stGP -> GLI_aDEF_stColorsRLID + (*p_ListOfIndexes)) -> xG * p_stGP -> GLI_stColorsMul . xG + stTempColor.xG ;
		stComputedColor.xB = (p_stGP -> GLI_aDEF_stColorsRLID + (*p_ListOfIndexes)) -> xB * p_stGP -> GLI_stColorsMul . xB + stTempColor.xB;
		stComputedColor.xA = (p_stGP -> GLI_aDEF_stColorsRLID + (*p_ListOfIndexes)) -> xA * p_stGP -> GLI_stColorsMul . xA + stTempColor.xA ;
    
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xA) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xA) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xA) & 0x000000FF);
		ulAlpha   = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xR) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xR) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xR) & 0x000000FF);
		ulRed     = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xG) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xG) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xG) & 0x000000FF);
		ulGreen   = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xB) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xB) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xB) & 0x000000FF);
		ulBlue    = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpColNew = 0x000000ff; /* (ulAlpha<<24) | (ulRed<<16) | (ulGreen<<8) | ulBlue;*/
		PSetRGBA(pScrVertex, ulRed, ulGreen, ulBlue, ulAlpha);
		*p_Baseu32Colors = ulTmpColNew;
		p_ListOfIndexes++;
	}  
}
 
 
 
/**********************************************************************************************/
/* Name: GLI_DRV_vColorVertexAlpha */
/* Goal : compute screen color for all vertices*/
/* Code: Steve McCalla*/
/* OPTIMIZED : Yes*/
/**********************************************************************************************/
 
void 
GLI_DRV_vColorVertexAlpha ( GLI_tdstInternalGlobalValuesFor3dEngine *p_stGP,
 							ACP_tdxIndex xNumber)
{
	GLI_tdstAligned2DVector *p_Baseu32Colors;
	GLI_tdScreenVertx	*pScrVertex;
	GEO_tdstColor stComputedColor,*p_SRCColors;
	GEO_tdstColor stTempColor;
	unsigned long ulTmpCol1, ulTmpCol2, ulTmpCol3;
	unsigned long ulAlpha, ulRed, ulGreen, ulBlue;
	unsigned long ulTmpColNew;
 
	stTempColor.xR = p_stGP->GLI_stColorsAdd.xR + GLI_C_fMagicNumber;
	stTempColor.xG = p_stGP->GLI_stColorsAdd.xG + GLI_C_fMagicNumber;
	stTempColor.xB = p_stGP->GLI_stColorsAdd.xB + GLI_C_fMagicNumber;
	stTempColor.xA = p_stGP->GLI_stColorsAdd.xA + GLI_C_fMagicNumber;
 
	p_Baseu32Colors = p_stGP->GLI_ScreenPoint;
	p_SRCColors		= p_stGP->GLI_aDEF_stColorsRLID;
	pScrVertex		= GLI_ScreenVertices;
 
	while (xNumber--)
	{
		stComputedColor.xR = p_SRCColors->xR * p_stGP->GLI_stColorsMul.xR + stTempColor.xR;
		stComputedColor.xG = p_SRCColors->xG * p_stGP->GLI_stColorsMul.xG + stTempColor.xG;
		stComputedColor.xB = p_SRCColors->xB * p_stGP->GLI_stColorsMul.xB + stTempColor.xB;
		stComputedColor.xA = p_SRCColors->xA * p_stGP->GLI_stColorsMul.xA + stTempColor.xA;
 
		ulTmpCol1 = (~(*((unsigned long *)&stTempColor.xA) & 0x80000000)) & 0x00000001;
		ulTmpCol2 = (*((unsigned long *)&stTempColor.xA) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stTempColor.xA) & 0x000000FF);
		ulAlpha = ulTmpCol1 * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xR) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xR) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xR) & 0x000000FF);
		ulRed = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xG) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xG) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xG) & 0x000000FF);
		ulGreen = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpCol1 = (*((unsigned long *)&stComputedColor.xB) & 0x00400000);
		ulTmpCol2 = (*((unsigned long *)&stComputedColor.xB) & 0x0000FF00) ? 0x000000ff: 0x00000000;
		ulTmpCol3 = (*((unsigned long *)&stComputedColor.xB) & 0x000000FF);
		ulBlue = (ulTmpCol1 >> 22) * (ulTmpCol2 | ulTmpCol3);
 
		ulTmpColNew =  (ulAlpha<<24) | (ulRed<<16) | (ulGreen<<8) | ulBlue;
 
		p_Baseu32Colors->ulPackedColor = 0x000000ff; /* ulTmpColNew;*/
		PSetRGBA(pScrVertex, ulRed, ulGreen, ulBlue, ulAlpha);
		pScrVertex++;
		p_Baseu32Colors++;
		p_SRCColors++;
	}
}
 
 
 
 
#ifdef __cplusplus
}/* extern "C" */
#endif
