简体   繁体   English

通过 DirectX12 工具包绘制球体

[英]Drawing spheres via DirectX12 Tool Kit

I have three problems when trying to draw spheres:尝试绘制球体时遇到三个问题:

  1. Sphere is glitching in some angles:球体在某些角度出现故障: 在此处输入图像描述
  2. Don't know how to apply a color to sphere不知道如何将颜色应用于球体
  3. Can't draw two spheres at one - only one is shown.无法同时绘制两个球体 - 仅显示一个。

Code.代码。 Render:使成为:

void Game::Render()
{
if (m_timer.GetFrameCount() == 0)
{
return;
}

m_deviceResources->Prepare();
Clear();

auto commandList = m_deviceResources->GetCommandList();
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Render");

auto commandList2 = m_deviceResources->GetCommandList();
PIXBeginEvent(commandList2, PIX_COLOR_DEFAULT, L"Draw Sphere");

m_shapeEffect->Apply(m_deviceResources->GetCommandList());

for (int i(0); i < vsphere; i++)
{
m_shapeEffect->SetWorld(d3dsphere[i]->world);
d3dsphere[i]->Draw(commandList2);
}

PIXEndEvent(commandList2);
PIXEndEvent(commandList);
PIXBeginEvent(m_deviceResources->GetCommandQueue(), PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
m_graphicsMemory->Commit(m_deviceResources->GetCommandQueue());
PIXEndEvent(m_deviceResources->GetCommandQueue());
}

How I create shape effect我如何创建形状效果

EffectPipelineStateDescription pd(
&VertexPositionColor::InputLayout,
CommonStates::Opaque,
CommonStates::DepthNone,
CommonStates::CullNone,
rtState,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE);
m_shapeEffect = std::make_unique<BasicEffect>(device, EffectFlags::VertexColor, pd);

d3dsphere is a D3DSphere class object; d3dsphere是一个D3DSphere class object; the constructor and Draw method构造函数和Draw方法

D3DSphere(float x, float y, float z, float radius, float r, float g, float b, float a)  
{
this->x = x;
this->y = y;
this->z = z;

this->radius = radius;

this->shape = GeometricPrimitive::CreateSphere(radius*2.0f);

this->world = Matrix::Identity;
this->world = XMMatrixMultiply(this->world, Matrix::CreateTranslation(x, y, z));
this->index = vsphere;

d3dsphere[vsphere] = this;
vsphere++;
}

void D3DSphere::Draw(ID3d12GraphicsCommandList* commandList)
{
   this->shape->Draw(commandList);
}

Perhaps it will be useful if I figure, that the sphere radius(and everything in my scene) is very tiny(~10^-6)如果我认为球体半径(以及我场景中的所有内容)非常小(~10^-6),也许会很有用

By unknown for me reasons, the code of examples in Microsoft DirectX12 Tool Kit wiki page in most is not compatible with the headers I have installed via NuGet packages - I have different constructors, different arguments in methods.由于我不知道的原因, Microsoft DirectX12 Tool Kit wiki 页面中的示例代码与我通过 NuGet 包安装的头文件不兼容 - 我有不同的构造函数,不同的 arguments 方法。 I think the problem is that I am using Visual Studio 15, but Microsoft recommends to use at least 17(there are two different DirectX12TK NuGet packages - one for VS15 and one for VS17 and above).我认为问题在于我使用的是 Visual Studio 15,但微软建议至少使用 17(有两种不同的 DirectX12TK NuGet 包 - 一种用于 VS15,一种用于 VS17 及更高版本)。 It is weird.真奇怪。


I solved the third problem by changing code in rendering:我通过更改渲染代码解决了第三个问题:

for(int i(0);i<vsphere;i++)
{
m_shapeEffect->SetMatrices(d3dsphere->world, m_view, m_projection);
m_shapeEffect->Apply(m_deviceResources->GetCommandList());
d3dsphere[i]->Draw();
}

DirectX12TK NuGet package version I use is 2019.12.17.1我使用的DirectX12TK NuGet package版本是2019.12.17.1

Here you told the Pipeline State Object that you are using VertexPositionColor and want per-vertex color:在这里,您告诉 Pipeline State Object 您正在使用VertexPositionColor并希望每个顶点颜色:

EffectPipelineStateDescription pd(
&VertexPositionColor::InputLayout,
CommonStates::Opaque,
CommonStates::DepthNone,
CommonStates::CullNone,
rtState,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE);
m_shapeEffect = std::make_unique<BasicEffect>(device, EffectFlags::VertexColor, pd);

The actual vertex type created by GeometricPrimitive's factory methods is VertexPositionNormalTexture (the VertexType type alias is there to make that a bit easier). GeometricPrimitive 的工厂方法创建的实际顶点类型是VertexPositionNormalTextureVertexType类型别名是为了让它更容易一些)。

Therefore, the vertex data contains因此,顶点数据包含

struct VertexPositionNormalTexture
{
    XMFLOAT3 position;
    XMFLOAT3 normal;
    XMFLOAT2 textureCoordinate;
}

But you told the vertex shader that it's:但是你告诉顶点着色器它是:

const D3D12_INPUT_ELEMENT_DESC VertexPositionColor::InputElements[] =
{
    { "SV_Position", 0, DXGI_FORMAT_R32G32B32_FLOAT,    0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
    { "COLOR",       0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
};

Change your BasicEffect to:将您的 BasicEffect 更改为:

EffectPipelineStateDescription pd(
&GeometricPrimitive::VertexType::InputLayout,
CommonStates::Opaque,
CommonStates::DepthNone,
CommonStates::CullNone,
rtState,
D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE);
m_shapeEffect = std::make_unique<BasicEffect>(device, EffectFlags::None, pd);

It will look boring without EffectFlags::Lighting and m_shapeEffect->EnableDefaultLighting();没有EffectFlags::Lightingm_shapeEffect->EnableDefaultLighting();看起来会很无聊, but should render. ,但应该渲染。

If you want to create a vertex format other than VertexPositionNormalTexture , you can use the GeometricPrimitive custom geometry methods to generate the shape data, but you'll need implement the creation of the VB/IB and rendering in your own code (ie the GeometricPrimitive::CreateCustom method only supports VertexPositionNormalTexture ).如果要创建VertexPositionNormalTexture以外的顶点格式,可以使用GeometricPrimitive自定义几何方法来生成形状数据,但您需要在自己的代码中实现 VB/IB 的创建和渲染(即GeometricPrimitive::CreateCustom方法仅支持VertexPositionNormalTexture )。

D3D12_VERTEX_BUFFER_VIEW m_vertexBufferView;
D3D12_INDEX_BUFFER_VIEW m_indexBufferView;

UINT m_indexCount;

Microsoft::WRL::ComPtr<ID3D12Resource> m_vertexBuffer;
Microsoft::WRL::ComPtr<ID3D12Resource> m_indexBuffer;
// Create shape data
std::vector<VertexPositionNormalTexture> vertices;
std::vector<uint16_t> indices;
GeometricPrimitive::CreateSphere(vertices, indices);

std::vector<VertexPositionColor> newVerts;
newVerts.reserve(vertices.size());
for (auto it : vertices)
{
    VertexPositionColor v;
    v.position = it.position;
    v.color = XMFLOAT4(it.normal.x, it.normal.y, it.normal.z, 1.f);
    newVerts.emplace_back(v);
}

// Place data on upload heap
size_t vsize = newVerts.size() * sizeof(VertexPositionColor);
SharedGraphicsResource vb = GraphicsMemory::Get().Allocate(vsize);
memcpy(vb.Memory(), newVerts.data(), vsize);

size_t isize = indices.size() * sizeof(uint16_t);
SharedGraphicsResource ib = GraphicsMemory::Get().Allocate(isize);
memcpy(ib.Memory(), indices.data(), isize);

// You can render directly from the 'upload' heap or as shown here create static IB/VB resources
ResourceUploadBatch resourceUpload(device);
resourceUpload.Begin();

CD3DX12_HEAP_PROPERTIES heapProperties(D3D12_HEAP_TYPE_DEFAULT);

auto vdesc = CD3DX12_RESOURCE_DESC::Buffer(vsize);
auto idesc = CD3DX12_RESOURCE_DESC::Buffer(isize);

DX::ThrowIfFailed(device->CreateCommittedResource(
    &heapProperties, D3D12_HEAP_FLAG_NONE, &vdesc, D3D12_RESOURCE_STATE_COPY_DEST,
    nullptr, IID_PPV_ARGS(m_vertexBuffer.GetAddressOf())));

DX::ThrowIfFailed(device->CreateCommittedResource(
    &heapProperties, D3D12_HEAP_FLAG_NONE, &idesc, D3D12_RESOURCE_STATE_COPY_DEST,
    nullptr, IID_PPV_ARGS(m_indexBuffer.GetAddressOf())));

resourceUpload.Upload(m_vertexBuffer.Get(), vb);
resourceUpload.Upload(m_indexBuffer.Get(), ib);

resourceUpload.Transition(m_vertexBuffer.Get(),
    D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
resourceUpload.Transition(m_indexBuffer.Get(),
    D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_INDEX_BUFFER);

auto uploadResourcesFinished = resourceUpload.End(m_deviceResources->GetCommandQueue());
uploadResourcesFinished.wait();

// Create matching effect for new vertex layout
EffectPipelineStateDescription psd(
    &VertexPositionColor::InputLayout,
    CommonStates::Opaque,
    CommonStates::DepthDefault,
    CommonStates::CullNone,
   rtState);

m_shapeEffect = std::make_unique<BasicEffect>(device, EffectFlags::VertexColor, psd);

// Set up buffer views
m_vertexBufferView = { m_vertexBuffer->GetGPUVirtualAddress(), UINT(vsize), sizeof(VertexPositionColor) };
m_indexBufferView = { m_indexBuffer->GetGPUVirtualAddress(), UINT(isize), DXGI_FORMAT_R16_UINT };

m_indexCount = UINT(indices.size());
m_shapeEffect->Apply(commandList);

commandList->IASetVertexBuffers(0, 1, &m_vertexBufferView);
commandList->IASetIndexBuffer(&m_indexBufferView);
commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

commandList->DrawIndexedInstanced(m_indexCount, 1, 0, 0, 0);

The latest release of the DirectX Tool Kit for DX12 that supports Visual Studio 2015 is December 2019 which is directxtk12_desktop_2015 .支持 Visual Studio 2015 的DirectX Tool Kit for DX12的最新版本是2019 年 12 月,即directxtk12_desktop_2015 There's very little functional or API difference between December 2019 and April 2020, so I'm not sure why you think the wiki is outdated. 2019 年 12 月和 2020 年 4 月之间几乎没有功能或 API 差异,所以我不确定您为什么认为 wiki 已过时。 What NuGet package and version are you using?您使用的是什么 NuGet package 和版本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM