home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 09 - QuickDraw 3D / Box / Setup.c < prev   
Encoding:
C/C++ Source or Header  |  1995-05-03  |  10.0 KB  |  370 lines  |  [TEXT/MPCC]

  1. /****************************/
  2. /*       SETUP.C                */
  3. /****************************/
  4.  
  5.  
  6. /****************************/
  7. /*    EXTERNALS             */
  8. /****************************/
  9.  
  10. #include <qd3d.h>
  11. #include <QD3DView.h>
  12. #include <QD3DDrawContext.h>
  13. #include <QD3DCamera.h>
  14. #include <QD3DLight.h>
  15. #include <QD3DShader.h>
  16. #include <QD3DGroup.h>
  17. #include <QD3DRenderer.h>
  18.  
  19. /****************************/
  20. /*    PROTOTYPES            */
  21. /****************************/
  22.  
  23. extern    void CreateMyBoxObject(void);
  24. extern    void DoFatalAlert(Str255 s);
  25.  
  26. void InitMyScene(void);
  27. TQ3DrawContextObject CreateMyDrawContex(WindowPtr theWindow);
  28. void CreateMyStylesAndShader(void);
  29. TQ3GroupObject CreateMyLightGroup(void);
  30. TQ3CameraObject CreateMyCamera(TQ3DrawContextObject drawContext);
  31. void CreateMyView(void);
  32.  
  33. /****************************/
  34. /*    CONSTANTS             */
  35. /****************************/
  36.  
  37.  
  38. #define    MY_WIND_ID        400
  39.  
  40. /*********************/
  41. /*    VARIABLES      */
  42. /*********************/
  43.  
  44. WindowPtr            gMyWindow;
  45.  
  46. TQ3CameraObject        gMyCameraObject;
  47. TQ3ViewObject        gMyViewObject;
  48. TQ3StyleObject        gMyStyleObject_Interpolation;
  49. TQ3StyleObject        gMyStyleObject_Backfacing;
  50. TQ3StyleObject        gMyStyleObject_Fillstyle;
  51. TQ3ShaderObject        gMyShaderObject_IlluminationShader;
  52.  
  53.  
  54.  
  55. /************** INIT MY SCENE *******************/
  56. //
  57. // Does everything needed to prepare a window for rendering
  58. //
  59.  
  60. void InitMyScene(void)
  61. {
  62.                 /* GET THE WINDOW TO USE */
  63.  
  64.     gMyWindow = GetNewCWindow(MY_WIND_ID,nil,(WindowPtr)-1L);    // get from resource        
  65.     if (gMyWindow == nil)
  66.         DoFatalAlert("\pWhere did the window go?");                
  67.     SetPort    (gMyWindow);
  68.  
  69.     CreateMyView();                    // create view object
  70.     CreateMyStylesAndShader();        // create styles & shader objects
  71.     
  72.     CreateMyBoxObject();            // create a box object
  73. }
  74.  
  75.  
  76. /******************* CREATE MY VIEW *************************/
  77. //
  78. // This creates our view object.
  79. //
  80. // INPUT    :    none
  81. // OUTPUT    :    none
  82. //
  83. // NOTE: gMyViewObject is a global variable of type TQ3ViewObject
  84. //
  85.  
  86. void CreateMyView(void)
  87. {
  88. TQ3Status                myErr;
  89. TQ3DrawContextObject    myDrawContext;
  90. TQ3RendererObject        myRenderer;
  91. TQ3GroupObject            myLights;
  92.     
  93.     
  94.                 /* CREATE NEW VIEW OBJECT */
  95.                 
  96.     gMyViewObject = Q3View_New();
  97.     if (gMyViewObject == nil)
  98.         DoFatalAlert("\pQ3View_New failed!");
  99.  
  100.     
  101.             /* CREATE A DRAW CONTEXT OBJECT */
  102.     
  103.     myDrawContext = CreateMyDrawContex(gMyWindow);
  104.     if (myDrawContext == nil)
  105.         DoFatalAlert("\pMyNewDrawContext Failed!");
  106.  
  107.  
  108.             /* ASSIGN DRAW CONTEXT TO THE VIEW */
  109.                 
  110.     myErr = Q3View_SetDrawContext(gMyViewObject, myDrawContext);
  111.     if ( myErr == kQ3Failure )
  112.         DoFatalAlert("\pSetDrawContext Failed!");
  113.  
  114.  
  115.                 /* CREATE RENDERER OBJECT */
  116.             
  117.     myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive);
  118.     if (myRenderer == nil)
  119.         DoFatalAlert("\pRenderer_NewFromType Failed!");
  120.  
  121.  
  122.             /* ASSIGN RENDERER TO THE VIEW */
  123.  
  124.     myErr = Q3View_SetRenderer(gMyViewObject, myRenderer);
  125.     if ( myErr == kQ3Failure )
  126.         DoFatalAlert("\pSetRenderer Failed!");
  127.  
  128.  
  129.             /* CREATE A CAMERA OBJECT */
  130.  
  131.     gMyCameraObject = CreateMyCamera(myDrawContext);
  132.     if (gMyCameraObject == nil)
  133.         DoFatalAlert("\pCreateMyCamera Failed!");
  134.  
  135.  
  136.             /* ASSIGN CAMERA TO THE VIEW */
  137.                     
  138.     myErr = Q3View_SetCamera(gMyViewObject, gMyCameraObject);
  139.     if ( myErr == kQ3Failure )
  140.         DoFatalAlert("\pSetCamera Failed!");
  141.  
  142.  
  143.             /* CREATE A LIGHT GROUP OBJECT */
  144.             
  145.     myLights = CreateMyLightGroup();
  146.     if ( myLights == nil )
  147.         DoFatalAlert("\pCreateMyLightGroup Failed!");
  148.         
  149.         
  150.         /* ASSIGN LIGHT GROUP TO THE VIEW */
  151.         
  152.     myErr = Q3View_SetLightGroup(gMyViewObject, myLights);
  153.     if ( myErr == kQ3Failure )
  154.         DoFatalAlert("\pSetLightGroup Failed!");
  155. }
  156.  
  157.  
  158. /**************** CREATE MY DRAW CONTEXT *********************/
  159. //
  160. // Creates a DRAW CONTEX which will be attached to the flythru VIEW object.
  161. // The draw context defines how a frame is drawn to a window or offscreen buffer.
  162. //
  163. // INPUT    :    pointer to the window we will be drawing in
  164. // OUTPUT    :    the new Draw Object
  165. //
  166.  
  167. TQ3DrawContextObject CreateMyDrawContex(WindowPtr theWindow)
  168. {
  169. TQ3DrawContextObject    myDrawContext;
  170. TQ3DrawContextData        myDrawContextData;
  171. TQ3MacDrawContextData    myMacDrawContextData;
  172. TQ3ColorARGB            clearColor = {1,0,0,0.25};            // dark blue
  173.  
  174.  
  175.             /* FILL IN DRAW CONTEXT DATA */
  176.  
  177.     myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;    // how to clear
  178.     myDrawContextData.clearImageColor = clearColor;                    // color to clear with
  179.     myDrawContextData.pane.min.x = (theWindow->portRect).left;        // set bounds
  180.     myDrawContextData.pane.max.x = (theWindow->portRect).right;
  181.     myDrawContextData.pane.min.y = (theWindow->portRect).top;
  182.     myDrawContextData.pane.max.y = (theWindow->portRect).bottom;
  183.     myDrawContextData.paneState = kQ3True;                            // use bounds = yes
  184.     myDrawContextData.maskState = kQ3False;                            // no mask
  185.     myDrawContextData.doubleBufferState = kQ3True;                    // use double buffering
  186.  
  187.  
  188.                 /* SET MAC SPECIFIC DATA */
  189.                 
  190.     myMacDrawContextData.drawContextData = myDrawContextData;
  191.     myMacDrawContextData.window = (CWindowPtr)theWindow;            // set window to draw into
  192.     myMacDrawContextData.library = kQ3Mac2DLibraryNone;
  193.     myMacDrawContextData.viewPort = nil;                            // (for GX only)
  194.     myMacDrawContextData.grafPort = nil;
  195.  
  196.  
  197.         /* CREATE MACINTOSH DRAW CONTEXT BASED ON THAT DATA */
  198.  
  199.     myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData);
  200.     if (myDrawContext == nil)
  201.         DoFatalAlert("\pQ3MacDrawContext_New failed");
  202.  
  203.     return (myDrawContext);
  204. }
  205.  
  206.  
  207. /****************** CREATE MY CAMERA *********************/
  208. //
  209. // Creates a camera which will be attached to the VIEW object.
  210. //
  211. // INPUT    :    the draw context object
  212. // OUTPUT    :     the camera Object
  213. //
  214.  
  215. TQ3CameraObject CreateMyCamera(TQ3DrawContextObject drawContext)
  216. {
  217. TQ3CameraObject                    myCamera;
  218. TQ3CameraData                    myCameraData;
  219. TQ3ViewAngleAspectCameraData    myViewAngleCameraData;
  220. TQ3Point3D                        cameraFrom = { 4.0, 5.0, 6.0 };
  221. TQ3Point3D                        cameraTo = { 0.0, 0.0, 0.0 };
  222. TQ3Vector3D                        cameraUp = { 0.0, 1.0, 0.0 };
  223. TQ3Area                            pane;
  224.  
  225.  
  226.                 /* FILL IN CAMERA DATA */
  227.                 
  228.     myCameraData.placement.cameraLocation = cameraFrom;            // set camera coords
  229.     myCameraData.placement.pointOfInterest = cameraTo;            // set "looking at" coords
  230.     myCameraData.placement.upVector = cameraUp;                    // set a vector that's "up"
  231.     myCameraData.range.hither = 0.1;                            // set frontmost Z dist
  232.     myCameraData.range.yon = 100.0;                                // set farthest Z dist
  233.     myCameraData.viewPort.origin.x = -1.0;                        // set view origins
  234.     myCameraData.viewPort.origin.y = 1.0;
  235.     myCameraData.viewPort.width = 2.0;
  236.     myCameraData.viewPort.height = 2.0;
  237.  
  238.     myViewAngleCameraData.cameraData = myCameraData;
  239.     myViewAngleCameraData.fov = 0.8;                            // set field of view angle
  240.     Q3DrawContext_GetPane(drawContext,&pane);                    // get window pane info
  241.     myViewAngleCameraData.aspectRatioXToY =                      // set aspect ratio of window
  242.             (pane.max.x-pane.min.x)/(pane.max.y-pane.min.y);
  243.  
  244.  
  245.         /* CREATE THE NEW CAMERA OBJECT BASED ON THAT DATA */
  246.                 
  247.     myCamera = Q3ViewAngleAspectCamera_New(&myViewAngleCameraData);
  248.  
  249.     return ( myCamera );
  250. }
  251.  
  252.  
  253.  
  254. /********************* CREATE MY LIGHT GROUP ************************/
  255. //
  256. // Creates a light group which will be attached to the VIEW object.
  257. //
  258. // INPUT    : none
  259. // OUTPUT    : the new Light Group Object
  260. //
  261.  
  262. TQ3GroupObject CreateMyLightGroup(void)
  263. {
  264. TQ3GroupPosition        myGroupPosition;
  265. TQ3GroupObject            myLightGroup;
  266. TQ3LightData            myLightData;
  267. TQ3DirectionalLightData    myDirectionalLightData;
  268. TQ3LightObject            myAmbientLight, myFillLight;
  269. TQ3Vector3D                fillDirection = { -0.7, -1.0, 0 };
  270. TQ3ColorRGB                whiteLight = { 1.0, 1.0, 1.0 };
  271.  
  272.     myLightData.isOn = kQ3True;                                    // light is ON
  273.     myLightData.color = whiteLight;                                // set color of light
  274.  
  275.             /* CREATE AMBIENT LIGHT */
  276.  
  277.     myLightData.brightness = 0.4;                                // set brightness value
  278.     myAmbientLight = Q3AmbientLight_New(&myLightData);            // make new light
  279.     if (myAmbientLight == nil)
  280.         DoFatalAlert("\pQ3AmbientLight_New Failed!");
  281.     
  282.  
  283.             /* CREATE DIRECTIONAL LIGHT */
  284.             
  285.     myLightData.brightness = 0.8;                                // set brightness
  286.     myDirectionalLightData.lightData = myLightData;                // refer to general light info
  287.     myDirectionalLightData.castsShadows = kQ3False;                // no shadows
  288.     myDirectionalLightData.direction = fillDirection;            // set fill direction vector
  289.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);    // make new light object
  290.     if (myFillLight == nil)
  291.         DoFatalAlert("\pQ3DirectionalLight_New Failed!");
  292.  
  293.  
  294.             /* CREATE LIGHT GROUP */
  295.  
  296.     myLightGroup = Q3LightGroup_New();
  297.     if ( myLightGroup == nil )
  298.         DoFatalAlert("\pQ3LightGroup_New Failed!");
  299.  
  300.  
  301.         /* ADD AMBIENT LIGHT OBJECT TO THE GROUP */
  302.  
  303.     myGroupPosition = Q3Group_AddObject(myLightGroup, myAmbientLight);
  304.     if ( myGroupPosition == 0 )
  305.         DoFatalAlert("\p Q3Group_AddObject Failed!");
  306.         
  307.         
  308.         /* ADD DIRECTIONAL LIGHT OBJECT TO THE GROUP */
  309.         
  310.     myGroupPosition = Q3Group_AddObject(myLightGroup, myFillLight);
  311.     if ( myGroupPosition == 0 )
  312.         DoFatalAlert("\p Q3Group_AddObject Failed!");
  313.  
  314.  
  315.         /* DISPOSE OF OUR REFERENCES TO THE LIGHT OBJECTS */
  316.         
  317.     Q3Object_Dispose(myFillLight);    
  318.     Q3Object_Dispose(myAmbientLight);    
  319.  
  320.     return ( myLightGroup );    
  321. }
  322.  
  323.  
  324. /**************** CREATE MY STYLES AND SHADER ****************/
  325. //
  326. // Creates style objects which define how the scene is to be rendered.
  327. // It also sets the shader object.
  328. //
  329. // INPUT    :    none
  330. // OUTPUT    :    none
  331. //
  332.  
  333. void CreateMyStylesAndShader(void)
  334. {
  335.  
  336.                     /* SET INTERPOLATION (FOR SHADING) */
  337.                     
  338.     gMyStyleObject_Interpolation =                             // do vertex interpolation
  339.                 Q3InterpolationStyle_New(kQ3InterpolationStyleVertex);
  340.     if (gMyStyleObject_Interpolation == nil )
  341.         DoFatalAlert("\p Q3InterpolationStyle_New Failed!");
  342.  
  343.  
  344.                     /* SET BACKFACING */
  345.  
  346.     gMyStyleObject_Backfacing =                             // remove backfaces
  347.         Q3BackfacingStyle_New(kQ3BackfacingStyleBoth);
  348.     if (gMyStyleObject_Backfacing == nil )
  349.         DoFatalAlert("\p Q3BackfacingStyle_New Failed!");
  350.  
  351.  
  352.                 /* SET POLYGON FILL STYLE */
  353.                         
  354.     gMyStyleObject_Fillstyle =                                 // we want the polys filled
  355.         Q3FillStyle_New(kQ3FillStyleFilled);
  356.     if (gMyStyleObject_Fillstyle == nil )
  357.         DoFatalAlert("\p Q3FillStyle_New Failed!");
  358.  
  359.  
  360.                 /* SET SHADER TO LAMBERT */
  361.  
  362.     gMyShaderObject_IlluminationShader = Q3LambertIllumination_New();
  363.     if (gMyShaderObject_IlluminationShader == nil )
  364.         DoFatalAlert("\p Q3LambertIllumination_New Failed!");
  365. }
  366.  
  367.  
  368.  
  369.  
  370.