/******************************************************************************************
FILE :                            CLIPDomat.C
 
  VERS :  1.00 \ Marc Villemain - version directX5
  0.00 \ Philippe Vimont 
  
    DATE :  15.05.97
    Select materials
    
*******************************************************************************************/
#include "Gli_st.h"
#include "GLI_Defn.h"
#include "polygon.h"
#include "mater_st.h"
#include "light_st.h"
#include "PvObj_st.h"
#include "ScrVert.h"
#include "Liste.h"
#include "SelecTex.h"
#include "watrplan.h"
#include "proj.h"
#include "vpt3D.h"
#include "camera.h"
#include "material.h"
#include "texture.h"
#include "GliBench.h
#include <kamui.h>
#include "GLI_interface.h"
 
#ifdef __cplusplus
extern "C" 
{
#endif
#ifdef ZARMA
}
#endif
extern  KMVERTEXBUFFDESC	VertexBufferDesc;
/*extern long GLD_RenderingCounter;*/
 
extern KMVERTEX_03 CurrentDestXYZ[4];
extern long TRYIT;  
 
extern GLI_tdxValue xZFogDepth;
/*extern GEO_tdstColor stColorDepthCuing;*/
/*extern float xZDepthStart,xZDepthEnd,xZDepthConstant;*/
/*extern float xFogIntensityStart;*/
/*extern float xFogIntensityEnd;*/
/*extern GLI_tdxValue xDepthStartSave,xZDepthEndSave;*/
extern KMSURFACEDESC stSpecialTexture;
extern KMSURFACEDESC stSpecialTexture20;
extern KMSURFACEDESC stSpecialTexture21;
extern KMVERTEXCONTEXT		stRenderingContex;
void GLI_fnSwitchTwiddled(short *p_Texture , long size );
 
short VignetteBuffer[1024L * 1024L];
/*static long bisready = 0;*/
/*void ZZZGLI_ZOOOOOOOOOOOORRRRRRGGGLL();*/
#define GLI_M_ChangeRenderingParrameter( Renderingflag, Renderingvar, Renderingval) \
{\
    if (stRenderingContex . Renderingvar != (Renderingval))\
{\
    stRenderingContex . RenderState |= (Renderingflag);\
    stRenderingContex . Renderingvar = (Renderingval);\
}\
}
 
 
void GLI_BigInitKamuiRendering(GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT)
{
    memset( &stRenderingContex, 0, sizeof(stRenderingContex));	/* Crear Context*/
    stRenderingContex . RenderState	= KM_DEPTHMODE | KM_CULLINGMODE	| KM_SCREENCOORDINATION	| KM_SHADINGMODE | KM_MODIFIER 			| KM_ZWRITEDISABLE	| 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 					| KM_COLORTYPE			| KM_USESPECULAR| KM_UVFORMAT;
    stRenderingContex . ParamType			= KM_POLYGON;
    stRenderingContex . ListType			= KM_OPAQUE_POLYGON;
    stRenderingContex . ColorType			= KM_PACKEDCOLOR;/*KM_FLOATINGCOLOR;*/
    stRenderingContex . bUseSpecular		= FALSE;/*TRUE;*/
    stRenderingContex . UVFormat			= KM_32BITUV;
    stRenderingContex . DepthMode			= KM_GREATER;
    stRenderingContex . CullingMode		= KM_NOCULLING;
    stRenderingContex . ScreenCoordination	= KM_SCREEN;/*KM_PROJECTIVE;//KM_SCREEN;*/
    stRenderingContex . ShadingMode		= KM_NOTEXTUREGOURAUD;
    stRenderingContex . SelectModifier		= KM_NOMODIFIER;
    stRenderingContex . bZWriteDisable		= FALSE;			
    stRenderingContex . SRCBlendingMode	= KM_ONE;
    stRenderingContex . DSTBlendingMode	= KM_ZERO;
    stRenderingContex . bSRCSel			= FALSE;			
    stRenderingContex . bDSTSel			= FALSE;			
    stRenderingContex . FogMode			= KM_NOFOG;			
    stRenderingContex . FogMode			= KM_FOGTABLE;
    stRenderingContex . bUseSpecular		= FALSE;
    stRenderingContex . bUseAlpha			= FALSE;
    stRenderingContex . FlipUV				= KM_NOFLIP;
    stRenderingContex . ClampUV			= KM_CLAMP_UV;/*KM_NOCLAMP;*/
    stRenderingContex . FilterMode			= KM_BILINEAR;/*KM_POINT_SAMPLE;*/
    stRenderingContex . bSuperSample		= FALSE;
    stRenderingContex . MipMapAdjust		= 0x00000003;
    stRenderingContex . TextureShadingMode	= KM_MODULATE_ALPHA;
    stRenderingContex . bColorClamp		= FALSE;
    stRenderingContex . PaletteBank		= 0;
    stRenderingContex . fFaceColorAlpha	= 1.0f;
    stRenderingContex . fFaceColorRed		= 1.0f;
    stRenderingContex . fFaceColorGreen	= 1.0f;
    stRenderingContex . fFaceColorBlue		= 1.0f;
    stRenderingContex . fOffsetColorAlpha	= 1.0f;
    stRenderingContex . fOffsetColorRed	= 1.0f;
    stRenderingContex . fOffsetColorGreen	= 1.0f;
    stRenderingContex . fOffsetColorBlue	= 1.0f;
    kmProcessVertexRenderState(&stRenderingContex);
}
 
