您需要确保 C++ 和 HLSL 结构与在顶点格式中指定的顺序相匹配(如果您指定 XYZ 然后是 Normal,则您的结构必须与此匹配),然后您需要使用 device->CreateBuffer 来创建顶点缓冲区,来自顶点结构数组,之后顶点结构数组可以被释放和释放,因为 DirectX 将独立管理缓冲区数据,要更改渲染循环中的数据,缓冲区必须是可写的,并且可以使用 ID3D10Buffer Map 和 Unmap 创建后更新。
MSDN:
创建顶点缓冲区http://msdn.microsoft.com/en-us/library/windows/desktop/bb173544(v=vs.85).aspx
缓冲区:http: //msdn.microsoft.com/en-us/库/windows/desktop/bb173510(v=vs.85).aspx
例如: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;
};