#include "ldt.h"
 
HREF g_hRefLight;
 
void fn_vPostProcessLight ( HREF hRef )
{
  LDT_tdst_Link *pObject;
  LDT_tdst_Link *pGetFrom;
  int iType;
  short xCount;
  long *pVal;
 
  while( LDT_GetRefFromTable( hRef, &pObject, &pGetFrom, &iType, &xCount, &pVal )==0 )
  {
    POS_tdstCompletePosition * p_stMatrix = (POS_tdstCompletePosition *)pGetFrom->pObject;
    LGT_tdxHandleToLight hLight=(LGT_tdxHandleToLight)pObject->pObject;
 
    GLI_xSetLightMatrix(hLight,p_stMatrix);
 
            /* ATTENTION : dans light il y a une matrix et non un pointeur sur matrix*/
				    /* La matrice est donc (sauvagement) recopiée.*/
				    /* En toute rigueur je devrais libérer p_stMatrix, mais, à terme,*/
				    /* je pense que light contiendra un pointeur (sigh...)*/
    
  }
 
}
 
/*****************************************************************
Name: 
Description: Create callback for LDT
Author: Mircea Petrescu
Date: 8/25/98
Modified: 
*****************************************************************/
int fn_iCreateLight( LDT_tdst_Link *pLink )
{
  LGT_tdxHandleToLight hLight;
 
  GLI_xCreateLight(&hLight, 0);
 
  pLink->pObject=(void *)hLight;
  return 0;
}
 
