简体   繁体   English

Direct3D 11 - 绘制三角形

[英]Direct3D 11 - Drawing a Triangle

I have initialise all of Direct3D 11 but when i come to draw some geometry the geometry doesn't show. 我已经初始化了所有的Direct3D 11,但是当我绘制一些几何体时,几何体没有显示出来。

I have created the Pipeline, the Geometry and created a draw function for the geometry to be drawn. 我创建了Pipeline,Geometry并为要绘制的几何体创建了绘制函数。 My main question is can anyone help me understand why the Triangle isn't drawn in the window? 我的主要问题是,任何人都可以帮助我理解为什么三角形不会在窗口中绘制出来吗?

void cSystem::Draw(UINT count)
{
    // draw stuff...    
    UINT stride = sizeof(Vertex);
    UINT offset = 0;
    p_D3D11DeviceContext->IASetVertexBuffers(0, 1, &p_VertextBuffer, &stride, &offset);

    p_D3D11DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    p_D3D11DeviceContext->Draw(3, 0);

    if (count == 0)
    {
        INFO(L"Draw() Complete");
    }   
}

void cSystem::InitGeometry() // initialise the geometry for the scene
{   
    Vertex vertices[] =
    {
        { D3DXVECTOR3(0.0f, 0.5f, 0.5f), D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) },
        { D3DXVECTOR3(0.5f, -0.5f, 0.5f), D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) },
        { D3DXVECTOR3(-0.5f, -0.5f, 0.5f), D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f) }
    };

    D3D11_BUFFER_DESC bdsc;
    ZeroMemory(&bdsc, sizeof(D3D11_BUFFER_DESC));
    bdsc.Usage = D3D11_USAGE_IMMUTABLE;
    bdsc.ByteWidth = sizeof(Vertex) * 3;
    bdsc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bdsc.CPUAccessFlags = 0;
    bdsc.MiscFlags = 0;
    D3D11_SUBRESOURCE_DATA vinitdata;
    ZeroMemory(&vinitdata, sizeof(D3D11_SUBRESOURCE_DATA));
    vinitdata.pSysMem = vertices;

    p_D3D11Device->CreateBuffer(&bdsc, &vinitdata, &p_VertextBuffer);   

    /*D3D11_MAPPED_SUBRESOURCE ms;
    p_D3D11DeviceContext->Map(p_VertextBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);
    memcpy(ms.pData, vertices, sizeof(Vertex));
    p_D3D11DeviceContext->Unmap(p_VertextBuffer, NULL);*/

    INFO(L"InitGeometry() Complete");
}

void cSystem::InitPipeline(void)
{   
#if defined(DEBUG) || (_DEBUG)
    DWORD shaderflags = 0;
#endif
    //p_D3D11Device->GetFeatureLevel() == D3D_FEATURE_LEVEL_11_0;
    ID3D10Blob *VS_, *PS_ = NULL;
    D3DX11CompileFromFile(L"Shader.shader", 0, 0, "VS", "vs_5_0", 0, 0, 0, &VS_, 0, 0);
    D3DX11CompileFromFile(L"Shader.shader", 0, 0, "PS", "ps_5_0", 0, 0, 0, &PS_, 0, 0);

    p_D3D11Device->CreateVertexShader(VS_->GetBufferPointer(), VS_->GetBufferSize(), NULL, &p_VS);
    p_D3D11Device->CreatePixelShader(PS_->GetBufferPointer(), PS_->GetBufferSize(), NULL, &p_PS);

    p_D3D11DeviceContext->VSSetShader(p_VS, 0, 0);
    p_D3D11DeviceContext->PSSetShader(p_PS, 0, 0);

    D3D11_INPUT_ELEMENT_DESC layout[] =
    {
        { "POSITION", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    };
    UINT NumElements = ARRAYSIZE(layout);

    p_D3D11Device->CreateInputLayout(layout, NumElements, VS_->GetBufferPointer(),
    VS_->GetBufferSize(), &p_InputLayout);

    p_D3D11DeviceContext->IASetInputLayout(p_InputLayout);

    VS_->Release();
    PS_->Release();

    INFO(L"InitPipeline() Complete");
}

void cSystem::D3D11RenderFrame_(void)
{
    // clear the back buffer
    p_D3D11DeviceContext->ClearRenderTargetView(p_D3D11RenderTargetView, m_BackBufferColour[0]);
    // clear the depth buffer
    p_D3D11DeviceContext->ClearDepthStencilView(p_D3D11DepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0.0f);  

    // draw 
    Draw(count++);

    // switch the back buffer and the front buffer
    p_D3D11SwapChain->Present(0, 0);
}

The shader (or .fx file) is 着色器(或.fx文件)是

struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

VOut VS(float4 position : POSITION, float4 color : COLOR)
{
    VOut output;

    output.position = position;
    output.color = color;

    return output;
}


float4 PS(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
    return color;
}

A link to a pic of the program is here : http://i.imgur.com/JyNkPqQ.png 这里有一个图片链接: http//i.imgur.com/JyNkPqQ.png

The calls are made correctly in the execution of the program, but no display.. Just in case my question is forgotten from the top, why isn't the triangle displaying? 调用是在程序的执行中正确进行的,但没有显示..为了防止我的问题从顶部被遗忘,为什么不显示三角形?

i hope someone can help.. :) 我希望有人能帮帮忙.. :)

I'm no guru in DirectX11 (in fact I've just started recently) but it seems that even though your vertex shader will be given a D3DXVECTOR3 for the position of the vertex, you're telling it that POSITION is 我不是DirectX11中的大师(事实上我刚刚开始)但似乎即使你的顶点着色器将被赋予顶点位置的D3DXVECTOR3 ,你也告诉它POSITION

