简体   繁体   English

如何从DirectXTK(ToolKit)DirectX :: Model类获取所有顶点坐标以用于碰撞检测

[英]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.

相关问题 三角形内的顶点-碰撞检测 - Vertex inside a triangle - Collision detection 具有自定义HLSL着色器,顶点着色器输入签名的DirectX Toolkit加载模型 - DirectX Toolkit loaded model with custom HLSL shader Vertex shader input signature DirectX 11:无法从Model类渲染模型 - DirectX 11: Model not rendering from Model class 从 DirectX 11 中的 GPU 读回顶点缓冲区(并获取顶点) - Read Back Vertex Buffers from GPU in DirectX 11 (and get vertices) Directx 11-是将顶点缓冲区中的所有顶点推入顶点着色器还是将其索引? - Directx 11 - Are all Vertices from a vertex buffer pushed through to the vertex shader or just indexed ones? 如何进行圆和三角形的碰撞检测 - How to get collision detection of circle and triangle 在我的2D DirectX 9游戏中需要有关如何迭代嵌套循环以进行碰撞检测的帮助或建议 - Need help or advice on how to iterate a nested loop for collision detection in my 2d directx 9 game 如何让x和y从cvPoint分别与int进行协调? - How to get x and y cordinates separately to int from cvPoint? BGL:如何从顶点迭代器获取自定义顶点属性类实例? - BGL: How to get custom vertex properties class instance from vertex iterator? MoveIt:如何使用 collision_detection::CollisionEnv::distanceRobot 方法? - MoveIt: How to use collision_detection::CollisionEnv::distanceRobot method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM