如何使用Direct3D和c ++制作正方形?

时间:2014-12-09 04:32:03

标签: c++ directx direct3d

我一直在线阅读Directx教程,让我画一个三角形,我想试着弄清楚如何把它变成一个正方形。据我所知,我可以添加一个额外的顶点,以便它实际上将两个三角形与4个顶点组合在一起。现在,当我做这一切时,输出只显示一个三角形,我想知道错误在哪里。这是我目前的代码:

#include <Windows.h>
#include <windowsx.h>
#include <d3d9.h>

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

#pragma comment (lib, "d3d9.lib")

LPDIRECT3D9 d3d;

LPDIRECT3DDEVICE9 d3ddev;

LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;

void initD3D(HWND hWnd);

void render_frame(void);

void cleanD3D(void);

void init_graphics(void);


struct CUSTOMVERTEX { FLOAT X, Y, Z, RHW; DWORD COLOR; };
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

LRESULT CALLBACK WindowProc(HWND hWnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    HWND hWnd;

    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX); 

    wc.style = CS_HREDRAW | CS_VREDRAW;

    wc.lpfnWndProc = WindowProc;

    wc.hInstance = hInstance;

    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    wc.lpszClassName = L"WindowClass1";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
        L"WindowClass1",
        L"Pretty Triangle",
        WS_OVERLAPPEDWINDOW,
        0,
        0,
        SCREEN_WIDTH,
        SCREEN_HEIGHT,
        NULL,
        NULL,
        hInstance,
        NULL);

    ShowWindow(hWnd, nCmdShow);

    initD3D(hWnd);

    MSG msg;

    while (TRUE)
    {
        while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);

            DispatchMessage(&msg);
        }

        if (msg.message == WM_QUIT)
            break;

        render_frame();
    }

    cleanD3D();

    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case_WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

void initD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION);

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));

    d3dpp.Windowed = TRUE;

    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

    d3dpp.hDeviceWindow = hWnd;

    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

    d3dpp.BackBufferWidth = SCREEN_WIDTH;

    d3dpp.BackBufferHeight = SCREEN_HEIGHT;

    d3d->CreateDevice(D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        hWnd,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp,
        &d3ddev);

    init_graphics();
}

void render_frame(void)
{
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    d3ddev->SetFVF(CUSTOMFVF);

    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

    d3ddev->EndScene();

    d3ddev->Present(NULL, NULL, NULL, NULL);
}

void cleanD3D(void)
{
    v_buffer->Release();
    d3ddev->Release();
    d3d->Release();
}

void init_graphics(void)
{
    CUSTOMVERTEX vertices[] =
    {
        { 100.0f, 100.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
        { 132.0f, 100.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { 100.0f, 132.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 100.0f, 132.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
    };

    d3ddev->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX),
        0,
        CUSTOMFVF,
        D3DPOOL_MANAGED,
        &v_buffer,
        NULL);

    VOID*pVoid;

    v_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, vertices, sizeof(vertices));
    v_buffer->Unlock();
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您的最后一个顶点位于与第三个顶点相同的位置,因此您的第二个三角形是一条直线而不可见。我认为你的顶点数据中有一个拼写错误并想写

{ 132.0f, 132.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), }

作为使四边形完整的最后一个顶点。

答案 1 :(得分:0)

我在Direct3D上非常生疏,但我相信你的问题是你没有足够的顶点来绘制正方形,因为你正在使用DrawPrimative方法。此方法期望用于绘制正方形(使用2个trianges)的重复顶点存在于数组中,这意味着您应该定义6个顶点而不是4个。

如果您更改代码以改为调用DrawIndexedPrimitive,那么您不必定义重复的顶点,您会发现现有的顶点数组应该有效。

DrawIndexedPrimitive( D3DPT_TRIANGLELIST, // PrimitiveType
                0,                  // BaseVertexIndex
                0,                  // MinIndex
                4,                  // NumVertices
                0,                  // StartIndex
                2 );                // PrimitiveCount

这是一篇关于在Direct3D中绘制正方形的好文章,它也解释了索引的Veritces:http://msdn.microsoft.com/en-us/library/windows/desktop/bb147325(v=vs.85).aspx