我在使用DirectX中的顶点和索引缓冲区时遇到了一些麻烦。
这是一个定义多维数据集的函数,但我已经评论了所有内容,但前面是:
void GeometryGenerator::CreateCube(MeshData& meshData)
{
meshData.Vertices.resize(8);
meshData.Indices.resize(6);
float width;
float height;
float depth;
width = 45;
height = 45;
depth = 45;
XMFLOAT3 pos[8] =
{
XMFLOAT3(-0.5f * width, -0.5f * height, -0.5f * depth),
XMFLOAT3(-0.5f * width, -0.5f * height, 0.5f * depth),
XMFLOAT3(-0.5f * width, 0.5f * height, -0.5f * depth),
XMFLOAT3(-0.5f * width, 0.5f * height, 0.5f * depth),
XMFLOAT3( 0.5f * width, -0.5f * height, -0.5f * depth),
XMFLOAT3( 0.5f * width, -0.5f * height, 0.5f * depth),
XMFLOAT3( 0.5f * width, 0.5f * height, -0.5f * depth),
XMFLOAT3( 0.5f * width, 0.5f * height, 0.5f * depth)
};
unsigned short k[6] =
{
////1,0,2, // -x
////2,1,0,
////6,5,1, // +x
////6,7,5,
////
////0,1,5, // -y
////0,5,4,
////
////2,6,7, // +y
////2,7,3,
////4,6,0, // -z
////6,2,0,
7,3,1, //+z
5,7,1,
};
for(size_t i = 0; i < 8; ++i)
meshData.Vertices[i].Position = pos[i];
for(size_t i = 0; i < 6; ++i)
meshData.Indices[i] = k[i];
}
它绘制一个三角形和一条线,它们都没有到达预期的顶点。一个三角形看起来像7,3,0,并且有一条线覆盖1,0。我在下面以90度旋转附加了图像:
我很乐意发布任何必要的代码,但我怀疑你是否希望我发布我的整个项目。我认为可能有用的一些事情是......
我的顶点结构:
struct Vertex
{
DirectX::XMFLOAT3 pos;
DirectX::XMFLOAT3 color;
};
我的输入布局:
const D3D11_INPUT_ELEMENT_DESC vertexDesc[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
我的顶点着色器:
cbuffer ModelViewProjectionConstantBuffer : register(b0)
{
matrix model;
matrix view;
matrix projection;
};
struct VertexShaderInput
{
float3 pos : POSITION;
float3 color : COLOR0;
};
struct VertexShaderOutput
{
float4 pos : SV_POSITION;
float3 color : COLOR0;
};
VertexShaderOutput main(VertexShaderInput input)
{
VertexShaderOutput output;
float4 pos = float4(input.pos, 1.0f);
// Transform the vertex position into projected space.
pos = mul(pos, model);
pos = mul(pos, view);
pos = mul(pos, projection);
output.pos = pos;
// Pass through the color without modification.
output.color = input.color;
return output;
}
原始拓扑:
m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
索引缓冲区格式:
m_d3dContext->IASetIndexBuffer(
m_indexBuffer.Get(),
DXGI_FORMAT_R16_UINT,
0);
Vertex Stride and Offset:
UINT stride = sizeof(Vertex);
UINT offset = 0;
m_d3dContext->IASetVertexBuffers(
0,
1,
m_vertexBuffer.GetAddressOf(),
&stride,
&offset);
这是DrawIndexed电话:
m_d3dContext->DrawIndexed(
m_Indices.size(),
0,
0);
获取数据并创建索引缓冲区的代码:
GeometryGenerator generator;
GeometryGenerator::MeshData cubeData;
generator.CreateCube(cubeData);
//m_Indices is a std::Vector<Vertex>
m_Indices.resize(cubeData.Indices.size());
for(size_t i = 0; i < cubeData.Indices.size(); i++)
m_Indices[i] = cubeData.Indices[i];
D3D11_SUBRESOURCE_DATA indexBufferData = {0};
indexBufferData.pSysMem = &m_Indices[0];
indexBufferData.SysMemPitch = 0;
indexBufferData.SysMemSlicePitch = 0;
const UINT indexBufferWidth = m_Indices.size() * sizeof(UINT);
CD3D11_BUFFER_DESC indexBufferDesc(indexBufferWidth, D3D11_BIND_INDEX_BUFFER);
DX::ThrowIfFailed(
m_d3dDevice->CreateBuffer(
&indexBufferDesc,
&indexBufferData,
&m_indexBuffer));
谢谢你的帮助!我一直在使用Frank Luna的“DirectX 11 3D游戏编程简介”,但我找不到关于这些问题的讨论。
答案 0 :(得分:4)
您最有可能在以下某个方面犯了错误。任何错误都可能导致您正在观察的问题。
// Input layout defined correctly:
D3D11_INPUT_ELEMENT_DESC vertexDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 } };
// Vertex shader input matches input layout:
VS_OUT main(float3 pos : POSITION)
// Primitive topology set correctly:
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Index buffer format set correctly:
context->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R16_UINT, 0);
// Vertex stride and offset set correctly:
UINT stride = sizeof(XMFLOAT3);
UINT offset = 0;
context->IASetVertexBuffers(0, 1, vertexBuffer, &stride, &offset);
// Draw arguments are correct:
context->DrawIndexed(6, 0, 0);