#include "Acp_base.h"
 
#include "GMT/GmtHandl.h"
 
#include "GEO.h"
#include "GLI.h"
 
#include "MTH.h"
#include "COL.h"
#include "SPO.h"
#include "LST.h"
 
#include "MEC.h"
/*#include "ENV.h"*/
#include "SND.h"
/*#include "SRF.h"*/
#include "SCT.h"
 
#include "PCS.h"
#include "PO.h"
#include "IPO.h"
 
#include "ISI.h"
 
#include "RAY.h"
 
static GEO_tdstColor Tab_stColor[10000];
 
 
/* Compute RLI for all object in a super object.*/
/* This is a récursive function.*/
void RAY_vComputeAllIPOInSector(HIE_tdxHandleToSuperObject _hSuperObject)
{
/*  GEO_tdstVisualSet_ *p_stVisualSet;*/
  ACP_tdxHandleOfObject hPhysicalObject;
  MTH_tdxReal xTemp;
  ACP_tdxIndex xI,xJ;
  ACP_tdxHandleToRadiosity hISI;
	ACP_tdxIndex xNbVertex;
  HIE_tdxHandleToSuperObject hchildSO;
  GEO_tdxHandleToVisualSet p_stVisualSet;
  long lNbLOD;
  ISI_tdstColor *pstColorISI;
 
  HIE_fn_vPushMatrix (_hSuperObject);
 
  if (HIE_fn_ulGetSuperObjectType(_hSuperObject) & (HIE_C_ulIPO | HIE_C_ulIPO_Mirror))
  {
    p_stVisualSet=PO_fn_hGetVisualSet(IPO_fn_hGetPhysicalObject((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSuperObject)));
    lNbLOD=GLI_lGetVisualSetNumbertOfLOD(p_stVisualSet);
    hISI=IPO_fn_hGetRadiosity((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSuperObject));
    
    if(!hISI)
    {
      hISI=ISI_fn_hCreateISI(lNbLOD);
      for (xJ=0;xJ<lNbLOD;xJ++)
      {
        GLI_vGetVisualSetLOD(p_stVisualSet,xJ,&xTemp,&hPhysicalObject);
        ISI_fn_hCreateISILOD(hISI,xJ,hPhysicalObject->xNbPoints);
      }
      IPO_fn_vSetRadiosity((IPO_tdxHandleToInstanciatedPhysicalObject)HIE_fn_hGetSuperObjectObject(_hSuperObject),hISI);
    }
      
    for (xJ=0;xJ<lNbLOD;xJ++)
    {
      GLI_vGetVisualSetLOD(p_stVisualSet,xJ,&xTemp,&hPhysicalObject);
    
      GLI_xComputeLightForObject(hPhysicalObject,Tab_stColor);
 
      xNbVertex=ISI_fn_xGetNbVertexRLILOD(hISI,xJ);
      pstColorISI=ISI_fn_dGetVertexRLILOD(hISI,xJ);
      ISI_ColGEOToISI(pstColorISI,Tab_stColor,xNbVertex);
    }
  }
  HIE_M_ForEachChildOf(_hSuperObject,hchildSO,xI)
  {
    RAY_vComputeAllIPOInSector(hchildSO);
  }
 
  HIE_fn_vPopMatrix ();
}
 
 
/* Compute RLI for all sectors.*/
/* This is a récursive function.*/
void RAY_vComputeAllSectorRec(HIE_tdxHandleToSuperObject _hSuperObject)
{
  SECT_tdxHandleOfElementLstStaticLights hStaticLightsList;
  GLI_tdxHandleToLight hLight;
  ACP_tdxIndex xI;
  HIE_tdxHandleToSuperObject hchildSO;
 
  HIE_fn_vPushMatrix (_hSuperObject);
 
  if(HIE_fn_ulGetSuperObjectType(_hSuperObject)==HIE_C_ulSector)
  {
    GLI_vClearListOfLightInViewport(NULL);
 
    SECT_M_ForEachStaticLightsListInSector(_hSuperObject,hStaticLightsList,xI)
    {
      hLight=SECT_GetStaticLightsInList(hStaticLightsList);
	  if (GLI_ucGetObjectLighted(hLight) & GLI_C_LightMap)
         GLI_vAddLightToViewport(NULL,hLight);
    }
    HIE_M_ForEachChildOf(_hSuperObject,hchildSO,xI)
    {
      RAY_vComputeAllIPOInSector(hchildSO);
    }
  }
  else
  {
    HIE_M_ForEachChildOf(_hSuperObject,hchildSO,xI)
    {
      RAY_vComputeAllSector(hchildSO);
    }
  }
 
  HIE_fn_vPopMatrix ();
}
 
 
/* Compute RLI for all sectors.*/
/* This is a récursive function.*/
void RAY_vComputeAllSector(HIE_tdxHandleToSuperObject _hSuperObject)
{
  HIE_fn_bLoadIdentity();
  HIE_fn_vInvalidateAllGlobalMatrices();
  GLI_vResetTableOfActiveLightsForChar( GLI_C_ActivateLight );
  RAY_vComputeAllSectorRec(_hSuperObject);
  HIE_fn_vPopMatrix ();
}