void GLI_INTERFACE_FUNC(GLI_vDoTextureSelection)(GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT)
{
    static bFirst = 0;
    
    if (*GLD_p_lRenderingCounter != p_stGlobaleMT->GLD_RenderingCounter)
    {
        p_stGlobaleMT->GLD_RenderingCounter = *GLD_p_lRenderingCounter;
    }
    if (!bFirst)
    {
        bFirst = 1;
        GLI_BigInitKamuiRendering(p_stGlobaleMT);
    }
    stRenderingContex . RenderState	= 0;
    
    if (GLI_BIG_GLOBALS->xFogIsOn)
    {
        if ( GLI_BIG_GLOBALS->p_stLastComputedFog != GLI_BIG_GLOBALS->p_stActiveFog )
        {
            GLI_vSetFogParams( GLI_BIG_GLOBALS->p_stActiveFog );
            GLI_BIG_GLOBALS->p_stLastComputedFog = GLI_BIG_GLOBALS->p_stActiveFog;
        }
 
        GLI_M_ChangeRenderingParrameter(KM_FOGMODE	, FogMode , KM_FOGTABLE);
    }
    else
    {
        GLI_M_ChangeRenderingParrameter(KM_FOGMODE	, FogMode , KM_NOFOG);
    }
    
    
    if (p_stGlobaleMT -> lCurrentDrawMask & GLI_C_lIsTestingBackface)
    {
        if (p_stGlobaleMT -> lCurrentDrawMask & GLI_C_lNotInvertBackfaces)
        {
            GLI_M_ChangeRenderingParrameter(KM_CULLINGMODE , CullingMode ,KM_CULLCW);
        }
        else
        {
            GLI_M_ChangeRenderingParrameter(KM_CULLINGMODE , CullingMode ,KM_CULLCCW);
        }
    }
    else
        GLI_M_ChangeRenderingParrameter(KM_CULLINGMODE , CullingMode ,KM_NOCULLING);
    
    if (GLI_BIG_GLOBALS->lAMirrorIsDetected & 2)
    {
        GLI_M_ChangeRenderingParrameter(KM_FILTERMODE , FilterMode , KM_POINT_SAMPLE);
        GLI_M_ChangeRenderingParrameter(KM_SHADINGMODE , ShadingMode ,KM_TEXTUREGOURAUD);
        if ((!(p_stGlobaleMT -> lCurrentDrawMask & GLI_C_lIsNotGrided)) && (p_stGlobaleMT->xGlobalAlpha < 0.98f))
        {
            GLI_M_ChangeRenderingParrameter(KM_LISTTYPE , ListType ,KM_TRANS_POLYGON);
            GLI_M_ChangeRenderingParrameter(KM_SRCBLENDINGMODE , SRCBlendingMode ,KM_SRCALPHA);
            GLI_M_ChangeRenderingParrameter(KM_DSTBLENDINGMODE , DSTBlendingMode ,KM_INVSRCALPHA);
            GLI_M_ChangeRenderingParrameter(KM_USEALPHA , bUseAlpha , TRUE);
            GLI_M_ChangeRenderingParrameter(KM_SRCBLENDINGMODE , SRCBlendingMode ,KM_ONE);
            GLI_M_ChangeRenderingParrameter(KM_DSTBLENDINGMODE , DSTBlendingMode ,KM_ONE);
            GLI_M_ChangeRenderingParrameter(KM_USEALPHA , bUseAlpha , FALSE);
        } else {
            GLI_M_ChangeRenderingParrameter(KM_LISTTYPE , ListType ,KM_OPAQUE_POLYGON);
            GLI_M_ChangeRenderingParrameter(KM_SRCBLENDINGMODE , SRCBlendingMode ,KM_ONE);
            GLI_M_ChangeRenderingParrameter(KM_DSTBLENDINGMODE , DSTBlendingMode ,KM_ZERO);
            GLI_M_ChangeRenderingParrameter(KM_USEALPHA , bUseAlpha , FALSE);
        }
        stRenderingContex . pTextureSurfaceDesc = &stSpecialTexture;
    }
    else
    {
        if ((p_stGlobaleMT -> p_stCurrentTexture != NULL) && (p_stGlobaleMT -> p_stCurrentTexture ->  bIsAvailable))
        {
            GLI_M_ChangeRenderingParrameter(KM_FILTERMODE , FilterMode , KM_BILINEAR);
            GLI_M_ChangeRenderingParrameter(KM_SHADINGMODE , ShadingMode ,KM_TEXTUREGOURAUD);
            if ((p_stGlobaleMT -> p_stCurrentTexture -> lTextureCaps & (GLI_C_lAlphaTexture | GLI_C_lNZTexture | GLI_C_lAddTransparencyTexture) )
                || ((!(p_stGlobaleMT -> lCurrentDrawMask & GLI_C_lIsNotGrided)) && (p_stGlobaleMT->xGlobalAlpha < 0.98f)))
            {
                GLI_M_ChangeRenderingParrameter(KM_LISTTYPE , ListType ,KM_TRANS_POLYGON);
                if (p_stGlobaleMT -> p_stCurrentTexture -> lTextureCaps & GLI_C_lAddTransparencyTexture)
                {
                    GLI_M_ChangeRenderingParrameter(KM_SRCBLENDINGMODE , SRCBlendingMode ,KM_ONE);
                    GLI_M_ChangeRenderingParrameter(KM_DSTBLENDINGMODE , DSTBlendingMode ,KM_ONE);
                    GLI_M_ChangeRenderingParrameter(KM_USEALPHA , bUseAlpha , FALSE);
                } else {
                    GLI_M_ChangeRenderingParrameter(KM_SRCBLENDINGMODE , SRCBlendingMode ,KM_SRCALPHA);
                    GLI_M_ChangeRenderingParrameter(KM_DSTBLENDINGMODE , DSTBlendingMode ,KM_INVSRCALPHA);
                    GLI_M_ChangeRenderingParrameter(KM_USEALPHA , bUseAlpha , TRUE);
                } 
            } else
            {
                GLI_M_ChangeRenderingParrameter(KM_LISTTYPE , ListType ,KM_OPAQUE_POLYGON);
                GLI_M_ChangeRenderingParrameter(KM_SRCBLENDINGMODE , SRCBlendingMode ,KM_ONE);
                GLI_M_ChangeRenderingParrameter(KM_DSTBLENDINGMODE , DSTBlendingMode ,KM_ZERO);
                GLI_M_ChangeRenderingParrameter(KM_USEALPHA , bUseAlpha , FALSE);
            }
            
            switch (p_stGlobaleMT -> p_stCurrentTexture -> ucCylingMode)
            {
            case 0:
                GLI_M_ChangeRenderingParrameter(KM_CLAMPUV , ClampUV ,KM_CLAMP_UV);
                break;
            case GLI_C_lCylingUV:
                GLI_M_ChangeRenderingParrameter(KM_CLAMPUV , ClampUV ,KM_NOCLAMP);
                break;
            case GLI_C_lCylingU:
                GLI_M_ChangeRenderingParrameter(KM_CLAMPUV , ClampUV ,KM_CLAMP_U);
                break;
            case GLI_C_lCylingV:
                GLI_M_ChangeRenderingParrameter(KM_CLAMPUV , ClampUV ,KM_CLAMP_V);
                break;
            }
            /* STRIPS BUG*/
            /*                GLI_M_ChangeRenderingParrameter(KM_CLAMPUV , ClampUV ,KM_NOCLAMP);*/
            stRenderingContex . pTextureSurfaceDesc = p_stGlobaleMT -> p_stCurrentTexture -> p_stSpecParam;
        } else
        {
            GLI_M_ChangeRenderingParrameter(KM_SHADINGMODE , ShadingMode ,KM_NOTEXTUREGOURAUD);
            GLI_M_ChangeRenderingParrameter(KM_LISTTYPE , ListType ,KM_TRANS_POLYGON);
            GLI_M_ChangeRenderingParrameter(KM_SRCBLENDINGMODE , SRCBlendingMode ,KM_SRCALPHA);
            GLI_M_ChangeRenderingParrameter(KM_DSTBLENDINGMODE , DSTBlendingMode ,KM_INVSRCALPHA);
            GLI_M_ChangeRenderingParrameter(KM_USEALPHA , bUseAlpha , TRUE);
            p_stGlobaleMT-> p_stCurrentTexture = NULL;
        }
    }
    kmProcessVertexRenderState(&stRenderingContex);
    kmSetVertexRenderState(&stRenderingContex);
    kmStartVertexStrip/*Direct*/(&VertexBufferDesc);
}
 
 
 
