/*
 =======================================================================================
  Name        : Camera.c
  Description : Camera function
 =======================================================================================
 */
 
/*
Tested with LINT
*/
#include "gli_st.h"
#include "GLI_Defn.h"
#include "light_st.h"
#ifndef U64
#include "PvObj_st.h"
#include "Liste.h"
#include "vpt3D.h"
#endif
 
#include "camera.h"
#include "Init_Gli.h"
 
#ifdef __cplusplus
extern "C"
{
#endif
 
#include "camera.cxx"
 
/*
 =======================================================================================
    a special camera to correct some bug (anti bug) 
 =======================================================================================
 */
 
 
  
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Create a camera
 ----------------------------------------------------------------------------------------
 */
 
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Copy a camera
 ----------------------------------------------------------------------------------------
 */

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 07/06/99 */
void GLI_vCopyCamera ( GLI_tdstCamera *p_stCamDest ,GLI_tdstCamera *p_stCamSource )
{
    *p_stCamDest = *p_stCamSource ;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 07/06/99 */

 
/*
 ----------------------------------------------------------------------------------------
  Description   : Duplicate a camera
 ----------------------------------------------------------------------------------------
 */

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
GLI_tdstCamera *GLI_xDuplicateCamera ( GLI_tdstCamera *p_stCamSource )
{
    GLI_tdstCamera *xReturnValue;
    GLI_xCreateCamera ( &xReturnValue, p_stCamSource->lCameraMode);
    GLI_vCopyCamera ( xReturnValue,p_stCamSource );
    return (xReturnValue);
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

 
/*
 ----------------------------------------------------------------------------------------
  Description   : Access function to camera mode
 ----------------------------------------------------------------------------------------
 */

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void GLI_xGetCameraMode ( GLI_tdstCamera *p_stCam , long *p_lMode)
{
    *p_lMode = p_stCam -> lCameraMode;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

  

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void GLI_xSetCameraMode ( GLI_tdstCamera *p_stCam , long lMode)
{
    p_stCam -> lCameraMode = lMode;
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

  
/*
 ----------------------------------------------------------------------------------------
  Description   : return the lenght in pixels of 1.0 unity in the 3D worl at a Z distance
 ----------------------------------------------------------------------------------------
 */

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
MTH_tdxReal GLI_xGetCameraFocalizationPlane( GLI_tdstCamera *p_stCam )
{
    return (  p_stCam->xScreen );
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

 
/*
 ----------------------------------------------------------------------------------------
  Description   : Access function to camera near & far plane
 ----------------------------------------------------------------------------------------
 */
void GLI_xSetCameraNearFar( GLI_tdstCamera *p_stCam, GLI_tdxValue xNear , GLI_tdxValue xFar )
{
    p_stCam->xNear = GLI_C_xZClippingNear;    /*xNear;*/
    if ( xFar == GLI_M_FloatToValue( 0.0 ) )
        p_stCam -> xFar = GLI_M_FloatToValue( 100000.0 );
    else
        p_stCam -> xFar = xFar;
}                        
  

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void GLI_xGetCameraNearFar ( GLI_tdstCamera *p_stCam , GLI_tdxValue *p_xNear , GLI_tdxValue *p_xFar )
{
    *p_xNear = p_stCam -> xNear;
    *p_xFar = p_stCam -> xFar;
}                        
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */
                        
  
  
/*
 ----------------------------------------------------------------------------------------
  Description   : Access function to camera position matrix
 ----------------------------------------------------------------------------------------
 */
void GLI_xGetCameraMatrix ( GLI_tdstCamera *p_stCam, POS_tdstCompletePosition *p_stMatrix )
{
    *p_stMatrix = p_stCam -> stMatrix ;
}
  
void GLI_xSetCameraMatrix( GLI_tdstCamera *p_stCam , POS_tdstCompletePosition *p_stMatrix )
{
    p_stCam -> stMatrix = *p_stMatrix ;
}
  
/*
 ----------------------------------------------------------------------------------------
  Description   : Access function to camera aspect and ratio
 ----------------------------------------------------------------------------------------
 */
void GLI_xSetCameraAspectAndRatio ( GLI_tdstCamera *p_stCam, MTH_tdxReal xAlphaX, GLI_tdxValue xRatio )
{
    p_stCam->xAlphaX = xAlphaX ;
    p_stCam->xRatio  = xRatio ;
    p_stCam->xAlphaY = GLI_M_Mul ( xAlphaX , xRatio ) ;
}
  
void GLI_xGetCameraAspectAndRatio ( GLI_tdstCamera *p_stCam, MTH_tdxReal *p_xAlphaX , GLI_tdxValue *p_xRatio )
{
    *p_xAlphaX = p_stCam -> xAlphaX ;
    *p_xRatio = p_stCam->xRatio;
}
 
void GLI_xSetCameraRatio ( GLI_tdstCamera *p_stCam , GLI_tdxValue xRatio )
{
    p_stCam->xRatio = xRatio ;
    p_stCam->xAlphaY = GLI_M_Mul ( p_stCam->xAlphaX , xRatio ) ;
}
  

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void GLI_xGetCameraRatio ( GLI_tdstCamera *p_stCam , GLI_tdxValue *p_xRatio )
{
    *p_xRatio = p_stCam->xRatio ;
}  
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */
  
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Access function to viewport camera
 ----------------------------------------------------------------------------------------
 */
 
#ifdef U64
extern char g_bWeirdWaterEffect;
extern char g_bWeirdFireEffect;
extern int bChangedFarClippingSettings;
#endif
 
void GLI_xSetViewportCamera ( GLD_tdhDevice hDevice, GLD_tdhViewport hViewport, GLI_tdstCamera *p_stCam )
{
    GLD_tdstViewportAttributes      stViewAttrib;
    GLI_tdstSpecificAttributesFor3D *p_stSpecAttrib3D;
    
    if ( !GLD_bGetViewportAttributes( hDevice, hViewport, &stViewAttrib ) )
        return;
    p_stSpecAttrib3D = (GLI_tdstSpecificAttributesFor3D *)stViewAttrib.p_vSpecificToXD;
    
#ifdef U64
/* 	if ((g_bWeirdWaterEffect || g_bWeirdFireEffect) || ( p_stCam != p_stSpecAttrib3D->p_stCam ) || (bChangedFarClippingSettings)) */
#else
    if ( p_stCam != p_stSpecAttrib3D->p_stCam )
#endif
    {
        p_stSpecAttrib3D->p_stCam = p_stCam;
        GLI_xAdjustCameraToViewport ( hDevice, hViewport, p_stCam );
    }
}                         
  
void GLI_xGetViewportCamera ( GLD_tdhDevice hDevice, GLD_tdhViewport hViewport, GLI_tdstCamera **p_stCam )
{
    GLD_tdstViewportAttributes      stViewAttrib;
    GLI_tdstSpecificAttributesFor3D *p_stSpecAttrib3D;
    
    if ( !GLD_bGetViewportAttributes( hDevice, hViewport, &stViewAttrib ) )
      return ;
    p_stSpecAttrib3D = (GLI_tdstSpecificAttributesFor3D *)stViewAttrib.p_vSpecificToXD;
    (*p_stCam) = p_stSpecAttrib3D->p_stCam ;
}                         
  
 
 
/*
 ----------------------------------------------------------------------------------------
  Description   : Change le z dans le repère de la caméra d'un vertex dont les coordonées
                    sont dans le repère du monde, Tout en conservant les coordonnées x et y
                    à l'écran. 
  Author        : Yann Le Tensorer
  Date          : 07/01/97
 ----------------------------------------------------------------------------------------
 */

#ifndef _FIRE_DEADCODE_U64_ /* Added by RUC 04/06/99 */
void fn_vSetZCamera(GLD_tdhDevice hDev, GLD_tdhViewport hVp, MTH3D_tdstVector *p_stSrcVertex, MTH3D_tdstVector *p_stDstVertex, MTH_tdxReal z)
{
    GLI_tdstSpecificAttributesFor3D     *p_stSpecAttrib3D;
    GLD_tdstViewportAttributes          stViewAttrib;
    POS_tdstCompletePosition            stMatrix, stInvMatrix ;
    GLI_tdstCamera                      *p_stCam;
    MTH3D_tdstVector                    stTmpVertex;
    MTH_tdxReal                         z1;
 
    if ( !GLD_bGetViewportAttributes( hDev, hVp, &stViewAttrib ) ) 
        return;
    p_stSpecAttrib3D = (GLI_tdstSpecificAttributesFor3D *)stViewAttrib.p_vSpecificToXD;
    p_stCam = p_stSpecAttrib3D->p_stCam;
  
  
    /* passe dans le repère de la camera*/
    GLI_xGetCameraMatrix ( p_stCam , &stMatrix );
    POS_fn_vMulMatrixVertex ( &stTmpVertex , &stMatrix , p_stSrcVertex );    
  
    /* z1 est le z du point initial dans le repere de la camera*/
    z1=stTmpVertex.xZ;
    /* z est le nouveau z dans le repere de la camera*/
    stTmpVertex.xZ=z;	
    /* ajuste les coordonnées x et y pour que le point ait la même projection à l'écran*/
    stTmpVertex.xX=z*stTmpVertex.xX/z1;
    stTmpVertex.xY=z*stTmpVertex.xY/z1;
  
    /* Repasse dans le repère du monde*/
    POS_fn_vInvertMatrix ( &stInvMatrix , &stMatrix );  
    POS_fn_vMulMatrixVertex ( p_stDstVertex , &stInvMatrix , &stTmpVertex );    
}
#endif /* _FIRE_DEADCODE_U64_ */ /* Added by RUC 04/06/99 */

 
 
 
 
#ifdef __cplusplus
} /* extern "C" */
#endif
