[英]DirectX Index Buffer Index order
I'm having some trouble drawing with vertex and index buffers in DirectX. 我在使用DirectX中的顶点和索引缓冲区绘制时遇到了一些麻烦。
Here is a function that defines a cube, but I've commented everything out but the front face: 这是一个定义多维数据集的函数,但我已经评论了所有内容,但正面:
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];
} }
it draws one triangle and a line, neither of which go to the intended vertices. 它绘制一个三角形和一条线,两者都没有到达预期的顶点。 One triangle looks like it covers 7,3,0 and there is a line that covers 1,0.
一个三角形看起来像7,3,0,并且有一条线覆盖1,0。 I have attached images below at 90 degree rotations:
我在90度旋转时附上了下面的图像:
I am happy to post whatever code is necessary, but I doubt you want me to post my entire project. 我很乐意发布任何必要的代码,但我怀疑你是否要我发布我的整个项目。 A few things I think might be useful are......
我认为可能有用的一些事情是......
My Vertex Struct: 我的顶点结构:
struct Vertex
{
DirectX::XMFLOAT3 pos;
DirectX::XMFLOAT3 color;
};
My Input layout: 我的输入布局:
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 },
};
My Vertex Shader: 我的顶点着色器:
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;
}
Primitive Topology: 原始拓扑:
m_d3dContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
Index Buffer Format: 索引缓冲区格式:
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);
Here is the DrawIndexed call: 这是DrawIndexed调用:
m_d3dContext->DrawIndexed(
m_Indices.size(),
0,
0);
The code that gets the data and creates the Index Buffer: 获取数据并创建索引缓冲区的代码:
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));
Thank you for the help! 感谢您的帮助! I have been working with "Introduction to 3D Game Programming with DirectX 11" by Frank Luna but I can't find any discussion on issues like these.
我一直在使用Frank Luna的“使用DirectX 11进行3D游戏编程简介”,但我找不到关于这些问题的任何讨论。
You are most likely making a mistake in one of the following. 您最有可能在以下某个方面犯了错误。 A mistake in any could easily cause the issue you are observing.
任何错误都可能轻易导致您正在观察的问题。
// 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.