/* (c) Ubi Studios 1996-97-98*/
/* See Vincent Greco for any comment or question*/
 
#include "ACP_base.h"
#include "cpa_std.h"
 
#include "GEO.h"
#include "GLI.h"
#include "PCS.h"
#include "PO.h"
#include "IPO.h"
#include "POS.h"
 
#include "SPO/HieConst.h"
 
#include "LST.h"
#include "SPO/HieSpObj.h"
#include "SPO/HieHand.h"
#include "SPO/HieDef.h"
#include "SPO/HieMacro.h"
#include "SPO/HieMtStk.h"
#include "SPO/HieExt.h"
#include "../HieDrw.h"
#include "PRF.h"
 
extern int	g_iSPODisplayed;
extern ACP_tdxBool		bViewingInMirror;
 
#include "MEC.h"
/*#include "ENV.h"*/
#ifdef USE_IPT_DX5
#include "IPT_DX5.h" /* InPuT (absolutely before GAM.h)*/
#else /* USE_IPT_WIN */
#include "IPT.h" /* InPuT (absolutely before GAM.h)*/
#endif /* USE_IPT_WIN */
#include "PCS.h" /* Physical Collide Set */
#include "RND.h" /* RaNDom*/
#include "VIG.h" /* Vignette*/
/*#include "SRF.h" // Surface*/
#include "SND.h" /* SND*/
#include "SCT.h" /* SeCTor*/
#include "FIL.h" /* SeCTor*/
#include "PRT.h" /* PaRTicules*/
#include "Gam.h"
 
 
/*ANNECY JMD 17/02/98 {*/
#include "ISI.h"
/*END ANNECY JMD }*/
 
 
#define HIE_M_MustBeDisplayed(_lDrawMask)  \
  (( (_lDrawMask&GLI_C_lIsNotVisibleInRealWorld)     &&   (_lDrawMask&GLI_C_lIsNotDrawingInMirror) ) || \
( (_lDrawMask&GLI_C_lIsNotVisibleInSymetricWorld) &&  !(_lDrawMask&GLI_C_lIsNotDrawingInMirror) ))
 
#define HIE_C_InheritableFlags  \
(GLI_C_lIsNotVisibleInRealWorld|\
 GLI_C_lIsNotVisibleInSymetricWorld|\
 GLI_C_lCameraIsUnderWater|\
 GLI_C_lIsNotDrawingSuperObjectBoundingVolume|\
 GLI_C_lIsNotDrawingInMirror|\
 GLI_C_lNotInvertBackfaces|\
 GLI_C_lNotHideWhatIsUnderWater|\
 GLI_C_lIsNotWired)
 
 
/*
 * Blend RLI
 */
