void pvCamera_Fit(DocumentPtr theDocument) { TQ3Point3D from, to; TQ3BoundingBox viewBBox; float fieldOfView, hither, yon; if (!theDocument) return; if (!theDocument->fModel) return; pvBBox_Get(theDocument, &viewBBox); pvBBoxCenter(&viewBBox, &to); { TQ3Vector3D viewVector; TQ3Vector3D normViewVector; TQ3Vector3D eyeToFrontClip; TQ3Vector3D eyeToBackClip; TQ3Vector3D diagonalVector; float viewDistance; float maxDimension; Q3Point3D_Subtract(&viewBBox.max, &viewBBox.min, &diagonalVector); maxDimension = Q3Vector3D_Length(&diagonalVector); if (maxDimension == 0.0F) maxDimension = 1.0F; maxDimension *= 8.0F / 7.0F; from.x = to.x; from.y = to.y; from.z = to.z + (2 * maxDimension); Q3Point3D_Subtract(&to, &from, &viewVector); viewDistance = Q3Vector3D_Length(&viewVector); Q3Vector3D_Normalize(&viewVector, &normViewVector); maxDimension /= 2.0F; Q3Vector3D_Scale(&normViewVector, viewDistance - maxDimension, &eyeToFrontClip); Q3Vector3D_Scale(&normViewVector, viewDistance + maxDimension, &eyeToBackClip); hither = Q3Vector3D_Length(&eyeToFrontClip); yon = Q3Vector3D_Length(&eyeToBackClip); fieldOfView = Q3Math_RadiansToDegrees(1.25 * ErMath_Atan(maxDimension/hither)); } { TQ3ViewAngleAspectCameraData data; TQ3Vector3D up = { 0.0F, 1.0F, 0.0F }; data.cameraData.placement.cameraLocation = from; data.cameraData.placement.pointOfInterest = to; data.cameraData.placement.upVector = up; data.cameraData.range.hither = hither; data.cameraData.range.yon = yon; data.cameraData.viewPort.origin.x = -1.0F; data.cameraData.viewPort.origin.y = 1.0F; data.cameraData.viewPort.width = 2.0F; data.cameraData.viewPort.height = 2.0F; data.fov = Q3Math_DegreesToRadians(fieldOfView); { float w = (float)(theDocument->fWidth); float h = (float)(theDocument->fHeight); data.aspectRatioXToY = w/h; } if (theDocument->fView) { TQ3CameraObject camera; Q3View_GetCamera(theDocument->fView, &camera); if (camera) { Q3ViewAngleAspectCamera_SetData(camera, &data); Q3Object_Dispose(camera); } else { camera = Q3ViewAngleAspectCamera_New (&data); if (camera) { Q3View_SetCamera (theDocument->fView, camera); Q3Object_Dispose(camera); } } } } }
void QD3DSupport_InitDoc3DData( HWND window, DocumentPtr theDocument ) #endif { TQ3Status status; TQ3ShaderObject groundShaderObject; short partCount; #if TARGET_OS_WIN32 OSErr err; DWORD error; #endif /* Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library */ status = Q3Initialize(); if ( status == kQ3Failure ) { Utils_DisplayFatalErrorMsg("QD3D Initialization failure!"); } #if TARGET_OS_WIN32 err = QD3DSupport_QuickTimeInit (); if ( err != noErr ) { Utils_DisplayFatalErrorMsg("QTML Initialization failure!"); } #endif theDocument->fSplinePointsPtr = NULL; theDocument->fNumSplineNubs = 0; theDocument->fNumSplinePoints = 0; theDocument->fTrackIndex = 0; theDocument->fMainWindow = window; /* sets up the 3d data for the scene */ /* Create view for QuickDraw 3D. */ theDocument->fView = QD3DSupport_NewView( theDocument->fMainWindow ) ; if (theDocument->fView == NULL) { Utils_DisplayFatalErrorMsg("Failure creating a new view!"); } /* get the view's camera for use later */ Q3View_GetCamera(theDocument->fView, &theDocument->fCamera); /* the drawing styles: */ theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleVertex) ; theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleBoth ) ; theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ; /* get shaders for ground/track textures */ #if TARGET_OS_MAC groundShaderObject = QD3DSupport_CreateShaderFromTexture(kGrndTextureResID); theDocument->fTrackShader = QD3DSupport_CreateShaderFromTexture(kTextureRezID); #else if TARGET_OS_WIN32 error = Utils_Win32_BuildCurDirPath((Ptr)&theDocument->fGroundTextureFilePath, kGroundTextureFileName); if (!Utils_Win32_DoesFileExist((char *)&theDocument->fGroundTextureFilePath)) { Utils_DisplayFatalErrorMsg("Failure loading ground texture file GroundTexture.pct!"); } error = Utils_Win32_BuildCurDirPath((Ptr)&theDocument->fTrackTextureFilePath, kTrackTextureFileName); if (!Utils_Win32_DoesFileExist((char *)&theDocument->fGroundTextureFilePath)) { Utils_DisplayFatalErrorMsg("Failure loading track texture file MetalTrack.pct!"); } groundShaderObject = QD3DSupport_CreateShaderFromTexture((Ptr)&theDocument->fGroundTextureFilePath); theDocument->fTrackShader = QD3DSupport_CreateShaderFromTexture((Ptr)&theDocument->fTrackTextureFilePath); #endif /* create ground/track groups */ theDocument->fGroundGroup = QD3DSupport_GroundInit(groundShaderObject); theDocument->fTrackGroup = QD3DSupport_NewTrackGroup (theDocument->fTrackShader); /* load track parts */ #if TARGET_OS_WIN32 Track_LoadPartsFromFile((PartType *)&theDocument->fPartsList, &partCount); #elif TARGET_OS_MAC Track_LoadPartsFromRez((PartType *)&theDocument->fPartsList, &partCount); #endif /* build track from track parts */ if (partCount > 0) { Track_MakeRandomTrack((TrackSectionType *)&theDocument->fTrackSectionList, MAX_TRACK_SECTIONS); Track_CreateMasterNubList((TrackSectionType *)&theDocument->fTrackSectionList, partCount, (PartType *)&theDocument->fPartsList, (NubEntryType *)&theDocument->fNubArray, &theDocument->fNumSplineNubs); /* on output, new spline count */ Track_CalcSplineCurve(&theDocument->fSplinePointsPtr, MAX_SPLINE_POINTS, (NubEntryType *)&theDocument->fNubArray, theDocument->fNumSplineNubs, &theDocument->fNumSplinePoints, kTrackSubDivFactor); Track_BuildCoasterGeometry_Mesh(kSkipValue, theDocument->fTrackGroup, theDocument->fNumSplinePoints, theDocument->fSplinePointsPtr); } }