开发者

Rendering wireframe no-colour cube with D3DVERTEXELEMENT9 structure

开发者 https://www.devze.com 2023-03-08 17:12 出处:网络
I\'m pretty new to this structure and the book I\'m learning it from is absurdly unintuitive to me. I triple checked the code but I can\'t figure out why I can\'t get an image to pop up. The backgroun

I'm pretty new to this structure and the book I'm learning it from is absurdly unintuitive to me. I triple checked the code but I can't figure out why I can't get an image to pop up. The background is white and the HUD is there but the cube won't show up. It was the same when i tried to make it in colour.

#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();
}


I'm a friggin idiot. I set the world projection matrix and then didn't use it. Sorry for bothering you guys.

0

精彩评论

暂无评论...
验证码 换一张
取 消