unsigned char g_ucNbRLIUse = 0 ;
unsigned char g_ucIndex1 = 0 ;
unsigned char g_ucIndex2 = 0 ;
MTH_tdxReal   g_xPercent ;
void HIE_vSetCharactereRLI ( unsigned char ucNbRLI, unsigned char ucIndex1, unsigned char ucIndex2, MTH_tdxReal xPercent) 
{
  if ( ucNbRLI <= 2 || ucNbRLI >= 0 ) 
  {
    g_ucNbRLIUse = ucNbRLI ;
    g_ucIndex1   = ucIndex1 - 1;
    g_ucIndex2   = ucIndex2 - 1;
    g_xPercent   = xPercent ;
  }
}
 
 
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
/* New version*/
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
#if defined(USE_PROFILER) && !defined(PRESS_DEMO)
ACP_tdxBool fn_bSendOneSuperObjectToViewportReal
(
	GLD_tdxHandleToViewportAttributes _hVpt ,
  HIE_tdxHandleToSuperObject   _hSprObj ,
  long *_p_lDrawMask,
	long _lCullingResult
)
#else
ACP_tdxBool fn_bSendOneSuperObjectToViewport
(
	GLD_tdxHandleToViewportAttributes _hVpt ,
	HIE_tdxHandleToSuperObject   _hSprObj ,
	long *_p_lDrawMask,
	long _lCullingResult
)
#endif
{
	long							lType , lCullingResult;
	unsigned long					ulCurrentBits;
	GEO_tdxHandleToBoundingSphere	hBoundingSphere = NULL;
	GEO_tdxHandleToParallelBox		hParallelBox = NULL;
	MTH3D_tdstVector				stSphereCenter;
	MTH_tdxReal						xSphereRadius;
	ACP_tdxHandleToRadiosity		hRad;
	float							fGlobalAlphaTemp , fCullingTransparency;
	ACP_tdxBool						bBoxBoundingVolume;
	MTH3D_tdstVector				stMinPoint;
	MTH3D_tdstVector				stMaxPoint;
	POS_tdstCompletePosition		stIdentityMatrix;
 
	if ( _lCullingResult != GEO_C_lCullingIn )
	{
		/* Get bounding Volume*/
		bBoxBoundingVolume = (ACP_tdxBool) HIE_fn_SO_bHasABoxBoundingVolume(_hSprObj);
		
		if(bBoxBoundingVolume)
		{
			hParallelBox = (GEO_tdxHandleToParallelBox)HIE_fn_hGetSuperObjectBoundingVolume(_hSprObj);
		}
		else	
		{
			hBoundingSphere = (GEO_tdxHandleToBoundingSphere)HIE_fn_hGetSuperObjectBoundingVolume(_hSprObj);
		}
		
		if ((!bBoxBoundingVolume) && (hBoundingSphere))
		{
			MTH3D_M_vCopyVector(&stSphereCenter, GEO_fn_pGetCenterPointOfBoundingSphere(hBoundingSphere));
			xSphereRadius = GEO_fn_xGetRadiusOfBoundingSphere(hBoundingSphere);
		}
		
		if ( (bBoxBoundingVolume) && (hParallelBox) )
		{
			MTH3D_tdstVector	*p_stMinPoint;
			MTH3D_tdstVector	*p_stMaxPoint;
			MTH3D_tdstVector	stTranslation;
			
			p_stMinPoint = GEO_fn_pGetMinPointOfParallelBox(hParallelBox);
			p_stMaxPoint = GEO_fn_pGetMaxPointOfParallelBox(hParallelBox);
			stTranslation = (HIE_fn_hGetSuperObjectGlobalMatrix(_hSprObj))->stTranslationVector;
			
			MTH3D_M_vAddVector(&stMinPoint, p_stMinPoint , &stTranslation);
			MTH3D_M_vAddVector(&stMaxPoint, p_stMaxPoint , &stTranslation);					 
			
			POS_fn_vSetIdentityMatrix(&stIdentityMatrix);
		}
	}
 
	lType			= HIE_fn_ulGetSuperObjectType(_hSprObj);	
 
	if (lType == HIE_C_ulActor)
	{
		ulCurrentBits	= fn_ulStandardGameGetCustomBits(M_GetMSHandle(_hSprObj,StandardGame));
		lCullingResult	= GEO_C_lCullingIn; /* to force recursive culling*/
		if
		(
			(_lCullingResult != GEO_C_lCullingIn)
			||
			(
				( (!hBoundingSphere) && (!hParallelBox) )
				||
				(
					(!bBoxBoundingVolume)
					&& ((lCullingResult = GEO_lCullingSphereNoMMM(_hVpt, &stSphereCenter, xSphereRadius, HIE_fn_hGetSuperObjectGlobalMatrix(_hSprObj), _lCullingResult)) != GEO_C_lCullingOut)/* volontary assignment*/
				)
				|| 
				(
					(bBoxBoundingVolume)
					&& ((lCullingResult = GEO_lCullingBox(_hVpt, &stMinPoint, &stMaxPoint, _lCullingResult)) != GEO_C_lCullingOut) /* volontary assignment*/
				)
			)
	  )
		{
			*_p_lDrawMask |= ~HIE_C_InheritableFlags;
			*_p_lDrawMask &= HIE_fn_lGetSuperObjectDrawMask(_hSprObj);
			/* invisibility bit*/
			if ( HIE_M_MustBeDisplayed(*_p_lDrawMask) && !HIE_fn_SO_bIsHidden( _hSprObj ) )
			{
				/* clear*/
				ulCurrentBits &= ~GAM_C_CustBitOutOfVisibility;
			}
			#ifdef U64
			/*unnecessary on PC, because it was set as default before anything is sent to the viewport (default value)*/
			else
			{
				/* set*/
				ulCurrentBits |= GAM_C_CustBitOutOfVisibility;
			}
			#endif /* U64 */
			fn_vStandardGameSetCustomBits(M_GetMSHandle(_hSprObj,StandardGame), ulCurrentBits);
 
			/* culling bit*/
			if( lCullingResult == GEO_C_lCullingIn )
			{
				/* set*/
/* ANNECY MT - 12/10/98 {*/
				fn_vStandardGameSetUselessCulling(M_GetMSHandle(_hSprObj,StandardGame), TRUE);
/* END ANNECY MT }*/
			}
			else
			{
				/* clear*/
/* ANNECY MT - 12/10/98 {*/
				fn_vStandardGameSetUselessCulling(M_GetMSHandle(_hSprObj,StandardGame), FALSE);
/* END ANNECY MT }*/
			}
			return TRUE;
		}
		#ifdef U64
		/*unnecessary on PC, because it was set as default before anything is sent to the viewport (default value)*/
		else
		{
			ulCurrentBits |= GAM_C_CustBitOutOfVisibility;
			fn_vStandardGameSetCustomBits(M_GetMSHandle(_hSprObj,StandardGame), ulCurrentBits);
		}
		#endif /* U64 */
	}
	else
	{
		if
		(
			(_lCullingResult != GEO_C_lCullingIn)
			||
			(
				( (!hBoundingSphere) && (!hParallelBox) )
  				||
				(
					(!bBoxBoundingVolume)
					&& (lCullingResult = GEO_lCullingSphereNoMMM(_hVpt, &stSphereCenter, xSphereRadius, HIE_fn_hGetSuperObjectGlobalMatrix(_hSprObj), _lCullingResult) != GEO_C_lCullingOut)  /* volontary assignment*/
				)
				|| 
				(
					(bBoxBoundingVolume)
					&& ((lCullingResult = GEO_lCullingBox(_hVpt, &stMinPoint, &stMaxPoint, _lCullingResult)) != GEO_C_lCullingOut)/* volontary assignment*/
				)
			)
		)
		{
			*_p_lDrawMask &= HIE_fn_lGetSuperObjectDrawMask(_hSprObj);
			/**/
			if ( HIE_M_MustBeDisplayed(*_p_lDrawMask) && !HIE_fn_SO_bIsHidden( _hSprObj ) )
			{
				fGlobalAlphaTemp = GLI_vGetGlobalAlpha();
				fCullingTransparency = MTH_M_xRealToFloat( GEO_xGetZFarTransparencyLevel() );
				GLI_vSetGlobalAlpha( fCullingTransparency * HIE_fn_fGetSuperObjectTransparenceLevel(_hSprObj) );
				
				if ( lType == HIE_C_ulPO )
				{
					GEO_tdxHandleToVisualSet hVisualSet ;
					/*if the SuperObject is a leaf, its Bounding Volume is the same that*/
					/*the one of its linked object : no need to test it (so I set hBoundingVolume to NULL).*/
					/*unless the bounding volume of the SuperObject was NULL*/
					if(bBoxBoundingVolume)
					{
						MTH3D_M_vMiddleVector(&stSphereCenter, &stMinPoint, &stMaxPoint);
						MTH3D_M_vSubVector (& stSphereCenter , & stSphereCenter , & HIE_fn_hGetSuperObjectGlobalMatrix (_hSprObj) -> stTranslationVector);
					}
					else
					{
						if (!hBoundingSphere)
							hBoundingSphere = PO_fn_hGetBoundingVolume((PO_tdxHandleToPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj));
						
						MTH3D_M_vCopyVector(&stSphereCenter, GEO_fn_pGetCenterPointOfBoundingSphere(hBoundingSphere));
						xSphereRadius = GEO_fn_xGetRadiusOfBoundingSphere(hBoundingSphere);
					}					
					hVisualSet = PO_fn_hGetVisualSet((PO_tdxHandleToPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj)) ;
					if ( g_ucNbRLIUse == 1 ) 
					{
						hRad = GLI_hGetRLI ( hVisualSet, g_ucIndex1 ) ;
						PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
						GLI_lSendVisualSetToViewportWithRLI2(_hVpt,hVisualSet ,&stSphereCenter,*_p_lDrawMask, hRad);
					}
					else if ( g_ucNbRLIUse == 2 ) 
					{
						/* xNumISILOD = 0 ???--------------------------------------------|*/
						hRad = GLI_hComputeBlendRLI (hVisualSet, g_ucIndex1, g_ucIndex2, 0, g_xPercent) ;
						PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
						if ( hRad != NULL ) 
						{
							GLI_lSendVisualSetToViewportWithRLI2(_hVpt,hVisualSet,&stSphereCenter,*_p_lDrawMask, hRad);             
							ISI_fn_vFreeISI (hRad) ;
						}
						else 
						{
							if(((hBoundingSphere) || (hParallelBox)))
                            {
								GLI_lSendVisualSetToViewportBV2(_hVpt ,hVisualSet ,&stSphereCenter, xSphereRadius, *_p_lDrawMask);
                            }
							else
                            {
								GLI_lSendVisualSetToViewport2(_hVpt ,hVisualSet ,&stSphereCenter, *_p_lDrawMask);
                            }
						}
					}
					else 
					{
						PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
						if(((hBoundingSphere) || (hParallelBox)))
                        {
							GLI_lSendVisualSetToViewportBV2(_hVpt ,hVisualSet ,&stSphereCenter, xSphereRadius, *_p_lDrawMask);
                        }
						else
                        {
							GLI_lSendVisualSetToViewport2(_hVpt ,hVisualSet ,&stSphereCenter, *_p_lDrawMask);
                        }
					}
				}
				/*the linked object is IPO*/
				else if ( lType==HIE_C_ulIPO )
				{
					if ( bBoxBoundingVolume )
					{
						MTH3D_M_vMiddleVector(&stSphereCenter, &stMinPoint, &stMaxPoint);
						MTH3D_M_vSubVector (& stSphereCenter , & stSphereCenter , & HIE_fn_hGetSuperObjectGlobalMatrix (_hSprObj) -> stTranslationVector);
					}
					else
					{
						if ( !hBoundingSphere )
						{
							hBoundingSphere = PO_fn_hGetBoundingVolume(IPO_fn_hGetPhysicalObject((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj)));
						}
						
						MTH3D_M_vCopyVector(&stSphereCenter, GEO_fn_pGetCenterPointOfBoundingSphere(hBoundingSphere));
						xSphereRadius = GEO_fn_xGetRadiusOfBoundingSphere(hBoundingSphere);
					}
					hRad = IPO_fn_hGetRadiosity((IPO_tdxHandleToInstanciatedPhysicalObject) HIE_fn_hGetSuperObjectObject(_hSprObj));
					PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
					GLI_lSendVisualSetToViewportWithRLI2(_hVpt,PO_fn_hGetVisualSet(IPO_fn_hGetPhysicalObject((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj))),&stSphereCenter,*_p_lDrawMask, hRad);
				}
				/*the linked object is geometric*/
				else if ( lType &( HIE_C_ulEDT_Geometric | HIE_C_ulSpecialEffect))
				{
					PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
					GLI_xSendObjectToViewportWithLights(_hVpt, (ACP_tdxHandleOfObject)HIE_fn_hGetSuperObjectObject(_hSprObj), *_p_lDrawMask);
				}
				GLI_vSetGlobalAlpha( fGlobalAlphaTemp );
			}
			return TRUE;
		}
	}
    return (ACP_tdxBool) HIE_fn_SO_bCheckChildren(_hSprObj);
}
 
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
 
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
extern void GLI_fn_vDrawMirror(BOOL);
#if defined(USE_PROFILER) && !defined(PRESS_DEMO)
void fn_vSendOneModuleToViewPortReal(GLD_tdxHandleToViewportAttributes _hVpt,
									 HIE_tdxHandleToSuperObject   _hSprObj ,
									 long _lDrawMask)
#else
void fn_vSendOneModuleToViewPort(GLD_tdxHandleToViewportAttributes _hVpt,
                                 HIE_tdxHandleToSuperObject   _hSprObj ,
                                 long _lDrawMask)
#endif
{
    long                            lType;
	GEO_tdxHandleToBoundingSphere	hBoundingSphere = NULL;
	GEO_tdxHandleToParallelBox		hParallelBox = NULL;
	MTH3D_tdstVector				stSphereCenter;
	MTH_tdxReal						xSphereRadius;
	ACP_tdxHandleToRadiosity		hRad;
	float							fGlobalAlphaTemp , fCullingTransparency;
  void GLI_vSetLensFlareRef ( void *p_vRef ) ;
 
    GLI_vSetLensFlareRef ( (void *) _hSprObj ) ;
 
    fGlobalAlphaTemp = GLI_vGetGlobalAlpha();
    fCullingTransparency = MTH_M_xRealToFloat( GEO_xGetZFarTransparencyLevel() );
    GLI_vSetGlobalAlpha( fCullingTransparency * HIE_fn_fGetSuperObjectTransparenceLevel(_hSprObj) );
 
	lType = HIE_fn_ulGetSuperObjectType(_hSprObj);
    if ( lType == HIE_C_ulPO )
    {
        GEO_tdxHandleToVisualSet hVisualSet ;
        /*if the SuperObject is a leaf, its Bounding Volume is the same that*/
        /*the one of its linked object : no need to test it (so I set hBoundingVolume to NULL).*/
        /*unless the bounding volume of the SuperObject was NULL*/
        hBoundingSphere = PO_fn_hGetBoundingVolume((PO_tdxHandleToPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj));
 
        MTH3D_M_vCopyVector(&stSphereCenter, GEO_fn_pGetCenterPointOfBoundingSphere(hBoundingSphere));
        xSphereRadius = GEO_fn_xGetRadiusOfBoundingSphere(hBoundingSphere);
 
        hVisualSet = PO_fn_hGetVisualSet((PO_tdxHandleToPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj)) ;
        /* VL modif pour prendre en compte les RLI si un perso n'a pas de MSLight */
        if ( ( g_ucNbRLIUse == 0 ) && (GLI_lGetVisualSetNumbertOfLOD( hVisualSet ) == 1) )
        {
            hRad = GLI_hGetRLI ( hVisualSet, 0 ) ;
            PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
            GLI_lSendVisualSetToViewportWithRLI2(_hVpt,hVisualSet ,&stSphereCenter,_lDrawMask, hRad);
        }
        else
        /* fun modif VL */
        if ( g_ucNbRLIUse == 1 )
        {
            hRad = GLI_hGetRLI ( hVisualSet, g_ucIndex1 ) ;
            PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
            GLI_lSendVisualSetToViewportWithRLI2(_hVpt,hVisualSet ,&stSphereCenter,_lDrawMask, hRad);
        }
        else if ( g_ucNbRLIUse == 2 )
        {
            /* xNumISILOD = 0 ???--------------------------------------------|*/
            hRad = GLI_hComputeBlendRLI (hVisualSet, g_ucIndex1, g_ucIndex2, 0, g_xPercent) ;
            PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
            if ( hRad != NULL )
            {
                GLI_lSendVisualSetToViewportWithRLI2(_hVpt,hVisualSet,&stSphereCenter,_lDrawMask, hRad);
                ISI_fn_vFreeISI (hRad) ;
            }
            else
            {
                if(((hBoundingSphere) || (hParallelBox)))
                    GLI_lSendVisualSetToViewportBV2(_hVpt ,hVisualSet ,&stSphereCenter, xSphereRadius, _lDrawMask);
                else
                    GLI_lSendVisualSetToViewport2(_hVpt ,hVisualSet ,&stSphereCenter, _lDrawMask);
            }
        }
        else
        {
            PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
            if(((hBoundingSphere) || (hParallelBox)))
                GLI_lSendVisualSetToViewportBV2(_hVpt ,hVisualSet ,&stSphereCenter, xSphereRadius, _lDrawMask);
            else
                GLI_lSendVisualSetToViewport2(_hVpt ,hVisualSet ,&stSphereCenter, _lDrawMask);
        }
    }
    /*the linked object is IPO*/
    else if ( lType==HIE_C_ulIPO )
    {
        hBoundingSphere = PO_fn_hGetBoundingVolume(IPO_fn_hGetPhysicalObject((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj)));
        MTH3D_M_vCopyVector(&stSphereCenter, GEO_fn_pGetCenterPointOfBoundingSphere(hBoundingSphere));
        xSphereRadius = GEO_fn_xGetRadiusOfBoundingSphere(hBoundingSphere);
 
        hRad = IPO_fn_hGetRadiosity((IPO_tdxHandleToInstanciatedPhysicalObject) HIE_fn_hGetSuperObjectObject(_hSprObj));
        PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
        GLI_lSendVisualSetToViewportWithRLI2(_hVpt,PO_fn_hGetVisualSet(IPO_fn_hGetPhysicalObject((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj))),&stSphereCenter,_lDrawMask, hRad);
    }
    /*the linked object is IPO_Mirror*/
    else if ( lType==HIE_C_ulIPO_Mirror )
    {
		if (bViewingInMirror) 
		{
			hBoundingSphere = PO_fn_hGetBoundingVolume(IPO_fn_hGetPhysicalObject((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj)));
			MTH3D_M_vCopyVector(&stSphereCenter, GEO_fn_pGetCenterPointOfBoundingSphere(hBoundingSphere));
			xSphereRadius = GEO_fn_xGetRadiusOfBoundingSphere(hBoundingSphere);
 
			hRad = IPO_fn_hGetRadiosity((IPO_tdxHandleToInstanciatedPhysicalObject) HIE_fn_hGetSuperObjectObject(_hSprObj));
			PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
			GLI_fn_vDrawMirror( 1 );
			GLI_lSendVisualSetToViewportWithRLI2(_hVpt,PO_fn_hGetVisualSet(IPO_fn_hGetPhysicalObject((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSprObj))),&stSphereCenter,_lDrawMask, hRad);
			GLI_fn_vDrawMirror( 0 );
		}
    }
    /*the linked object is geometric*/
    else if( lType & (HIE_C_ulEDT_Geometric | HIE_C_ulSpecialEffect))
    {
        PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvComputed, 1 ); /* update number of computed objects*/
        GLI_xSendObjectToViewportWithLights(_hVpt, (ACP_tdxHandleOfObject)HIE_fn_hGetSuperObjectObject(_hSprObj), _lDrawMask);
    }
    GLI_vSetGlobalAlpha( fGlobalAlphaTemp );
}
 
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
#if defined(USE_PROFILER) && !defined(PRESS_DEMO)
 
ACP_tdxBool fn_bSendOneSuperObjectToViewport
(
	GLD_tdxHandleToViewportAttributes _hVpt,
	HIE_tdxHandleToSuperObject   _hSprObj,
	long *_p_lDrawMask,
	long _lCullingResult
)
{
    ACP_tdxBool bResult,bStopChrono;
	void *p_vData,*p_vData2;
 
 
	p_vData2 = NULL;
    PRF_fn_vSetIndependantVariable( PRF_C_ulIdpDisplayedElements, 0 );     /* init flag "faces displayed"*/
 
	bStopChrono = (PRF_fn_iGetFunctionNumberOfStart( PRF_C_ulFctDisplay , &p_vData ) > 0 ? 1 : 0);
	if( bStopChrono )
	{	
		if( PRF_fn_iGetFunctionNumberOfStart( PRF_C_ulFctDisplayCommon , &p_vData2 ) > 0 )
			PRF_fn_vStopChrono( PRF_C_ulFctDisplayCommon , p_vData2 );
		else
			p_vData2 = NULL;
 
		PRF_fn_vStopChrono( PRF_C_ulFctDisplay, p_vData );
		PRF_fn_vStartChrono( PRF_C_ulFctDisplaySpecific, PRF_C_pvMisc );
	}
 
    bResult = fn_bSendOneSuperObjectToViewportReal( _hVpt, _hSprObj, _p_lDrawMask, _lCullingResult);
 
	if( bStopChrono )
	{
		PRF_fn_vStopChrono( PRF_C_ulFctDisplaySpecific, PRF_C_pvMisc );
		PRF_fn_vStartChrono( PRF_C_ulFctDisplay, p_vData );
		if( p_vData2 )
			PRF_fn_vStartChrono( PRF_C_ulFctDisplayCommon, p_vData2 );
	}
    if( PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDisplayedElements ) )   /* at least one face has been displayed*/
 
    {
        PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvDisplayed, 1 );  /* update number of displayed objects*/
        PRF_fn_vSetIndependantVariable( PRF_C_ulIdpDisplayedObjects, 1 ); /* set flag "object displayed" for sector*/
    }
    return bResult;
}
 
 
void fn_vSendOneModuleToViewPort(GLD_tdxHandleToViewportAttributes _hVpt,
                                 HIE_tdxHandleToSuperObject   _hSprObj ,
                                 long _lDrawMask)
{
    ACP_tdxBool bStopChrono;
	void *p_vData,*p_vData2;
 
 
	p_vData2 = NULL;
    PRF_fn_vSetIndependantVariable( PRF_C_ulIdpDisplayedElements, 0 );     /* init flag "faces displayed"*/
 
	bStopChrono = (PRF_fn_iGetFunctionNumberOfStart( PRF_C_ulFctDisplay , &p_vData ) > 0 ? 1 : 0);
	if( bStopChrono )
	{
		if( PRF_fn_iGetFunctionNumberOfStart( PRF_C_ulFctDisplayCommon , &p_vData2 ) > 0 )
			PRF_fn_vStopChrono( PRF_C_ulFctDisplayCommon , p_vData2 );
		else
			p_vData2 = NULL;
 
		PRF_fn_vStopChrono( PRF_C_ulFctDisplay, p_vData );
		PRF_fn_vStartChrono( PRF_C_ulFctDisplaySpecific, PRF_C_pvMisc );
	}
 
    fn_vSendOneModuleToViewPortReal( _hVpt, _hSprObj, _lDrawMask );
 
	if( bStopChrono )
	{
		PRF_fn_vStopChrono( PRF_C_ulFctDisplaySpecific, PRF_C_pvMisc );
		PRF_fn_vStartChrono( PRF_C_ulFctDisplay, p_vData );
		if( p_vData2 )
			PRF_fn_vStartChrono( PRF_C_ulFctDisplayCommon, p_vData2 );
	}
    if( PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDisplayedElements ) )   /* at least one face has been displayed*/
 
    {
        PRF_fn_vIncreaseVariable( PRF_C_ulVarObjects + PRF_fn_lGetIndependantVariable( PRF_C_ulIdpDynOrSta ), PRF_C_pvDisplayed, 1 );  /* update number of displayed objects*/
        PRF_fn_vSetIndependantVariable( PRF_C_ulIdpDisplayedObjects, 1 ); /* set flag "object displayed" for sector*/
    }
}
 
 
void HIE_fn_vUpdateRasterForObject( GEO_tdstGeometricObject *p_stObject,
									unsigned long _ulRasterObject,
									unsigned long _ulRasterFace,
									unsigned long _ulRasterElement )
{
    /* take account Object*/
    if( p_stObject )
    {
        ACP_tdxIndex xIndexElement, xNbElements;
 
        /* update number of activated objects*/
        PRF_fn_vIncreaseVariable( _ulRasterObject, PRF_C_pvVisible, 1 );
/* ANNECY MT - 27/05/98 {*/
		/* update number of elements*/
        xNbElements = GEO_xGetGeometricObjectNumberOfElements( p_stObject );
		PRF_fn_vIncreaseVariable( _ulRasterElement, PRF_C_pvVisible, xNbElements );
 
        /* update number of faces*/
/* END ANNECY MT }*/
        for( xIndexElement = 0 ; xIndexElement < xNbElements ; xIndexElement++ )
        {
            switch( GEO_xGetElementType( p_stObject, xIndexElement ) )
            {
            case GEO_C_xElementIndexedTriangles:
                PRF_fn_vIncreaseVariable( _ulRasterFace, PRF_C_pvVisible, GEO_lGetIndexedTriangleNbFaces( p_stObject, xIndexElement ) );
                break;
            case GEO_C_xElementFaceMapDescriptors:
                PRF_fn_vIncreaseVariable( _ulRasterFace, PRF_C_pvVisible, ((GEO_tdstElementFaceMapDescriptors *)p_stObject -> d_stListOfElements[xIndexElement])->xNbFaces );
                break;
            case GEO_C_xElementSprites:
                PRF_fn_vIncreaseVariable( _ulRasterFace, PRF_C_pvVisible, ((GEO_tdstElementSprite *)p_stObject -> d_stListOfElements[xIndexElement])->xNbSprites * 2 );
                break;
            case GEO_C_xElementTMeshes:
                PRF_fn_vIncreaseVariable( _ulRasterFace, PRF_C_pvVisible, ((GEO_tdstElementTMeshes *) p_stObject -> d_stListOfElements[xIndexElement])->xNbFaces );
                break;
            case GEO_C_xElementPoints:
            case GEO_C_xElementLines:
            case GEO_C_xElementSpheres:
            case GEO_C_xElementAlignedBoxes:
            case GEO_C_xElementCones:
                break;
#ifdef USE_ALTIMAPS
            case GEO_C_xElementAltimap:
                PRF_fn_vIncreaseVariable( _ulRasterFace, PRF_C_pvVisible, ((GEO_tdstElementAltimap *)p_stObject -> d_stListOfElements[xIndexElement])->xNbFaces );
                break;
#endif
			default:
				break;
            }
        }
    }
}
 
void HIE_fn_vUpdateRasterForSuperObject( HIE_tdxHandleToSuperObject _hSuperObject,
										 unsigned long _ulRasterObject,
										 unsigned long _ulRasterFace,
										 unsigned long _ulRasterElement )
{
    GEO_tdstGeometricObject     *p_stObject;
    ACP_tdxIndex                i;
	HIE_tdxHandleToSuperObject	hChild;
 
	if(HIE_fn_lGetSuperObjectDrawMask(_hSuperObject) & GLI_C_lDrawNothing)
	{
		p_stObject = HIE_fn_p_stGetGeometricObjectFromSuperObject( _hSuperObject );
 
		/* take account SuperObject*/
		HIE_fn_vUpdateRasterForObject( p_stObject, _ulRasterObject, _ulRasterFace, _ulRasterElement );
 
		/* take account children*/
		HIE_M_ForEachChildOf(_hSuperObject, hChild, i)
		{
	/* ANNECY MT - 27/05/98 {*/
			HIE_fn_vUpdateRasterForSuperObject( hChild, _ulRasterObject, _ulRasterFace, _ulRasterElement );
	/* END ANNECY MT }*/
		}
	}
}
 
#endif /* USE_PROFILER && PRESS_DEMO */
 
/*
 * Use for Lens Flare
 */
