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

	Gel.cpp

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

	Author:	Jerome Bignon

	Date:	07/07/99

	Revision History

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

****************************************************************************************/
#include "math.h"
#include "Gel.h"


const MCGUID CLSID_GEL(R_CLSID_GEL);


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_GEL) res = new Gel;

	return res;
}

// Constructor:

Gel::Gel()
{
	// Data initialization :
	fData.fNbBranches=5;
}
 
void* Gel::GetExtensionDataBuffer()
{
	return &fData; // The Shell uses this pointer to set the values of the Gel's parameters
}
  
MCCOMErr Gel::ExtensionDataChanged()
{          
	return MC_S_OK;
}

short Gel::GetResID()
{
	return 133; // This is the view ID in the resource file
}
  
// I3DExLightsourceGel methods :
boolean Gel::GetGelValues(const TVector2& gelScreenPosition, TMCColorRGB &result)
{
	real alpha;
	real graylevel;
	alpha=atan2(gelScreenPosition[1],gelScreenPosition[0]);
	alpha*=fData.fNbBranches;

	while (alpha<0.0)			alpha += kRealTwoPI; // Modulo 2 pi
	while (alpha>kRealTwoPI)	alpha -= kRealTwoPI; // Modulo 2 pi

	if (alpha<kRealHalfPI)
	{
		graylevel=1.0-alpha/kRealHalfPI;
	}
	else if (alpha>kRealPI*1.5)
	{
		graylevel=(alpha-kRealPI*1.5)/kRealHalfPI;
	}
	else
	{
		graylevel=0.0; 
	}

	result.R=graylevel;
	result.G=graylevel;
	result.B=graylevel;

	return true;
}
