示例#1
0
BOOL JCXFile::parse(LPCSTR lpXFileData)
{
	if(lpXFileData == NULL)
	{
		return FALSE;
	}

	cleanUp();

	ID3DXFile* lpXFile = NULL;
	xVerifyFailedIf(D3DXFileCreate(&lpXFile))
		jccommon_releaseComM(lpXFile);
		return FALSE;
	xVerifyFailedEndIf

	xVerifyFailedIf(lpXFile->RegisterTemplates(D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES))
		jccommon_releaseComM(lpXFile);
		return FALSE;
	xVerifyFailedEndIf

	xVerifyFailedIf(lpXFile->RegisterTemplates(lpXFileData, jcstring_cslen(lpXFileData)))
		jccommon_releaseComM(lpXFile);
		return FALSE;
	xVerifyFailedEndIf

	D3DXF_FILELOADMEMORY fileMemData;
	fileMemData.lpMemory = lpXFileData;
	fileMemData.dSize = jcstring_cslen(lpXFileData);

	ID3DXFileEnumObject* lpEnum = NULL;
	xVerifyFailedIf(lpXFile->CreateEnumObject(&fileMemData, D3DXF_FILELOAD_FROMMEMORY, &lpEnum))
		jccommon_releaseComM(lpXFile);
		return FALSE;
	xVerifyFailedEndIf

	SIZE_T numChildren;
	xVerifyFailedIf(lpEnum->GetChildren(&numChildren))
		jccommon_releaseComM(lpXFile);
		jccommon_releaseComM(lpEnum);
		return FALSE;
	xVerifyFailedEndIf

	for (SIZE_T childIndex = 0; childIndex < numChildren; ++childIndex)
	{
		ID3DXFileData* lpXFileData = NULL;
		xVerifyFailedIf(lpEnum->GetChild(childIndex, &lpXFileData))
			break;
		xVerifyFailedEndIf
		parseChildren(lpXFileData, FALSE, NULL, NULL);
		jccommon_releaseComM(lpXFileData);
	}

	jccommon_releaseComM(lpXFile);
	jccommon_releaseComM(lpEnum);

	return TRUE;
}
示例#2
0
bool loadXFile( const std::string& filename, FrameNode* rootFrameNode, LPDIRECT3DDEVICE9 d3dDevice )
{
    ID3DXFile* dxFile = 0;
    ID3DXFileEnumObject* dxFileEnum = 0;

    // Create the file parsing structure
    if( APP_ERROR( FAILED( D3DXFileCreate( &dxFile ) ) )( "Couldn't create IDirectXFile to parse source file" ) )
        goto FAILED;

    // Register templates for parsing an X file
    if( APP_ERROR( FAILED( dxFile->RegisterTemplates( (LPVOID)D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES ) ) )
            ( "Failed to register D3DRM X-file templates" ) )
        goto FAILED;

    // Register extended templates for parsing an X file
    if( APP_ERROR( FAILED( dxFile->RegisterTemplates( (LPVOID)XSKINEXP_TEMPLATES, sizeof(XSKINEXP_TEMPLATES) - 1 ) ) )
            ( "Failed to register extended X-file templates" ) )
        goto FAILED;

    // Create an enumerator so that we can look at the top-level entries in the file
    if( APP_ERROR( FAILED( dxFile->CreateEnumObject( (LPCVOID)filename.c_str(), D3DXF_FILELOAD_FROMFILE, &dxFileEnum ) ) )
            ( "Couldn't create enumerator for .X file in memory" ) )
        goto FAILED;

    // Scan all of the top-level objects in the file
    bool continueLoading = true;
    SIZE_T numberOfChildren;
    dxFileEnum->GetChildren( &numberOfChildren );
    for( SIZE_T i = 0; continueLoading && i < numberOfChildren; ++i )
    {
        ID3DXFileData* dxFileDataObject = NULL;
        continueLoading = !APP_ERROR( FAILED( dxFileEnum->GetChild( i, &dxFileDataObject ) ) || !rootFrameNode->load( dxFileDataObject, d3dDevice ) )( "Couldn't load element" );
        SAFE_RELEASE( dxFileDataObject );
    }

    // Free the file objects
    SAFE_RELEASE( dxFileEnum );
    SAFE_RELEASE( dxFile );

    // Success
    return true;


FAILED:

    SAFE_RELEASE( dxFileEnum );
    SAFE_RELEASE( dxFile );

    return false;
}
BOOL cMesh::Load(cGraphics *Graphics, char *Filename, char *TexturePath)
{
  ID3DXFile           *pDXFile = NULL;
  ID3DXFileEnumObject *pDXEnum = NULL;
  ID3DXFileData       *pDXData = NULL;
  sFrame                 *TempFrame, *FramePtr;
  sMesh                  *Mesh;

  // Free prior mesh object data
  Free();

  // Error checking
  if((m_Graphics = Graphics) == NULL || Filename == NULL)
    return FALSE;

  // Create the file object
  if(FAILED(D3DXFileCreate(&pDXFile)))	  
    return FALSE;

  // Register the templates
  if(FAILED(pDXFile->RegisterTemplates((LPVOID)D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES))) {
    pDXFile->Release();
    return FALSE;
  }

  // Create an enumeration object
  if(FAILED(pDXFile->CreateEnumObject((LPVOID)Filename, DXFILELOAD_FROMFILE, &pDXEnum))) {
    pDXFile->Release();
    return FALSE;
  }

  // Create a temporary frame
  TempFrame = new sFrame();

  SIZE_T count;

  if (FAILED(pDXEnum->GetChildren(&count)))
  {
	  return FALSE;
  }

  SIZE_T c = 0;

  // Loop through all objects looking for the frames and meshes
  while(c < count) {
	if (FAILED(pDXEnum->GetChild(c, &pDXData)))
		return FALSE;
    c++;
    ParseXFileData(pDXData, TempFrame, TexturePath);
    ReleaseCOM(pDXData);
  }

  // Release used COM objects
  ReleaseCOM(pDXEnum);
  ReleaseCOM(pDXFile);

  // See if we should keep the tempframe as root
  if(TempFrame->m_MeshList != NULL) {
    m_Frames = TempFrame;
    m_Frames->m_Name = new char[7];
    strcpy(m_Frames->m_Name, "%ROOT%");
  } else {
    // Assign the root frame and release temporary frame
    m_Frames = TempFrame->m_Child;
    FramePtr = m_Frames;
    while(FramePtr != NULL) {
      FramePtr->m_Parent = NULL;
      FramePtr = FramePtr->m_Sibling;
    }
    TempFrame->m_Child = NULL;
    delete TempFrame;
  }

  // Match frames to bones (for matrices)
  MapFramesToBones(m_Frames);

  // Calculate bounding box and sphere
  if((Mesh = m_Meshes) != NULL) {
    while(Mesh != NULL) {
      m_Min.x = min(m_Min.x, Mesh->m_Min.x);
      m_Min.y = min(m_Min.y, Mesh->m_Min.y);
      m_Min.z = min(m_Min.z, Mesh->m_Min.z);
      m_Max.x = max(m_Max.x, Mesh->m_Max.x);
      m_Max.y = max(m_Max.y, Mesh->m_Max.y);
      m_Max.z = max(m_Max.z, Mesh->m_Max.z);
      m_Radius = max(m_Radius, Mesh->m_Radius);

      Mesh = Mesh->m_Next;
    }
  }

  return TRUE;
}