Sign in to follow this  
Alex Swinney

How to fill in D3DVERTEXELEMENT9 structure?

Recommended Posts

To define your vertex declaration, you have to create an array of D3DVERTEXELEMENT9 structures. D3DVERTEXELEMENT9 is defined as:


D3DVERTEXELEMENT9 {
WORD Stream; // This is the stream this element resides in
WORD Offset; // Offset from the beginning of the vertex data. Get this by adding up the size of the types defined before this element.
BYTE Type; // The data type (ie D3DDECLTYPE_FLOAT3)
BYTE Method; // Use D3DDECLMETHOD_DEFAULT here
BYTE Usage; // Defines what the data will be used for (this is the semantic in your shader - ie D3DDECLUSAGE_POSITION)
BYTE UsageIndex; // Modifies the usage data to allow the user to specify multiple usage types.
}






You can find a list of D3DDECLTYPEs here. The most commonly used ones are float1, float2, float3, float4, and D3DCOLOR.

You can find a list of D3DDECLUSAGEs here. Commonly used ones are POSITION, NORMAL, TEXCOORD, and TANGENT.

An declaration of D3DVERTEXELEMENT9s usually goes like this:


D3DVERTEXELEMENT9 simple_decl[] =
{
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},
{0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
D3DDECL_END() // this macro is needed as the last item!
};





This specific declaration has 3 elements:
float3 POSITION
float3 NORMAL
float2 TEXCOORD


Once you have your array of D3DVERTEXELEMENT9s, you need to create an IDirect3DVertexDeclaration9. The good thing is, this is easy:


// d3dDevice is your LPDIRECT3DDEVICE9
// simpleDecl is the array of D3DVERTEXELEMENT9s we defined above
IDirect3DVertexDeclaration9* myVertexDecl;
d3dDevice->CreateVertexDeclaration(simple_decl, &myVertexDecl);



.

That's it! Now, you can create your vertex buffers and everything like that. We only need to do one more thing - before rendering the VB, we have to set the appropriate vertex declaration. This way, Direct3D knows what format our vertex buffer is in.


// d3dDevice is your LPDIRECT3DDEVICE9
// myVertexDecl is the LPDIRECT3DVERTEXDECLARATION9 that we defined above
d3dDevice->SetVertexDeclaration( myVertexDecl );

// You are now ready to go!
// Set all your stream sources, and DrawIndexedPrimitive()





And that is all it takes [wink]

Share this post


Link to post
Share on other sites
Quote:
Original post by Namethatnobodyelsetook
Quote:
Original post by Alex Swinney
How does one go about filling this structure in to create a mesh?

You don't create a mesh out of D3DVERTEXELEMENT9s, it just describes the contents of the VB (or in the case of streams, all the VBs).


Yes, but it is needed in order to create a mesh, and that was the point;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this