使用D3DVERTEXELEMENT9结构渲染线框无色立方体

时间:2011-05-25 09:06:32

标签: c++ cube directx-9 wireframe

我对这个结构很陌生,我从中学到的这本书对我来说是荒谬的不直观。我仔细检查了代码,但我无法弄清楚为什么我无法弹出一个图像。背景为白色,HUD在那里,但立方体不会出现。当我试图用它做颜色时,它是一样的。

#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <d3dx9core.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
//LPDIRECT3DTEXTURE9 quadTexture;
LPD3DXFONT m_font;
D3DCOLOR fontColor;
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
LPDIRECT3DINDEXBUFFER9 i_buffer = NULL;
struct ColorVertex{
    D3DXVECTOR3 pos;
    ColorVertex(FLOAT X, FLOAT Y, FLOAT Z):pos(X, Y, Z){}
    static IDirect3DVertexDeclaration9* Decl;
};
IDirect3DVertexDeclaration9* ColorVertex::Decl = 0;
void initD3D(HWND hWnd);
void render_frame(void);
void cleanD3D(void);
void setVertices(void);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int  nCmdShow){
    #define SCREEN_WIDTH GetSystemMetrics(SM_CXSCREEN)
    #define SCREEN_HEIGHT GetSystemMetrics(SM_CYSCREEN)
    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"WindowClass";
    RegisterClassEx(&wc);
    hWnd = CreateWindowEx(NULL,
                          L"WindowClass",
                          L"v2",
                          WS_EX_TOPMOST | WS_POPUP,
                          0, 0,
                          SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);
    initD3D(hWnd);
    setVertices();
    D3DXCreateFont( d3ddev, 20, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Arial"), &m_font );
    D3DVERTEXELEMENT9 Elements[] = {
        {0,  0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
        D3DDECL_END()
    };
    d3ddev->CreateVertexDeclaration(Elements, &ColorVertex::Decl);
    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 = FALSE;
    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);
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
    d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
}

void setVertices(void){
    d3ddev->CreateVertexBuffer(8*sizeof(ColorVertex),
                           0,
                           0,
                           D3DPOOL_MANAGED,
                           &v_buffer,
                           NULL);
    ColorVertex* cube = 0;
    v_buffer->Lock(0, 0, (void**)&cube, 0);
            cube[0] = ColorVertex(-1.0f, -1.0f, -1.0f);
            cube[1] = ColorVertex(-1.0f,  1.0f, -1.0f);
            cube[2] = ColorVertex( 1.0f,  1.0f, -1.0f);
            cube[3] = ColorVertex( 1.0f, -1.0f, -1.0f);
            cube[4] = ColorVertex(-1.0f, -1.0f,  1.0f);
            cube[5] = ColorVertex(-1.0f,  1.0f,  1.0f);
            cube[6] = ColorVertex( 1.0f,  1.0f,  1.0f);
            cube[7] = ColorVertex( 1.0f, -1.0f,  1.0f);
    v_buffer->Unlock();
    d3ddev->CreateIndexBuffer(12*3*sizeof(WORD),
                            0,
                            D3DFMT_INDEX16,
                            D3DPOOL_MANAGED,
                            &i_buffer,
                            NULL);
    WORD* k = 0;
    i_buffer->Lock(0, 0, (void**)&k, 0);
        k[0] = 0; k[1] = 1; k[2] = 2;
        k[3] = 0; k[4] = 2; k[5] = 3;
  // Back face.
        k[6] = 4; k[7]  = 6; k[8] = 5;
        k[9] = 4; k[10] = 7; k[11] = 6;

  // Left face.
        k[12] = 4; k[13] = 5; k[14] = 1;
        k[15] = 4; k[16] = 1; k[17] = 0;

  // Right face.
        k[18] = 3; k[19] = 2; k[20] = 6;
        k[21] = 3; k[22] = 6; k[23] = 7;

  // Top face.
        k[24] = 1; k[25] = 5; k[26] = 6;
        k[27] = 1; k[28] = 6; k[29] = 2;

  // Bottom face.
        k[30] = 4; k[31] = 0; k[32] = 3;
        k[33] = 4; k[34] = 3; k[35] = 7;

    i_buffer->Unlock();
}

void render_frame(void){
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);
    d3ddev->BeginScene();
    d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(ColorVertex));
    d3ddev->SetIndices(i_buffer);
    D3DXMATRIX matView;
    D3DXMatrixLookAtLH(&matView,
                        &D3DXVECTOR3 (0.0f, 5.0f, -5.0f),// the camera position
                        &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), // the look-at position
                        &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));// the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);
    D3DXMATRIX matProjection;
    D3DXMatrixPerspectiveFovLH(&matProjection,
                                D3DXToRadian(90),
                                (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,
                                1.0f,// the near view-plane
                                5000.0f);// the far view-plane
    d3ddev->SetVertexDeclaration(ColorVertex::Decl);
    d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
    fontColor = D3DCOLOR_ARGB(255,0,0,255);   
    RECT rct;
    rct.left=10;
    rct.right=SCREEN_WIDTH-10;
    rct.top=10;
    rct.bottom=rct.top+20;
    //WCHAR lookAtPos [32000];
    //swprintf_s(lookAtPos, 32000, L"X=%f, Z=%f, Y=%f", camDirX, camDirZ, camDirY);
    m_font->DrawText(NULL, L"Hello World", -1, &rct, 0, fontColor );
    d3ddev->EndScene();
    d3ddev->Present(NULL, NULL, NULL, NULL);
}

void cleanD3D(void){
    d3ddev->Release();
    d3d->Release();
    i_buffer->Release();
    v_buffer->Release();
    ColorVertex::Decl->Release();
}

1 个答案:

答案 0 :(得分:0)

我是个白痴。我设置了世界投影矩阵,然后没有使用它。很抱歉打扰你们。