/*****************************************************************
Name: 
Description: Load callback for LDT
Author: Mircea Petrescu
Date: 8/26/98
Modified: 
*****************************************************************/
int fn_iLoadLight( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  LGT_tdxHandleToLight hLight=(LGT_tdxHandleToLight)pLink->pObject;
  POS_tdstCompletePosition *p_stMatrix;
  long lOnOff;
  GEO_tdstColor stColor;
  long lTypeOfLight;
  char a255_cLongName[255];
  MTH3D_tdstVector myGetVector;
	char *szName=LDT_szGetParam(1);
	int iLen;
 
  if(stricmp(szName,"Parallel")==0)	lTypeOfLight=GLI_C_lParallelLight;
  else if(stricmp(szName,"Spherical")==0)	lTypeOfLight=GLI_C_lSphericalLight;
  else if(stricmp(szName,"HotSpot")==0)	lTypeOfLight=GLI_C_lHotSpotLight;
  else if(stricmp(szName,"Ambient")==0)	lTypeOfLight=GLI_C_lAmbientLight;
  else if(stricmp(szName,"ParallelBox")==0) lTypeOfLight=GLI_C_lParallelLight_With_Limit;
  else if(stricmp(szName,"Fog")==0) lTypeOfLight=GLI_C_lFogLight;
 
  GLI_vSetLightType( hLight, lTypeOfLight );
 
  iLen=LDT_ComputeSectionName(pLink,a255_cLongName);
  SCR_M_v_Link_SetAdditionalLong(SCR_fnp_st_Link_SetValue(LGT_fn_p_stGetLightLinkTable(), a255_cLongName, (unsigned long)hLight), 1, iLen);
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
  	    switch (*(long*)szEntry)
  	    {
  	      case 'SteS' : /* SetState */
  	      {
				    lOnOff=atoi( LDT_szGetParam( 1 ) );
				    GLI_xSetLightState(hLight, lOnOff);
  	      }
  	      break;
  	      case 'CteS' : /* SetColor */
  	      {
				    stColor.xR=(GEO_tdxColorValue)atof( LDT_szGetParam( 1 ) );
				    stColor.xG=(GEO_tdxColorValue)atof( LDT_szGetParam( 2 ) );
				    stColor.xB=(GEO_tdxColorValue)atof( LDT_szGetParam( 3 ) );
				    stColor.xA=(GEO_tdxColorValue)atof( LDT_szGetParam( 4 ) );
        
				    GLI_xSetLightColor(hLight,&stColor);
  	      }
  	      break;
  	      case 'NteS' : /* SetNearFar */
  	      {
    				GLI_vSetLightNearFar( hLight, MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 1 ) )), MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 2 ) )));
  	      }
  	      break;
  	      case 'AteS' : /* SetAlphas */
  	      {
    				GLI_vSetLightAlphas( hLight, MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 1 ) )), MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 2 ) )));
  	      }
  	      break;
  	      case 'MteS' : /* SetMatrix */
  	      {
		    p_stMatrix = (POS_tdstCompletePosition *)LDT_LoadSection( LDT_szGetParam(1) );
 
            LDT_AddToRefsTable( g_hRefLight, pLink, 0, 0 );
          
  	      }
  	      break;
  	      case 'ejbO' : /* ObjectLighted */
  	      {
    				GLI_vSetObjectLighted(hLight,(unsigned char)atoi( LDT_szGetParam( 1 ) ));
  	      }
  	      break;
  	      case 'niaP' : /* PaintLight */
  	      {
    				GLI_vSetPaintLight(hLight);
  	      }
  	      break;
  	      case 'lAoN' : /* NoAlphaLight */
  	      {
    				GLI_vSetAlphaLightFlag (hLight, 2);
  	      }
  	      break;
  	      case 'hplA' : /* AlphaLight */
  	      {
    				GLI_vSetAlphaLight(hLight);
  	      }
  	      break;
  	      case 'IteS' : 
  	        switch( *(long *)(szEntry+8) )
            {
  	          case 'PniM' : /* SetInterMinPos */
  	          {
				        myGetVector.xX =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 1 ) ));
				        myGetVector.xY =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 2 ) ));
				        myGetVector.xZ =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 3 ) ));
				        GLI_vSetLight_Inter_Min_Pos( hLight,myGetVector	);
  	          }
  	          break;
  	          case 'PxaM' : /* SetInterMaxPos */
  	          {
	  		        myGetVector.xX =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 1 ) ));
				        myGetVector.xY =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 2 ) ));
				        myGetVector.xZ =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 3 ) ));
				        GLI_vSetLight_Inter_Max_Pos( hLight,myGetVector	);
  	          }
  	          break;
  	          case 'ytis' : /* SetIntensityMinMax */
  	          {
    				    GLI_vSetLight_Intensity_Min_Max( hLight, MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 1 ) )), MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 2 ) )));
  	          }
  	          break;
            }
  	      break;
 
  	      case 'EteS' : 
  	      if( szEntry[9]=='i' ) /* SetExterMinPos */
          {
				    myGetVector.xX =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 1 ) ));
				    myGetVector.xY =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 2 ) ));
				    myGetVector.xZ =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 3 ) ));
				    GLI_vSetLight_Exter_Min_Pos( hLight,myGetVector	);
          }
          else /* SetExterMaxPos */
          {
				    myGetVector.xX =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 1 ) ));
				    myGetVector.xY =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 2 ) ));
				    myGetVector.xZ =  MTH_M_xFloatToReal((float)atof( LDT_szGetParam( 3 ) ));
				    GLI_vSetLight_Exter_Max_Pos( hLight,myGetVector	);
          }
  	      break;
  	      case 'kcaB' : /* BackgroundColor */
  	      {
				    stColor.xR=(GEO_tdxColorValue)atof( LDT_szGetParam( 1 ) );
				    stColor.xG=(GEO_tdxColorValue)atof( LDT_szGetParam( 2 ) );
				    stColor.xB=(GEO_tdxColorValue)atof( LDT_szGetParam( 3 ) );
				    stColor.xA=(GEO_tdxColorValue)atof( LDT_szGetParam( 4 ) );
 
				    GLI_xSetLightBackgroundColor(hLight,&stColor);
  	      }
  	      break;
  	    }
  	  }
  	}
  }
 
  return 0;
}