A four-component, 32-bit unsigned-normalized-integer format that supports 8 bits per channel including alpha. 一种四分量,32位无符号归一化整数格式,每个通道支持8位,包括alpha。

in your input layout. 在您的输入布局中。 Now that's confusing! 现在这令人困惑!

You should use DXGI_FORMAT_R32G32B32_FLOAT . 你应该使用DXGI_FORMAT_R32G32B32_FLOAT

Thank you for reading. 谢谢你的阅读。

This might not exactly be the answer, but hopefully it is helpful. 这可能不完全是答案,但希望它是有帮助的。 It is sometimes difficult to tell why something isn't drawing just from looking at the code. 有时很难说出为什么某些东西不是仅仅通过查看代码来绘制的。 There could be multiple reasons why, and sometimes they might be kind of hidden. 可能有多种原因,有时可能会隐藏它们。

Generally, when I encounter this problem, I load up a graphics debugger like the one included in Visual Studio, and I look at every piece of state sent to the GPU to figure out what went wrong. 通常,当我遇到这个问题时,我会加载一个图形调试器,就像Visual Studio中包含的图形调试器一样,我会查看发送到GPU的每个状态以找出问题所在。 This may be difficult at first, however, when you don't know exactly what each piece of state does and what it should be. 但是,如果你不确切地知道每个州的状况和它应该是什么,那么这一开始可能很困难。 But, eventually you learn and it takes the guesswork out figuring out what went wrong. 但是,最终你会学习,并且需要猜测出问题是什么。

Another thing that is usually helpful is to enable debug mode on your ID3D11Device . 另一件通常有用的事情是在ID3D11Device上启用调试模式。 To do this set the Flags parameter of D3D11CreateDevice or D3D11CreateDeviceAndSwapChain (whichever you are using) to D3D11_CREATE_DEVICE_DEBUG . 为此, D3D11CreateDeviceD3D11CreateDeviceAndSwapChainFlags参数(无论您使用哪个)设置为D3D11_CREATE_DEVICE_DEBUG This will output warnings if something is going wrong. 如果出现问题,这将输出警告。

Eventually you'll want to learn how to use the graphics debugger, but before you get to that point, I would recommend trying to follow the tutorial exactly at first, and then go back and change it more to your liking. 最后你会想学习如何使用图形调试器,但在你达到这一点之前,我建议你先尝试完全按照教程,然后再回头根据自己的喜好进行更改。 With each significant change you make, run your program to make sure it still works. 每做一次重大更改,运行程序以确保它仍然有效。 When it doesn't, you know what went wrong. 如果没有,你知道出了什么问题。

To your problem. 对你的问题。 One issue I see is that you haven't declared your color element in your input layout. 我看到的一个问题是你没有在输入布局中声明你的颜色元素。 You'll need to add this after your position element: 您需要在position元素后面添加:

{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }

Otherwise, your shader doesn't know where to find the color in your vertex stream. 否则,着色器不知道在顶点流中找到颜色的位置。 Note that position must also be DXGI_FORMAT_R32G32B32_FLOAT as mentioned in Nard's answer. 请注意,位置也必须是Nard的答案中提到的DXGI_FORMAT_R32G32B32_FLOAT Without these two things, CreateInputLayout will return a null input layout because it doesn't match the input signature of the vertex shader. 如果没有这两件事, CreateInputLayout将返回一个空输入布局,因为它与顶点着色器的输入签名不匹配。

Also be sure that you are setting up a viewport. 还要确保您正在设置视口。

There may be other issues, but it can be hard to tell without seeing every API call made. 可能还有其他问题,但如果没有看到每个API调用都很难说。

Notice that you didn't actually do any transform in vertex shader, according to my experience, add three transform matrix to the vertex shader should do the job. 请注意,您实际上并没有在顶点着色器中进行任何变换,根据我的经验,在顶点着色器中添加三个变换矩阵应该完成工作。

 cbuffer Transform
 {
     matrix mWorld;
     matrix mView;
     matrix mProjection;
 }

then in vertex shader, do this 然后在顶点着色器中,执行此操作

output.position= mul(position, mWorld);
output.position= mul(output.position, mView);
output.position= mul(output.position, mProjection);

you need to create a D3D11_BIND_CONSTANT_BUFFER buffer for it, and use ID3D11DeviceContext::VSSetConstantBuffers to bind it. 你需要为它创建一个D3D11_BIND_CONSTANT_BUFFER缓冲区,并使用ID3D11DeviceContext::VSSetConstantBuffers来绑定它。

you can use #include <DirectXMath.h> to control the matrix, for example, use XMMatrixTranslation and XMMatrixRotation to change mWorld, use XMMatrixLookAtLH to change mView, use XMMatrixPerspective to change mProjection. 你可以使用#include <DirectXMath.h>来控制矩阵,例如,使用XMMatrixTranslationXMMatrixRotation来改变mWorld,使用XMMatrixLookAtLH来改变mView,使用XMMatrixPerspective来改变mProjection。

after you get the matrix at cpu memory, you need use ID3D11DeviceContext::UpdateSubresource to update the data to gpu memory (the constant buffers that you created above). 在cpu内存中获取矩阵后,需要使用ID3D11DeviceContext::UpdateSubresource将数据更新为gpu内存(上面创建的常量缓冲区)。 remember to do XMMatrixTranspose before you pass the matrix. 记得在传递矩阵之前做XMMatrixTranspose

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

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