首页 > 解决方案 > 用一个函数 DirectX11 绘制 2 个四边形

问题描述

我正在学习 DirectX11,我正在尝试使用单个函数绘制 2 Quad,但是当我调用此函数时,该函数会覆盖以前的 QUAD。

如果您想查看问题,这里有一个 gif:

<a href="https://gyazo.com/b819ffc64975c1531434047b9b4a92f7"><video alt="Video from Gyazo" width="1280" autoplay muted loop playsinline controls><source src="https://i.gyazo.com/b819ffc64975c1531434047b9b4a92f7.mp4" type="video/mp4" /></video></a>

这是代码:

(这是我调用函数来绘制 QUAD 的地方)

void Application::ApplicationRun()
   {
       //wind.SetEventCallBack(std::bind(&Application::OnEvent, Instance, std::placeholders::_1));
       Vertex v[] =
       {
           Vertex(-0.5f,  -0.5f,  0.0f, 1.0f), /// botom Left Point  - [0]
           Vertex(-0.5f,   0.5f,  0.0f, 0.0f), //top Left Point     - [1]
           Vertex{ 0.0f, -0.5f,   1.0f, 1.0f},     //               -[2]
           Vertex(0.0f,   0.5f,  1.0f, 0.0f), //Right Point      - [3]


       };

       Vertex vv[] =
       {
           Vertex(-0.1f,  -0.1f,  0.0f, 0.0f), /// botom Left Point  - [0]
           Vertex(-0.1f,   0.1f,  0.0f, 0.0f), //top Left Point     - [1]
           Vertex{ 0.05f, -0.1f,   0.0f, 0.0f},     //               -[2]
           Vertex(0.05f,   0.1f,  0.0f, 0.0f), //Right Point      - [3]
       };
       DWORD indices[] =
       {
           0,1,2,
           1,2,3,
       };


       while (wind.PorcessMessage())
       {

       //  

           if (Armageddon::Application::GetInstance()->GetWindow()->GetNativeKeyBoard().KeyIsPressed(AG_KEY_A))
           {

               Log::GetLogger()->trace("A ");
               wind.GetWindowGraphics()->DrawTriangle(v, ARRAYSIZE(v));


           }
           if (Armageddon::Application::GetInstance()->GetWindow()->GetNativeKeyBoard().KeyIsPressed(AG_KEY_B))
           {

               Log::GetLogger()->trace("B");
               wind.GetWindowGraphics()->DrawTriangle(vv, ARRAYSIZE(vv));



           }

       }
   } 

(这里是 DrawTriangle 函数):

void Armageddon::D3D_graphics::DrawTriangle(Vertex v[], int Vertexcount)
{



   HRESULT hr = vertexBuffer.Initialize(this->device.Get(),this->device_context.Get() ,  v, Vertexcount);


   if (FAILED(hr))
   {
       Armageddon::Log::GetLogger()->error("FAILED INITIALIZE VERTEX BUFFER  ");

   }
   DWORD indices[] =
   {
       0,1,2,
       1,2,3,
   };

    hr = this->indicesBuffer.Init(this->device.Get(), indices, ARRAYSIZE(indices));

   hr = DirectX::CreateWICTextureFromFile(this->device.Get(), L"..\\TestApplication\\assets\\Textures\\tex.png",nullptr,textures.GetAddressOf());
   if (FAILED(hr))
   {
       Armageddon::Log::GetLogger()->error("FAILED INITIALIZE WIC TEXTURE  ");

   }


}

(这里是我初始化顶点和索引缓冲区的地方):

