【问题标题】:Why do not rendered in the window?为什么不在窗口中渲染?
【发布时间】:2016-06-08 16:12:42
【问题描述】:

我创建了一个程序。当我运行它时,一个灰色背景的窗口,必须创建一个带有三角形的黄色背景。有什么问题?

我一直在根据《Gornakova - DirectX 编程教程》一书进行编程

#include <windows.h>
#include <d3d9.h>

LPDIRECT3D9 pDirect3D = NULL;
LPDIRECT3DDEVICE9 pDirect3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 pBufferVershin = NULL;

struct CUSTOMVERTEX
{
    FLOAT x,y,z,rhw;
    DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

HRESULT InitialBufferVershin()
{
    CUSTOMVERTEX Vershin[] =
    {
        {300.0f, 300.0f, 0.5f, 1.0f, 0x00000fff, },
        {150.0f, 300.0f, 0.5f, 1.0f, 0x00000fff, },
        {150.0f, 150.0f, 0.5f, 1.0f, 0x00000fff, },
    };

    if(FAILED(pDirect3DDevice->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX,
                                                 D3DPOOL_DEFAULT, &pBufferVershin, NULL)))
    return E_FAIL;

    VOID* pBV;
    if(FAILED(pBufferVershin->Lock(0, sizeof(Vershin), (void**)&pBV, 0)))
        return E_FAIL;
    memcpy(pBV, Vershin, sizeof(Vershin));
    pBufferVershin->Unlock();
    return S_OK;
}

void DeleteDirect3D()
{
    if(pBufferVershin != NULL)
        pBufferVershin->Release();

    if(pDirect3DDevice != NULL)
        pDirect3DDevice->Release();

    if(pDirect3D != NULL)
        pDirect3D->Release();
}

void RenderingDirect3D()
{
    if(pDirect3DDevice == NULL)
        return;
    pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,0), 1.0f, 0);

    pDirect3DDevice->BeginScene();

        pDirect3DDevice->SetStreamSource(0, pBufferVershin, 0, sizeof(CUSTOMVERTEX));

        pDirect3DDevice ->SetFVF(D3DFVF_CUSTOMVERTEX);
        pDirect3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

        pDirect3DDevice->Present(NULL, NULL, NULL, NULL);

    pDirect3DDevice->EndScene();
}

LRESULT IntailDirect3D(HWND hwnd)
{
    if(NULL == (pDirect3D = Direct3DCreate9(D3D9b_SDK_VERSION)))
        return E_FAIL;

    D3DDISPLAYMODE Display;
    if(FAILED(pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display)))
        return E_FAIL;

    D3DPRESENT_PARAMETERS Direct3DParametr;
    ZeroMemory(&Direct3DParametr, sizeof(Direct3DParametr));
    Direct3DParametr.Windowed = TRUE;
    Direct3DParametr.SwapEffect = D3DSWAPEFFECT_DISCARD;
    Direct3DParametr.BackBufferFormat = Direct3DParametr.BackBufferFormat;
    if(FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,
                                     D3DDEVTYPE_HAL,
                                     hwnd,
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING,
                                     &Direct3DParametr, &pDirect3DDevice)))
        return E_FAIL;
        return S_OK;
}

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_PAINT:
        RenderingDirect3D();
        ValidateRect(hwnd, NULL);
        break;
    case WM_DESTROY:
        DeleteDirect3D();
        PostQuitMessage(0);
        return 0;
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE pPrevInstance, LPSTR lCmdLine, int nCmdShow)
{
    WNDCLASSEX wEx;
    MSG msg;
    wEx.cbSize = sizeof(WNDCLASSEX);
    wEx.style = CS_VREDRAW || CS_HREDRAW || CS_OWNDC || CS_DBLCLKS;
    wEx.lpfnWndProc = MainWndProc;
    wEx.cbClsExtra = 0;
    wEx.cbWndExtra = 0;
    wEx.hInstance = hInstance;
    wEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wEx.hCursor = LoadCursor(NULL, IDC_ARROW);
    wEx.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
    wEx.lpszMenuName = NULL;
    wEx.lpszClassName = "WINDOWCLASS";
    wEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassEx(&wEx);


    HWND hwnd;
    if(!(hwnd = CreateWindowEx(NULL,
                               "WINDOWCLASS",
                               "Базовое окно для DirectX",
                               WS_OVERLAPPED||CW_USEDEFAULT,
                               0,0,
                               500,400,
                               NULL,
                               NULL,
                               hInstance,
                               NULL)))
    {
        return 0;
    }
    if(SUCCEEDED((IntailDirect3D(hwnd))))
    {
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
        ZeroMemory(&msg, sizeof(msg));

        while(msg.message != WM_QUIT)
        {
            if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            else{RenderingDirect3D();}
        }
    }
    return 0;
}

