/*
Tested with LINT
*/
 
#include "Gli_st.h"
#include "GLI_Defn.h"
#include "light_st.h"
#include "PvObj_st.h"
#include "lines.h"
#include "proj.h"
#include "linear.h"
#include "matstack.h"
#include "vpt3D.h"
#include "camera.h"
#include "DLLInter.h"
#include "Object.h"

#include "acp_driver.h"

extern GLI_tdstInternalGlobalValuesFor3dEngine *GLI_BIG_GLOBALS;
 
#ifdef __cplusplus
extern "C"
{
#endif
  
/**********************************************************************************************/
/* Name: GLI_xDraw3DLine16 1.0*/
/* Goal: Draw 3d line unclipped and unprojected in 16b*/
/* Code: Philippe Vimont*/
/**********************************************************************************************/
/*void GLI_vSetZClip(float ZClip,GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT);*/
/*void GLI_NJA_vSendSingleLineToClip( GLD_tdstViewportAttributes *p_stVpt , GLI_tdstAligned3DVector *p_stVertex1 , GLI_tdstAligned2DVector *p_st2DVertex1 , GLI_tdstAligned3DVector *p_stVertex2 , GLI_tdstAligned2DVector *p_st2DVertex2 , GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT, long lDrawModeMask, GEO_tdstColor *p_stColor );*/
 
void GLI_xDraw3DLine16 ( GLD_tdstViewportAttributes *p_stVpt, MTH3D_tdstVector *p_stFirstPoint , MTH3D_tdstVector *p_stLastPoint , long lColor)
{
#ifdef _DEBUG 
	GLI_tdstAligned2DVector				a2_st2DVertex[4];
	GLI_tdstSpecificAttributesFor3D		*p_st3DAtributes;
	GLI_tdstAligned3DVector				a2_stVertex[4];
	GEO_tdstColor						stColor;
 
	p_st3DAtributes = (GLI_tdstSpecificAttributesFor3D*) (p_stVpt->p_vSpecificToXD);
  
 
	GLI_DRV_vSetZClip( (float)p_st3DAtributes -> p_stCam -> xNear, GLI_BIG_GLOBALS);
 

	/*turn points*/
	GLI_xSerialLinearOp(1,p_stFirstPoint,&a2_stVertex[0],g_p_stCurrentMatrix);
	GLI_xSerialLinearOp(1,p_stLastPoint,&a2_stVertex[1],g_p_stCurrentMatrix);
  
	/* project points       */
	GLI_xSerialProjection(p_st3DAtributes->p_stCam,2,a2_stVertex,a2_st2DVertex);
#ifdef USE_DIRECTX
	a2_st2DVertex[0].ulPackedColor = a2_st2DVertex[1].ulPackedColor = (0xFF000000 | lColor) ;
#else
	a2_st2DVertex[0].ulPackedColor = a2_st2DVertex[1].ulPackedColor = lColor;
#endif
	stColor.xR = (float)((lColor & 0x00FF0000)>>16) * 1.0f;
	stColor.xG = (float)((lColor & 0x0000FF00)>>8) * 1.0f;
	stColor.xB = (float)((lColor & 0x000000FF)>>0) * 1.0f;

    GLI_BIG_GLOBALS->lCurrentDrawMask = 0xFFFFFFFF;
    GLI_BIG_GLOBALS->lHierachDrawMask = 0xFFFFFFFF;
    GLI_BIG_GLOBALS->hCurrentMaterial = NULL;
    GLI_BIG_GLOBALS->gs_st_CurrentMatrix = g_p_stCurrentMatrix;
    GLI_BIG_GLOBALS->gs_st_CameraMatrix  = &p_st3DAtributes->p_stCam->stMatrix;
    GLI_BIG_GLOBALS->p_stCurrentCamera   = p_st3DAtributes->p_stCam;
    GLI_DRV_vSetZClip( GLI_BIG_GLOBALS->p_stCurrentCamera->xNear, GLI_BIG_GLOBALS);

#ifdef TEST
	{
		static float x0=0;
		static float y0=0;
		static float z0=0.2;

		static float x1=220;
		static float y1=333;
		static float z1=0.2;

		a2_st2DVertex[0].xX = x0;
		a2_st2DVertex[0].xY = y0;
		//a2_stVertex[0].xZ = z0;
		a2_st2DVertex[0].xOoZ = z0;

		a2_st2DVertex[1].xX = x1;
		a2_st2DVertex[1].xY = y1;
		//a2_stVertex[1].xZ = z1;
		a2_st2DVertex[1].xOoZ = z1;
	}
#endif

	GLI_DRV_vSendSingleLineToClip 
		(
			p_stVpt ,
			&a2_stVertex[0],
			&a2_st2DVertex[0],
			&a2_stVertex[1],
			&a2_st2DVertex[1],
			GLI_BIG_GLOBALS,
			C_lGouraudLineElement,& stColor
		);
#endif
}

void GLI_fnv_Draw2DLine( GLD_tdstViewportAttributes *p_stVpt, MTH3D_tdstVector *p_stFirstPoint, MTH3D_tdstVector *p_stLastPoint, long lColor )
{
    GEO_tdstColor						stColor;
    GLI_tdstAligned3DVector				a2_stVertex[2];
    GLI_tdstAligned2DVector				a2_st2DVertex[2];

    GLI_BIG_GLOBALS->hCurrentMaterial = NULL;
    GLI_BIG_GLOBALS->lCurrentDrawMask = 0xFFFFFFFF;
    GLI_BIG_GLOBALS->lHierachDrawMask = 0xFFFFFFFF;
    GLI_vDoMaterialSelection( GLI_BIG_GLOBALS );
        
    a2_st2DVertex[0].ulPackedColor = a2_st2DVertex[1].ulPackedColor = lColor;
 
	stColor.xR = (float)((lColor & 0x00FF0000)>>16) * 1.0f;
	stColor.xG = (float)((lColor & 0x0000FF00)>>8) * 1.0f;
	stColor.xB = (float)((lColor & 0x000000FF)>>0) * 1.0f;

    
    a2_st2DVertex[0].xX = p_stFirstPoint->xX * p_stVpt->dwWidth;
    a2_st2DVertex[0].xY = p_stFirstPoint->xY * p_stVpt->dwHeight;
    a2_stVertex[0].xZ = a2_st2DVertex[0].xOoZ = 1000000.0f;
    a2_st2DVertex[1].xX = p_stLastPoint->xX * p_stVpt->dwWidth;
    a2_st2DVertex[1].xY = p_stLastPoint->xY * p_stVpt->dwHeight;
    a2_stVertex[1].xZ = a2_st2DVertex[1].xOoZ = 1000000.0f;
     
	GLI_DRV_vSendSingleLineToClip 
		(
			p_stVpt ,
			&a2_stVertex[0],
			&a2_st2DVertex[0],
			&a2_stVertex[1],
			&a2_st2DVertex[1],
			GLI_BIG_GLOBALS,
			C_lGouraudLineElement, 
            &stColor
		);
}

void GLI_fnv_DrawRectangle( GLD_tdstViewportAttributes *p_stVpt, MTH3D_tdstVector *p_stFirstPoint, MTH3D_tdstVector *p_stLastPoint, long lColor )
{
    MTH3D_tdstVector stIntermediate;

    stIntermediate.xX = p_stFirstPoint->xX;
    stIntermediate.xY = p_stLastPoint->xY;

    GLI_fnv_Draw2DLine( p_stVpt, p_stFirstPoint, &stIntermediate, lColor );
    GLI_fnv_Draw2DLine( p_stVpt, &stIntermediate, p_stLastPoint, lColor );

    stIntermediate.xX = p_stLastPoint->xX;
    stIntermediate.xY = p_stFirstPoint->xY;

    GLI_fnv_Draw2DLine( p_stVpt, p_stFirstPoint, &stIntermediate, lColor );
    GLI_fnv_Draw2DLine( p_stVpt, &stIntermediate, p_stLastPoint, lColor );
}
  
  
#ifdef __cplusplus
} /* extern "C" */
#endif
