1

此代码工作正常:

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

屏幕清除为我清晰的颜色,但屏幕上没有绘制任何形状。

我认为 &m_Vertices[0] 相当于一个数组?我检查了调试中的值,数组和我的 CreateCube 函数都产生了相同的数据。

4

1 回答 1

1

sizeof(m_Vertices)为您提供矢量对象本身的大小,而不是它存储的元素数量。你可能想要的是m_Vertices.size() * sizeof(Vertex).

于 2013-10-22T16:18:49.747 回答