首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用DX11绘制矩形

用DX11绘制矩形
EN

Stack Overflow用户
提问于 2022-08-24 10:41:06
回答 1查看 91关注 0票数 0

我需要用directx 11绘制一个简单的矩形(不是填充框)。

我找到了这个密码:

代码语言:javascript
运行
复制
    const float x = 0.1;
    const float y = 0.1;
    const float height = 0.9;
    const float width = 0.9;
    
   VERTEX OurVertices[] =
    {
        { x, y, 0, col },
        { x + width, y, 0, col },
        { x, y + height, 0, col },
        { x + width, y, 0, col },
        { x + width, y + height, 0 , col },
        { x, y + height, 0, col }
    };

    static const XMVECTORF32 col = { 1.f, 2.f, 3.f, 4.f };

    // this is the function used to render a single frame
    void RenderFrameTest(void)
    {
        // float color[4] = { 0.0f, 0.2f, 0.4f, 1.0f };
        float color[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
    
        // clear the back buffer to a deep blue
        devcon->ClearRenderTargetView(backbuffer, color);
    
        // select which vertex buffer to display
        UINT stride = sizeof(VERTEX);
        UINT offset = 0;
    
         devcon->IASetVertexBuffers(0, 1, &pVBuffer, &stride, &offset);
        // select which primtive type we are using
      //   draw the vertex buffer to the back buffer
        devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
        devcon->Draw(sizeof(OurVertices) / sizeof(VERTEX), 0);
      
        swapchain->Present(0, 0);
    
    }
    
    

   
    void DrawRectangle(float x, float y, float width, float height, XMVECTORF32 col)
    {
        D3D11_BUFFER_DESC bd;
        ZeroMemory(&bd, sizeof(bd));
        bd.Usage = D3D11_USAGE_DYNAMIC;
        bd.ByteWidth = sizeof(OurVertices);
        bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
        bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
        HRESULT val = dev->CreateBuffer(&bd, NULL, &pVBuffer);                   // create the buffer
        D3D11_MAPPED_SUBRESOURCE ms;
        val = devcon->Map(pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);   // map the buffer
        memcpy(ms.pData, OurVertices, sizeof(OurVertices));                      // copy the data
        devcon->Unmap(pVBuffer, NULL);
    }

但结果并不如我所料:

我怀疑问题是OurVertices数组和"D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP“,但我一般没有使用DirectX的经验。

你能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-24 13:41:45

您为一个矩形定义了6个顶点,这意味着您希望使用TriangleList拓扑而不是TriangleStrip拓扑。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73471717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档