HRESULT Initialize(ID3D11Device* device , ID3D11DeviceContext* device_context, T* data, UINT numElements)
    {

        this->bufferSize = numElements;
        this->stride = sizeof(T);

        D3D11_BUFFER_DESC vertex_buffer_desc;

        ZeroMemory(&vertex_buffer_desc, sizeof(vertex_buffer_desc));

        vertex_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
        vertex_buffer_desc.ByteWidth = sizeof(Vertex) * numElements;
        vertex_buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
        vertex_buffer_desc.CPUAccessFlags = 0;
        vertex_buffer_desc.MiscFlags = 0;


        D3D11_SUBRESOURCE_DATA VertexBufferData;
        ZeroMemory(&VertexBufferData, sizeof(VertexBufferData));
        VertexBufferData.pSysMem = data;

        HRESULT hr = device->CreateBuffer(&vertex_buffer_desc, &VertexBufferData, buffer.GetAddressOf());
        UINT offset = 0;
        device_context->IASetVertexBuffers(0, 1, buffer.GetAddressOf(), &stride, &offset);
        return hr;
    };```


HRESULT Init(ID3D11Device* device, DWORD* data, UINT n_indices)
{

    D3D11_SUBRESOURCE_DATA Indice_buffer_data;

    this->buffer_size = n_indices;
    D3D11_BUFFER_DESC Indice_buffer_desc;
    ZeroMemory(&Indice_buffer_desc, sizeof(Indice_buffer_desc));

    Indice_buffer_desc.Usage = D3D11_USAGE_DEFAULT;
    Indice_buffer_desc.ByteWidth = sizeof(DWORD) * n_indices;
    Indice_buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    Indice_buffer_desc.CPUAccessFlags = 0;
    Indice_buffer_desc.MiscFlags = 0;



    ZeroMemory(&Indice_buffer_data, sizeof(Indice_buffer_data));
    Indice_buffer_data.pSysMem = data;

    HRESULT hr = device->CreateBuffer(&Indice_buffer_desc, &Indice_buffer_data, buffer.GetAddressOf());

    return hr;
};

这是我画四边形的地方:




    void Armageddon::D3D_graphics::RenderFrame()
{
    float color[] = { 0.1f,0.1f,0.1f,1.0f };
    ImGui_ImplDX11_NewFrame();
    ImGui_ImplWin32_NewFrame();
    ImGui::NewFrame();

    if (show_demo_window)
        ImGui::ShowDemoWindow(&show_demo_window);

    // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
    {
        static float f = 0.0f;
        static int counter = 0;

        ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.

        ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
        ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
        ImGui::Checkbox("Another Window", &show_another_window);

        ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
        ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color

        if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)
            counter++;
        ImGui::SameLine();
        ImGui::Text("counter = %d", counter);

        ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
        ImGui::End();
    }

    // 3. Show another simple window.
    if (show_another_window)
    {
        ImGui::Begin("Another Window", &show_another_window);   // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
        ImGui::Text("Hello from another window!");
        if (ImGui::Button("Close Me"))
            show_another_window = false;
        ImGui::End();
    }

    ImGui::Render();
    this->device_context->OMSetRenderTargets(1, target_view.GetAddressOf(), this->depthStencilView.Get());

    this->device_context->ClearRenderTargetView(this->target_view.Get(), color);
    this->device_context->ClearDepthStencilView(this->depthStencilView.Get(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
    //ICI QU'ON FAIT TOUT LES RENDU APRES AVOIR CLEAN LE PLAN

    this->device_context->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY::D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);

    f += 0.1;
    ConstantBuffer.data.mat = DirectX::XMMatrixRotationRollPitchYaw(0.0f,0.0f,f);
    ConstantBuffer.data.mat = DirectX::XMMatrixTranspose(ConstantBuffer.data.mat);
    ConstantBuffer.data.Yoffset = f;
    ConstantBuffer.data.Xoffset = 0;
   // Armageddon::Log::GetLogger()->trace(ConstantBuffer.data.Yoffset);
    if (!ConstantBuffer.ApplyChanges())
    {
        Armageddon::Log::GetLogger()->error("ERRO WHEN APPLYING CHANGES");
    }   

    this->device_context->VSSetConstantBuffers(0, 1, ConstantBuffer.GetAdressOf());


    /***********SHADER*******************************/

    this->device_context->VSSetShader(vertexShader.GetShader(), NULL, 0);
    this->device_context->PSSetShader(pixelShader.GetShader(), NULL, 0);
    this->device_context->IASetInputLayout(this->vertexShader.GetInputLayout());

    /***********Texture Sampler*******************************/

    this->device_context->PSSetSamplers(0, 1, this->ColorSampler.GetAddressOf());

    /***********DEPHT BUFFER*******************************/

    this->device_context->OMSetDepthStencilState(this->depthStencilState.Get(), 0);

    /***********RASTERIZER STATE*******************************/

    this->device_context->RSSetState(this->rasterizerState.Get());


    /***********UPDATE LES CONSTANTS BUFFER*******************************/





    UINT stride = sizeof(Vertex);
    UINT offset = 0;
    this->device_context->IASetIndexBuffer(indicesBuffer.Get(), DXGI_FORMAT_R32_UINT, 0);

   //this->device_context->IASetVertexBuffers(0, 1, vertexBuffer.GetAddressOf(), &stride, &offset);

    this->device_context->PSSetShaderResources(0, 1, this->textures.GetAddressOf());




   this->device_context->DrawIndexed(indicesBuffer.GetSize(), 0,0);


    ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

    this->swapchain->Present(1,0);



}

标签: c++drawingdirectx-11

解决方案


推荐阅读