滑移网格模型:Direct3D中使用网格模型



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 roduces 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-d 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 specied file.
( FAILED( D3DXLoadMeshFromX( "tiger.x", D3DXMESH_SYSTEMMEM, 
                               g_pd3dDevice, NULL, 
                               &pD3DXMtrlBuffer, &g_dwNumMaterials, 
                               &g_pMesh ) ) )
     E_FAIL;

The first parameter that D3DXLoadMeshFromX accepts is a poer to a  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 specying 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.
第 2个参数告诉Direct3D如何创建网格模型本例使用D3DXMESH_SYSTEMMEM标记哪相当于指定了D3DXMESH_VB_SYSTEMMEM和D3DXMESH_IB_SYSTEMMEM这两个这两个标记告诉Direct3D将网格模型索引缓冲(index buffer)和顶点缓冲(vertex buffer)存放于系统存储器

The third parameter is a poer to a Direct3D device that will be used to render the mesh.
第 3个参数是个指针指向Direct3D设备这也是将用于模型渲染

The fourth parameter is a poer 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  to NULL.
第 4个参数是指向个ID3DXBuffer对象指针这个对象将填满有关相邻每个面信息这个信息在本例中并不需要因此此参数设置为NULL



The fth parameter also takes a poer to a ID3DXBuffer object. After this method is finished, this object will be filled with D3DXMATERIAL structures for the mesh.
第 5个参数也同样指向个ID3DXBuffer对象在这个函式完成的后这个对象将填满模型D3DXMATERIAL结构数据

The sixth parameter is a poer to the number of D3DXMATERIAL structures placed o the ppMaterials .gif' /> after the method s.
第 6个参数指向个整形变量当函式返回时保存D3DXMATERIAL结构数量于ppMaterials列队中

The seventh parameter is the address of a poer to a mesh object, representing the loaded mesh.
第 7个参数返回个指向网格模型对象地址返回所载入模型数据

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 poer to the material buffer. The following code fragment uses the ID3DXBuffer::GetBufferPoer method to get this poer.
这是本例工程所做取得指向材质缓冲下面部分代码展示了如何通过ID3DXBuffer::GetBufferPoer函式来取得指针数据

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

The following code fragment creates  mesh and texture objects based on the total number of materials for the mesh.
下面代码片段创建新模型和根据材质中模型所统计纹理对象

g_pMeshMaterials =  D3DMATERIAL8[g_dwNumMaterials];
g_pMeshTextures =  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 = d3dxMaterials.MatD3D;

The second step is to  the ambient color for the material, as shown in the following code fragment.
第 2步为材质设置环境颜色代码如下:

g_pMeshMaterials.Ambient = g_pMeshMaterials.Dfuse;

The final step is to create the texture for the material, as shown in the following code fragment.
最后为材质创建纹理贴图代码如下:

// Create the texture.
( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, 
                                       d3dxMaterials.pTextureFilename, 
                                       &g_pMeshTextures ) ) )
    g_pMeshTextures = 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.
模型连同相应材质和纹理都已载入模型已经准备好用于显示渲染接下来我们看看第 2步:渲染个模型对象


Step 2: Rendering a Mesh Object 
第 2步:渲染个网格模型对象

In step 1 the mesh was loaded and is now ready to be rendered. It is divided o a sub for each material that was loaded for the mesh. To render each sub, the mesh is rendered in a loop. The first step in the loop is to  the material for the sub, as shown in the following code agment.
在第步里模型已经载入并准备好用于渲染模型载入后它是以每个材质分开存放在个子集中需要对每个子集进行渲染模型是在个循环中渲染在循环里为子集设置材质参看下面代码片段:



g_pd3dDevice->SetMaterial( &g_pMeshMaterials );

The second step in the loop is to  the texture for the sub, as shown in the following code fragment.
第 2步在循环里为子集设置纹理参看下面代码片段:

g_pd3dDevice->SetTexture( 0, g_pMeshTextures );

After ting the material and texture, the sub is drawn with the ID3DXBaseMesh::DrawSub method, as shown in the following code fragment.
在设置好材质和纹理后子集通过ID3DXBaseMesh::DrawSub函式思路方法来绘制参看下面代码片段:

g_pMesh->DrawSub( i );

The DrawSub method takes a DWORD that species which sub of the mesh to draw. This sample uses a value that is incremented each time the loop runs.
DrawSub函式思路方法通过个DWORD参数指定哪个子集用于模型绘制本例中使用个增加变量在每个时间里循环执行

After using a mesh, it is important to properly remove the mesh from memory, as described in Step 3: Unloading a Mesh Object.
使用模型的后完全释放模型所使用内存是很重要接下来我们看看第 3步:释放个网格模型对象


Step 3: Unloading a Mesh Object 
第 3步:释放个网格模型对象

After any Microsoft? DirectX? program finishes, it needs to deallocate any DirectX objects that it used and invalidate the poers to them. The mesh objects used in this sample also need to be deallocated. When it receives a WM_DESTROY message, the Meshes sample project calls Cleanup, an application-d function, to handle this.
在所有Microsoft DirectX程式完成后需要查找所有DirectX对象指针是在使用中还是无效在本例中模型对象同样也需要如此当程式接收到WM_DESTROY消息本例子工程将Cleanup函式这是个应用程式定义函式

The following code fragment deletes the material list.
下面代码片段释放材质列表:

( g_pMeshMaterials )
    delete g_pMeshMaterials;

The following code fragment deallocates each individual texture that was loaded and then deletes the texture list.
下面代码片段分别将每个纹理进行释放并删除纹理列表:

( g_pMeshTextures )
{
    for( DWORD i = 0; i < g_dwNumMaterials; i )
    {
        ( g_pMeshTextures )
        g_pMeshTextures->Release;
    }

    delete g_pMeshTextures;
}

The following code fragment deallocates the mesh object.
下面代码片段检测并释放模型对象:

//Delete the mesh object
( g_pMesh )
    g_pMesh->Release;

This tutorial has shown you how to load and render meshes. This is the last tutorial in this section. To see how a typical Direct3D application is written, see DirectX Graphics C/C Samples.
这个开发指南告诉你如何载入和渲染模型想知道如何编写标准Direct3D应用程式请看DirectXGraphics C/C 例子

本例子代码下载来源于Microsoft DirectX 8.1 SDK 帮助文档

原文 CopyRight  Microsoft.com
译文 CopyRight  中国游戏开发技术资源网     Sea_Bug
您可以任意转载但未经许可不可用于商业目转载必须保留此声明2002/6/17
Tags:  direct3d不可用 direct3d加速 direct3d 滑移网格模型

延伸阅读

最新评论

发表评论