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

	Rainbow.cpp

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

	Author: Jerome Bignon

	Date: 04/29/99

	Revision history

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

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

#include "math.h"
#include "Rainbow.h"

const MCGUID CLSID_Rainbow(R_CLSID_Rainbow);


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 Rainbow
	TBasicUnknown* res = NULL;

	if (classId == CLSID_Rainbow) res = new Rainbow;

	return res;
}


Rainbow::Rainbow()		// We just initialize the Rainbow object
{
	RainbowPublicData.fIntensity = 100;			// 100% of intensity
	RainbowPublicData.fModeLocalOrGlobal = 1;	// Local as default ( 0 : Global )
}
	
void* Rainbow::GetExtensionDataBuffer()
{
	return ((void*) &(RainbowPublicData));
}

boolean Rainbow::IsEqualTo(I3DExShader* aShader)		// Use it to compare two Rainbow
{
	return ((RainbowPublicData.fIntensity == ((Rainbow*)aShader)->RainbowPublicData.fIntensity)
		   &&(RainbowPublicData.fModeLocalOrGlobal == ((Rainbow*)aShader)->RainbowPublicData.fModeLocalOrGlobal));
}

MCCOMErr Rainbow::GetShadingFlags(ShadingFlags& theFlags)
{
	theFlags.fNeedsUV = false;
	theFlags.fConstantChannelsMask = kNoChannel;

	return MC_S_OK;
}

EShaderOutput Rainbow::GetImplementedOutput()
{
	return kUsesGetColor ;	// Tell the shell that the Rainbow Shader is a Color Shader
}

real Rainbow::GetColor(TMCColorRGBA& result,boolean& fullArea,ShadingIn& shadingIn)
{
	real temp = RainbowPublicData.fIntensity;
	temp/=100.0;
	
	if (RainbowPublicData.fModeLocalOrGlobal==1)
	{
		result.red		= 0.5 * (shadingIn.fNormalLoc.x * temp + 1);
		result.green	= 0.5 * (shadingIn.fNormalLoc.y * temp + 1);
		result.blue		= 0.5 * (shadingIn.fNormalLoc.z * temp + 1);
  	}
	else
	{
		result.red		= 0.5 * (shadingIn.fGNormal.x * temp + 1);
		result.green	= 0.5 * (shadingIn.fGNormal.y * temp + 1);
		result.blue		= 0.5 * (shadingIn.fGNormal.z * temp + 1);
    }
	fullArea = true;

	return 1.0f;
}
