顶点缓冲区的向量和数组之间的区别是什么

时间:2014-04-08 01:04:33

标签: c++ arrays vector

我有以下代码可以很好地呈现正方形:

            static const VertexPositionColor cubeVertices[] =
            {
                { XMFLOAT3( 1.0f, -1.0f, 0.0f ), XMFLOAT3( 1.0f, 0.0f, 0.0f ) },
                { XMFLOAT3( -1.0f, -1.0f, 0.0f ), XMFLOAT3( 1.0f, 0.0f, 0.0f ) },
                { XMFLOAT3( 1.0f, 1.0f, 0.0f ), XMFLOAT3( 1.0f, 0.0f, 0.0f ) },
                { XMFLOAT3( -1.0f, 1.0f, 0.0f ), XMFLOAT3( 1.0f, 0.0f, 0.0f ) },
            };

            D3D11_SUBRESOURCE_DATA vertexBufferData = { 0 };
            vertexBufferData.pSysMem = &cubeVertices;
            vertexBufferData.SysMemPitch = 0;
            vertexBufferData.SysMemSlicePitch = 0;
            CD3D11_BUFFER_DESC vertexBufferDesc( sizeof( cubeVertices ), D3D11_BIND_VERTEX_BUFFER );
            DX::ThrowIfFailed(
                m_deviceResources->GetD3DDevice()->CreateBuffer(
                &vertexBufferDesc,
                &vertexBufferData,
                &m_vertexBuffer[ a ]
                )
                );

现在,拿这个代码......这是完全相同sizeof(96)的精确复制品...但是这个使用了一个向量...为什么它什么都没有渲染?

        FormatCollada* colladaObj = new FormatCollada();
        static const vector<VertexPositionColor> cubeVertices = colladaObj->Format( *geometryData->Collada->LibraryGeometries->Geometry[ a ] );

        D3D11_SUBRESOURCE_DATA vertexBufferData = { 0 };
        vertexBufferData.pSysMem = &cubeVertices;
        vertexBufferData.SysMemPitch = 0;
        vertexBufferData.SysMemSlicePitch = 0;
        CD3D11_BUFFER_DESC vertexBufferDesc( sizeof( VertexPositionColor ) * cubeVertices.size(), D3D11_BIND_VERTEX_BUFFER );
        DX::ThrowIfFailed(
            m_deviceResources->GetD3DDevice()->CreateBuffer(
            &vertexBufferDesc,
            &vertexBufferData,
            &m_vertexBuffer[ a ]
            )
            );

1 个答案:

答案 0 :(得分:0)

&cubeVertices没有提供vector的第一个元素的地址,它会为您提供矢量对象本身的地址。您可能想要cubeVertices.data()