home *** CD-ROM | disk | FTP | other *** search
- // Copyright (c)1995 Ray Dream, Inc. All Rights Reserved.
- /* $Id: COMBack.cpp 1.4 1997/04/10 00:55:53 damien Exp $ */
-
- ////////////////////////////////////////////////////////////////////////
- // BackgroundShader Example : Sunset //
- //--------------------------------------------------------------------//
- // Implementation of the Sunset Interface //
- ////////////////////////////////////////////////////////////////////////
-
- #include "math.h"
-
- #ifndef __COMBACK__
- #include "COMBACK.h"
- #endif
-
- #ifndef __BACKDLL__
- #include "BackDLL.h"
- #endif
-
- // Failure Handling
- #ifndef __3DCOFAIL__
- #include "3DCoFail.h"
- #endif
-
- #undef INTERFACE
- #define INTERFACE Sunset
-
- // Constructor / Destructor of the C++ Object :
- Sunset::Sunset() {
- fCRef=0; // Reference Counter
- // Data initialisation :
- fData.fSunColor.Mode=0; // The sun color is in RGB Mode
- fData.fSunColor.R =1.0; // Red Sun
- fData.fSunColor.G =0.0;
- fData.fSunColor.B =0.0;
- fData.fSunDiameter =0.5; // diameter of 30' in degree
- fData.fWestDirection=180.0;
-
- fData.fZenithColor.Mode=0;
- fData.fZenithColor.R =0.0;
- fData.fZenithColor.G =0.0;
- fData.fZenithColor.B =0.5;
-
- fData.fWestColor.Mode=0;
- fData.fWestColor.R =1.0;
- fData.fWestColor.G =0.5;
- fData.fWestColor.B =0.0;
-
- fData.fEastColor.Mode=0;
- fData.fEastColor.R =0.0;
- fData.fEastColor.G =0.0;
- fData.fEastColor.B =0.0;
-
- fData.fEarthColor.Mode=0;
- fData.fEarthColor.R =153.0/255.0;
- fData.fEarthColor.G =102.0/255.0;
- fData.fEarthColor.B =51.0/255.0;
-
- ExtensionDataChanged(); // preprocess values
-
- }
-
- Sunset::~Sunset() {
- global_count_Obj--;
- }
-
- // IUnknown Interface :
- HRESULT Sunset::QueryInterface(THIS_ REFIID riid,LPVOID FAR* ppvObj) {
- *ppvObj=NULL;
-
- // The Sunset knows the interfaces of the parent Objects
- if (IsEqualIID(riid, IID_IUnknown))
- *ppvObj=(LPVOID)this;
- else if (IsEqualIID(riid, IID_I3DExBackground))
- *ppvObj=(LPVOID)this;
- else if (IsEqualIID(riid, IID_I3DExDataExchanger))
- *ppvObj=(LPVOID)this;
- else if (IsEqualIID(riid, IID_I3DExtension))
- *ppvObj=(LPVOID)this;
-
- // we must add reference if we return an interface
- if (*ppvObj!=NULL) {
- ((LPUNKNOWN)*ppvObj)->AddRef();
- return NOERROR;
- }
- else {
- return ResultFromScode(E_NOINTERFACE);
- }
- }
-
- ULONG Sunset::AddRef(THIS) {
- return fCRef++;
- }
-
- ULONG Sunset::Release(THIS) {
- ULONG UnreleaseObject=fCRef--;
-
- if (fCRef==0)
- delete this; // No reference left, so destroy the object
-
- return UnreleaseObject;
- // local variable used, because fCRef can be destroyed before.
- }
-
- // I3DExtension methods :
- I3DExtension* Sunset::Clone(THIS) {
- Sunset* theClone = new Sunset;
- if (theClone) {
- theClone->AddRef();
- theClone->fData=fData; // copy the SunsetData
- theClone->fSunLimit=fSunLimit;
- theClone->fWestVector[0]=fWestVector[0];
- theClone->fWestVector[1]=fWestVector[1];
- theClone->fWestVector[2]=fWestVector[2];
- }
- return theClone;
- }
-
- HRESULT Sunset::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
- InitCoFailure(shellUtilities);
- return NOERROR;
- }
-
- // I3DExDataExchanger methods :
- ExtensionDataMap* Sunset::GetExtensionDataMap(THIS) {
- return NULL;
- }
-
- void* Sunset::GetExtensionDataBuffer(THIS) {
- return &fData; // used by the shell to set the new parameters
- }
-
- HRESULT Sunset::ExtensionDataChanged(THIS) {
- NUM3D sun_direction,sun_radius,voidsinus;
-
- // preprocess values
- fWestVector[2]=0.0;
- 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 NOERROR;
- }
-
- HRESULT Sunset::HandleEvent(THIS_ ULONG SourceID) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- short Sunset::GetResID(THIS) {
- return 135; // this is the view ID in the resource file.
- }
-
- // I3DExBackground methods
-
- //Environment background function :
- HRESULT Sunset::GetBackgroundColor(THIS_ VECTOR3D* direction, COLOR3D* resultColor) {
- NUM3D in_sun;
- 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
- *resultColor=fData.fEarthColor;
- }
- else if (in_sun>fSunLimit) { // You look directly at the sun
- *resultColor=fData.fSunColor;
- }
- else if (in_sun>0.0) { // You look in the West Direction
- resultColor->Mode=0;
- resultColor->R =fData.fWestColor.R*in_sun+fData.fZenithColor.R*(1.0-in_sun);
- resultColor->G =fData.fWestColor.G*in_sun+fData.fZenithColor.G*(1.0-in_sun);
- resultColor->B =fData.fWestColor.B*in_sun+fData.fZenithColor.B*(1.0-in_sun);
- }
- else { // You look in the East Direction
- resultColor->Mode=0;
- resultColor->R =-fData.fEastColor.R*in_sun+fData.fZenithColor.R*(1.0+in_sun);
- resultColor->G =-fData.fEastColor.G*in_sun+fData.fZenithColor.G*(1.0+in_sun);
- resultColor->B =-fData.fEastColor.B*in_sun+fData.fZenithColor.B*(1.0+in_sun);
- }
- return NOERROR;
- }
-
-