/****************************************************************************************

	COMBack.cpp

	Copyright (c)1990 - 1999 MetaCreations. All rights reserved.

	Author:	Jerome Bignon

	Date:	06/21/99

	Revision History

	when		who			what & why
	=========== ===========	=============================================================
	08/20/99	cph			Put in Extension3DInit() and Extension3DCleanup() methods

****************************************************************************************/

#include "math.h"
#include "BACK.h"


const MCGUID CLSID_Back(R_CLSID_Back);

void Extension3DInit(IMCUnknown* utilities)
{
	// Perform your dll initialization here
}

void Extension3DCleanup()
{
	// Perform any nec clean-up here
}
TBasicUnknown* MakeCOMObject(const MCCLSID& classId)	// This method instanciate
{														// the object COMBack
	TBasicUnknown* res = NULL;

	if (classId == CLSID_Back) res = new Back;

	return res;
}

// Constructor / Destructor of the C++ Object :
Back::Back()
{
	// Data initialisation :
//	fData.fSunColor.Mode=0;     // The sun color is in RGB Mode
	fData.fSunColor.red   =1.0f;	// Red Sun
	fData.fSunColor.green   =0.0f;
	fData.fSunColor.blue   =0.0f;
	fData.fSunDiameter  =0.5f;	// diameter of 30' in degree
	fData.fWestDirection=180.0f;

//	fData.fZenithColor.Mode=0;
	fData.fZenithColor.red   =0.0f;
	fData.fZenithColor.green   =0.0f;
	fData.fZenithColor.blue   =0.5f;

//	fData.fWestColor.Mode=0;
	fData.fWestColor.red   =1.0f;
	fData.fWestColor.green   =0.5f;
	fData.fWestColor.blue   =0.0f;

//	fData.fEastColor.Mode=0;
	fData.fEastColor.red   =0.0f;
	fData.fEastColor.green   =0.0f;
	fData.fEastColor.blue   =0.0f;

//	fData.fEarthColor.Mode=0;
	fData.fEarthColor.red   =153.0f/255.0f;
	fData.fEarthColor.green   =102.0f/255.0f;
	fData.fEarthColor.blue   =51.0f/255.0f;

	ExtensionDataChanged();		// preprocess values 
}
   
// IExDataExchanger methods :
  
void* Back::GetExtensionDataBuffer()
{
	return &fData; // used by the shell to set the new parameters
}
  
MCCOMErr Back::ExtensionDataChanged()
{
	real sun_direction,sun_radius,voidsinus;

	// preprocess values
	fWestVector[2]=0.0f;
	sun_direction=-fData.fWestDirection;
	fWestVector[0]=sin(sun_direction);
	fWestVector[1]=cos(sun_direction);	//QuickSinCos(sun_direction,fWestVector[0],fWestVector[1]);
	sun_radius=fData.fSunDiameter/2;
	voidsinus=sin(sun_radius);
	fSunLimit=cos(sun_radius);			//QuickSinCos(sun_radius,voidsinus,fSunLimit);

	return MC_S_OK;
}

int16 Back::GetResID()
{
	return 135; // this is the view ID in the resource file.
}
  
//Environment background function :
MCCOMErr Back::GetBackgroundColor(TMCColorRGB& color,boolean& fullAreaDone,const TVector3& direction)
{
	real in_sun;

	fullAreaDone = true;
	in_sun=(direction)[0]*fWestVector[0] + (direction)[1]*fWestVector[1] + (direction)[2]*fWestVector[2];
	if ((direction)[2]<0.0)
	{ // You look the earth and not the sky
		color=fData.fEarthColor;
	}
	else if (in_sun>fSunLimit)
	{ // You look directly at the sun
		color=fData.fSunColor;
	}
	else if (in_sun>0.0)
	{ // You look in the West Direction
		color.R   =fData.fWestColor.red*in_sun+fData.fZenithColor.red*(1.0-in_sun);
		color.G   =fData.fWestColor.green*in_sun+fData.fZenithColor.green*(1.0-in_sun);
		color.B   =fData.fWestColor.blue*in_sun+fData.fZenithColor.blue*(1.0-in_sun);
	}
	else
	{ // You look in the East Direction
		color.R   =-fData.fEastColor.red*in_sun+fData.fZenithColor.red*(1.0+in_sun);
		color.G   =-fData.fEastColor.green*in_sun+fData.fZenithColor.green*(1.0+in_sun);
		color.B   =-fData.fEastColor.blue*in_sun+fData.fZenithColor.blue*(1.0+in_sun);
	}
	return MC_S_OK;
}
  
