0
\$\begingroup\$

suppose i have a mesh, it can undergo any kind of changes - bend, stretch, even been torn. How should i update vertex buffer in DirectX for this mesh. For now i recreate it every time, and i don't see any chances for another approach because i don't know what part of vertices array can change between steps.

Is there any known approaches for this problem?

\$\endgroup\$
0
\$\begingroup\$

When creating your vertex buffer, specify D3D11_USAGE_DYNAMIC and D3D11_CPU_ACCESS_WRITE in the buffer description (in Usage and CPUAccessFlags members). This will create you a dynamic vertex buffer, which you can update by ID3D11DeviceContext::Map -ping it, copying data to it and ID3D11DeviceContext::Unmap -ping at the end. Something like this:

ID3d11DeviceContext* context; 
ID3D11Buffer* buffer; //your vertex buffer
Your_Data_Type* data; //your vertex array
int dataSize; //size of data to copy
//...

D3D11_MAPPED_SUBRESOURCE mappedResource;
void* dataPtr;
context->Map(buffer,0,D3D11_MAP_WRITE_DISCARD,0,&mappedResource);
dataPtr = (void*)mappedResource.pData;
memcpy(dataPtr,data,dataSize));
context->Unmap(buffer,0);

Another option is to create a normal vertex buffer with D3D11_USAGE_DEFAULT flag and no cpu access flags and update the buffer by calling ID3D11DeviceContext::UpdateSubResource, but it will be slower.
Like this:

context->UpdateSubresource( buffer, 0, nullptr, data, 0, 0 );
\$\endgroup\$
0
\$\begingroup\$

It's called GPU skinning (or a variation of it)

Add several uniforms that represent the changes.

Then add attributes to the vertices that represent how much they are affected by each change.

For example bulging would be going towards the normal vector:

outPos = MVP*(pos_in + bulgeFactor * normal * bulgeUniform)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.