home *** CD-ROM | disk | FTP | other *** search
/ Ray Dream Studio 5 / Ray Dream.iso / pc / DreamSDK / Windows / SAMPLES / POSTR / SAND / COMSAND.CPP next >
Encoding:
C/C++ Source or Header  |  1997-07-11  |  6.0 KB  |  219 lines

  1. // Copyright (c)1996 Ray Dream, Inc. All Rights Reserved.
  2. /* $Id: COMSand.cpp 1.13 1997/07/07 16:58:03 damien Exp $ */
  3.  
  4. ////////////////////////////////////////////////////////////////////////
  5. //   Post renderer Example : Sand                                     //
  6. //--------------------------------------------------------------------//
  7. //   Implementation of the Sand Interface                            //
  8. //////////////////////////////////////////////////////////////////////// 
  9.  
  10. #ifndef __COMSand__
  11. #include "COMSand.h"
  12. #endif
  13.  
  14. #ifndef __SandDLL__
  15. #include "SandDLL.h"
  16. #endif
  17.  
  18. // Failure Handling
  19. #ifndef __3DCOFAIL__
  20. #include "3DCoFail.h"
  21. #endif
  22.  
  23. #ifndef __3DBSTYPE__
  24. #include "3DBSType.h"
  25. #endif
  26.  
  27. #ifndef __I3DSHUTI__
  28. #include "I3DShuti.h"
  29. #endif
  30.  
  31. #include "StdLib.h"
  32.  
  33. #undef INTERFACE
  34. #define INTERFACE Sand
  35.  
  36. IShUtilities* sandShellUtilities;
  37.  
  38. long RandomColor(long value) {
  39.     if ((rand()*0xFE/RAND_MAX) >= value) {
  40.         return 0;
  41.         }
  42.     else {
  43.         return 0xFF;
  44.         }
  45.     }
  46.  
  47. // Constructor / Destructor of the C++ Object :
  48. Sand::Sand() {
  49.   fCRef=0; // Reference Counter
  50.   }
  51.   
  52. Sand::~Sand() {
  53.   global_count_Obj--; 
  54.   }
  55.   
  56. // IUnknown Interface :
  57. HRESULT Sand::QueryInterface(THIS_ REFIID riid,LPVOID* ppvObj) {
  58.   *ppvObj=NULL;
  59.   
  60.   // The Sand knows the interfaces of the parent Objects
  61.   if (IsEqualIID(riid, IID_IUnknown))
  62.     *ppvObj=(LPVOID)this;
  63.   else if (IsEqualIID(riid, IID_I3DExPostRenderer))
  64.     *ppvObj=(LPVOID)this;
  65.   else if (IsEqualIID(riid, IID_I3DExDataExchanger))
  66.     *ppvObj=(LPVOID)this;
  67.   else if (IsEqualIID(riid, IID_I3DExtension))
  68.     *ppvObj=(LPVOID)this;
  69.     
  70.   // we must add reference if we return an interface
  71.   if (*ppvObj!=NULL) {
  72.     ((LPUNKNOWN)*ppvObj)->AddRef();
  73.     return NOERROR;
  74.     }
  75.   else {
  76.     return ResultFromScode(E_NOINTERFACE);
  77.     }
  78.   }
  79.  
  80. ULONG Sand::AddRef(THIS) {
  81.   return fCRef++;
  82.   }
  83.   
  84. ULONG Sand::Release(THIS) {
  85.   ULONG UnreleaseObject=fCRef--;
  86.   
  87.   if (fCRef==0)
  88.      delete this; // No reference left, so destroy the object
  89.   
  90.   return UnreleaseObject;
  91.   // local variable used, because fCRef can be destroyed before.
  92.   }
  93.   
  94. // I3DExtension methods :
  95. I3DExtension* Sand::Clone(THIS) {
  96.   Sand* theClone = new Sand;
  97.   if (theClone) {
  98.     theClone->AddRef();
  99.     }                               
  100.   return theClone;
  101.   }   
  102.  
  103. HRESULT Sand::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
  104.     // not called anymore
  105.   return NOERROR;
  106.   }
  107.  
  108. // I3DExDataExchanger methods :
  109. ExtensionDataMap* Sand::GetExtensionDataMap(THIS) {
  110.   return NULL;
  111.   }
  112.  
  113. void* Sand::GetExtensionDataBuffer(THIS) {
  114.   return 0; // used by the shell to set the new parameters
  115.   }
  116.   
  117. HRESULT Sand::ExtensionDataChanged(THIS) {
  118.   return NOERROR;
  119.   }
  120.  
  121. HRESULT Sand::HandleEvent(THIS_ ULONG SourceID) {
  122.   return ResultFromScode(E_NOTIMPL);
  123.   }
  124.  
  125. short Sand::GetResID(THIS) {
  126.   return 128; // this is the view ID in the resource file.
  127.   }
  128.   
  129. // I3DExPostRenderer methods :
  130. void Sand::Filter (THIS_ I3DShScene* theScene,IShRasterOffscreen* theImage,IUnknown** theBuffers,
  131.                                         IShGraphicDevice* gd, /*TExternalRenderer*/void* renderer, void* renderHelper) {
  132.     ULONG depth;
  133.     RECT3D BRect;
  134.     BRect.top = 0;
  135.     BRect.left = 0;
  136.     BRect.right = 32000;
  137.     BRect.bottom = 32000;
  138.     
  139.     BufferChunk* aChunk=0;
  140.     LRECT3D* aClip=new LRECT3D;
  141.     LRECT3D* cursorClip=new LRECT3D;
  142.     RECT3D* shortRect=new RECT3D;
  143.     ULONG chunkH, chunkV, rowBytes;
  144.  
  145.     theImage->GetChunkInfo(&chunkH, &chunkV, &rowBytes, &depth);
  146.     if (depth == 32) {
  147.         // creating the cursor
  148.         IShRasterOffscreen* theCursor;
  149.         sandShellUtilities->CoCreateInstance(CLSID_StandardRasterOffscreen,0,0,IID_IShRasterOffscreen,(void**)&theCursor);
  150.         theCursor->InitOffscreen((short)chunkH, (short)chunkV, (short)depth);
  151.         IEnumChunk* FirstChunk = theCursor->EnumChunks(&BRect);
  152.         FirstChunk->Next(1,&aChunk,0);
  153.         FirstChunk->Release();
  154.         theCursor->GetChunkRect(aChunk, shortRect);
  155.         cursorClip->top=shortRect->top;
  156.         cursorClip->left=shortRect->left;
  157.         cursorClip->bottom=shortRect->bottom;
  158.         cursorClip->right=shortRect->right;
  159.         theCursor->LockChunk(aChunk);
  160.         long* PCurChunk;
  161.         PCurChunk = (long*)(theCursor->GetChunkData(aChunk));
  162.         ULONG i,j;
  163.         for (i=0; i<chunkV; i++) {
  164.             for (j=0; j<chunkH; j++) {
  165.                 *(PCurChunk++)=(((i^j)&8)==0?0x0000000:0x0FFFF00); // square black or yellow each 8 pixels
  166.                 }
  167.             }
  168.         theCursor->UnlockChunk(aChunk);
  169.  
  170.         IEnumChunk* iter = theImage->EnumChunks(&BRect);
  171.         for (; (iter->Next(1,&aChunk,0)) == NO_ERROR; ) {
  172.  
  173.             theImage->GetChunkRect(aChunk, shortRect);
  174.             aClip->top=shortRect->top;
  175.             aClip->left=shortRect->left;
  176.             aClip->bottom=shortRect->bottom;
  177.             aClip->right=shortRect->right;
  178.             shortRect->top=cursorClip->top;
  179.             shortRect->left=cursorClip->left;
  180.             shortRect->bottom=cursorClip->bottom;
  181.             shortRect->right=cursorClip->right;
  182.             gd->DrawOffscreen(theCursor, *shortRect, *aClip);
  183.             theImage->LockChunk(aChunk);
  184.             long* data = (long*)theImage->GetChunkData(aChunk);
  185.             for (short vv = aClip->top; vv< aClip->top + chunkV; vv++)    {
  186.                 theImage->SetPosition(aClip->left, vv);
  187.                 for (short hh = aClip->left; hh< aClip->left + chunkH; hh++)    {
  188.                     long color;
  189.                     theImage->Get(&color);
  190.                     (*data) = RandomColor(color & 0xFF) + (RandomColor((color>>8) & 0xFF)<<8) + (RandomColor((color>>16) & 0xFF)<<16);
  191.  
  192.                     theImage->GoRight();
  193.                     data++;
  194.                     } // for hh
  195.                 } // for vv
  196.             theImage->UnlockChunk(aChunk);
  197.             shortRect->top=aClip->top;
  198.             shortRect->left=aClip->left;
  199.             shortRect->bottom=aClip->bottom;
  200.             shortRect->right=aClip->right;
  201.             gd->DrawOffscreen(theImage, *shortRect, *aClip);
  202.  
  203.             } // for iter
  204.         iter->Release();
  205.         theCursor->Release();
  206.         } // if depth
  207.  
  208.     }
  209.  
  210. enum {kDistance=1, kPositionX=2, kPositionY=4, kPositionZ=8,
  211.             kNormalX=0x10, kNormalY=0x20, kNormalZ=0x40,
  212.             kAlpha=0x80, kIndex=0x100, kSurfaceU=0x200, kSurfaceV=0x400};
  213. enum {kNumberOfGBuffers=20};
  214.  
  215. void Sand::GetBufferNeeds (THIS_ /*RenderFilterNeeds&*/long* needs, /*TExternalRenderer*/void* renderer) {
  216.     *needs = 0;
  217.     }
  218.  
  219.