Jump to content
  • Advertisement
Sign in to follow this  
ryan20fun

Manualy Filling A ID3DXMesh

This topic is 2435 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All.
im having trouble wrapping my head around filling the ID3DXMesh manualy.

how would i manually set the vertexes / normals / textures and what ever else is required ?

ive read a bunch of different posts on a similer thing, but cant get my head around what to do.

so whats some psuedo code to do this ?( some links might be nice )

how do you normally handle models ?( IE: do you use your own model loader / manager etc )

Thanks In Advance.

Share this post


Link to post
Share on other sites
Advertisement
The D3DX mesh is little more than a wrapper for a vertex buffer, an index buffer, an attribute buffer (that stores the subset index for each face for DrawSubset method to use) and the vertex format specifier. It does include convenience methods to draw and manipulate the internal buffers, though.

In D3D, it is common that attributes (position, normal, uv...) of vertices are interleaved in one vertex buffer. Though it is possible to use multiple streams at the API core level, the D3DX mesh interface uses only one stream (at least in D3D9, I don't remember if this is required in later versions). Therefore, the vertex data of a mesh can be stored as an array of simple structure that contains the desired components.

The primitive type of the D3DX mesh is always an indexed triangle list. That is, there are exactly (3*facecount) indices in the index buffer.

In order to "manually" fill the buffers, you lock them for writing, copy your stuff to the pointer you get from the lock method, and unlock them to commit the changes and to allow the system to use them for rendering.

Check the tutorials in the SDK, they cover the raw buffer usage as well as the usage of the D3DXMesh.

Share this post


Link to post
Share on other sites

how would i manually set the vertexes / normals / textures and what ever else is required ?


First, declare your own vertex structure that contains each data you need. Like this one:
struct Vertex_POS_NOR_TEX
{
vector3 position;
vector3 normal;
vector2 texCoords;
};


Then create a vertex declaration:
D3DVERTEXELEMENT9 vertElem[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, 20, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
};

Note that the order of each element is the same as your vertex structure. The second parameter of each element is very important. It tells you how much bytes the distance (offset in bytes) between current element and the head is. In our example, the head is POSITION (because in our Vertex_POST_NOR_TEX structure, the first member is position), so the second parameter must be 0. You know, a float is 4-bytes long. So, NORMAL data must be 12 bytes distant from head, because POSITION has 3 floats (3 x 4bytes = 12 bytes). TEXCOORD data is 20 bytes distant to the head, because NORMAL has 3 floats and TEXCOORD has 2 floats. In total, 5 floats = 5 x 4 = 20 bytes.

After that, you can create your mesh:
LPD3DXMESH pYourMesh = NULL;
D3DXCreateMesh (yourVertexCount, yourPolygonCount, D3DXMESH_MANAGED, vertElem /* take a look above */, yourD3DDevice, &pYourMesh);

Note that D3DXMESH_MANAGED flag can be different and depends on your need. For example, if you manipulate your mesh frequently, you can use D3DXMESH_DYNAMIC. For details, take a look at D3DXMESH flags in SDK docs.

Now the most important step comes: Filling the buffers. As nik02 said, ID3DXMesh interface lets you play with vertex and index buffers, also attribute buffers (for subset manipulating). You can think of ID3DXMesh interface like this:
struct ID3DXMesh
{
LPDIRECT3DVERTEXBUFFER9 pVertBuf;
LPDIRECT3DINDEXBUFFER9 pIndexBuf;
DWORD polyCount;
DWORD vertCount;

// blah blah...
};

For vertices, lock the vertex buffer, copy your vertex data, and unlock it. And for indices, lock the index buffer, copy your index data, and unlock it. Don't forget to call optimization functions: ID3DXMesh::OptimizeInplace(), D3DXCleanMesh(), D3DXWeldVertices(), ....

That's it! You created your mesh and now you can draw it by calling ID3DXMesh::DrawSubset().


how do you normally handle models ?( IE: do you use your own model loader / manager etc )


I'm using my own model loader. It reads the data from file and creates meshes per subsets:
For each subset returned from file
{
Create a vertex buffer and an index buffer;
Create a texture;
Create an effect/shader
Fill the buffers;
Load the texture;
Load the effect;
Fill other members (i.e. vertex count, index count, vertex size etc.)
}


To draw the entire model, I call IDirect3DDevice9::SetIndices(), IDirect3DDevice9::SetStreamSource() and IDirect3DDevice9::DrawIndexedPrimitive().

PS: Some links and book excerpts might be useful for you:
- Introduction to 3D Game Programming with DirectX 9.0c : A Shader Approach (from F. Luna) - Chapter 14 Available at Google Books
- http://www.scribd.co...Creating-a-Mesh
- MSDN, SDK docs and samples.

hth.
-R

Share this post


Link to post
Share on other sites
In addition to what [color="#284b72"]programci_84 said, if you want using DrawSubset method of the mesh, you need to fill the attribute buffer of the mesh too.

The attribute buffer stores the subset index for each face, so that the system knows which triangle belongs to which subset. The optimize methods can convert this data to ranges and sort the actual faces to match those ranges, therefore making the DrawSubset method much more efficient. Subset data itself does not have any other intrinsic meaning to the D3D core.

Of course, it is possible to use the vertex and index buffers of the mesh manually and draw it using DrawPrimitive, if you want to.This is a good approach if the mesh generally represents one renderable object.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!