I have a std::vector<MyVertex>
where MyVertex
is a struct. I have to push this data to the vertex buffer in Direct3D 9 and the examples I've seen use a memcpy
. Unfortunately, memcpy
crashes my application so I'm definitely doing something wrong.
std::vector<MyVertex> m_VertsBuff0;
void* vbPtr;
vertexbuffer->Lock (0, 0, &vbPtr, D3DLOCK_DISCARD);
memcpy (vbPtr, &m_VertsBuff0[0], sizeof(m_VertsBuff0)); // also tried sizeof(MyVertex)*m_VertsBuff0.size()
// std::copy(m_VertsBuff0.begin(), m_VertsBuff0.end(), vbPtr); // gives a compiler error void* unknown size
vertexbuffer->Unlock ();
device->SetStreamSource (0, vertexbuffer, 0, sizeof(m_VertsBuff0[0]));
Update:
This was working before when I just used an array
instead of a vector
. It didn't seem that I had to initialize the void*
in the first place, because the example was working just fine. Then I changed it to a vector
and it went wrong. Why is it that I have to initialize the void*
all of a sudden and doing so still crashes my application.
memcpy (vbPtr, m_VertsBuff0.data(), sizeof(MyVertex) * m_VertsBuff0.size());
You must initialize your vbPtr;
void* vbPtr = new char[v.size() * sizeof(MyVertex)];
memcpy (vbPtr, v.data(), v.size() * sizeof(MyVertex));