如何在C ++(DirectX图形)中使用DrawLine

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在C ++(DirectX图形)中使用DrawLine相关的知识,希望对你有一定的参考价值。

// Draw a grid background.
        int width = static_cast<int>(rtSize.width);
        int height = static_cast<int>(rtSize.height);

        for (int x = 0; x < width; x += 10)
        
            m_pRenderTarget->DrawLine(
                D2D1::Point2F(static_cast<FLOAT>(x), 0.0f),
                D2D1::Point2F(static_cast<FLOAT>(x), rtSize.height),
                m_pLightSlateGrayBrush,
                0.5f
                );
        

这是文档中的示例。我已经包含“ D2d1.h”,我只是不知道如何创建“ m_pRenderTarget”。我正在写一个Kinect项目,我想在图像上画一条线。我真的很新,请帮助我。

答案

您看到此页面了吗?

Create an ID2D1HwndRenderTarget

快速入门教程中有如何使用Direct2D的详细步骤。

您也可以下载Windows SDK,示例包含Direct2D演示,其中包含如何创建Direct2D渲染目标的完整步骤

我已经编写了一个程序来绘制矩形,稍有变化,它可以绘制一条线,仅供参考

#include <windows.h>
#include <D2D1.h>

#define SAFE_RELEASE(P) if(P)P->Release() ; P = NULL ;

ID2D1Factory*           g_pD2DFactory   = NULL; // Direct2D factory
ID2D1HwndRenderTarget*  g_pRenderTarget = NULL; // Render target
ID2D1SolidColorBrush*   g_pBlackBrush   = NULL; // A black brush, reflect the line color

VOID CreateD2DResource(HWND hWnd)

    if (!g_pRenderTarget)
    
        HRESULT hr ;

        hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_pD2DFactory) ;
        if (FAILED(hr))
        
            MessageBox(hWnd, "Create D2D factory failed!", "Error", 0) ;
            return ;
        

        // Obtain the size of the drawing area
        RECT rc ;
        GetClientRect(hWnd, &rc) ;

        // Create a Direct2D render target
        hr = g_pD2DFactory->CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(
            hWnd, 
            D2D1::SizeU(rc.right - rc.left,rc.bottom - rc.top)
            ), 
            &g_pRenderTarget
            ) ;
        if (FAILED(hr))
        
            MessageBox(hWnd, "Create render target failed!", "Error", 0) ;
            return ;
        

        // Create a brush
        hr = g_pRenderTarget->CreateSolidColorBrush(
            D2D1::ColorF(D2D1::ColorF::Black),
            &g_pBlackBrush
            ) ;
        if (FAILED(hr))
        
            MessageBox(hWnd, "Create brush failed!", "Error", 0) ;
            return ;
        
    


VOID DrawLine(HWND hwnd)

    CreateD2DResource(hwnd) ;

    g_pRenderTarget->BeginDraw() ;

    // Clear background color to White
    g_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));

    // Draw Rectangle
    g_pRenderTarget->DrawLine(
        D2D1::Point2F(100.0f, 100.0f),
        D2D1::Point2F(500.0f, 500.0f),
        g_pBlackBrush
        );

    HRESULT hr = g_pRenderTarget->EndDraw() ;
    if (FAILED(hr))
    
        MessageBox(NULL, "Draw failed!", "Error", 0) ;
        return ;
    



VOID Cleanup()

    SAFE_RELEASE(g_pRenderTarget) ;
    SAFE_RELEASE(g_pBlackBrush) ;
    SAFE_RELEASE(g_pD2DFactory) ;


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)   

    switch (message)    
    
    case   WM_PAINT:
        DrawLine(hwnd) ;
        return 0 ;

    case WM_KEYDOWN: 
         
            switch( wParam ) 
             
            case VK_ESCAPE: 
                SendMessage( hwnd, WM_CLOSE, 0, 0 ); 
                break ; 
            default: 
                break ; 
             
         
        break ; 

    case WM_DESTROY: 
        Cleanup(); 
        PostQuitMessage( 0 ); 
        return 0; 
    

    return DefWindowProc (hwnd, message, wParam, lParam) ;


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow )


    WNDCLASSEX winClass ;

    winClass.lpszClassName = "Direct2D";
    winClass.cbSize        = sizeof(WNDCLASSEX);
    winClass.style         = CS_HREDRAW | CS_VREDRAW;
    winClass.lpfnWndProc   = WndProc;
    winClass.hInstance     = hInstance;
    winClass.hIcon         = NULL ;
    winClass.hIconSm       = NULL ;
    winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    winClass.hbrBackground = NULL ;
    winClass.lpszMenuName  = NULL;
    winClass.cbClsExtra    = 0;
    winClass.cbWndExtra    = 0;

    if (!RegisterClassEx (&winClass))   
    
        MessageBox ( NULL, TEXT( "This program requires Windows NT!" ), "error", MB_ICONERROR) ;
        return 0 ;  
       

    HWND hwnd = CreateWindowEx(NULL,  
        "Direct2D",                 // window class name
        "Draw Rectangle",           // window caption
        WS_OVERLAPPEDWINDOW,        // window style
        CW_USEDEFAULT,              // initial x position
        CW_USEDEFAULT,              // initial y position
        600,                        // initial x size
        600,                        // initial y size
        NULL,                       // parent window handle
        NULL,                       // window menu handle
        hInstance,                  // program instance handle
        NULL) ;                     // creation parameters

        ShowWindow (hwnd, iCmdShow) ;
        UpdateWindow (hwnd) ;

        MSG    msg ;  
        ZeroMemory(&msg, sizeof(msg)) ;

        while (GetMessage (&msg, NULL, 0, 0))  
        
            TranslateMessage (&msg) ;
            DispatchMessage (&msg) ;
        

        return msg.wParam ;

另一答案

@@ zdd,谢谢您的榜样。我搜索了更快的gdi绘图。使用gdi,win10、4ghz,gtx1070,在hbitmap缓冲区上绘制2000行需要我31ms,现在使用d2d大约需要6ms。看来,此d2d确实是速度的提高。谢谢!

以上是关于如何在C ++(DirectX图形)中使用DrawLine的主要内容,如果未能解决你的问题,请参考以下文章

如何在 DirectX 中查询 GPU 使用情况?

如何通过在 glsl 中注册来设置着色器常量?

C# Directx App 在 Release 中崩溃

如何在WPF窗口中使用WinRT SwapChainPanel控件?

我应该如何在 directx/opengl 中解释 RGB 颜色?

如何制作小游戏