home *** CD-ROM | disk | FTP | other *** search
- /****************************/
- /* SETUP.C */
- /****************************/
-
-
- /****************************/
- /* EXTERNALS */
- /****************************/
-
- #include <qd3d.h>
- #include <QD3DView.h>
- #include <QD3DDrawContext.h>
- #include <QD3DCamera.h>
- #include <QD3DLight.h>
- #include <QD3DShader.h>
- #include <QD3DGroup.h>
- #include <QD3DRenderer.h>
-
- /****************************/
- /* PROTOTYPES */
- /****************************/
-
- extern void CreateMyBoxObject(void);
- extern void DoFatalAlert(Str255 s);
-
- void InitMyScene(void);
- TQ3DrawContextObject CreateMyDrawContex(WindowPtr theWindow);
- void CreateMyStylesAndShader(void);
- TQ3GroupObject CreateMyLightGroup(void);
- TQ3CameraObject CreateMyCamera(TQ3DrawContextObject drawContext);
- void CreateMyView(void);
-
- /****************************/
- /* CONSTANTS */
- /****************************/
-
-
- #define MY_WIND_ID 400
-
- /*********************/
- /* VARIABLES */
- /*********************/
-
- WindowPtr gMyWindow;
-
- TQ3CameraObject gMyCameraObject;
- TQ3ViewObject gMyViewObject;
- TQ3StyleObject gMyStyleObject_Interpolation;
- TQ3StyleObject gMyStyleObject_Backfacing;
- TQ3StyleObject gMyStyleObject_Fillstyle;
- TQ3ShaderObject gMyShaderObject_IlluminationShader;
-
-
-
- /************** INIT MY SCENE *******************/
- //
- // Does everything needed to prepare a window for rendering
- //
-
- void InitMyScene(void)
- {
- /* GET THE WINDOW TO USE */
-
- gMyWindow = GetNewCWindow(MY_WIND_ID,nil,(WindowPtr)-1L); // get from resource
- if (gMyWindow == nil)
- DoFatalAlert("\pWhere did the window go?");
- SetPort (gMyWindow);
-
- CreateMyView(); // create view object
- CreateMyStylesAndShader(); // create styles & shader objects
-
- CreateMyBoxObject(); // create a box object
- }
-
-
- /******************* CREATE MY VIEW *************************/
- //
- // This creates our view object.
- //
- // INPUT : none
- // OUTPUT : none
- //
- // NOTE: gMyViewObject is a global variable of type TQ3ViewObject
- //
-
- void CreateMyView(void)
- {
- TQ3Status myErr;
- TQ3DrawContextObject myDrawContext;
- TQ3RendererObject myRenderer;
- TQ3GroupObject myLights;
-
-
- /* CREATE NEW VIEW OBJECT */
-
- gMyViewObject = Q3View_New();
- if (gMyViewObject == nil)
- DoFatalAlert("\pQ3View_New failed!");
-
-
- /* CREATE A DRAW CONTEXT OBJECT */
-
- myDrawContext = CreateMyDrawContex(gMyWindow);
- if (myDrawContext == nil)
- DoFatalAlert("\pMyNewDrawContext Failed!");
-
-
- /* ASSIGN DRAW CONTEXT TO THE VIEW */
-
- myErr = Q3View_SetDrawContext(gMyViewObject, myDrawContext);
- if ( myErr == kQ3Failure )
- DoFatalAlert("\pSetDrawContext Failed!");
-
-
- /* CREATE RENDERER OBJECT */
-
- myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive);
- if (myRenderer == nil)
- DoFatalAlert("\pRenderer_NewFromType Failed!");
-
-
- /* ASSIGN RENDERER TO THE VIEW */
-
- myErr = Q3View_SetRenderer(gMyViewObject, myRenderer);
- if ( myErr == kQ3Failure )
- DoFatalAlert("\pSetRenderer Failed!");
-
-
- /* CREATE A CAMERA OBJECT */
-
- gMyCameraObject = CreateMyCamera(myDrawContext);
- if (gMyCameraObject == nil)
- DoFatalAlert("\pCreateMyCamera Failed!");
-
-
- /* ASSIGN CAMERA TO THE VIEW */
-
- myErr = Q3View_SetCamera(gMyViewObject, gMyCameraObject);
- if ( myErr == kQ3Failure )
- DoFatalAlert("\pSetCamera Failed!");
-
-
- /* CREATE A LIGHT GROUP OBJECT */
-
- myLights = CreateMyLightGroup();
- if ( myLights == nil )
- DoFatalAlert("\pCreateMyLightGroup Failed!");
-
-
- /* ASSIGN LIGHT GROUP TO THE VIEW */
-
- myErr = Q3View_SetLightGroup(gMyViewObject, myLights);
- if ( myErr == kQ3Failure )
- DoFatalAlert("\pSetLightGroup Failed!");
- }
-
-
- /**************** CREATE MY DRAW CONTEXT *********************/
- //
- // Creates a DRAW CONTEX which will be attached to the flythru VIEW object.
- // The draw context defines how a frame is drawn to a window or offscreen buffer.
- //
- // INPUT : pointer to the window we will be drawing in
- // OUTPUT : the new Draw Object
- //
-
- TQ3DrawContextObject CreateMyDrawContex(WindowPtr theWindow)
- {
- TQ3DrawContextObject myDrawContext;
- TQ3DrawContextData myDrawContextData;
- TQ3MacDrawContextData myMacDrawContextData;
- TQ3ColorARGB clearColor = {1,0,0,0.25}; // dark blue
-
-
- /* FILL IN DRAW CONTEXT DATA */
-
- myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor; // how to clear
- myDrawContextData.clearImageColor = clearColor; // color to clear with
- myDrawContextData.pane.min.x = (theWindow->portRect).left; // set bounds
- myDrawContextData.pane.max.x = (theWindow->portRect).right;
- myDrawContextData.pane.min.y = (theWindow->portRect).top;
- myDrawContextData.pane.max.y = (theWindow->portRect).bottom;
- myDrawContextData.paneState = kQ3True; // use bounds = yes
- myDrawContextData.maskState = kQ3False; // no mask
- myDrawContextData.doubleBufferState = kQ3True; // use double buffering
-
-
- /* SET MAC SPECIFIC DATA */
-
- myMacDrawContextData.drawContextData = myDrawContextData;
- myMacDrawContextData.window = (CWindowPtr)theWindow; // set window to draw into
- myMacDrawContextData.library = kQ3Mac2DLibraryNone;
- myMacDrawContextData.viewPort = nil; // (for GX only)
- myMacDrawContextData.grafPort = nil;
-
-
- /* CREATE MACINTOSH DRAW CONTEXT BASED ON THAT DATA */
-
- myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData);
- if (myDrawContext == nil)
- DoFatalAlert("\pQ3MacDrawContext_New failed");
-
- return (myDrawContext);
- }
-
-
- /****************** CREATE MY CAMERA *********************/
- //
- // Creates a camera which will be attached to the VIEW object.
- //
- // INPUT : the draw context object
- // OUTPUT : the camera Object
- //
-
- TQ3CameraObject CreateMyCamera(TQ3DrawContextObject drawContext)
- {
- TQ3CameraObject myCamera;
- TQ3CameraData myCameraData;
- TQ3ViewAngleAspectCameraData myViewAngleCameraData;
- TQ3Point3D cameraFrom = { 4.0, 5.0, 6.0 };
- TQ3Point3D cameraTo = { 0.0, 0.0, 0.0 };
- TQ3Vector3D cameraUp = { 0.0, 1.0, 0.0 };
- TQ3Area pane;
-
-
- /* FILL IN CAMERA DATA */
-
- myCameraData.placement.cameraLocation = cameraFrom; // set camera coords
- myCameraData.placement.pointOfInterest = cameraTo; // set "looking at" coords
- myCameraData.placement.upVector = cameraUp; // set a vector that's "up"
- myCameraData.range.hither = 0.1; // set frontmost Z dist
- myCameraData.range.yon = 100.0; // set farthest Z dist
- myCameraData.viewPort.origin.x = -1.0; // set view origins
- myCameraData.viewPort.origin.y = 1.0;
- myCameraData.viewPort.width = 2.0;
- myCameraData.viewPort.height = 2.0;
-
- myViewAngleCameraData.cameraData = myCameraData;
- myViewAngleCameraData.fov = 0.8; // set field of view angle
- Q3DrawContext_GetPane(drawContext,&pane); // get window pane info
- myViewAngleCameraData.aspectRatioXToY = // set aspect ratio of window
- (pane.max.x-pane.min.x)/(pane.max.y-pane.min.y);
-
-
- /* CREATE THE NEW CAMERA OBJECT BASED ON THAT DATA */
-
- myCamera = Q3ViewAngleAspectCamera_New(&myViewAngleCameraData);
-
- return ( myCamera );
- }
-
-
-
- /********************* CREATE MY LIGHT GROUP ************************/
- //
- // Creates a light group which will be attached to the VIEW object.
- //
- // INPUT : none
- // OUTPUT : the new Light Group Object
- //
-
- TQ3GroupObject CreateMyLightGroup(void)
- {
- TQ3GroupPosition myGroupPosition;
- TQ3GroupObject myLightGroup;
- TQ3LightData myLightData;
- TQ3DirectionalLightData myDirectionalLightData;
- TQ3LightObject myAmbientLight, myFillLight;
- TQ3Vector3D fillDirection = { -0.7, -1.0, 0 };
- TQ3ColorRGB whiteLight = { 1.0, 1.0, 1.0 };
-
- myLightData.isOn = kQ3True; // light is ON
- myLightData.color = whiteLight; // set color of light
-
- /* CREATE AMBIENT LIGHT */
-
- myLightData.brightness = 0.4; // set brightness value
- myAmbientLight = Q3AmbientLight_New(&myLightData); // make new light
- if (myAmbientLight == nil)
- DoFatalAlert("\pQ3AmbientLight_New Failed!");
-
-
- /* CREATE DIRECTIONAL LIGHT */
-
- myLightData.brightness = 0.8; // set brightness
- myDirectionalLightData.lightData = myLightData; // refer to general light info
- myDirectionalLightData.castsShadows = kQ3False; // no shadows
- myDirectionalLightData.direction = fillDirection; // set fill direction vector
- myFillLight = Q3DirectionalLight_New(&myDirectionalLightData); // make new light object
- if (myFillLight == nil)
- DoFatalAlert("\pQ3DirectionalLight_New Failed!");
-
-
- /* CREATE LIGHT GROUP */
-
- myLightGroup = Q3LightGroup_New();
- if ( myLightGroup == nil )
- DoFatalAlert("\pQ3LightGroup_New Failed!");
-
-
- /* ADD AMBIENT LIGHT OBJECT TO THE GROUP */
-
- myGroupPosition = Q3Group_AddObject(myLightGroup, myAmbientLight);
- if ( myGroupPosition == 0 )
- DoFatalAlert("\p Q3Group_AddObject Failed!");
-
-
- /* ADD DIRECTIONAL LIGHT OBJECT TO THE GROUP */
-
- myGroupPosition = Q3Group_AddObject(myLightGroup, myFillLight);
- if ( myGroupPosition == 0 )
- DoFatalAlert("\p Q3Group_AddObject Failed!");
-
-
- /* DISPOSE OF OUR REFERENCES TO THE LIGHT OBJECTS */
-
- Q3Object_Dispose(myFillLight);
- Q3Object_Dispose(myAmbientLight);
-
- return ( myLightGroup );
- }
-
-
- /**************** CREATE MY STYLES AND SHADER ****************/
- //
- // Creates style objects which define how the scene is to be rendered.
- // It also sets the shader object.
- //
- // INPUT : none
- // OUTPUT : none
- //
-
- void CreateMyStylesAndShader(void)
- {
-
- /* SET INTERPOLATION (FOR SHADING) */
-
- gMyStyleObject_Interpolation = // do vertex interpolation
- Q3InterpolationStyle_New(kQ3InterpolationStyleVertex);
- if (gMyStyleObject_Interpolation == nil )
- DoFatalAlert("\p Q3InterpolationStyle_New Failed!");
-
-
- /* SET BACKFACING */
-
- gMyStyleObject_Backfacing = // remove backfaces
- Q3BackfacingStyle_New(kQ3BackfacingStyleBoth);
- if (gMyStyleObject_Backfacing == nil )
- DoFatalAlert("\p Q3BackfacingStyle_New Failed!");
-
-
- /* SET POLYGON FILL STYLE */
-
- gMyStyleObject_Fillstyle = // we want the polys filled
- Q3FillStyle_New(kQ3FillStyleFilled);
- if (gMyStyleObject_Fillstyle == nil )
- DoFatalAlert("\p Q3FillStyle_New Failed!");
-
-
- /* SET SHADER TO LAMBERT */
-
- gMyShaderObject_IlluminationShader = Q3LambertIllumination_New();
- if (gMyShaderObject_IlluminationShader == nil )
- DoFatalAlert("\p Q3LambertIllumination_New Failed!");
- }
-
-
-
-
-