📄 ch14p1_3dopenalsound.cpp
字号:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
// these are done here so that you can move the camera around during a freeze
// frame, ala The Matrix
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
ProcessInput();
m_Camera.Update(m_fElapsedTime);
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
// Clear the backbuffer
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0x000000, 1.0f, 0L );
// Now that our fire texture's updated, we can begin rendering the scene
if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
{
// draw skybox
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
m_SkyBox.Render(m_Camera.GetViewMatrix());
// draw the ground
m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_Camera.GetViewMatrix());
m_pd3dDevice->SetVertexShader( D3DFVF_XYZ_DIFFUSE_TEX1 );
// draw Object
D3DXMatrixTranslation(&matWorld, 0, 4, 0);
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE);
D3DLIGHT8 light;
light.Type = D3DLIGHT_DIRECTIONAL;
light.Diffuse.r = 1;
light.Diffuse.g = 1;
light.Diffuse.b = 1;
light.Diffuse.a = 1;
light.Direction.x = -1;
light.Direction.y = -1;
light.Direction.z = 0;
light.Ambient.r = 0.1f;
light.Ambient.g = 0.1f;
light.Ambient.b = 0.1f;
light.Ambient.a = 1;
m_pd3dDevice->SetLight(0, &light);
m_pd3dDevice->LightEnable(0, 1);
// rotate the ship
D3DXMATRIX matrX, matrY, matrZ;
D3DXMatrixRotationX(&matrX, m_rAngleX);
D3DXMatrixRotationY(&matrY, m_rAngleY);
D3DXMatrixRotationZ(&matrZ, m_rAngleZ);
D3DXMatrixIdentity(&matWorld);
D3DXMatrixMultiply(&matWorld, &matWorld, &matrX);
D3DXMatrixMultiply(&matWorld, &matWorld, &matrY);
D3DXMatrixMultiply(&matWorld, &matWorld, &matrZ);
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pObject->Render( m_pd3dDevice );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
char buf[256];
_snprintf(buf, 256, "Position: (%.2f, %.2f, %.2f)",
(float)m_Camera.GetPosition().x, (float)m_Camera.GetPosition().y,
(float)m_Camera.GetPosition().z);
m_pFontSmall->DrawText( 2, 0, D3DCOLOR_ARGB(255,255,255,0), buf );
_snprintf(buf, 256, "Velocity: (%.2f, %.2f, %.2f)",
(float)m_ListenerVelocity.x, (float)m_ListenerVelocity.y,
(float)m_ListenerVelocity.z);
m_pFontSmall->DrawText( 2, 15, D3DCOLOR_ARGB(255,255,255,0), buf );
_snprintf(buf, 256, "Warp Speed: %s (Press G)", m_WarpSpeed ? "ON" : "OFF");
m_pFontSmall->DrawText( 2, 30, D3DCOLOR_ARGB(255,255, m_WarpSpeed ? 0 : 255, 0), buf );
_snprintf(buf, 256, "Spaceship Beep: %s (Press B)", m_BeepActive ? "ON" : "OFF");
m_pFontSmall->DrawText( 2, 45, D3DCOLOR_ARGB(255,255, m_BeepActive ? 0 : 255, 0), buf );
_snprintf(buf, 256, "Press R to reset your position");
m_pFontSmall->DrawText( 2, 60, D3DCOLOR_ARGB(255,255,255,255), buf );
// End the scene.
m_pd3dDevice->EndScene();
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
m_pFont->InitDeviceObjects( m_pd3dDevice );
m_pFontSmall->InitDeviceObjects( m_pd3dDevice );
m_SkyBox.SetSize(100.0f);
m_EngineSource->PlayFromStart();
if( FAILED( m_pObject->Create(m_pd3dDevice, _T("Ch14p1_Object.x") ) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_InputManager.CreateDevices(m_hWnd, false, true);
m_SkyBox.RestoreDeviceObjects(m_pd3dDevice,
"Ch14p1_SkyBox_Top.bmp",
"Ch14p1_SkyBox_Bottom.bmp",
"Ch14p1_SkyBox_Front.bmp",
"Ch14p1_SkyBox_Back.bmp",
"Ch14p1_SkyBox_Left.bmp",
"Ch14p1_SkyBox_Right.bmp"
);
m_pObject->RestoreDeviceObjects( m_pd3dDevice );
m_pFont->RestoreDeviceObjects();
m_pFontSmall->RestoreDeviceObjects();
m_Camera.SetPosition(D3DXVECTOR3(0.0f, 7.0f, -20.0f));
// Set the world matrix
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Set projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 0.1f, 100.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
m_pd3dDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD );
// create a light for our Object
D3DLIGHT8 light;
light.Ambient.a = light.Ambient.g = light.Ambient.b = light.Ambient.r = 1.0;
light.Diffuse.a = light.Diffuse.g = light.Diffuse.b = light.Diffuse.r = 1.0;
light.Position = D3DXVECTOR3(0.0,5.0,2.0);
light.Type = D3DLIGHT_DIRECTIONAL;
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE);
m_pd3dDevice->SetLight(0, &light);
m_pd3dDevice->LightEnable(0, true);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_InputManager.DestroyDevices();
m_pObject->InvalidateDeviceObjects();
m_pFont->InvalidateDeviceObjects();
m_pFontSmall->InvalidateDeviceObjects();
m_SkyBox.InvalidateDeviceObjects();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Called when the app is exiting, or the device is being changed,
// this function deletes any device dependent objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::DeleteDeviceObjects()
{
m_pFont->DeleteDeviceObjects();
m_pFontSmall->DeleteDeviceObjects();
m_pObject->Destroy();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FinalCleanup()
// Desc: Called before the app exits, this function gives the app the chance
// to cleanup after itself.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FinalCleanup()
{
GetOpenALManager()->UnInit();
SAFE_DELETE( m_pFont );
SAFE_DELETE( m_pFontSmall );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfirmDevice()
// Desc: Called during device intialization, this code checks the device
// for some minimum set of capabilities
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior,
D3DFORMAT Format )
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: Message proc function to handle key and menu input
//-----------------------------------------------------------------------------
LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam )
{
if (uMsg == WM_KEYUP && (wParam == 'g' || wParam == 'G')) {
m_WarpSpeed = !m_WarpSpeed;
}
if (uMsg == WM_KEYUP && (wParam == 'r' || wParam == 'R')) {
m_Camera.SetPosition(D3DXVECTOR3(0,4,-10));
}
if (uMsg == WM_KEYUP && (wParam == 'b' || wParam == 'B')) {
m_BeepActive = !m_BeepActive;
}
// Pass remaining messages to default handler
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -