ITPub博客

首页 > Linux操作系统 > Linux操作系统 > DirectX 9.0 3D 游戏开发编程基础之三

DirectX 9.0 3D 游戏开发编程基础之三

原创 Linux操作系统 作者:whufly85_ 时间:2011-07-01 23:20:19 0 删除 编辑
      本节主要演示了利用D3D绘制一个茶壶,也即绘制系统自带的Mesh的方法。
/*
 *在D3D窗口中绘制茶壶
 *whufly
 *2011年6月15日
 *
 *
 */


#include
#include
#include

IDirect3D9 *g_pD3D = NULL;
IDirect3DDevice9  *g_pDevice = NULL;

ID3DXMesh *g_pMesh = NULL;

HRESULT InitD3D(HWND hwnd)
{
    g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
    if (g_pD3D == NULL)
        return E_FAIL;

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp,sizeof(d3dpp));
    //d3dpp.Windowed = true;
    //d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    //d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.BackBufferWidth = 500;
    d3dpp.BackBufferHeight = 400;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; //像素格式
    d3dpp.BackBufferCount = 1;
    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3dpp.MultiSampleQuality = 0;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hwnd;
    d3dpp.Windowed = true ; // fullscreen
    d3dpp.EnableAutoDepthStencil = true;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // depth format
    d3dpp.Flags = 0;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;


    if (FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pDevice)))
    {
        return E_FAIL;
    }

    D3DXCreateTeapot(g_pDevice,&g_pMesh,NULL);
    D3DXVECTOR3 position(0.0f, 0.0f, -3.0f);
    D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
    D3DXMatrixLookAtLH(&V, &position, &target, &up);
    g_pDevice->SetTransform(D3DTS_VIEW, &V);

    //
    // Set projection matrix.
    //

    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(
        &proj,
        D3DX_PI * 0.5f, // 90 - degree
        (float)1366 / (float)768,
        1.0f,
        1000.0f);
    g_pDevice->SetTransform(D3DTS_PROJECTION, &proj);

    //
    // Switch to wireframe. mode.
    //

    g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
    g_pDevice->SetRenderState(D3DRS_LIGHTING,FALSE);

    return S_OK;
}

void Cleanup()
{
    if(g_pMesh != NULL)
        g_pMesh->Release();
   
    if (g_pDevice != NULL)
        g_pDevice->Release();

    if (g_pD3D)
        g_pD3D->Release();
}

void Render()
{
    if (g_pDevice != NULL)
    {


        g_pDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,0/*D3DCOLOR_ARGB(0,255,0,0)*/,1.0f,0);
       
        if (SUCCEEDED(g_pDevice->BeginScene()))
        {
            g_pMesh->DrawSubset(0);
            g_pDevice->EndScene();
        }

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

LRESULT WINAPI WinProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch (msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_PAINT:
            //*、、*/Render();
            break;
        default:
            break;
    }

    return DefWindowProc(hwnd,msg,wParam,lParam);
}


INT WINAPI WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
{
    WNDCLASS wndclass;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.hCursor = 0;
    wndclass.hIcon = 0;
    wndclass.lpfnWndProc = WinProc;
    wndclass.hInstance = hInstance;
    wndclass.lpszClassName = "D3DClass";
    wndclass.lpszMenuName = 0;
    wndclass.style. = CS_HREDRAW | CS_VREDRAW;

    if (!RegisterClass(&wndclass))
       return FALSE;

    HWND hwnd = CreateWindow("D3DClass","D3DWindow",WS_OVERLAPPEDWINDOW,0,0,500,400,NULL,NULL,hInstance,NULL);
    if (hwnd == NULL)
       return FALSE;

   
    if (SUCCEEDED(InitD3D(hwnd)))
    {
        ShowWindow(hwnd,SW_SHOW);
        UpdateWindow(hwnd);

        MSG msg;
        ZeroMemory(&msg,sizeof(msg));

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

    UnregisterClass("D3DClass",hInstance);
    Cleanup();
    return 0;
}
       注意将以下语句加上g_pDevice->SetRenderState(D3DRS_LIGHTING,FALSE);否则窗口什么也看不见。





     

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24818796/viewspace-701186/,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2011-06-02

  • 博文量
    3
  • 访问量
    4979