0
votes

I am writing an application that needs to adapt to various potential vertex formats. The meshes I am loading may or may not have any number of potential vertex attribts (color, normals, texturecoords, weights, and so on).

A simple example of a declaration looks like this:

D3DVERTEXELEMENT9 simple_decl[] =
{
    {0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
    {0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,   0},
    {0, 20, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,   0},
    {0, 24, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,   1},
    D3DDECL_END()
};


struct MYVERTS
{   
    float x,y,z,u,v;
    DWORD colr;
    float nx,ny,nz;
};

Basically a D3DVERTEXELEMENT9 is an array of offsets. Each offset being the size of the data memebers of "MYVERTS".

If the user exports a model with just XYZ coordinates and color then i want to dynamically generate a struct with 3 floats and 1 dword and then generate a declaration with offsets and D3DDECLUSAGE.

I have no clue how to do this. Does anyone have any advice?

1

1 Answers

1
votes

You won't be able to "dynamically generate a struct", but you can start by filling in your own D3DVERTEXELEMENT9 array. A simple method is to parse the user model for the components and types that it's using and push_back() into a std::vector<D3DVERTEXELEMENT9>.

After you've parsed all components from the model, you can calculate the offsets for each component (the second column of numbers in your example). You'll need to calculate another "offset" from the final component to the start of the next, and this becomes your vertex stride. In your example it's 24, plus the size of D3DDECLTYPE_FLOAT3 or 12, for a vertex stride of 36.

Once you have the offsets and the stride, you can write the vertex data from the user model into the correct bytes in your vertex buffer.

You can even pass the contents of the vector to D3D after pushing in the D3DDECL_END().