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);
		}

}
bool LoadModel_QD3D(FileSpecifier& Spec, Model3D& Model)
{
	// Clear out the final model object
	Model.Clear();
	
	// Test for QD3D/Quesa's presence and initialize it if not present
	if (QD3D_Presence_Checked)
	{
		if (!QD3D_Present) return false;
	}
	else
	{
		QD3D_Presence_Checked = true;
		
		// MacOS QD3D; modify this for Quesa as appropriate
		if ((void*)Q3Initialize != (void*)kUnresolvedCFragSymbolAddress)
		{
			TQ3Status Success = Q3Initialize();
			QD3D_Present = (Success == kQ3Success);
		}
		
		// Do additional setup;
		// if the triangulator could not be created, then act as if
		// QD3D/Quesa had not been loaded
		if (QD3D_Present)
		{
			Q3Error_Register(QD3D_Error_Handler,0);
			QD3D_Present = CreateTriangulator();
		}
		if (!QD3D_Present)
			Q3Exit();
	}
	
	if (DBOut)
	{
		// Read buffer
		const int BufferSize = 256;
		char Buffer[BufferSize];
		Spec.GetName(Buffer);
		fprintf(DBOut,"Loading QuickDraw-3D model file %s\n",Buffer);
	}
	
	TQ3Object ModelObject = LoadModel(Spec);
	if (!ModelObject) return false;
	
	StartAccumulatingVertices();
	
	if (Q3View_StartRendering(TriangulatorView) == kQ3Failure)
	{
		if (DBOut) fprintf(DBOut,"ERROR: couldn't start triangulation 'rendering'\n");
		Q3Object_Dispose(ModelObject);
		return false;
	}
	do
	{
		Q3SubdivisionStyle_Submit(&TesselationData, TriangulatorView);
		if (Q3Object_Submit(ModelObject, TriangulatorView) == kQ3Failure)
		{
			if (DBOut) fprintf(DBOut,"ERROR: model could not be 'rendered'\n");
		}
	}
	while (Q3View_EndRendering(TriangulatorView) == kQ3ViewStatusRetraverse);

	// Done with the model
	Q3Object_Dispose(ModelObject);
	
	GetVerticesIntoModel(Model);
	
	return !Model.Positions.empty();
}