/******************************************************************************************
FILE :                            CLIPDomat.C

  VERS :  1.00 \ Marc Villemain - version directX5
  0.00 \ Philippe Vimont 
  
    DATE :  15.05.97
    Select materials
    
*******************************************************************************************/
#include <SGL2.h>
#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 "GLI_interface.h"
#include "GliBench.h"

extern SGLCONTEXT SGLContext;
extern SGLCONTEXT2 SGLContext2;
extern SGLVERTEX aDEFCurrentDestXYZ[];
extern SGLVERTEX *CurrentDestXYZ;
extern sgl_intermediate_map SGL2_IM_sPecialtexture;

#ifdef __cplusplus
extern "C" 
{
#endif
#ifdef ZARMA
}
#endif
//extern long GLD_RenderingCounter;

extern int stSpecialTexture;

//********************************************************************************************
// Name: GLI_vDoTextureSelection
// Goal: 
// Code: Philippe Vimont 
// OPTIMMIZED : No
//********************************************************************************************
//KMSURFACEDESC stSpecialTexture;
short VignetteBuffer[1024L * 1024L];

#define SGL2_ChangeRenderingParrametter(a,b,c)\
{\
    if (SGLContext.a != b)\
    {\
        SGLContext.a = b;\
        SGLContext.u32Flags |= c;\
    }\
}\

//    if (SGLContext2.a != b)
#define SGL2_ChangeRenderingParrametterC2(a,b,c)\
{\
    {\
        SGLContext.u32Flags |= SGLTT_DIRECT_EXTENSIONS;\
        SGLContext2.a = b;\
        SGLContext2.u32Flags |= c;\
    }\
}\


void GLI_BigInitSGL2Rendering(GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT)
{
	SGLContext2.u32Flags			= 0;
	SGLContext2.n32DeviceName		= 0;
	SGLContext2.u32BlendCntl		= 0;
	SGLContext2.eDepthCntl			= sgl_zcomp_lessequal;
	SGLContext2.fShadowScaling		= 0.0f;
	SGLContext2.fVertexFogR			= 1.0f;
	SGLContext2.fVertexFogG			= 1.0f;
	SGLContext2.fVertexFogB			= 1.0f;
	SGLContext.pSGLContext2			= &SGLContext2;
    SGLContext.bFogOn               = FALSE;
	SGLContext.bCullBackfacing      = FALSE;
    SGLContext.bDoClipping          = TRUE;
    SGLContext.cBackgroundColour[0] = 0.0f;
	SGLContext.cBackgroundColour[1] = 0.0f;
	SGLContext.cBackgroundColour[2] = 0.0f;
	SGLContext.eShadowLightVolMode  = NO_SHADOWS_OR_LIGHTVOLS;
	SGLContext.bFlipU               = FALSE;
	SGLContext.bFlipV               = FALSE;
    SGLContext.bDoUVTimesInvW       = TRUE;
	SGLContext.RenderRegions        = 0;
    SGLContext.n32MipmapOffset      = -1;
    SGLContext.u32GlobalTrans       = 0x000000;
    SGLContext.eFilterType          = sgl_tf_bilinear;
    SGLContext.u32Flags =  SGLTT_TEXTURE | SGLTT_GOURAUD | SGLTT_DIRECT_EXTENSIONS;
    SGL2_ChangeRenderingParrametterC2(eTextureBlend,sgl_stb_texascale,SGLTT2_TEXTUREBLEND);
}

void GLI_INTERFACE_FUNC(GLI_vDoTextureSelection)(GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT)
{
    static bFirst = 1;
    
    if (*GLD_p_lRenderingCounter != p_stGlobaleMT->GLD_RenderingCounter)
    {
        p_stGlobaleMT->GLD_RenderingCounter = *GLD_p_lRenderingCounter;
    }
    if (bFirst)
    {
        GLI_BigInitSGL2Rendering(p_stGlobaleMT);
    }
    bFirst = 0;
    SGLContext.u32Flags =  SGLTT_DIRECT_EXTENSIONS;
    SGLContext2.u32Flags =  0;
    
    if (GLI_BIG_GLOBALS->xFogIsOn)
    {
        // FOG ON
        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;
        }
        SGLContext.bFogOn               = TRUE;
    }
    else
    {
        // FOG OFF
        SGLContext.bFogOn               = FALSE;
    }


    if (p_stGlobaleMT -> lCurrentDrawMask & GLI_C_lIsTestingBackface)
    {
        if (p_stGlobaleMT -> lCurrentDrawMask & GLI_C_lNotInvertBackfaces)
        {
            // Backface Clock wize
        }
        else
        {
            // Backface CCW
        }
    }
    else
    {
        //no backface
    }

