Jump to content

  • Log In with Google      Sign In   
  • Create Account

Manualy Filling A ID3DXMesh


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 ryan20fun   Members   -  Reputation: 1030

Like
0Likes
Like

Posted 14 December 2011 - 06:23 AM

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.
Never say Never, Because Never comes too soon. - ryan20fun

Disclaimer: Each post of mine is intended as an attempt of helping and/or bringing some meaningfull insight to the topic at hand. Due to my nature, my good intentions will not always be plainly visible. I apologise in advance and assure you I mean no harm and do not intend to insult anyone.

Sponsor:

#2 Nik02   Crossbones+   -  Reputation: 2824

Like
0Likes
Like

Posted 14 December 2011 - 06:48 AM

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.

Niko Suni


#3 programci_84   Members   -  Reputation: 335

Like
1Likes
Like

Posted 15 December 2011 - 01:55 AM

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
There's no "hard", and "the impossible" takes just a little time.

#4 Nik02   Crossbones+   -  Reputation: 2824

Like
0Likes
Like

Posted 16 December 2011 - 07:12 AM

In addition to what 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.

Niko Suni





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS