Direct3D中使用網格模型的實現方法(轉)

post0發表於2007-08-12
Direct3D中使用網格模型的實現方法(轉)[@more@]

  Complicated geometry is usually modeled using 3-D modeling software and saved to a file. An example of this is the .x file format. Microsoft? Direct3D? uses meshes to load the objects from these files. Meshes are somewhat complicated, but Microsoft? Direct3DX contains functions that make using meshes easier. The Meshes sample project introduces the topic of meshes and shows how to load, render, and unload a mesh.

  複雜的幾何模型通常是由3D建模軟體建立並儲存到檔案中。例如.x檔案就是這樣的一種格式,Microsoft Direct3D使用的網格模型都是載入這些檔案中的物件。即使模型有些複雜,但在Microsoft Direct3D中包含的函式建立使用它是非常容易,本例子就是介紹網格模型如何從載入渲染得到我們所需的效果,並且如何對其進行釋放。

  

  This tutorial shows how to load, render, and unload a mesh using the following steps.

  這個指南展示如何載入、渲染、並釋放一個網格模型所需要的步驟:

  

  Step 1: Loading a Mesh Object

  第一步:載入一個網格模型物件

  

  A Microsoft? Direct3D? application must first load a mesh before using it. The Meshes sample project loads the tiger mesh by calling InitGeometry, an application-defined function, after loading the required Direct3D objects.

  一個Microsoft Direct3D的應用程式在使用網格模型物件之前必須進行初始化,本例中,呼叫InitGeometry函式用於載入一個老虎的網格模型,這是一個應用程式定義的函式,在載入必要的Direct3D物件(系統初始化)後呼叫。

  

  A mesh needs a material buffer that will store all the materials and textures that will be used. The function starts by declaring a material buffer as shown in the following code fragment.

  一個網格模型需要材質緩衝,將儲存要使用的材質以及紋理。函式開始先宣告一個材質緩衝如同以下的程式碼片段:

  

  LPD3DXBUFFER pD3DXMtrlBuffer;

  

  The following code fragment uses the D3DXLoadMeshFromX method to load the mesh.

  下面這部分使用D3DXLoadMeshFromX的方法來載入網格模型。

  

  // Load the mesh from the specified file.

  if( FAILED( D3DXLoadMeshFromX( "tiger.x", D3DXMESH_SYSTEMMEM,

                  g_pd3dDevice, NULL,

                  &pD3DXMtrlBuffer, &g_dwNumMaterials,

                  &g_pMesh ) ) )

    return E_FAIL;

  

  The first parameter that D3DXLoadMeshFromX accepts is a pointer to a string that tells the name of the Microsoft DirectX? file to load. This sample loads the tiger mesh from Tiger.x.

  第一個引數D3DXLoadMeshFromX 從一個指向字串的指標獲得需要Microsoft DirectX載入的檔名,本例中從Tiger.x檔案中載入老虎模型。

  

  The second parameter tells Direct3D how to create the mesh. The sample uses the D3DXMESH_SYSTEMMEM flag, which is equivalent to specifying both D3DXMESH_VB_SYSTEMMEM and D3DXMESH_IB_SYSTEMMEM. Both of these flags tell Direct3D to put the index buffer and vertex buffer for the mesh in system memory.

  第二個引數告訴Direct3D如何建立網格模型。本例使用D3DXMESH_SYSTEMMEM標記,哪相當於指定了D3DXMESH_VB_SYSTEMMEM和D3DXMESH_IB_SYSTEMMEM這兩個。這兩個標記告訴Direct3D將網格模型的索引緩衝(index buffer)和頂點緩衝(vertex buffer)存放於系統儲存器。

  

  The third parameter is a pointer to a Direct3D device that will be used to render the mesh.

  第三個引數是一個指標,指向Direct3D裝置,這也是將用於模型渲染。

  

  The fourth parameter is a pointer to an ID3DXBuffer object. This object will be filled with information about neighbors for each face. This information is not required for this sample, so this parameter is set to NULL.

  第四個引數是指向一個ID3DXBuffer物件的指標,這個物件將填滿關於相鄰每一個面的資訊。這個資訊在本例中並不需要,因此此引數設定為NULL。

  

  The fifth parameter also takes a pointer to a ID3DXBuffer object. After this method is finished, this object will be filled with D3DXMATERIAL structures for the mesh.

  第五個引數也同樣指向一個ID3DXBuffer物件,在這個函式完成之後,這個物件將填滿模型的D3DXMATERIAL結構資料。

  

  The sixth parameter is a pointer to the number of D3DXMATERIAL structures placed into the ppMaterials array after the method returns.

  第六個引數指向一個整形變數,當函式返回時儲存D3DXMATERIAL結構的數量於ppMaterials列隊中。

  

  The seventh parameter is the address of a pointer to a mesh object, representing the loaded mesh.

  第七個引數返回一個指向網格模型物件的地址,返回所載入的模型資料。

  

  After loading the mesh object and material information, you need to extract the material properties and texture names from the material buffer.

  在載入模型物件和材質資訊後,你需要從材質緩衝中篩選出材質屬性和紋理名稱。

  

  The Meshes sample project does this by first getting the pointer to the material buffer. The following code fragment uses the ID3DXBuffer::GetBufferPointer method to get this pointer.

  這是本例工程所做的第一步,取得指向材質緩衝,下面部分程式碼展示瞭如何透過ID3DXBuffer::GetBufferPointer函式來取得指標資料。

  

  D3DXMATERIAL* d3dxMaterials = (D3DXMATERIAL*)pD3DXMtrlBuffer->GetBufferPointer();

  

  The following code fragment creates new mesh and texture objects based on the total number of materials for the mesh.

  下面的程式碼片段建立新的模型和根據材質中模型所統計的紋理物件。

  

  g_pMeshMaterials = new D3DMATERIAL8[g_dwNumMaterials];

  g_pMeshTextures = new LPDIRECT3DTEXTURE8[g_dwNumMaterials];

  

  For each material in the mesh the following steps occur.

  為模型中每一個材質執行下面的步驟。

  

  The first step is to copy the material, as shown in the following code fragment.

  第一步,複製材質,程式碼如下:

  

  g_pMeshMaterials[i] = d3dxMaterials[i].MatD3D;

  

  The second step is to set the ambient color for the material, as shown in the following code fragment.

  第二步,為材質設定環境顏色,程式碼如下:

  

  g_pMeshMaterials[i].Ambient = g_pMeshMaterials[i].Diffuse;

  

  The final step is to create the texture for the material, as shown in the following code fragment.

  最後一步,為材質建立紋理貼圖,程式碼如下:

  

  // Create the texture.

  if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice,

                      d3dxMaterials[i].pTextureFilename,

                      &g_pMeshTextures[i] ) ) )

    g_pMeshTextures[i] = NULL;

  }

  

  After loading each material, you are finished with the material buffer and need to release it by calling IUnknown::Release.

  在載入每一個材質之後,你完成材質緩衝並需要釋放它呼叫IUnknown::Release。

  

  pD3DXMtrlBuffer->Release();

  

  The mesh, along with the corresponding materials and textures are loaded. The mesh is ready to be rendered to the display, as described in Step 2: Rendering a Mesh Object.

  模型連同相應的材質和紋理都已載入,模型已經準備好用於顯示渲染,接下來我們看看第二步:渲染一個模型物件。

  

  

  Step 2: Rendering a Mesh Object

  第二步:渲染一個網格模型物件

  

  In step 1 the mesh was loaded and is now ready to be rendered. It is divided into a subset for each material that was loaded for the mesh. To render each subset, the mesh is rendered in a loop. The first step in the loop is to set the material for the subset, as shown in the following code agment.

  在第一步裡,模型已經載入並準備好用於渲染,模型載入後,它是以每一個材質分開存放在一個子集中,需要對每個子集進行渲染,模型是在一個迴圈中渲染。第一步,在迴圈裡為子集設定材質,參看下面程式碼片段:

  

  g_pd3dDevice->SetMaterial( &g_pMeshMaterials[i] );

  

  The second step in the loop is to set the texture for the subset, as shown in the following code fragment.

  第二步,在迴圈裡為子集設定紋理,參看下面程式碼片段:

  

  g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] );

  

  After setting the material and texture, the subset is drawn with the ID

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-951737/,如需轉載,請註明出處,否則將追究法律責任。

相關文章