此代码可以正常工作:
Vertex cubeVertices[] =
{
{XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
{XMFLOAT3(-0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
{XMFLOAT3(-0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
{XMFLOAT3(-0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
{XMFLOAT3( 0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
{XMFLOAT3( 0.5f, -0.5f, 0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
{XMFLOAT3( 0.5f, 0.5f, -0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
{XMFLOAT3( 0.5f, 0.5f, 0.5f), XMFLOAT3(0.0f, 0.133f, 0.333f)},
};
D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
vertexBufferData.pSysMem = cubeVertices;
vertexBufferData.SysMemPitch = 0;
vertexBufferData.SysMemSlicePitch = 0;
//THE PROBLEM IS HERE
//if I use sizeof(m_Vertices) it no longer works.
//I still don't understand though because m_Vertices should still be in scope
CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(cubeVertices), D3D11_BIND_VERTEX_BUFFER);
DX::ThrowIfFailed(
m_d3dDevice->CreateBuffer(
&vertexBufferDesc,
&vertexBufferData,
&m_vertexBuffer));
它在我的屏幕上绘制了一个可爱的立方体。
此代码不起作用:
GeometryGenerator generator;
GeometryGenerator::MeshData cubeData;
generator.CreateCube(cubeData);
//I thought it might be a scope problem
//m_Vertices is a std::Vector<Vertex>
m_Vertices.resize(cubeData.Vertices.size());
for(size_t i = 0; i < cubeData.Vertices.size(); ++i)
{
m_Vertices[i].pos = cubeData.Vertices[i].Position;
m_Vertices[i].color = XMFLOAT3(0.0f, 0.133f, 0.333f);
}
D3D11_SUBRESOURCE_DATA vertexBufferData = {0};
vertexBufferData.pSysMem = &m_Vertices[0];
vertexBufferData.SysMemPitch = 0;
vertexBufferData.SysMemSlicePitch = 0;
//THE PROBLEM IS HERE
//if I use sizeof(m_Vertices) it no longer works.
//I still don't understand though because m_Vertices should still be in scope
CD3D11_BUFFER_DESC vertexBufferDesc(sizeof(m_Vertices), D3D11_BIND_VERTEX_BUFFER);
DX::ThrowIfFailed(
m_d3dDevice->CreateBuffer(
&vertexBufferDesc,
&vertexBufferData,
&m_vertexBuffer));
屏幕清除为我的清晰颜色,但屏幕上没有任何形状。
我认为&amp; m_Vertices [0]相当于一个数组?我已经检查了调试中的值,数组和我的CreateCube函数都生成了相同的数据。
答案 0 :(得分:1)
sizeof(m_Vertices)
为您提供矢量对象本身的大小,而不是它存储的元素数量。你可能想要的是m_Vertices.size() * sizeof(Vertex)
。