C++ DirectX索引缓冲区索引顺序
我在DirectX中绘制顶点和索引缓冲区时遇到一些问题 这是一个定义立方体的函数,但我已经注释掉了除正面以外的所有内容:C++ DirectX索引缓冲区索引顺序,c++,directx,C++,Directx,我在DirectX中绘制顶点和索引缓冲区时遇到一些问题 这是一个定义立方体的函数,但我已经注释掉了除正面以外的所有内容: void GeometryGenerator::CreateCube(MeshData& meshData) { meshData.Vertices.resize(8); meshData.Indices.resize(6); float width; float height; float depth; width = 45; height = 45;
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];
我的输入布局:
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);
顶点步长和偏移:
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));
GeometryGenerator-generator;
GeometryGenerator::MeshData立方体数据;
生成器.CreateCube(cubeData);
//m_索引是一个std::向量
m_index.resize(cubeData.index.size());
对于(size_t i=0;iCreateBuffer(
&indexBufferDesc,
&indexBufferData,
&m_indexBuffer));
谢谢你的帮助!我一直在学习Frank Luna的“DirectX 11 3D游戏编程入门”,但我找不到任何关于此类问题的讨论。您很可能在以下问题中犯了错误。任何方面的错误都很容易导致您正在观察的问题
// 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);
您最有可能在以下某个方面出错。任何方面的错误都很容易导致您正在观察的问题
// 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);
您是否将消隐设置为
D3D11\u CULL\u NONE
?不,我应该这样做吗?您应该尝试一下,它将始终从两侧渲染三角形,如果它有效,请回答,我将写一个答案我需要设置光栅化器来实现这一点还是有其他方法?我认为您必须,我来自DX9,在那里很容易更改消隐模式。您是否将消隐设置为D3D11\u CULL\u NONE
?不,我应该这样做吗?您应该试试,它将始终从两侧渲染三角形,如果可行,回答,我会写一个答案我需要设置一个光栅化器来实现这一点,还是有其他方法?我想你必须这样做,我来自DX9,在那里很容易改变剔除模式。我看了所有这些,但看不到它。我发布了上面代码的所有部分,以便您可以看到我的内容。如果有任何其他代码对您有帮助,请告诉我。@Eric在索引缓冲区创建代码中,您有一条注释“m_Indexes is a std::Vector”。这可能不准确,但我确实在网上找到了类似定义的GeometryGenerator::MeshData的代码,它将m_索引定义为向量。这与IASetIndexBuffer
中指定的格式不匹配。将格式更改为DXGI\u格式\u R32\u UINT
,或将m\u索引更改为向量
。非常感谢!看起来我需要进一步研究我的格式和数据类型。我查看了所有这些,但看不到。我发布了上面代码的所有部分,以便您可以看到我的内容。如果有任何其他代码对您有帮助,请告诉我。@Eric在索引缓冲区创建代码中,您有一条注释“m_Indexes is a std::Vector”。这可能不准确,但我确实在网上找到了类似定义的GeometryGenerator::MeshData的代码,它将m_索引定义为向量。这与IASetIndexBuffer
中指定的格式不匹配。将格式更改为DXGI\u格式\u R32\u UINT
,或将m\u索引更改为向量
。非常感谢!看起来我需要进一步研究我的格式和数据类型。