extern GLI_tdstInternalGlobalValuesFor3dEngine *GLI_BIG_GLOBALS;
extern  KMVERTEXBUFFDESC	VertexBufferDesc;
void  GLI_vWaitEndOfRendering();
void GLI_vWaitEndOfVertexTransfert();
void GLI_vWaitEndOfVSync();
void GLI_DRV_vWrite16bBitmapToBackBuffer( void *_p_vSourceBuffer, long _lWidth, long _lHeight, long _lDestLeft, long _lDestTop, long _lDestRight, long _lDestBottom )
{
    KMVERTEX_03 gld_CurrentDestXYZ[4];
    GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT;
    static bFirst = 0;
    
    p_stGlobaleMT = GLI_BIG_GLOBALS;
    
    if (!bFirst)
    {
        GLI_BigInitKamuiRendering(p_stGlobaleMT);
    } 
    bFirst++;
    
    if ((bFirst & 0x1F) ) /* == QUADRUPLE SPEED!!*/
    {
    /*        GLI_vWaitEndOfVSync();
        GLI_vWaitEndOfRendering();*/
        GLD_MDRV_vBlitStretched16b(  512,512,VignetteBuffer  ,512,_lWidth,_lHeight,_p_vSourceBuffer);
        kmLoadTexture(&stSpecialTexture,(void *)VignetteBuffer,FALSE,FALSE);
    }/*  */
    /*    GLI_vWaitEndOfVSync();*/
    
    /*        return;*/
 
    /* ------------------------       BEURK      ---------------------------------*/
    stRenderingContex . RenderState	= 0;
    GLI_M_ChangeRenderingParrameter(KM_SHADINGMODE , ShadingMode ,KM_TEXTUREGOURAUD);
    GLI_M_ChangeRenderingParrameter(KM_LISTTYPE , ListType ,KM_OPAQUE_POLYGON);
    GLI_M_ChangeRenderingParrameter(KM_SRCBLENDINGMODE , SRCBlendingMode ,KM_ONE);
    GLI_M_ChangeRenderingParrameter(KM_DSTBLENDINGMODE , DSTBlendingMode ,KM_ZERO);
    GLI_M_ChangeRenderingParrameter(KM_USEALPHA , bUseAlpha , FALSE);
    GLI_M_ChangeRenderingParrameter(KM_CLAMPUV , ClampUV ,KM_CLAMP_UV);
    GLI_M_ChangeRenderingParrameter(KM_CLAMPUV , ClampUV ,KM_CLAMP_UV);
    GLI_M_ChangeRenderingParrameter(KM_FILTERMODE , FilterMode , KM_POINT_SAMPLE);
    GLI_M_ChangeRenderingParrameter(KM_FOGMODE	, FogMode , KM_NOFOG);
    stRenderingContex . pTextureSurfaceDesc = &stSpecialTexture;/*  */
    kmProcessVertexRenderState(&stRenderingContex);
    kmSetVertexRenderState(&stRenderingContex);
    kmStartVertexStrip/*Direct*/(&VertexBufferDesc);
    /* ------------------------  End       BEURK      ------------------------------*/
    gld_CurrentDestXYZ[0] . fX = 0.0f;
    gld_CurrentDestXYZ[0] . fY = 0.0f;
    gld_CurrentDestXYZ[1] . fX = 0.0f;
    gld_CurrentDestXYZ[1] . fY = 480.0f;
    gld_CurrentDestXYZ[2] . fX = 640.0f;
    gld_CurrentDestXYZ[2] . fY = 480.0f;
    gld_CurrentDestXYZ[3] . fX = 640.0f;
    gld_CurrentDestXYZ[3] . fY = 0.0f;
    gld_CurrentDestXYZ[0] . fU = 0.0f;
    gld_CurrentDestXYZ[0] . fV = 0.0f;
    gld_CurrentDestXYZ[1] . fU = 0.0f;
    gld_CurrentDestXYZ[1] . fV = 1.0f;
    gld_CurrentDestXYZ[2] . fU = 1.0f;
    gld_CurrentDestXYZ[2] . fV = 1.0f;
    gld_CurrentDestXYZ[3] . fU = 1.0f;
    gld_CurrentDestXYZ[3] . fV = 0.0f;
    *(unsigned long *)&gld_CurrentDestXYZ[0].uBaseRGB =
        *(unsigned long *)&gld_CurrentDestXYZ[1].uBaseRGB =
        *(unsigned long *)&gld_CurrentDestXYZ[2].uBaseRGB =
        *(unsigned long *)&gld_CurrentDestXYZ[3].uBaseRGB = 0xffffffff;
    gld_CurrentDestXYZ[0] . fZ = 
        gld_CurrentDestXYZ[1] . fZ = 
        gld_CurrentDestXYZ[2] . fZ = 
        gld_CurrentDestXYZ[3] . fZ =1.0f;
    gld_CurrentDestXYZ[0].ParamControlWord	= 
        gld_CurrentDestXYZ[1].ParamControlWord	= 
        gld_CurrentDestXYZ[3].ParamControlWord	= 
        KM_VERTEXPARAM_NORMAL;
    gld_CurrentDestXYZ[2].ParamControlWord	= KM_VERTEXPARAM_ENDOFSTRIP;
    kmSetVertex(&VertexBufferDesc,&gld_CurrentDestXYZ[0],KM_VERTEXTYPE_03,sizeof(gld_CurrentDestXYZ[1]));
    kmSetVertex(&VertexBufferDesc,&gld_CurrentDestXYZ[1],KM_VERTEXTYPE_03,sizeof(gld_CurrentDestXYZ[1]));
    kmSetVertex(&VertexBufferDesc,&gld_CurrentDestXYZ[3],KM_VERTEXTYPE_03,sizeof(gld_CurrentDestXYZ[1]));
    kmSetVertex(&VertexBufferDesc,&gld_CurrentDestXYZ[2],KM_VERTEXTYPE_03,sizeof(gld_CurrentDestXYZ[1]));
}
 
 
float GLI_km_IndexTableToOoZ(long index)
{
    return (float)( pow ( 2.0f , (index >> 4)) * (float)((index & 0x0f) + 16) / 16.0f);
}
 
