一起做游戲(二)


//創建D3D對象
    g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

//獲取顯卡信息
    D3DDISPLAYMODE d3ddm;
    g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );

 //設置合適的顯示模式
   D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
 
    d3dpp.Windowed               = TRUE;
    d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat       = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;

 //創建設備
   g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
        g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &d3dpp, &g_pd3dDevice );

//創建並設置投影矩陣(透視投影)
    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian( 45.0f ),
                                640.0f / 480.0f, 0.1f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

//設置渲染環境參數(燈光等)    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, TRUE );

    // Setup a simple directional light and some ambient...
 g_pLight0.Type = D3DLIGHT_DIRECTIONAL;
 g_pLight0.Direction = D3DXVECTOR3( 1.0f, 0.0f, 1.0f );

    g_pLight0.Diffuse.r = 1.0f;
    g_pLight0.Diffuse.g = 1.0f;
    g_pLight0.Diffuse.b = 1.0f;
 g_pLight0.Diffuse.a = 1.0f;

 g_pLight0.Specular.r = 1.0f;
 g_pLight0.Specular.g = 1.0f;
 g_pLight0.Specular.b = 1.0f;
 g_pLight0.Specular.a = 1.0f;

    g_pd3dDevice->SetLight( 0, &g_pLight0 );
    g_pd3dDevice->LightEnable( 0, TRUE );

    g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_COLORVALUE( 0.2f, 0.2f, 0.2f, 1.0f ) );
    ZeroMemory( &g_teapotMtrl, sizeof(D3DMATERIAL9) );

    g_teapotMtrl.Diffuse.r = 1.0f;
    g_teapotMtrl.Diffuse.g = 1.0f;
    g_teapotMtrl.Diffuse.b = 1.0f;
    g_teapotMtrl.Diffuse.a = 1.0f;

    // 裝載MESH
    D3DXLoadMeshFromX( "teapot.x", D3DXMESH_SYSTEMMEM, g_pd3dDevice,
                       NULL, NULL, NULL, NULL, &g_pTeapotMesh );

    //如果需要可以訪問后備緩沖區描述參數
    LPDIRECT3DSURFACE9 pBackBuffer = NULL;
    D3DSURFACE_DESC d3dsd;
    g_pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
    pBackBuffer->GetDesc( &d3dsd );
    pBackBuffer->Release();
    g_dwBackBufferWidth  = d3dsd.Width;
    g_dwBackBufferHeight = d3dsd.Height; 

 

/*注:可以同時創建多個設備
還可以創建后備緩沖鏈 ,如:
g_pd3dDevice->GetSwapChain( 0, &g_swapChain_0 );
 g_pd3dDevice->CreateAdditionalSwapChain( &d3dpp, &g_swapChain_1 );
通過如下操作
LPDIRECT3DSURFACE9 pBackBuffer = NULL;
 g_swapChain_0->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
 g_pd3dDevice->SetRenderTarget( 0, pBackBuffer );
*/


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com