gpt4 book ai didi

c++ - DirectX Index Buffer 索引顺序

转载 作者:行者123 更新时间:2023-11-30 01:56:44 36 4
gpt4 key购买 nike

我在使用 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 度的图片:

0 degrees 90 degrees 180 degrees 360 degrees

我很乐意发布任何必要的代码,但我怀疑您是否希望我发布我的整个项目。我认为可能有用的一些东西是......

我的顶点结构:

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);

顶点跨度和偏移:

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 的“Introduction to 3D Game Programming with DirectX 11”,但我找不到关于此类问题的任何讨论。

最佳答案

您很可能在以下其中一项中犯了错误。任何一个错误都可能很容易导致您观察到的问题。

// 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);

关于c++ - DirectX Index Buffer 索引顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19529899/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com