/*  if (GLI_BIG_GLOBALS->lAMirrorIsDetected & 2)
    {
        // Use computed texture.
    }
    else*/
    {
  if ((p_stGlobaleMT -> p_stCurrentTexture != NULL) && (p_stGlobaleMT -> p_stCurrentTexture ->  bIsAvailable))
        {
            // texture gouraud Rendering ww
            SGLContext.u32Flags |=  SGLTT_TEXTURE | SGLTT_GOURAUD | SGLTT_MIPMAPOFFSET;
            SGLContext2.u32Flags =  0;
            SGLContext.nTextureName = *(int *)&p_stGlobaleMT -> p_stCurrentTexture -> p_stSpecParam;
            if ((!(p_stGlobaleMT->lAMirrorIsDetected & 2)) &&
                ((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))))
            {
                // Transparent polygone
                if (p_stGlobaleMT -> p_stCurrentTexture -> lTextureCaps & GLI_C_lAddTransparencyTexture)
                {
                    // Blendig ADD
//                    SGL2_ChangeRenderingParrametterC2(eTextureBlend,sgl_stb_texascale,SGLTT2_TEXTUREBLEND);
                    SGL2_ChangeRenderingParrametterC2(
                        u32BlendCntl,
                        sgl_blend_cntl(sgl_blend_one,sgl_blend_one,sgl_accum2_notused),
                        SGLTT2_BLENDCNTL);
//                    SGLContext2.u32Flags		= SGLTT2_BLENDCNTL | SGLTT2_TEXTUREBLEND;
                } else {
                    // Blending texture alpha
                    SGL2_ChangeRenderingParrametterC2(
                        u32BlendCntl,
                        sgl_blend_cntl(sgl_blend_srca,sgl_blend_invsrca,sgl_accum2_notused),
                        SGLTT2_BLENDCNTL);
                } 
            } else
            {
                // No blending
                  SGL2_ChangeRenderingParrametterC2(
                        u32BlendCntl,
                        sgl_blend_cntl(sgl_blend_one,sgl_blend_zero,sgl_accum2_notused),
                        SGLTT2_BLENDCNTL);
            } 
            
            SGLContext2.u32Flags &= 0xffffffff - SGLTT2_CLAMPU - SGLTT2_CLAMPV;
            switch (p_stGlobaleMT -> p_stCurrentTexture -> ucCylingMode)
            {
            case GLI_C_lCylingUV:
                // No Cycling
                break;
            case 0:
                // Cycling UV
                SGLContext.u32Flags |= SGLTT_DIRECT_EXTENSIONS;
                SGLContext2.u32Flags |= SGLTT2_CLAMPU | SGLTT2_CLAMPV;
                break;
            case GLI_C_lCylingV:
                // Cycling V (yes, V , a little mistake)
                SGLContext.u32Flags |= SGLTT_DIRECT_EXTENSIONS;
                SGLContext2.u32Flags |= SGLTT2_CLAMPV ;
                break;
            case GLI_C_lCylingU:
                // Cycling U
                SGLContext.u32Flags |= SGLTT_DIRECT_EXTENSIONS;
                SGLContext2.u32Flags |= SGLTT2_CLAMPU;
                break;
            }
        } else //*/
        {
            // Gouraud rendering
            SGLContext.u32Flags =  SGLTT_GOURAUD /*| SGLTT_DIRECT_EXTENSIONS*/;
            SGLContext2.u32Flags =  0;
        }
    }
	// This value is ignored if the SGLTT_GLOBALTRANS
	// flag is not set.
    if ((!(p_stGlobaleMT -> lCurrentDrawMask & GLI_C_lIsNotGrided)) && (p_stGlobaleMT->xGlobalAlpha < 0.98f))
    {
        // Alpha blending
        SGL2_ChangeRenderingParrametter(u32GlobalTrans,(unsigned long)(p_stGlobaleMT->xGlobalAlpha * 255.0f),SGLTT_GLOBALTRANS);

    } else {
        // No blending
    }
//    SGLContext.u32Flags |= SGLTT_HIGHLIGHT;
/*    SGLContext.u32Flags |= SGLTT_DIRECT_EXTENSIONS;
    SGLContext.u32Flags |= SGLTT_VERTEXFOG;
    SGLContext2.u32Flags |=  SGLTT2_VERTEXFOGCOLOUR;
    SGLContext2.fVertexFogR = 0.0f;
    SGLContext2.fVertexFogG = 1.0f;
    SGLContext2.fVertexFogB = 0.0f;*/
    
}


extern GLI_tdstInternalGlobalValuesFor3dEngine *GLI_BIG_GLOBALS;
extern void GLI_fnSwitchTwiddled(short *p_Texture , long size );

