/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///																																  ///
///		FICHIER :	ZooSound.cpp			    																				  ///
///																																  ///
///		NATURE	:	Define the Sound properties of the scene                           											  ///
///																																  ///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////








#include	"..\Scene Graph\ZooSound.h"


//	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//  Voir pour la fonction de copy de H3d pour un ZSound !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//	!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///		ZSound Class
///
///		- Classe des sons 3D
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
extern mmachine	mm ;
extern int AsSnd ;

	
///////////////////////////////////////////////////////////////////////////
/// Constructor															///
///////////////////////////////////////////////////////////////////////////

ZSound::ZSound(int s3d, int flag) : ZNodeGraph(s3d)
{
	type = SND_TYPE_ID ;

	this->flag = flag ;

	pWSound = NULL ;
}


 
///////////////////////////////////////////////////////////////////////////
/// Destructor															///
///////////////////////////////////////////////////////////////////////////
ZSound::~ZSound()
{
	if(pWSound)	
	{
		if( flag & AS_SND_LINKED_TO_H3D )  OBJdelTH( mm, AsSnd, (int)pWSound );
		pWSound->pZSound = NULL;
	}
	ZNodeGraph::~ZNodeGraph();
}



///////////////////////////////////////////////////////////////////////////
// Update parameters of the 3D sound									///
///////////////////////////////////////////////////////////////////////////
void ZSound::update( ZSound* pZSoundListenerActif )
{
	// position dans le repère global = 'worldPos'

	// position par rapport au père = 'PRS->pos'


	if( pZSoundListenerActif == this  ) 
	{	
		ZVector4	direcFRONT;
		direcFRONT.SetCoord(0.0f, 0.0f, -1.0f, 0.0f);
		direcFRONT *= worldMat;
		
		ZVector4	direcUP;
		direcUP.SetCoord(0.0f, 1.0f, 0.0f, 0.0f);
		direcUP *= worldMat;
		pWSound->pHListener->m_pListener->SetPosition(worldPos.x, worldPos.y, -worldPos.z, DS3D_DEFERRED ) ;
		pWSound->pHListener->m_pListener->SetOrientation( direcFRONT.x, direcFRONT.y, -direcFRONT.z, direcUP.x, direcUP.y, -direcUP.z, DS3D_DEFERRED ) ;
		
	}
	else if( pWSound->pHSound )
	{
		ZVector4	direcFRONT;
		direcFRONT.SetCoord(0.0f, 0.0f, -1.0f, 0.0f);
		direcFRONT *= worldMat;
		pWSound->pHSound->m_p3DBuffer->SetPosition(worldPos.x, worldPos.y, -worldPos.z, DS3D_DEFERRED ) ;
		pWSound->pHSound->m_p3DBuffer->SetConeOrientation( direcFRONT.x, direcFRONT.y, -direcFRONT.z, DS3D_DEFERRED ) ;
	}
	pZSoundListenerActif->pWSound->pHListener->m_pListener->CommitDeferredSettings() ;
}


