template <class VertexType>
void Primitive::SetVertices(ID3D11Device* device, vector<VertexType> vertices, int size)
{
// Fill out the D3D11_BUFFER_DESC struct.
D3D11_BUFFER_DESC vbd;
vbd.Usage = D3D11_USAGE_DYNAMIC;
vbd.ByteWidth = sizeof(VertexType) * size;
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vbd.MiscFlags = 0;
// Set the init data.
D3D11_SUBRESOURCE_DATA initData;
initData.pSysMem = &vertices[0];
// Create the vertex buffer.
HR(device->CreateBuffer(&vbd, &initData, &mVertexBuffer));
mNumVertices = size;
}
And this is the attempt to update the vertex buffer:
void Terrain::UpdateVertices()
{
ID3D11DeviceContext* context = GetD3DContext();
D3D11_MAPPED_SUBRESOURCE resource;
HRESULT hr = context->Map(mPrimitive->GetVertices(), 0, D3D11_MAP_WRITE, 0, &resource);
Vertex* vertices = (Vertex*)resource.pData;
for(int i = 0; i < mPrimitive->NumVertices(); i++)
{
vertices[i].Pos.y = GetHeight(vertices->Pos.x, vertices->Pos.z); // Change the vertex Y pos.
}
context->Unmap(mPrimitive->GetVertices(), 0);
}
I get no error if I use D3D11_MAP_WRITE_DISCARD instead. I'm not even sure if my approach to update the vertex buffer is correct in this situation. It's a terrain editor where the user will be able to use tools to change the terrain. It feels unneccessary to update every vertex in the terrain when the tools only will effect a certain radius. Is UpdateSubresource() better in this case?
Anyways, I want to get it working with Map() so I then can compare the performance. Thanks for helping
EDIT: The code tags are messing with me...
Edited by simpler, 24 October 2012 - 08:04 AM.







