[英]How to get all vertex cordinates from DirectXTK (ToolKit) DirectX::Model class to use for collision detection
I'm, doing some basic rendering with DirectXToolKit and I would like to be able to get the vertex coordinates for each model in order to compute collisions between models. 我正在使用DirectXToolKit做一些基本的渲染,我希望能够获取每个模型的顶点坐标,以便计算模型之间的碰撞。
currently, I have some test code to load the model, but the ID3D11Buffer
loads internally using CreateFromSDKMESH
当前,我有一些测试代码可以加载模型,但是ID3D11Buffer
内部使用CreateFromSDKMESH
加载
void Model3D::LoadSDKMESH(ID3D11Device* p_device, ID3D11DeviceContext* device_context, const wchar_t* file_mesh)
{
mAlpha = 1.0f;
mTint = DirectX::Colors::White.v;
mStates.reset(new DirectX::CommonStates(p_device));
auto fx = new DirectX::EffectFactory(p_device);
fx->SetDirectory(L"media");
mFxFactory.reset(fx);
mBatch.reset(new DirectX::PrimitiveBatch<DirectX::VertexPositionColor>(device_context));
mBatchEffect.reset(new DirectX::BasicEffect(p_device));
mBatchEffect->SetVertexColorEnabled(true);
{
void const* shaderByteCode;
size_t byteCodeLength;
mBatchEffect->GetVertexShaderBytecode(&shaderByteCode, &byteCodeLength);
HR(p_device->CreateInputLayout(DirectX::VertexPositionColor::InputElements,
DirectX::VertexPositionColor::InputElementCount,
shaderByteCode, byteCodeLength,
mBatchInputLayout.ReleaseAndGetAddressOf()));
}
mModel = DirectX::Model::CreateFromSDKMESH(p_device, file_mesh, *mFxFactory);
}
I know there is a way to get vertexes from the ID3D11Buffer
, answered here: How to read vertices from vertex buffer in Direct3d11 我知道有一种方法可以从ID3D11Buffer
获取顶点,在这里回答: 如何从Direct3d11中的顶点缓冲区读取顶点
But they suggest not loading from GPU memory. 但是他们建议不要从GPU内存中加载。 So I assume it's better to load vertices ahead of time into a separate container. 因此,我认为最好事先将顶点加载到单独的容器中。
I looked into CreateFromSDKMESH
and there are a few functions that are publicly accessible without making changes to XTK 我调查了CreateFromSDKMESH
,有一些函数可以公开访问而无需更改XTK
In order to get Vertices while loading a model, replace the line mModel = DirectX::Model::CreateFromSDKMESH(p_device, file_mesh, *mFxFactory);
为了在加载模型时获得顶点,请替换行mModel = DirectX::Model::CreateFromSDKMESH(p_device, file_mesh, *mFxFactory);
in the question above with: 在上面的问题中:
size_t data_size = 0;
std::unique_ptr<uint8_t[]> v_data;
HRESULT hr = DirectX::BinaryReader::ReadEntireFile(file_mesh, v_data, &data_size);
if (FAILED(hr))
{
DirectX::DebugTrace("CreateFromSDKMESH failed (%08X) loading '%ls'\n", hr, file_mesh);
throw std::exception("CreateFromSDKMESH");
}
uint8_t* mesh_data = v_data.get();
mModel = DirectX::Model::CreateFromSDKMESH(p_device, v_data.get(), data_size, *mFxFactory, false, false);
mModel->name = file_mesh;
auto v_header = reinterpret_cast<const DXUT::SDKMESH_HEADER*>(mesh_data);
auto vb_array = reinterpret_cast<const DXUT::SDKMESH_VERTEX_BUFFER_HEADER*>(mesh_data + v_header->VertexStreamHeadersOffset);
if(v_header->NumVertexBuffers < 1)
throw std::exception("Vertex Buffers less than 1");
auto& vertex_header = vb_array[0];
uint64_t buffer_data_offset = v_header->HeaderSize + v_header->NonBufferDataSize;
uint8_t* buffer_data = mesh_data + buffer_data_offset;
auto verts_pairs = reinterpret_cast<std::pair<Vector3,Vector3>*>(buffer_data + (vertex_header.DataOffset - buffer_data_offset));
There, accessing a coordinate should be as simple as 在那里,访问坐标应该很简单
float x = verts_pairs[0].first.x;
and the total number of vertices is stored in 顶点总数存储在
vertex_header.NumVertices
Dont forget that after loading the vertex buffer gets deleted, so you may want to do something like: 不要忘记,在加载顶点缓冲区后,该缓冲区将被删除,因此您可能需要执行以下操作:
memcpy(vertexBuffer, reinterpret_cast<std::pair<Vector3,Vector3>*>(buffer_data + (vertex_header.DataOffset - buffer_data_offset)), vertexCnt);
Also, vertex buffer doesn't get transformed with draw functions, so you will need to do transforms yourselves 此外,顶点缓冲区不会通过绘制函数进行转换,因此您需要自己进行转换
Thanks, 谢谢,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.