Direct3D-空间物体旋转demo

#include <d3d9.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include <strsafe.h>
#pragma warning( default : 4996 )
#include <d3dx9core.h> //新增


wchar_t *g_pClassName = L"Transform";
wchar_t *g_pWindowName = L"空间坐标变换实例";
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3D9         g_pD3D = NULL; //Direct3d对象

LPD3DXFONT          g_pFont = 0;    //字体对象
WCHAR*              strText = L"鼠标左键控制着色模式";                           
RECT                          clientRect;                                    

//
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuf = NULL;
LPDIRECT3DINDEXBUFFER9 g_pIndexBuf = NULL;
//顶点结构
struct CUSTOMVERTEX
{
    FLOAT _x,_y,_z; //顶点的位置
    DWORD _color;   //顶点的颜色
    CUSTOMVERTEX(FLOAT x,FLOAT y,FLOAT z,DWORD color)
        :_x(x),_y(y),_z(z),_color(color){}
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
HRESULT InitDirect3D(HWND hWnd)
{

    //初始化D3D
    // 创建D3D对象,该对象用于创建D3D设备对象
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    //设置D3DPRESENT_PARAMETERS结构,准备创建D3D设备对象 
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;  //全屏或窗口
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//指定表面在交换链中是如何被交换的。D3DSWAPEFFECT_DISCARD:后备缓存的内容被复制到屏幕上后,后备缓存的内容失效,可以丢弃
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;//后备缓冲的格式,若不熟悉,可设置为D3DFMT_UNKNOWN,这时它将使用桌面的格式


    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
    //-------------字体部分-----------------
    //创建字体对象
    // Device state would normally be set here
    if( FAILED(D3DXCreateFont(g_pd3dDevice,0,0,0,0,0,0,0,0,0,L"Arial",&g_pFont)))
        return E_FAIL;
    //获取窗口客户区
    GetClientRect(hWnd,&clientRect);
    //--------------顶点部分-----------------
    //设置剔除模式为不剔除任何面
    //g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
    //设置图元填充模式为线框模式
    //g_pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
    //
    //创建顶点缓存
    g_pd3dDevice->CreateVertexBuffer(8 * sizeof(CUSTOMVERTEX), 0 ,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,
        &g_pVertexBuf,NULL
        );
    //填充顶点数据 _
    CUSTOMVERTEX *pVertices = NULL;
    g_pVertexBuf->Lock(0,0,(void**)&pVertices,0);
    pVertices[0] = CUSTOMVERTEX(-5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0));
    pVertices[1] = CUSTOMVERTEX(-5.0f,5.0f,5.0f,D3DCOLOR_XRGB(0,255,0));
    pVertices[2] = CUSTOMVERTEX(5.0f,5.0f,5.0f,D3DCOLOR_XRGB(0,0,255));
    pVertices[3] = CUSTOMVERTEX(5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,255,0));
    pVertices[4] = CUSTOMVERTEX(-5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(255,255,255));
    pVertices[5] = CUSTOMVERTEX(-5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(255,0,255));
    pVertices[6] = CUSTOMVERTEX(5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(0,255,255));
    pVertices[7] = CUSTOMVERTEX(5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(0,0,0));
    g_pVertexBuf->Unlock();
    //创建索引缓存
    g_pd3dDevice->CreateIndexBuffer(36 * sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_DEFAULT,&g_pIndexBuf,NULL);
    //填充索引数据
    WORD * pIndices = NULL;
    g_pIndexBuf->Lock(0,0,(void**)&pIndices,0);
    pIndices[0] = 0;pIndices[1] = 1;pIndices[2]=2;
    pIndices[3] = 0;pIndices[4] = 2;pIndices[5]=3;
    pIndices[6] = 0;pIndices[7] = 3;pIndices[8]=7;

    pIndices[9] = 0;pIndices[10] = 7;pIndices[11]=4;

    pIndices[12] = 0;pIndices[13] = 4;pIndices[14]=5;

    pIndices[15] = 0;pIndices[16] = 5;pIndices[17]=1;

    pIndices[18] = 2;pIndices[19] = 6;pIndices[20]=7;

    pIndices[21] = 2;pIndices[22] = 7;pIndices[23]=3;
    pIndices[24] = 2;pIndices[25] = 5;pIndices[26]=6;
    pIndices[27] = 2;pIndices[28] = 1;pIndices[29]=5;
    pIndices[30] = 4;pIndices[31] = 6;pIndices[32]=5;
    pIndices[33] = 4;pIndices[34] = 7;pIndices[35]=6;
    g_pIndexBuf->Unlock();
    return S_OK;
}
VOID Direct3DRender()
{
    g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    g_pd3dDevice->BeginScene();
    //设置世界矩阵
    D3DXMATRIX matWorld,Rx,Ry,Rz;
    D3DXMatrixIdentity(&matWorld);
    D3DXMatrixRotationX(&Rx,::timeGetTime() / 1000.0f);
    D3DXMatrixRotationY(&Ry,::timeGetTime() / 1000.0f);
    D3DXMatrixRotationZ(&Rz,::timeGetTime() / 1000.0f);
    matWorld = Rx * Ry * Rz * matWorld;
    g_pd3dDevice->SetTransform(D3DTS_WORLD,&matWorld);
    //设置取景变换矩阵
    D3DXMATRIX matView;
    D3DXVECTOR3 vEye(0.0f,0.0f,-30.0f);
    D3DXVECTOR3 vAt(0.0f,0.0f,0.0f);
    D3DXVECTOR3 vUp(0.0f,1.0f,0.0f);
    D3DXMatrixLookAtLH( &matView,&vEye,&vAt,&vUp);
    g_pd3dDevice->SetTransform(D3DTS_VIEW,&matView);
    //设置投影变换矩阵
    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH(&matProj,D3DX_PI / 4.0f,1.0f,1.0f,1000.0f);
    g_pd3dDevice->SetTransform(D3DTS_PROJECTION,&matProj);
    //设置渲染状态
    g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
    g_pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
    //渲染三角形
    g_pd3dDevice->SetStreamSource(0,g_pVertexBuf,0,sizeof(CUSTOMVERTEX));
    g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    g_pd3dDevice->SetIndices(g_pIndexBuf);
    g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12);
    //
    g_pd3dDevice->EndScene();
    g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
}
VOID Cleanup()
{
    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
    //释放顶点缓存对象
    if(g_pIndexBuf != NULL)
    {
        g_pIndexBuf->Release();
    }
    //释放顶点缓存对象
    if(g_pVertexBuf != NULL)
        g_pVertexBuf->Release();
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
// 消息处理函数 回调函数
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        //窗口关闭
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
        //窗口绘制
        case WM_PAINT:

            Direct3DRender();
            //ValidateRect( hWnd, NULL );

            return 0;
        case WM_LBUTTONDOWN:    //单击鼠标左键切换着色模式

             Direct3DRender();
            //ValidateRect( hWnd, NULL );
            return 0;
        case WM_RBUTTONDOWN:    //单击鼠标右键切换填充模式

             Direct3DRender();
            //ValidateRect( hWnd, NULL );//使窗口有效 清除消息队列中的WM_PAINT消息

            return 0;

    }

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




//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: 这是整个工程的入口函数,从这里开始理结构,,

//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
    UNREFERENCED_PARAMETER( hInst );


    //定义窗口类 
    WNDCLASSEX wc =
    {
        sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
        GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
        L"DirectX游戏编程一", NULL
    };
    //注册窗口类

    RegisterClassEx( &wc );     
    /* 下一步是创建实际的窗口 */
    HWND hWnd = CreateWindow( L"DirectX游戏编程一", L"DirectX游戏编程:空间坐标变换实例",  
                              WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
                              NULL, NULL, wc.hInstance, NULL );

    // Initialize Direct3D 初始化D3D设备
    /*如果创建成功,则进入消息循环*/
    if( SUCCEEDED( InitDirect3D( hWnd ) ) )
    {

             ShowWindow( hWnd, SW_SHOWDEFAULT );
             UpdateWindow( hWnd );



        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) )//提取消息
        {

            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }


    UnregisterClass( L"DirectX游戏编程一", wc.hInstance );  
    return 0;

    }
}

Direct3D-空间物体旋转demo_第1张图片

你可能感兴趣的:(direct9)