为什么不在窗口中渲染?

时间:2016-02-25 18:04:04

标签: c++ directx render

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

我一直在编写基于这本书" 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;
}

1 个答案:

答案 0 :(得分:0)

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

一些明显的错误:

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

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

WS_OVERLAPPED||CW_USEDEFAULT更改为WS_OVERLAPPED,它应该是合乎逻辑的OR,并且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;
}