//extern  KMVERTEXBUFFDESC	VertexBufferDesc;
void  GLI_vWaitEndOfRendering();
void GLI_vWaitEndOfVertexTransfert();
void GLI_vWaitEndOfVSync();
short VigDifferences[4];
void GLI_DRV_vWrite16bBitmapToBackBuffer( void *_p_vSourceBuffer, long _lWidth, long _lHeight, long _lDestLeft, long _lDestTop, long _lDestRight, long _lDestBottom )
{
    SGLVERTEX gld_CurrentDestXYZ[4];
    static bFirst = 0;
    GLI_tdstInternalGlobalValuesFor3dEngine *p_stGlobaleMT;
    
    p_stGlobaleMT = GLI_BIG_GLOBALS;
    
    if (!bFirst)
    {
        GLI_BigInitSGL2Rendering(p_stGlobaleMT);
    } 
    bFirst++;

//    if ((bFirst & 0x1F)== 0 ) // == QUADRUPLE SPEED!!
    {                                   
//        sgl_intermediate_map SGL2_IM;
/*        GLI_vWaitEndOfVSync();
        GLI_vWaitEndOfRendering();*/
        memset(&SGL2_IM_sPecialtexture , 0 ,  sizeof (sgl_intermediate_map));
        GLD_MDRV_vBlitStretched16b(  512,480,VignetteBuffer  ,512,_lWidth,_lHeight,_p_vSourceBuffer);
        SGL2_IM_sPecialtexture . pixels = (void *)VignetteBuffer;
        GLI_fnSwitchTwiddled(VignetteBuffer , 512 );
 //       sgl_preprocess_texture( sgl_map_rgb_565 , sgl_map_512x512 ,0,0,&SGL2_IM_sPecialtexture ,NULL ,&SGL2_IM);

        sgl_set_texture( stSpecialTexture,FALSE,FALSE,&SGL2_IM_sPecialtexture,NULL);
    }//*/
//    GLI_vWaitEndOfVSync();
    SGLContext.u32Flags |=  SGLTT_TEXTURE | SGLTT_GOURAUD;
    SGLContext2.u32Flags =  0;
    SGLContext.nTextureName = stSpecialTexture;
    SGL2_ChangeRenderingParrametterC2(
        u32BlendCntl,
        sgl_blend_cntl(sgl_blend_one,sgl_blend_zero,sgl_accum2_notused),
        SGLTT2_BLENDCNTL);
    
    //        return;
    /* ------------------------       BEURK      ---------------------------------*/
    /* ------------------------  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] . fUOverW = 0.0f;
    gld_CurrentDestXYZ[0] . fVOverW = 0.0f;
    gld_CurrentDestXYZ[1] . fUOverW = 0.0f;
    gld_CurrentDestXYZ[1] . fVOverW = 0.9375f;
    gld_CurrentDestXYZ[2] . fUOverW = 1.0f;
    gld_CurrentDestXYZ[2] . fVOverW = 0.9375f;
    gld_CurrentDestXYZ[3] . fUOverW = 1.0f;
    gld_CurrentDestXYZ[3] . fVOverW = 0.0f;
    gld_CurrentDestXYZ[0].u32Colour =
    gld_CurrentDestXYZ[1].u32Colour =
    gld_CurrentDestXYZ[2].u32Colour =
    gld_CurrentDestXYZ[3].u32Colour = 0xffffffff;
    gld_CurrentDestXYZ[0] . fInvW  = 
        gld_CurrentDestXYZ[1] . fInvW  = 
        gld_CurrentDestXYZ[2] . fInvW  = 
        gld_CurrentDestXYZ[3] . fInvW  = 0.01f;
    sgltri_quads ( &SGLContext , 1 , NULL , gld_CurrentDestXYZ );
}


void GLI_INTERFACE_FUNC(GLI_vSetFogParams) ( tdstFogParams *_p_stFogParams )
{
    SGL_FOG GLI_SGLFog;

    GLI_SGLFog.w_start = 0.5f * GLI_C_xZClippingNear / _p_stFogParams->xDepthStart;
    GLI_SGLFog.w_end = 0.5f *  GLI_C_xZClippingNear / _p_stFogParams->xDepthEnd;
    GLI_SGLFog.fog_density = _p_stFogParams->xBlendEnd / 255.0f;
    SGLContext.fFogR = _p_stFogParams->stColor.xR;
    SGLContext.fFogG = _p_stFogParams->stColor.xG;
    SGLContext.fFogB = _p_stFogParams->stColor.xB;
    
    sgl_linear_fog(&GLI_SGLFog);
}
#ifdef __cplusplus
} //extern "C"
#endif
