home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Samples / SampleCode / MarkerPick / MarkerPickShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  8.4 KB  |  332 lines  |  [TEXT/MPCC]

  1. // MarkerPickShell.c - QuickDraw 3d routines
  2. //
  3. // This is MarkerPick, an adaptation of box, the QuickDraw 3D starter program,
  4. // written by Nick Thompson.
  5. //
  6. // Julian Gómez - May 31, 1995
  7. // 
  8. // ©1994-95 Apple computer Inc., All Rights Reserved
  9. //
  10.  
  11. // system headers
  12. #include <Desk.h>
  13. #include <Dialogs.h>
  14. #include <DiskInit.h>
  15. #include <Fonts.h>
  16. #include <Menus.h>
  17. #include <PictUtil.h>
  18. #include <QDOffScreen.h>
  19. #include <QuickDraw.h>
  20. #include <SegLoad.h>
  21. #include <StandardFile.h>
  22. #include <TextEdit.h>
  23.  
  24. // for QuickDraw 3D
  25. #include "QD3D.h"
  26. #include "QD3DMath.h"
  27. #include "QD3DDrawContext.h"
  28. #include "QD3DShader.h"
  29. #include "QD3DTransform.h"
  30. #include "QD3DGroup.h"
  31. #include "QD3DPick.h"
  32.  
  33.  
  34. #include "MarkerPickShell.h"
  35. #include "MarkerPickSupport.h"
  36.  
  37. //-------------------------------------------------------------------------------------------
  38.  
  39. struct _documentRecord {
  40.     TQ3ViewObject    fView ;                    // the view for the scene
  41.     TQ3GroupObject    fModel ;                // object in the scene being modelled
  42.     TQ3StyleObject    fInterpolation ;        // interpolation style used when rendering
  43.     TQ3StyleObject    fBackFacing ;            // whether to draw shapes that face away from the camera
  44.     TQ3StyleObject    fFillStyle ;            // whether drawn as solid filled object or decomposed to components
  45.     TQ3Matrix4x4        fRotation;                // the transform for the model
  46. };
  47.  
  48. typedef struct _documentRecord DocumentRec, *DocumentPtr, **DocumentHdl ;
  49.  
  50.  
  51. //-------------------------------------------------------------------------------------------
  52. // function prototypes
  53.  
  54. static void         InitToolbox( void ) ;
  55. static void         MainEventLoop( void ) ;
  56. static void         HandleKeyPress(EventRecord *event) ;
  57. static void         HandleOSEvent(EventRecord *event) ;
  58. void InitDocumentData( DocumentPtr theDocument ) ;
  59. TQ3Status DocumentDraw3DData( DocumentPtr theDocument ) ;
  60. void DisposeDocumentData( DocumentPtr theDocument) ;
  61.  
  62. void DoPick(Point, DocumentPtr);
  63.  
  64.  
  65. //-------------------------------------------------------------------------------------------
  66. //
  67.  
  68. Boolean         gQuitFlag         = false ;
  69. WindowPtr        gMainWindow        = nil ;
  70. DocumentRec        gDocument ;
  71.  
  72. //-------------------------------------------------------------------------------------------
  73. // main()
  74. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  75. // and enter the main event loop.  On exit from the main event loop, we want to call
  76. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  77.  
  78. void main(void)
  79. {
  80.     TQ3Status    myStatus;
  81.     Rect        rBounds = { 50, 50, 450, 450 } ;
  82.     Str255        title = "\pMarker Pick" ;
  83.  
  84.     InitToolbox() ;
  85.     
  86.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  87.     myStatus = Q3Initialize();
  88.  
  89.     if ( myStatus == kQ3Failure )
  90.         DebugStr("\pErInitialize returned failure.");            
  91.  
  92.     // set up our globals
  93.     gQuitFlag = false ;
  94.     gMainWindow = NewCWindow(nil,&rBounds,title,true,noGrowDocProc,(WindowPtr)-1,true,0) ;
  95.  
  96.     InitDocumentData( &gDocument ) ;
  97.     
  98.     MainEventLoop();
  99.     
  100.     DisposeDocumentData( &gDocument ) ;
  101.     
  102.     //    Close our connection to the QuickDraw 3D library
  103.     myStatus = Q3Exit();
  104.     if ( myStatus == kQ3Failure )
  105.         DebugStr("\pErExit returned failure.");
  106.     
  107. }
  108.  
  109. //-------------------------------------------------------------------------------------------
  110. //
  111.  
  112. void InitDocumentData( DocumentPtr theDocument ) 
  113. {
  114.     // sets up the 3d data for the scene
  115.     // Create view for QuickDraw 3D.
  116.     theDocument->fView = MyNewView( (WindowPtr)gMainWindow ) ;
  117.  
  118.     // the main display group:
  119.     theDocument->fModel = MyNewModel() ;
  120.  
  121.     // the drawing styles:
  122.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  123.     theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleBoth ) ;
  124.     theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  125.  
  126.     // set the rotation matrix the identity matrix
  127.     Q3Matrix4x4_SetIdentity(&theDocument->fRotation);        
  128. }
  129.  
  130. void DisposeDocumentData( DocumentPtr theDocument)
  131. {
  132.     Q3Object_Dispose(theDocument->fView) ;                // the view for the scene
  133.     Q3Object_Dispose(theDocument->fModel) ;                // object in the scene being modelled
  134.     Q3Object_Dispose(theDocument->fInterpolation) ;        // interpolation style used when rendering
  135.     Q3Object_Dispose(theDocument->fBackFacing) ;        // whether to draw shapes that face away from the camera
  136.     Q3Object_Dispose(theDocument->fFillStyle) ;            // whether drawn as solid filled object or decomposed to components
  137.  
  138. }
  139. //-----------------------------------------------------------------------------
  140. // 
  141.  
  142. TQ3Status DocumentDraw3DData( DocumentPtr theDocument )
  143. {    
  144.     Q3View_StartRendering(theDocument->fView );
  145.     do {
  146.         Q3Style_Submit( theDocument->fInterpolation, theDocument->fView );
  147.         Q3Style_Submit( theDocument->fBackFacing, theDocument->fView );
  148.         Q3Style_Submit( theDocument->fFillStyle, theDocument->fView );
  149.         Q3MatrixTransform_Submit( &theDocument->fRotation, theDocument->fView );
  150.         Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView );
  151.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  152.     return kQ3Success ;
  153. }
  154.  
  155.  
  156. //----------------------------------------------------------------------------------
  157.  
  158. //-------------------------------------------------------------------------------------------
  159. //
  160.  
  161. short HiWrd(long aLong)
  162. {
  163.     return    (((aLong) >> 16) & 0xFFFF) ;
  164. }
  165.  
  166. //-------------------------------------------------------------------------------------------
  167. //
  168.  
  169. short LoWrd(long aLong)
  170. {
  171.     return    ((aLong) & 0xFFFF) ;
  172.  
  173. }
  174.  
  175. //-------------------------------------------------------------------------------------------
  176. //
  177.  
  178. void InitToolbox()
  179. {
  180.     Handle        menuBar = nil;
  181.  
  182.     MaxApplZone() ;
  183.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  184.     
  185.     InitGraf( &qd.thePort );
  186.     InitFonts();
  187.     InitWindows();
  188.     InitCursor();
  189.  
  190.     FlushEvents( everyEvent, 0 ) ;
  191.     // initialize application globals
  192.     
  193.     gQuitFlag = false;
  194.     
  195. }
  196.  
  197.  
  198. //-------------------------------------------------------------------------------------------
  199. //
  200. void MainEventLoop()
  201. {
  202.     EventRecord     event;
  203.     WindowPtr       window;
  204.     short           thePart;
  205.     Rect            screenRect, updateRect;
  206.     Point            aPoint = {100, 100};
  207.     Point            clickPoint;
  208.     CGrafPtr        savedPort ;
  209.     
  210.  
  211.     while( !gQuitFlag )
  212.     {
  213.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  214.         {
  215.  
  216.             switch (event.what) {
  217.                 case mouseDown:
  218.                 
  219.                     thePart = FindWindow( event.where, &window );
  220.                     
  221.                     switch( thePart ) {
  222.                         case inMenuBar: 
  223.                             break;
  224.                         
  225.                         case inDrag:
  226.                     
  227.                             screenRect = (**GetGrayRgn()).rgnBBox;
  228.                             DragWindow( window, event.where, &screenRect );
  229.                             break ;
  230.                     
  231.                         case inContent:
  232.                     
  233.                             if (window != FrontWindow())
  234.                                 SelectWindow( window );
  235.                             else {
  236.                                 GetPort((GrafPtr*)&savedPort);
  237.                                 SetPort(window);
  238.                                 clickPoint = event.where;
  239.                                 GlobalToLocal(&clickPoint);
  240.                                 SetPort((GrafPtr)savedPort);
  241.                                 DoPick(clickPoint, &gDocument);
  242.                             }
  243.                             break ;
  244.                     
  245.                         case inGoAway:
  246.                             if (TrackGoAway( window, event.where )) {
  247.                                 DisposeWindow ( window );
  248.                                 gQuitFlag = true;
  249.  
  250.                             }
  251.                             break ;
  252.                             
  253.                         default:
  254.                             break ;
  255.                     }
  256.                     break ;
  257.                             
  258.                         
  259.                 case updateEvt:
  260.                 
  261.                     window = (WindowPtr)event.message;
  262.                     updateRect = (**(window->visRgn)).rgnBBox;
  263.                     SetPort( window ) ;
  264.                     BeginUpdate( window );
  265.                     DocumentDraw3DData( &gDocument ) ;
  266.                     EndUpdate( window );
  267.                     break ;
  268.                     
  269.                 case keyDown:
  270.                 case autoKey:
  271.                     HandleKeyPress(&event);
  272.                     break;
  273.                     
  274.                 case diskEvt:
  275.                     if ( HiWrd(event.message) != noErr ) 
  276.                         (void) DIBadMount(aPoint, event.message);
  277.                     break;
  278.                     
  279.                 case osEvt:
  280.                 case activateEvt:
  281.                     break;
  282.  
  283.  
  284.             }
  285.         }
  286.     }
  287. }
  288.  
  289.  
  290. //-------------------------------------------------------------------------------------------
  291. //
  292. void HandleKeyPress(EventRecord *event)
  293. {
  294. #pragma unused ( event )
  295. }
  296.  
  297. //-------------------------------------------------------------------------------------------
  298. //
  299.  
  300.  
  301.  
  302. void DoPick(Point where, DocumentPtr theDocument)
  303.     {
  304.     TQ3WindowPointPickData    data;
  305.     unsigned long            i;
  306.     unsigned long            numHits;
  307.     TQ3PickObject            pick;
  308.     
  309.     data.data.sort = kQ3PickSortNone;
  310.     data.data.mask = 0;
  311.     data.data.numHitsToReturn = kQ3ReturnAllHits;
  312.     
  313.     data.point.x = where.h;
  314.     data.point.y = where.v;
  315.     data.vertexTolerance = 1;
  316.     data.edgeTolerance = 1;
  317.     
  318.     if ((pick = Q3WindowPointPick_New(&data)) == NULL)
  319.         return;
  320.         
  321.     Q3View_StartPicking(theDocument->fView, pick);
  322.     do {
  323.         Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView );
  324.     } while (Q3View_EndPicking(theDocument->fView) == kQ3ViewStatusRetraverse );
  325.     
  326.     if (Q3Pick_GetNumHits(pick, &numHits) == kQ3Success)
  327.         for (i=0; i<numHits; i++)
  328.             SysBeep(5);
  329.             
  330.     Q3Object_Dispose(pick);
  331.     }
  332.