directxstructurevertexdata

How does DirectX read in vertices?


So I've went through some tutorials and resources such as the DirectX Documentation / Reference itself and either I missed something or I really can't find an answer to my question.

The question is as stated in the title: How does DirectX read in Vertices into Vertex Buffers?

I've understood, of course, that you have to provide one or more FVF codes. But it doesn't say anywhere how to properly set up your Vertex struct. The only thing I could imagine would be, that DirectX checks flags in a linear "timeline", of course, so one flag which may require the same data types and orders must come first in the struct as well.

As a little example for what I mean:

struct MyVertex {
    float x, y, z;
    float nx, ny, ny;
};

!=

struct MyVertex {
    float nx, ny, nz;
    float x, y, z;
};

with the FVF codes:

D3DFVF_XYZ | D3DFVF_NORMAL

and nx, ny, nz representing the 3D coordinates of the normal vertex.

Any help on how to properly set up your vertex struct is appreciated...

Sincerely,

Derija


Solution

  • You need to ensure the C++ and HLSL Structure, matches the order in which it was specified in the Vertex Format (if you specified XYZ then Normal, your structure must match this), then you need to use the device->CreateBuffer to create the vertex buffer, from the array of vertex structures, after which the arrays of vertex structures may be released and freed as DirectX will manage the buffer data independently from there, to alter the data in the render loop, the buffer must be writable, and can be updated after create using ID3D10Buffer Map and Unmap.

    MSDN:
    Create Vertexbufferm http://msdn.microsoft.com/en-us/library/windows/desktop/bb173544(v=vs.85).aspx Buffer: http://msdn.microsoft.com/en-us/library/windows/desktop/bb173510(v=vs.85).aspx

    Eg: C++

    D3D10_INPUT_ELEMENT_DESC layoutPosTexNormCInstanced[] = 
    {
    {"POSITION",     0, DXGI_FORMAT_R32G32B32_FLOAT,    0,  0, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"TEXCOORD",     0, DXGI_FORMAT_R32G32_FLOAT,       0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"NORMAL",       0, DXGI_FORMAT_R32G32B32_FLOAT,    0, 20, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"TANGENT",      0, DXGI_FORMAT_R32G32B32_FLOAT,    0, 32, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"BINORMAL",     0, DXGI_FORMAT_R32G32B32_FLOAT,    0, 44, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"BLENDINDICES", 0, DXGI_FORMAT_R8G8B8A8_SINT,      0, 56, D3D10_INPUT_PER_VERTEX_DATA, 0 }, //4
    {"mTransform",   0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1,  0, D3D10_INPUT_PER_INSTANCE_DATA, 1 },
        {"mTransform",   1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 16, D3D10_INPUT_PER_INSTANCE_DATA, 1 },
        {"mTransform",   2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 32, D3D10_INPUT_PER_INSTANCE_DATA, 1 },
        {"mTransform",   3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 48, D3D10_INPUT_PER_INSTANCE_DATA, 1 },
    };
    
    
    //Contains the position, texture coordinate and normal for lighting calculations.
    struct DX10VertexNormal
    {
    //Constructor.
    DX10VertexNormal()
    {
        ZeroMemory(this, sizeof(DX10VertexNormal));
        boneIndex[0] = -1;
        boneIndex[1] = -1;
        boneIndex[2] = -1;
        boneIndex[3] = -1;
    };
    
    //PAD to 4.
    D3DXVECTOR3 pos;
    D3DXVECTOR2 tcoord;
    D3DXVECTOR3 normal;
    D3DXVECTOR3 tangent;
    D3DXVECTOR3 binormal;
    int boneIndex[4];
    
    };
    

    HLSL:

    ///Holds the vertex shader data for rendering 
    ///instanced mesh data, with position, texture coord, 
    ///and surface normal for lighting calculations.
    struct VS_Instanced_PosTexNorm_INPUT
    {
      float4 Pos: POSITION;
      float2 Tex: TEXCOORD;
      float3 Norm: NORMAL;
      float3 Tangent: TANGENT;
      float3 Binormal: BINORMAL;
      int4 boneIndex: BLENDINDICES;
      row_major float4x4 mTransform : mTransform;
      uint InstanceId : SV_InstanceID;
    };