ACP_tdxBool HIE_fn_bIsSegmentIntersectedInCameraSector (POS_tdstCompletePosition *p_stCameraPos, POS_tdstCompletePosition *p_stSPOPos) {
  MTH3D_tdstVector stOrg, stOrgVect, stVect, stNormal, stHit ;
  HIE_tdxHandleToSuperObject hSprObjHit, hSector ;
  POS_tdstCompletePosition   hCameraPosInv ;
  HIE_tdxHandleToSuperObject hMainCamera = CAM_fn_hGetActiveCamera(1); /* 1 ... aaargh !*/
  ACP_tdxIndex xIndex;
  SECT_tdxHandleOfSectorObject hSectorObject;
  SECT_tdxHandleOfElementLstGraphicInteraction hGraphicInteractionChild;
 
  /* coordonnées camera dans le repere du monde*/
  POS_fn_vInvertIsoMatrix( &hCameraPosInv, p_stCameraPos );
  POS_fn_vGetTranslationVector(&hCameraPosInv, &stOrg) ;
  
  /* coordonnées du centre d'objet dans le repere du monde*/
  POS_fn_vGetTranslationVector(p_stSPOPos, &stVect) ;
  MTH3D_M_vSubVector(&stOrgVect, &stVect, &stOrg) ;
  
  /* le secteur courant*/
  hSector = fn_h_SectInfoGetCurrentSector(M_GetMSHandle(hMainCamera, SectInfo)) ;
 
  /* distance dans stVect qui est reduit a chaque intersection*/
  MTH3D_M_vCopyVector(&stVect, &stOrgVect) ;
  if ( HIE_bIntersectSegmentWithFirstSuperObject (&stOrg, &stVect, hSector, &stHit, &stNormal, &hSprObjHit) ){
    return (1) ;
  }
  /* For each sector in graphic interaction*/
  hSectorObject=(SECT_tdxHandleOfSectorObject)HIE_fn_hGetSuperObjectObject(hSector);
  LST2_M_StaticForEachElementOf(  &(hSectorObject->stListOfSectorsInGraphicInteraction), hGraphicInteractionChild, xIndex) {
    MTH3D_M_vCopyVector(&stVect, &stOrgVect) ;
    if ( HIE_bIntersectSegmentWithFirstSuperObject( &stOrg, &stVect, hGraphicInteractionChild->hPointerOfSectorSO,
      &stHit, &stNormal, &hSprObjHit) ){
      return (1) ;
    }
  }
 
  return (0) ;
}
