DirectX 90 3D mesh 1

Source: Internet
Author: User

Grid (1)

Geometric Information

The ID3DXBaseMesh Interface contains a vertex cache (used to store grid vertices) and an index cache (used to determine how a vertex forms a mesh of triangle units ).

HRESULT ID3DXMesh: GetVertexBuffer (LPDIRECT3DVERTEXBUFFER9 * ppVB );

HRESULT ID3DXMesh: GetIndexBuffer (LPDIRECT3DINDEXBUFFER9 * ppIB );

Example:

IDirect3DVertexBuffer9 * vb = 0;

Mesh-> GetVertexBuffer (& vb );

IDirect3DIndexBuffer9 * ib = 0;

Mesh-> GetIndexBuffer (& ib );

Lock the cache and then perform read/write operations

HRESULT ID3DXMesh: LockVertexBuffer (DWORD Flags, BYTE ** ppDate );

......

HRESULT ID3DXMesh: UnlockVertexBuffer ();

HRESULT ID3DXMesh: LockIndexBuffer (DWORD Flags, BYTE ** ppDate );

......

HRESULT ID3DXMesh: UnlockIndexBuffer ();

Among them, Flags describes how to lock.

The following are some other ID3DXMesh interface methods related to the mesh geometric structure:

DWORD GetFVF () // return the vertex format

DWORD GetNumVertices () // returns the number of vertices in the vertex cache.

DWORD GetNumBytesPerVertex () // returns the number of bytes occupied by a vertex.

DWORD GetNumFaces () // returns the number of faces (triangles) in the mesh.

Subset and attribute Cache

A mesh consists of one or more subsets. A subset is a set of triangles in a grid that can be rendered with the same attributes. The attribute here refers to the material, texture, and rendering status.

To distinguish different subsets, each subset specifies a unique non-negative integer value. Each triangle unit in the grid is assigned an attribute ID to specify the subset of the triangle situation unit. The property IDs of these triangle cells are stored in the attribute buffer of the grid. This attribute can be an array of DWORD. The number of elements in the property cache is the same as that in the style layout.

Adjacent Information

The adjacent information is calculated to optimize the Attribute Table.

Each triangle has three sides. Each side can have up to three adjacent triangles. Therefore, the dimension of the adjacent array must be ID2DXBaseMesh: GetNumFaces () * 3, each triangle in the mesh has three possible adjacent slices. If one of the adjacent arrays is equal to ulong max = 4294967295, the slow State indicates that a specific edge in the grid does not have adjacent slices.

Many D3D mesh creation functions can output the adjacent information. You can also use the following method:

HRESULT ID3DXMesh: GenerateAdjacency (

FLOAT fEpsilon,

DWORD * pAdjacenc

)

Description

· FEpsilon is a very small positive number (epsilon) value, which can be regarded as the same point only when two points are close to some degree under a certain distance measurement.

· PAdhacency refers to a pointer to an array of the DOWRD type, which stores the adjacent information.

For example:

 

Std: vector <DWORD> adjacencyBuffer (Mesh-> GetNumFaces () * 3 );

Mesh-> GenerateAdjacency (0.0f, & adjacencyBuffer [0]);

Draw

The ID3DXMesh interface provides the DrawSubset (DWORD AttribId) method to draw each triangle in the subset of the AttribId indicator.

For example, to draw all triangles in the subset 0, we will write:

Mesh-> DrawSubset (0 );

To draw the entire mesh, we must draw all subsets of the mesh. This is very convenient to use 0, 1, 2 ,..., N-1 indicates the subset. n indicates the total number of subsets. There is a corresponding material and texture array, that is, the subset I corresponds to the I of the material and texture array. This allows us to simply use loops to render the mesh:

 

Grid Optimization

To draw a grid more efficiently, we can restructure the vertices and indexes in the style. This process is called Mesh optimization ). Use the direct South method to complete the optimization.