【问题讨论】:

    标签: c++ directx render


    【解决方案1】:

    看起来代码没有绘制任何东西,也许它是教程的一部分

    几个明显的错误:

    将逻辑或更改为按位或:|||

    wEx.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
    

    WS_OVERLAPPED||CW_USEDEFAULT 更改为WS_OVERLAPPED,同样应该是逻辑或,而CW_USEDEFAULT 无论如何也不属于那里。

    这表示样式是上述所有标志的组合(有点像+ 运算符)

    删除整个部分:

    case WM_PAINT:
        RenderingDirect3D();
        ValidateRect(hwnd, NULL);
        break;
    

    WM_PAINT 必须与BeginPaint/EndPaint 正确处理或根本不处理。


    编辑:
    #include <windows.h>
    #include <d3d9.h>
    
    LPDIRECT3D9 pDirect3D = NULL;
    LPDIRECT3DDEVICE9 pDirect3DDevice = NULL;
    LPDIRECT3DVERTEXBUFFER9 pBufferVershin = NULL;
    
    struct CUSTOMVERTEX
    {
        FLOAT x, y, z, rhw;
        DWORD color;
    };
    #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
    
    HRESULT InitialBufferVershin()
    {
        CUSTOMVERTEX Vershin[] =
        {
            { 300.0f, 300.0f, 0.5f, 1.0f, 0x00000fff, },
            { 150.0f, 300.0f, 0.5f, 1.0f, 0x00000fff, },
            { 150.0f, 150.0f, 0.5f, 1.0f, 0x00000fff, },
        };
    
        if (FAILED(pDirect3DDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX), 0, 
            D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &pBufferVershin, NULL)))
            return E_FAIL;
    
        VOID* pBV;
        if (FAILED(pBufferVershin->Lock(0, sizeof(Vershin), (void**)&pBV, 0)))
            return E_FAIL;
        memcpy(pBV, Vershin, sizeof(Vershin));
        pBufferVershin->Unlock();
        return S_OK;
    }
    
    void DeleteDirect3D()
    {
        if (pBufferVershin) pBufferVershin->Release();
        if (pDirect3DDevice) pDirect3DDevice->Release();
        if (pDirect3D) pDirect3D->Release();
    }
    
    void RenderingDirect3D()
    {
        if (pDirect3DDevice == NULL)
            return;
        pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0);
    
        pDirect3DDevice->BeginScene();
    
        pDirect3DDevice->SetStreamSource(0, pBufferVershin, 0, sizeof(CUSTOMVERTEX));
    
        pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
        pDirect3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
    
        pDirect3DDevice->EndScene();
    
        pDirect3DDevice->Present(NULL, NULL, NULL, NULL);//****changed
    }
    
    LRESULT IntailDirect3D(HWND hwnd)
    {
        if (NULL == (pDirect3D = Direct3DCreate9(D3D9b_SDK_VERSION)))
            return E_FAIL;
    
        D3DDISPLAYMODE Display;
        if (FAILED(pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display)))
            return E_FAIL;
    
        D3DPRESENT_PARAMETERS Direct3DParametr;
        ZeroMemory(&Direct3DParametr, sizeof(Direct3DParametr));
        Direct3DParametr.Windowed = TRUE;
        Direct3DParametr.SwapEffect = D3DSWAPEFFECT_DISCARD;
        Direct3DParametr.BackBufferFormat = Direct3DParametr.BackBufferFormat;
        if (FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,
            D3DDEVTYPE_HAL,
            hwnd,
            D3DCREATE_HARDWARE_VERTEXPROCESSING,
            &Direct3DParametr, &pDirect3DDevice)))
            return E_FAIL;
    
        return S_OK;
    }
    
    LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch (msg)
        {
        case WM_DESTROY:
            DeleteDirect3D();
            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int)
    {
        WNDCLASSEX wEx = { sizeof(WNDCLASSEX) };
        wEx.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
        wEx.lpfnWndProc = MainWndProc;
        wEx.hInstance = hInstance;
        wEx.hCursor = LoadCursor(NULL, IDC_ARROW);
        wEx.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
        wEx.lpszClassName = "WINDOWCLASS";
        RegisterClassEx(&wEx);
    
        HWND hwnd = CreateWindowEx(NULL, "WINDOWCLASS", "TEST", 
            WS_VISIBLE | WS_OVERLAPPEDWINDOW,
            0, 0, 800, 600, NULL, NULL, hInstance, NULL);
        if (!hwnd)
            return 0;
    
        if (SUCCEEDED((IntailDirect3D(hwnd))))
        {
            InitialBufferVershin();//****changed
            MSG msg = { 0 };
            while (msg.message != WM_QUIT)
            {
                if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
                else
                {
                    RenderingDirect3D();
                }
            }
        }
    
        return 0;
    }
    

    【讨论】:

    • 但是作者在书中画了一个黄色背景上的蓝色三角形!
    • 我做了一些更改,它现在应该可以运行了。请参阅我放置****changed 的行
    猜你喜欢
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多