home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Third Party SDKs / ATI RAVE SDK / Samples / QD3D Tests / BoxTex (ATI) / src / BoxTexShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-07  |  14.7 KB  |  592 lines  |  [TEXT/MPCC]

  1. //
  2. // This is box, the QuickDraw 3D starter program.  Written for the
  3. // Quickdraw 3D sample code
  4. //
  5. // This file contains utility routines for QuickDraw 3d sample code.
  6. // This app shows how to apply a texture shader to an object.  Bear in
  7. // mind that any object that you wish to texture map needs to have
  8. // UV parameters applied.
  9. //
  10. // Nick Thompson, AppleLink: DEVSUPPORT (devsupport@applelink.apple.com)
  11. //
  12. // ©1994-5 Apple Computer Inc., All Rights Reserved
  13.  
  14. // system headers
  15. #include <Menus.h>
  16. #include <Devices.h>
  17. #include <Events.h>
  18. #include <Dialogs.h>
  19. #include <DiskInit.h>
  20. #include <Fonts.h>
  21. #include <Menus.h>
  22. #include <PictUtils.h>
  23. #include <QDOffScreen.h>
  24. #include <QuickDraw.h>
  25. #include <SegLoad.h>
  26. #include <StandardFile.h>
  27. #include <TextEdit.h>
  28.  
  29. // for QuickDraw 3D
  30. #include "QD3D.h"
  31. #include "QD3DMath.h"
  32. #include "QD3DDrawContext.h"
  33. #include "QD3DShader.h"
  34. #include "QD3DTransform.h"
  35. #include "QD3DGroup.h"
  36.  
  37.  
  38. #include "BoxTexShell.h"
  39. #include "BoxTex3DSupport.h"
  40. #include "Textures.h"
  41.  
  42. #define ATI_ACCESS_RAVE_CONTEXT            1
  43. #if ATI_ACCESS_RAVE_CONTEXT
  44.   /*
  45.    * headers
  46.    */
  47.   #include <Timer.h>
  48.   #include <string.h>
  49.   #include "ATIRave.h"
  50.  
  51.   /*
  52.    * defines
  53.    */
  54.   #define MILLIONTHS            0.000001F
  55.   #define MAX( a, b )            ( ((a) > (b)) ? (a) : (b) )
  56.  
  57.   /*
  58.    * prototypes
  59.    */
  60.   void             ATI_SetRaveState( GDHandle    GDevice, TQ3ViewObject viewObject );
  61.   TQAEngine     *ATI_FindRaveEngine( TQADevice *device );
  62.   GDHandle         ATI_FindWindowDevice( WindowPtr pWin );
  63.   unsigned long MicrosecondCount( void );
  64.   void             DisplayString( int x, int y, char *str );
  65.   void             callBack( const TQADrawContext    *, const TQADevice *, const TQARect *, void    *);
  66.  
  67.   /*
  68.    * global variables
  69.    */  
  70.   unsigned long    start, end;
  71.   float            seconds;
  72.   unsigned long    count = 0;
  73.   char             str[100];
  74.   RGBColor        BLACK = { 0, 0, 0 };
  75.   Boolean         filter = false;
  76.   
  77.   TQANoticeMethod gNotice;
  78. #endif
  79.  
  80. //-------------------------------------------------------------------------------------------
  81. // function prototypes
  82.  
  83. static void         InitToolbox( void ) ;
  84. static void         MainEventLoop( void ) ;
  85. static void         HandleKeyPress(EventRecord *event) ;
  86. static void         HandleOSEvent(EventRecord *event) ;
  87. void InitDocumentData( DocumentPtr theDocument ) ;
  88. TQ3Status DocumentDraw3DData( DocumentPtr theDocument ) ;
  89. void DisposeDocumentData( DocumentPtr theDocument) ;
  90.  
  91. //-------------------------------------------------------------------------------------------
  92. //
  93.  
  94. Boolean         gQuitFlag         = false ;
  95. WindowPtr        gMainWindow        = nil ;
  96. DocumentRec        gDocument ;
  97.  
  98. //-------------------------------------------------------------------------------------------
  99. // main()
  100. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  101. // and enter the main event loop.  On exit from the main event loop, we want to call
  102. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  103.  
  104. void main(void)
  105. {
  106.     TQ3Status    myStatus;
  107.     Rect        rBounds = { 50, 50, 450, 450 } ;
  108.     Str255        title = "\pSpinning Box - click to toggle filtering" ;
  109.  
  110.     InitToolbox() ;
  111.     
  112.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  113.     myStatus = Q3Initialize();
  114.     if ( myStatus == kQ3Failure )
  115.         DebugStr("\pErInitialize returned failure.");            
  116.  
  117.     // set up our globals
  118.     gQuitFlag = false ;
  119.     gMainWindow = NewCWindow(nil,&rBounds,title,true,noGrowDocProc,(WindowPtr)-1,true,nil); ;
  120.  
  121.     InitDocumentData( &gDocument ) ;
  122.     
  123.     MainEventLoop();
  124.     
  125.     DisposeDocumentData( &gDocument ) ;
  126.     
  127.     //    Close our connection to the QuickDraw 3D library
  128.     myStatus = Q3Exit();
  129.     if( myStatus == kQ3Failure )
  130.     {
  131.         //DebugStr("\pErExit returned failure.");
  132.     }    
  133. }
  134.  
  135. //-------------------------------------------------------------------------------------------
  136. //
  137.  
  138. void InitDocumentData( DocumentPtr theDocument ) 
  139. {
  140.     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  141.     
  142.     theDocument->fGroupScale = 1;                
  143.     theDocument->fGroupCenter = myOrigin ;            
  144.  
  145.     // sets up the 3d data for the scene
  146.     // Create view for QuickDraw 3D.
  147.     theDocument->fView = MyNewView( (WindowPtr)gMainWindow ) ;
  148.  
  149.     // the main display group:
  150.     theDocument->fModel = MyNewModel() ;
  151.     
  152.     // the drawing styles:
  153.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  154.     theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleRemove ) ;
  155.     theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  156.  
  157.     // set the rotation matrix the identity matrix
  158.     Q3Matrix4x4_SetIdentity(&theDocument->fRotation);
  159.  
  160.     AdjustCamera(    &gDocument, 
  161.                     (gMainWindow->portRect.right - gMainWindow->portRect.left),
  162.                     (gMainWindow->portRect.bottom - gMainWindow->portRect.top) ) ;
  163.  
  164.   #if ATI_ACCESS_RAVE_CONTEXT
  165.     ATI_SetRaveState( ATI_FindWindowDevice(gMainWindow), theDocument->fView );
  166.   #endif            
  167. }
  168.  
  169. void DisposeDocumentData( DocumentPtr theDocument)
  170. {
  171.     Q3Object_Dispose(theDocument->fView) ;                // the view for the scene
  172.     Q3Object_Dispose(theDocument->fModel) ;                // object in the scene being modelled
  173.     Q3Object_Dispose(theDocument->fInterpolation) ;        // interpolation style used when rendering
  174.     Q3Object_Dispose(theDocument->fBackFacing) ;        // whether to draw shapes that face away from the camera
  175.     Q3Object_Dispose(theDocument->fFillStyle) ;            // whether drawn as solid filled object or decomposed to components
  176.  
  177. }
  178.  
  179. //-----------------------------------------------------------------------------
  180. // 
  181.  
  182. TQ3Status DocumentDraw3DData( DocumentPtr theDocument )
  183. {    
  184.     Q3View_StartRendering(theDocument->fView );
  185.     do {
  186.         SubmitScene( theDocument ) ;
  187.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  188.     return kQ3Success ;
  189. }
  190.  
  191. //-------------------------------------------------------------------------------------------
  192. //
  193.  
  194. short HiWrd(long aLong)
  195. {
  196.     return    (((aLong) >> 16) & 0xFFFF) ;
  197. }
  198.  
  199. //-------------------------------------------------------------------------------------------
  200. //
  201.  
  202. short LoWrd(long aLong)
  203. {
  204.     return    ((aLong) & 0xFFFF) ;
  205.  
  206. }
  207.  
  208. //-------------------------------------------------------------------------------------------
  209. //
  210.  
  211. void InitToolbox()
  212. {
  213.     Handle        menuBar = nil;
  214.  
  215.     MaxApplZone() ;
  216.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  217.     
  218.     InitGraf( &qd.thePort );
  219.     InitFonts();
  220.     InitWindows();
  221.     InitCursor();
  222.  
  223.     FlushEvents( everyEvent, 0 ) ;
  224.     // initialize application globals
  225.     
  226.     gQuitFlag = false;
  227.     
  228. }
  229.  
  230.  
  231. //-------------------------------------------------------------------------------------------
  232. //
  233. void MainEventLoop()
  234. {
  235.     EventRecord     event;
  236.     WindowPtr       window;
  237.     short           thePart;
  238.     Rect            screenRect, updateRect;
  239.     Point            aPoint = {100, 100};
  240.  
  241.   #if ATI_ACCESS_RAVE_CONTEXT    
  242.     count = 0;
  243.     start = MicrosecondCount();
  244.   #endif
  245.       
  246.     while( !gQuitFlag )
  247.     {
  248.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  249.         {
  250.  
  251.             switch (event.what) {
  252.                 case mouseDown:
  253.                 
  254.                     thePart = FindWindow( event.where, &window );
  255.                     
  256.                     switch( thePart ) {
  257.                         case inMenuBar: 
  258.                             break;
  259.                         
  260.                         case inDrag:
  261.                     
  262.                             screenRect = (**GetGrayRgn()).rgnBBox;
  263.                             DragWindow( window, event.where, &screenRect );
  264.  
  265.                           #if ATI_ACCESS_RAVE_CONTEXT
  266.                             ATI_SetRaveState( ATI_FindWindowDevice(window), gDocument.fView );        
  267.                             count = 0;
  268.                             start = MicrosecondCount();
  269.                           #endif
  270.                             break ;
  271.                     
  272.                         case inContent:
  273.                     
  274.                             if (window != FrontWindow())
  275.                                 SelectWindow( window );
  276.     
  277.                           #if ATI_ACCESS_RAVE_CONTEXT
  278.                             ATI_SetRaveState( ATI_FindWindowDevice(window), gDocument.fView );                                
  279.                           #endif
  280.                                 
  281.                             break ;
  282.                     
  283.                         case inGoAway:
  284.                             if (TrackGoAway( window, event.where )) {
  285.                                 DisposeWindow ( window );
  286.                                 gQuitFlag = true;
  287.  
  288.                             }
  289.                             break ;
  290.                             
  291.                         default:
  292.                             break ;
  293.                     }
  294.                     break ;
  295.                             
  296.                         
  297.                 case updateEvt:
  298.                 
  299.                     window = (WindowPtr)event.message;
  300.                     updateRect = (**(window->visRgn)).rgnBBox;
  301.                     SetPort( window ) ;
  302.                     BeginUpdate( window );
  303.                     DocumentDraw3DData( &gDocument ) ;
  304.                     EndUpdate( window );
  305.  
  306.                     #if ATI_ACCESS_RAVE_CONTEXT                    
  307.                     /*
  308.                      * display frame rate
  309.                      */
  310.                     end     = MicrosecondCount();
  311.                     seconds = (float)(MAX((end-start),1))*MILLIONTHS;        
  312.                     sprintf( str, "fps = %5.2f", ++count/seconds);
  313.                     DisplayString( 2, 10, str );
  314.                     DisplayString( 100, 10, (filter) ? "Filtering ON" : "Filtering Off" );
  315.                   #endif
  316.                     break ;
  317.                     
  318.                 case keyDown:
  319.                 case autoKey:
  320.                     HandleKeyPress(&event);
  321.                     break;
  322.                     
  323.                 case diskEvt:
  324.                     if ( HiWrd(event.message) != noErr ) 
  325.                         (void) DIBadMount(aPoint, event.message);
  326.                     break;
  327.                     
  328.                 case osEvt:
  329.                 case activateEvt:
  330.                     break;
  331.  
  332.  
  333.             }
  334.         }
  335.         else {
  336.             // we received a null event, rotate the cube
  337.             TQ3Matrix4x4    tmp;
  338.             Rect            theRect = ((GrafPtr)gMainWindow)->portRect ;
  339.             
  340.             SetPort((GrafPtr)gMainWindow) ;
  341.             //Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  342.             Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.0, 0.001, 0.001);
  343.             Q3Matrix4x4_Multiply(&gDocument.fRotation, &tmp, &gDocument.fRotation);
  344.  
  345.             InvalRect( &theRect ) ;
  346.         }
  347.     }
  348. }
  349.  
  350.  
  351. //-------------------------------------------------------------------------------------------
  352. //
  353. void HandleKeyPress(EventRecord *event)
  354. {}
  355.  
  356. //-------------------------------------------------------------------------------------------
  357. //
  358.  
  359. #if ATI_ACCESS_RAVE_CONTEXT
  360.  
  361. /*
  362.  *    Function:    MicrosecondCount()
  363.  *
  364.  */
  365. unsigned long MicrosecondCount( void )
  366. {    
  367.     UnsignedWide currentCount;
  368.     
  369.     Microseconds(¤tCount);
  370.     
  371.     return( currentCount.lo );
  372. }
  373.  
  374. /*
  375.  *    Function:    DisplayString()
  376.  *
  377.  */
  378. void DisplayString( int x, int y, char *str )
  379. {
  380.     char buf[256];
  381.                 
  382.     RGBForeColor( &BLACK );
  383.     MoveTo( x, y );
  384.     
  385.     strcpy( buf+1, str );
  386.     buf[0] = strlen( buf+1 );
  387.     DrawString( (const unsigned char *)buf );
  388. }
  389.  
  390. /*
  391.  * Function:    ATI_SetRaveState()
  392.  *
  393.  */
  394. void ATI_SetRaveState( GDHandle    GDevice, TQ3ViewObject viewObject )
  395. {
  396.     OSErr            iErr;
  397.     TQADevice       RaveDevice;
  398.     TQADrawContext    *raveDrawContext = NULL;
  399.     TQAEngine       *ATIRaveEngine;
  400.     long            response = (long)&RaveDevice;
  401.         
  402.     /*
  403.      * get handle to desired GDevice
  404.      */    
  405.     if( GDevice == NULL )
  406.         return;
  407.  
  408.     /*
  409.      * fill TQADevice struct + use this to find
  410.      * if there is an ATI Engine on this device
  411.      */        
  412.     RaveDevice.deviceType         = kQADeviceGDevice;
  413.     RaveDevice.device.gDevice     = GDevice;
  414.  
  415.     if( (ATIRaveEngine = ATI_FindRaveEngine( &RaveDevice )) == NULL )
  416.         return;
  417.  
  418.     /*
  419.      * now we know we have an ATI Engine, so use
  420.      * Gestalt call to query for pointer to current
  421.      * RAVE draw context.  The parameter "response"
  422.      * is used both as input and output.  On input
  423.      * it holds the address of the TQADevice we're
  424.      * interested in.  If successful, on output it
  425.      * holds the current RAVE draw context pointer
  426.      */
  427.     Q3View_StartRendering( viewObject );
  428.     
  429.     iErr = QAEngineGestalt( ATIRaveEngine, 
  430.                             (TQAGestaltSelector)kQATIGestalt_CurrentContext, 
  431.                             &response );
  432.    
  433.        Q3View_Cancel( viewObject );
  434.        
  435.        if( iErr != kQANoErr || response == NULL || response == (long)&RaveDevice )
  436.            return;
  437.            
  438.     raveDrawContext = (TQADrawContext *)response;
  439.  
  440.     /*
  441.      * toggle filtering mode
  442.      */
  443.     filter = !filter;
  444.     QASetInt(     raveDrawContext, kQATag_TextureFilter,     
  445.                                  (filter) ? kQATextureFilter_Best : kQATextureFilter_Fast );
  446.             
  447.        /* 
  448.         * Set standard Rave context state variables - these
  449.         * are not required, but are just here to show the
  450.         * kinds of things you can do
  451.         */                               
  452.     QASetInt(     raveDrawContext, kQATag_TextureOp,         kQATextureOp_Modulate    );
  453.     QASetInt(     raveDrawContext, kQATag_Blend,             kQABlend_Interpolate    );
  454.     QASetInt(     raveDrawContext, kQATag_ZFunction,        kQAZFunction_LE            );
  455.     QASetInt(     raveDrawContext, kQATag_Antialias,        kQAAntiAlias_Off        );
  456.     QASetInt(     raveDrawContext, kQATag_PerspectiveZ,    kQAPerspectiveZ_Off        );
  457.     QASetInt(     raveDrawContext, kQATagGL_BlendSrc,     GL_SRC_ALPHA             );
  458.     QASetInt(     raveDrawContext, kQATagGL_BlendDst,     GL_ONE_MINUS_DST_ALPHA     );
  459.  
  460.     gNotice.bufferNoticeMethod = callBack;
  461.     QASetNoticeMethod( raveDrawContext, kQAMethod_BufferComposite, gNotice, NULL );
  462.     
  463.        /* 
  464.         * Set ATI specific Rave context state variables
  465.         *                                
  466.     QASetInt(     raveDrawContext, (TQATagInt)kATIFogMode,         kQATIFogLinear    );
  467.     QASetFloat( raveDrawContext, (TQATagFloat)kATIFogColor_r,     0.0             );
  468.     QASetFloat( raveDrawContext, (TQATagFloat)kATIFogColor_g,     0.2             );
  469.     QASetFloat( raveDrawContext, (TQATagFloat)kATIFogColor_b,     0.0                );
  470.     QASetFloat( raveDrawContext, (TQATagFloat)kATIFogStart,     0.0             );
  471.     QASetFloat( raveDrawContext, (TQATagFloat)kATIFogEnd,         1.0                );
  472.     QASetFloat( raveDrawContext, (TQATagFloat)kATIFogDensity,     1.0             );
  473.      */
  474. }
  475.  
  476. /*
  477.  * Function:    ATI_FindRaveEngine()
  478.  *
  479.  */
  480. TQAEngine *ATI_FindRaveEngine( TQADevice *device )
  481. {
  482.     TQAEngine         *tmpEngine;
  483.     unsigned long    vendorID;
  484.     
  485.     /*
  486.      * try to find ATI RAVE engine
  487.      */
  488.     for(    tmpEngine = QADeviceGetFirstEngine(device); 
  489.             tmpEngine;
  490.             tmpEngine = QADeviceGetNextEngine(device, tmpEngine) )
  491.     {
  492.         QAEngineGestalt( tmpEngine, kQAGestalt_VendorID, &vendorID );        
  493.         if( vendorID == 1 )
  494.             return( QAEngineCheckDevice(tmpEngine, device) ? NULL : tmpEngine );
  495.     }
  496.     
  497.     /*
  498.      * if no ATI RAVE engine, return NULL
  499.      */
  500.     return( NULL );
  501. }
  502.  
  503. /*
  504.  * Function:    ATI_FindWindowDevice()
  505.  *
  506.  */
  507. GDHandle ATI_FindWindowDevice( WindowPtr pWin )
  508. {
  509.     GDHandle    hDevice;
  510.     Rect        rWin;
  511.     Rect        intersect;
  512.     
  513.     if( pWin == NULL )
  514.         return( NULL );
  515.         
  516.     rWin = ((*(((WindowPeek)pWin)->strucRgn))->rgnBBox);
  517.     
  518.     for( hDevice = GetDeviceList(); 
  519.          hDevice; 
  520.          hDevice = GetNextDevice(hDevice) )
  521.     {
  522.         /*
  523.          * test for active monitors that entirely contain rect
  524.          */
  525.         if( TestDeviceAttribute(hDevice, screenDevice) &&
  526.             TestDeviceAttribute(hDevice, screenActive) &&
  527.             SectRect(&rWin, &((*hDevice)->gdRect), &intersect) &&
  528.             EqualRect(&rWin, &intersect) )
  529.         {
  530.             return( hDevice );
  531.         }    
  532.     }
  533.  
  534.     return( NULL );    
  535. }
  536.  
  537. /*
  538.  * Function:    callBack()
  539.  *
  540.  */
  541. void callBack(     const TQADrawContext    *drawContext,        /* Draw context */
  542.                 const TQADevice            *buffer,            /* TQADevice describing back buffer */
  543.                 const TQARect            *dirtyRect,            /* Minimum area to process; NULL means whole buffer */
  544.                 void                    *refCon )
  545. {
  546.     long                 rowBytes     = buffer->device.memoryDevice.rowBytes;
  547.     TQAImagePixelType     pixelType    = buffer->device.memoryDevice.pixelType;
  548.     long                 width        = buffer->device.memoryDevice.width;
  549.     long                 height        = buffer->device.memoryDevice.height;
  550.     void                 *baseAddr    = buffer->device.memoryDevice.baseAddr;
  551.     int                    pixSize     = (pixelType == kQAPixel_ARGB32) ? 32 : 16;
  552.     int                    starty         = height/2 - 50;
  553.     int                    startx         = width/2 - 50;
  554.     unsigned char         *rowPtr        = (unsigned char *)baseAddr + starty*rowBytes;
  555.     int                    x, y;
  556.  
  557.     /*
  558.      * mess with the pixMap memory
  559.      */
  560.     if( pixSize == 16 )
  561.     {
  562.         unsigned short    *pixPtr;
  563.         
  564.         for( y = 0; y < 100; y++ )
  565.         {
  566.             pixPtr = (unsigned short *)rowPtr + startx;
  567.             
  568.             for( x = 0; x < 100; x++ )
  569.                 *pixPtr++ ^= 0xFFFF;
  570.                 
  571.             rowPtr += rowBytes;
  572.         }
  573.     }
  574.     else
  575.     {
  576.         unsigned long    *pixPtr;
  577.         
  578.         for( y = 0; y < 100; y++ )
  579.         {
  580.             pixPtr = (unsigned long *)rowPtr + startx;
  581.             
  582.             for( x = 0; x < 100; x++ )
  583.                 *pixPtr++ ^= 0xFFFFFFFF;
  584.                 
  585.             rowPtr += rowBytes;
  586.         }
  587.     }
  588. }
  589. #endif
  590.  
  591.  
  592.