HRESULT ID3DXMesh: OptimizeInplace (

DWORD Flags,

Const dword * pAdjacencyIn,

DWORD * pAdjacencyOut,

DWORD * pFaceRemap,

LPD3DXBUFFER * ppVertexRemap

);

· Flags indicates the type of optimization method to be executed. It can be a combination of the following:

· D3DXMESHOPT _ COMPACT removes useless vertices and indexes from the grid.

· D3DXMESHOPT_ATTRSORT sorts triangles based on attributes and adjusts attribute tables, which makes the execution of DrawSubset more effective.

· D3DXMESHOPT _ VERTEXCACHE increases the hit rate of vertex cache.

· D3DXMESHOPT _ STRIPREORDE restructured the vertex index to make the triangle as long as possible.

· D3dxmeshopt _ ignoreverts only optimizes index information and ignores vertex information.

Note: d3dxmeshopt_vertexcache and d3dxmeshopt_stripreorder cannot be used at the same time.

· Padjacencyin refers to the pointer to the adjacent array of the unoptimized mesh.

· Padjacencyout points to a DWORD array, which is used to fill the optimized grid adjacent information. The dimension of the array must contain three elements: id3dxmesh: getnumfaces. You can set it to 0.

· Pfaceremap points to a DWORD array, which translates the re-painting information of the mesh. The dimension of the array should be id3dxmesh: getnumfaces (). When a grid is optimized, the index cache surface may be moved; that is, the I entry in pfaceremap indicates the index value of the first original face to be moved. If you do not need this information, you can set it to 0.

· Ppvertexremap refers to the address of the id3dxmesh Object Pointer, which stores the re-delivery information of vertices. The number of vertices contained in the cache should be id3dxmesh: getnumvertices. After a grid is optimized, the vertex may be moved. Vertex drawing information is used to indicate that the original vertex is moved to a new position. That is to say, the I entry in ppvertexremap indicates the new position of the original I vertex. If you do not need this information, you can set it to 0.

A simpler method is the optimize method, which outputs an optimized mesh instead of being optimized based on the original mesh:

Hresult optimize (
DWORD flags,
Const DWORD * padjacencyin,
DWORD * padjacencyout,
DWORD * pfaceremap,
Lpd3dxbuffer * ppvertexremap,
Lpd3dxmesh * ppoptmesh
);

Attribute Table(P162)

When a mesh is optimized using the d3dxmeshopt_attrsort parameter, the geometric information of the mesh is sorted by attribute, so that the vertices/indexes of each subset Form Continuous blocks.

Grid (D3DXCreateMeshFVF)

Hresult d3dxcreatemeshfvf (

DWORD numfaces,

DWORD numvertices,

DWORD options,

DWORD fvf,

Lpdirect3ddevice9 pdevice,

Lpd3dxmesh * ppmesh

);

· The total number of patches that the numfaces mesh will have. Greater than 0

· Total number of nunvertices grids. Greater than 0

· Options: the tag used to create a grid.

· The d3dxmesh _ 32bit mesh uses a 32-bit index.

· D3DXMESH _ MANAGED mesh data will be stored in the hosting pool

· D3DXMESH _ WRITEONLY specifies that the mesh is read-only.

· D3DMXESH _ DYNAMIC the grid cache will use DYNAMIC memory

· Flexible vertex formats of FVF vertices stored in the mesh

· PDevice pointer related to the grid

· Pointer to the mesh object created by ppMesh

For example:

// 12 triangles and 24 vertices

Hr = D3DXCreateMeshFVF (12, 24, D3DXMESH_MANAGED, Vertex: FVF, Device, & Mesh );

For (int I = 0; I <numSubsets; I ++)

{

Device-> SetMaterial (mtrls [I]);

Device-> SetTexture (0, textures [I]);

Mesh-> DrawSubset (I );

}

DirectX 9.0 3D Game Development Programming Basics

Jiangxi University of Technology FangSH 2010-5-

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.