2017-05-22 47 views
0

我是用DirectXToolKit做一些基本的渲染,我希望能夠獲得每個模型的頂點座標爲了計算模型之間的衝突。如何從DirectXTK(ToolKit)獲取所有頂點座標DirectX :: Model類用於碰撞檢測

目前

,我有一些測試代碼加載模式,但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); 
} 

我知道有一種方式來獲得從ID3D11Buffer頂點,回答在這裏: How to read vertices from vertex buffer in Direct3d11

但他們建議不要從GPU內存加載。所以我認爲最好是提前加載頂點到一個單獨的容器中。

回答

0

我看着CreateFromSDKMESH並且有可公開訪問,無需進行更改XTK

爲了得到頂點在加載模式中的一些功能,更換線路mModel = DirectX::Model::CreateFromSDKMESH(p_device, file_mesh, *mFxFactory);在上面的問題有:

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)); 

有,訪問座標應儘可能簡單

float x = verts_pairs[0].first.x; 和頂點的總數被存儲在

vertex_header.NumVertices

不要忘記,加載頂點緩衝之後被刪除,所以你可能想要做的事,如:

memcpy(vertexBuffer, reinterpret_cast<std::pair<Vector3,Vector3>*>(buffer_data + (vertex_header.DataOffset - buffer_data_offset)), vertexCnt); 

此外,頂點緩存沒有得到轉化與平局的功能,所以你需要做一些改變

謝謝,