home *** CD-ROM | disk | FTP | other *** search
/ Ray Dream Studio 5 / Ray Dream.iso / pc / DreamSDK / Windows / SAMPLES / BACKGRND / BACK / COMBACK.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-11  |  5.4 KB  |  183 lines

  1. // Copyright (c)1995 Ray Dream, Inc. All Rights Reserved.
  2. /* $Id: COMBack.cpp 1.4 1997/04/10 00:55:53 damien Exp $ */
  3.  
  4. ////////////////////////////////////////////////////////////////////////
  5. //   BackgroundShader Example : Sunset                               //
  6. //--------------------------------------------------------------------//
  7. //   Implementation of the Sunset Interface                          //
  8. //////////////////////////////////////////////////////////////////////// 
  9.  
  10. #include "math.h"
  11.  
  12. #ifndef __COMBACK__
  13. #include "COMBACK.h"
  14. #endif
  15.  
  16. #ifndef __BACKDLL__
  17. #include "BackDLL.h"
  18. #endif
  19.  
  20. // Failure Handling
  21. #ifndef __3DCOFAIL__
  22. #include "3DCoFail.h"
  23. #endif
  24.  
  25. #undef INTERFACE
  26. #define INTERFACE Sunset
  27.  
  28. // Constructor / Destructor of the C++ Object :
  29. Sunset::Sunset() {
  30.   fCRef=0; // Reference Counter
  31.   // Data initialisation :
  32.   fData.fSunColor.Mode=0;                // The sun color is in RGB Mode
  33.   fData.fSunColor.R   =1.0;     // Red Sun
  34.   fData.fSunColor.G   =0.0;
  35.   fData.fSunColor.B   =0.0;
  36.   fData.fSunDiameter  =0.5; // diameter of 30' in degree
  37.   fData.fWestDirection=180.0;
  38.   
  39.   fData.fZenithColor.Mode=0;
  40.   fData.fZenithColor.R   =0.0;
  41.   fData.fZenithColor.G   =0.0;
  42.   fData.fZenithColor.B   =0.5;
  43.   
  44.   fData.fWestColor.Mode=0;
  45.   fData.fWestColor.R   =1.0;
  46.   fData.fWestColor.G   =0.5;
  47.   fData.fWestColor.B   =0.0;
  48.   
  49.   fData.fEastColor.Mode=0;
  50.   fData.fEastColor.R   =0.0;
  51.   fData.fEastColor.G   =0.0;
  52.   fData.fEastColor.B   =0.0;
  53.   
  54.   fData.fEarthColor.Mode=0;
  55.   fData.fEarthColor.R   =153.0/255.0;
  56.   fData.fEarthColor.G   =102.0/255.0;
  57.   fData.fEarthColor.B   =51.0/255.0;
  58.  
  59.   ExtensionDataChanged();  // preprocess values
  60.   
  61.   }
  62.   
  63. Sunset::~Sunset() {
  64.   global_count_Obj--; 
  65.   }
  66.   
  67. // IUnknown Interface :
  68. HRESULT Sunset::QueryInterface(THIS_ REFIID riid,LPVOID FAR* ppvObj) {
  69.   *ppvObj=NULL;
  70.   
  71.   // The Sunset knows the interfaces of the parent Objects
  72.   if (IsEqualIID(riid, IID_IUnknown))
  73.     *ppvObj=(LPVOID)this;
  74.   else if (IsEqualIID(riid, IID_I3DExBackground))
  75.     *ppvObj=(LPVOID)this;
  76.   else if (IsEqualIID(riid, IID_I3DExDataExchanger))
  77.     *ppvObj=(LPVOID)this;
  78.   else if (IsEqualIID(riid, IID_I3DExtension))
  79.     *ppvObj=(LPVOID)this;
  80.     
  81.   // we must add reference if we return an interface
  82.   if (*ppvObj!=NULL) {
  83.     ((LPUNKNOWN)*ppvObj)->AddRef();
  84.     return NOERROR;
  85.     }
  86.   else {
  87.     return ResultFromScode(E_NOINTERFACE);
  88.     }
  89.   }
  90.  
  91. ULONG Sunset::AddRef(THIS) {
  92.   return fCRef++;
  93.   }
  94.   
  95. ULONG Sunset::Release(THIS) {
  96.   ULONG UnreleaseObject=fCRef--;
  97.   
  98.   if (fCRef==0)
  99.      delete this; // No reference left, so destroy the object
  100.   
  101.   return UnreleaseObject;
  102.   // local variable used, because fCRef can be destroyed before.
  103.   }
  104.   
  105. // I3DExtension methods :
  106. I3DExtension* Sunset::Clone(THIS) {
  107.   Sunset* theClone = new Sunset;
  108.   if (theClone) {
  109.     theClone->AddRef();
  110.     theClone->fData=fData; // copy the SunsetData
  111.     theClone->fSunLimit=fSunLimit;
  112.     theClone->fWestVector[0]=fWestVector[0];
  113.     theClone->fWestVector[1]=fWestVector[1];
  114.     theClone->fWestVector[2]=fWestVector[2];
  115.     }                               
  116.   return theClone;
  117.   }   
  118.  
  119. HRESULT Sunset::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
  120.   InitCoFailure(shellUtilities);
  121.   return NOERROR;
  122.   }
  123.  
  124. // I3DExDataExchanger methods :
  125. ExtensionDataMap* Sunset::GetExtensionDataMap(THIS) {
  126.   return NULL;
  127.   }               
  128.   
  129. void* Sunset::GetExtensionDataBuffer(THIS) {
  130.   return &fData; // used by the shell to set the new parameters
  131.   }
  132.   
  133. HRESULT Sunset::ExtensionDataChanged(THIS) {
  134.   NUM3D sun_direction,sun_radius,voidsinus;
  135.  
  136.   // preprocess values
  137.   fWestVector[2]=0.0;
  138.   sun_direction=-fData.fWestDirection;
  139.   fWestVector[0]=sin(sun_direction);
  140.     fWestVector[1]=cos(sun_direction); //QuickSinCos(sun_direction,fWestVector[0],fWestVector[1]);
  141.   sun_radius=fData.fSunDiameter/2;
  142.   voidsinus=sin(sun_radius);
  143.     fSunLimit=cos(sun_radius); //QuickSinCos(sun_radius,voidsinus,fSunLimit);
  144.  
  145.   return NOERROR;
  146.   }
  147.  
  148. HRESULT Sunset::HandleEvent(THIS_ ULONG SourceID) {
  149.   return ResultFromScode(E_NOTIMPL);
  150.   }
  151.  
  152. short Sunset::GetResID(THIS) {
  153.   return 135; // this is the view ID in the resource file.
  154.   }
  155.   
  156. // I3DExBackground methods
  157.  
  158. //Environment background function :
  159. HRESULT Sunset::GetBackgroundColor(THIS_ VECTOR3D* direction, COLOR3D* resultColor) {
  160.   NUM3D in_sun;
  161.   in_sun=(*direction)[0]*fWestVector[0] + (*direction)[1]*fWestVector[1] + (*direction)[2]*fWestVector[2];
  162.   if ((*direction)[2]<0.0) { // You look the earth and not the sky
  163.     *resultColor=fData.fEarthColor;
  164.     }
  165.   else if (in_sun>fSunLimit) { // You look directly at the sun
  166.     *resultColor=fData.fSunColor;
  167.     }
  168.   else if (in_sun>0.0) { // You look in the West Direction
  169.     resultColor->Mode=0;
  170.     resultColor->R   =fData.fWestColor.R*in_sun+fData.fZenithColor.R*(1.0-in_sun);
  171.     resultColor->G   =fData.fWestColor.G*in_sun+fData.fZenithColor.G*(1.0-in_sun);
  172.     resultColor->B   =fData.fWestColor.B*in_sun+fData.fZenithColor.B*(1.0-in_sun);
  173.     }
  174.   else { // You look in the East Direction
  175.     resultColor->Mode=0;
  176.     resultColor->R   =-fData.fEastColor.R*in_sun+fData.fZenithColor.R*(1.0+in_sun);
  177.     resultColor->G   =-fData.fEastColor.G*in_sun+fData.fZenithColor.G*(1.0+in_sun);
  178.     resultColor->B   =-fData.fEastColor.B*in_sun+fData.fZenithColor.B*(1.0+in_sun);
  179.     }
  180.   return NOERROR;
  181.   }
  182.   
  183.