1
\$\begingroup\$

I am having trouble at rendering vertices stored in a std::vector.

// Create and initialize the vertex buffer.
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(D3D11_BUFFER_DESC));
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.ByteWidth = sizeof(VertexData) * this->vertex_data.size();
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;

D3D11_SUBRESOURCE_DATA resourceDataVertex;
ZeroMemory(&resourceDataVertex, sizeof(D3D11_SUBRESOURCE_DATA));
resourceDataVertex.pSysMem = &(this->vertex_data);

HRESULT hr = pDevice->CreateBuffer(&vertexBufferDesc, &resourceDataVertex, &(this->pVertexBuffer));
if (FAILED(hr)) return false;

this->vertex_data being a std::vector<VertexData> with VertexData being:

// Vertex data
struct VertexData {
    XMFLOAT3 v;
    XMFLOAT2 vt;
    XMFLOAT3 vn;

    VertexData(XMFLOAT3 v, XMFLOAT2 vt, XMFLOAT3 vn) : v{ v }, vt{ vt }, vn{ vn } {}
};

Now I'm not sure doing resourceDataVertex.pSysMem = &(this->vertex_data); is a good idea. I can render another mesh without problem just by changing the ID3D11Buffer vertex buffer (said mesh being known at compile-time and stored in a C-Array).

This time around I simply get no render at all. I triple checked my object is in sight and vertex_data is fully populated.

\$\endgroup\$
2
  • \$\begingroup\$ If other meshes render seamlessly check if the indices in the indexBuffer are in the right order. \$\endgroup\$ Commented Oct 16, 2015 at 16:20
  • \$\begingroup\$ Other mesh uses indices but this one doesn't : drawing it with Draw(6, 0);. Mesh is a cube face with just 2 triangles. Other mesh draws too with same drawing func - in a glitchy way but sure does ;) What about my pointer to data ? \$\endgroup\$ Commented Oct 16, 2015 at 16:22

1 Answer 1

1
\$\begingroup\$

&(this->vertex_data) is the address of the std::vector container object itself, not the address of the data you stored in it.

Instead use &(this->vertex_data.front())

\$\endgroup\$
4
  • \$\begingroup\$ I guess this is what I was looking for. Testing this right now thanks (didn't know about front() ;) - genius mate =) thanks a lot! \$\endgroup\$ Commented Oct 16, 2015 at 17:27
  • \$\begingroup\$ this->vertex_data.data() is also an option after C++11 \$\endgroup\$ Commented Oct 16, 2015 at 17:55
  • \$\begingroup\$ Correct. With VS 2012 or later, you can use this->vertex_data.data() \$\endgroup\$ Commented Oct 16, 2015 at 18:02
  • \$\begingroup\$ Actually, this->vertex_data.data() also works with VS 2010. \$\endgroup\$ Commented Oct 19, 2015 at 21:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.