void GLI_INTERFACE_FUNC(GLI_vSetFogParams) ( tdstFogParams *_p_stFogParams )
{
	long         i;
    float        km_FogTable[256], RealZ , fCoef;
    float        fStart,  fEnd ;
    KMPACKEDARGB kmColor;
    GLI_tdxValue xBlendStart, xBlendEnd;
 
    
    kmSetFogDensity(0xFF08);
	
/*    xZDepthEndSave  = _p_stFogParams->xDepthEnd;*/
	xDepthStartSave = _p_stFogParams->xDepthStart;
    xBlendStart     = _p_stFogParams->xBlendStart / 255.0f;
    xBlendEnd       = _p_stFogParams->xBlendEnd / 255.0f;
 
    fStart          = _p_stFogParams->xDepthStart;
    fEnd            = _p_stFogParams->xDepthEnd;
    fCoef           = 1.0f / ( fEnd - fStart );
    
    for (i = 0 ; i < 256 ; i++)
    {
        RealZ = GLI_km_IndexTableToOoZ(i) / 512.0f;
        RealZ = GLI_C_xZClippingNear / RealZ; 
        /* realZ == Now Real Depth, not 1/W !!!*/
        /* les salaud!!!*/
        if (RealZ < fStart)
            km_FogTable[i] = xBlendStart;
        else
        {
            if (RealZ > fEnd)
                km_FogTable[i] = xBlendEnd;
            else
            {
                km_FogTable[i] = ( RealZ - fStart ) * fCoef;
                km_FogTable[i] *= xBlendEnd - xBlendStart;
                km_FogTable[i] += xBlendStart;
            }
        }
    }
    kmSetFogTable((PKMFLOAT)km_FogTable);
    *(long *)&kmColor = RGB_MAKE((long)( _p_stFogParams->stColor.xR * 255.f),(long)(_p_stFogParams->stColor.xG * 255.f),(long)(_p_stFogParams->stColor.xB * 255.f));
    kmSetFogTableColor(kmColor);
}
/*EVL*/
 
#ifdef __cplusplus
} /*extern "C"*/
#endif
