📄 ch23p1_simplewater.cpp
字号:
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
// draw skybox
m_pd3dDevice->SetVertexShader(D3DFVF_XYZ_DIFFUSE_TEX1);
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView);
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
m_SkyBox.Render(matView);
// draw ground plane
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView);
m_pd3dDevice->SetTexture( 0, m_GroundPlane.GetTexture() );
m_pd3dDevice->SetStreamSource( 0, m_GroundPlane.GetVB(), sizeof(VERTEX_XYZ_DIFFUSE_TEX1) );
m_pd3dDevice->SetIndices( m_GroundPlane.GetIB(), 0L );
m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0,
m_GroundPlane.GetNumVerts(), 0, m_GroundPlane.GetNumIndices()/3 );
// draw water
D3DLIGHT8 light; ZeroMemory(&light, sizeof(D3DLIGHT8));
D3DUtil_InitLight(light, D3DLIGHT_DIRECTIONAL, -0.0f, 20.0f, -10.0f);
light.Ambient.a = 1.0f;
light.Ambient.g = 0.4f;
light.Ambient.b = 0.4f;
light.Ambient.r = 0.4f;
light.Diffuse.a = 1.0f;
light.Diffuse.g = 0.8f;
light.Diffuse.b = 0.8f;
light.Diffuse.r = 0.8f;
light.Direction = D3DXVECTOR3(0.0f, -1.0f, 0.0f);
m_pd3dDevice->SetLight(0, &light);
m_pd3dDevice->LightEnable(0, true);
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
if (bRenderWater) {
D3DXMATRIX matTrans, matScale;
D3DXMatrixTranslation(&matTrans, 0.0f, 1.1f, 0.0f);
D3DXMatrixScaling(&matScale, 16.0f, 1.0f, 20.0f);
matWorld = matScale * matTrans;
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
// Turn on texture-coord generation for cubemapping
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT3 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSU, D3DTADDRESS_MIRROR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ADDRESSV, D3DTADDRESS_MIRROR );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_BLENDDIFFUSEALPHA );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG1, D3DTA_CURRENT );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLORARG2, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 2, D3DTSS_COLOROP, D3DTOP_DISABLE );
m_pd3dDevice->SetTextureStageState( 2, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
m_pd3dDevice->SetTexture( 0, m_pCubeMap );
m_Water.Render();
// Restore the render states
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
}
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
D3DXMatrixIdentity(&matWorld);
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pd3dDevice->SetVertexShader(D3DFVF_XYZ_DIFFUSE_TEX1);
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE);
m_pPoolFrame->Render(m_pd3dDevice);
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);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
HRESULT hr;
m_iLightPosX = 0;
m_iLightPosY = 0;
m_InputManager.CreateDevices(m_hWnd, true, true);
m_SkyBox.RestoreDeviceObjects(m_pd3dDevice,
"Ch23p1_SkyboxTop.bmp",
"Ch23p1_SkyboxBottom.bmp",
"Ch23p1_SkyboxFront.bmp",
"Ch23p1_SkyboxBack.bmp",
"Ch23p1_SkyboxLeft.bmp",
"Ch23p1_SkyboxRight.bmp"
);
m_GroundPlane.RestoreDeviceObjects(m_pd3dDevice, "Ch23p1_GroundTexture.bmp", 512.0f, 512.0f, 16);
m_Water.RestoreDeviceObjects(m_pd3dDevice, 64, "Ch23p1_InPool.jpg");
m_pPoolFrame = new CD3DMesh;
// Create the cubemap, with a format that matches the backbuffer, since
// we'll be rendering into it
if( FAILED( hr = m_pd3dDevice->CreateCubeTexture( CUBEMAP_RESOLUTION, 1, D3DUSAGE_RENDERTARGET,
m_d3dsdBackBuffer.Format,
D3DPOOL_DEFAULT, &m_pCubeMap ) ) )
return E_FAIL;
// We create a separate Z buffer for the cube faces, because user could
// resize rendering window so that it is smaller than a cube face. In
// this case we cannot use the rendering window Z buffer for cube faces.
if( FAILED( hr = m_pd3dDevice->CreateDepthStencilSurface(
CUBEMAP_RESOLUTION, CUBEMAP_RESOLUTION,
D3DFMT_D16, D3DMULTISAMPLE_NONE, &m_pCubeFaceZBuffer) ) )
return E_FAIL;
TCHAR buf[256];
DXUtil_ConvertGenericStringToAnsi(buf, "Ch23p1_PoolFrame.x", sizeof(buf));
if( FAILED( m_pPoolFrame->Create(m_pd3dDevice, buf) ) )
return E_FAIL;
m_pPoolFrame->RestoreDeviceObjects(m_pd3dDevice);
m_pFont->RestoreDeviceObjects();
m_pFontSmall->RestoreDeviceObjects();
m_Camera.SetPosition(D3DXVECTOR3(0.0f, 7.0f, -20.0f));
// 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->SetTextureStageState( 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
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 );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_InputManager.DestroyDevices();
m_pFont->InvalidateDeviceObjects();
m_pFontSmall->InvalidateDeviceObjects();
m_SkyBox.InvalidateDeviceObjects();
m_GroundPlane.InvalidateDeviceObjects();
m_Water.InvalidateDeviceObjects();
if (m_pPoolFrame) {
m_pPoolFrame->Destroy();
SAFE_DELETE(m_pPoolFrame);
}
SAFE_RELEASE( m_pCubeMap );
SAFE_RELEASE( m_pCubeFaceZBuffer );
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_GroundPlane.DeleteDeviceObjects();
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()
{
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 )
{
float f;
// Pass remaining messages to default handler
if (uMsg == WM_KEYDOWN) {
switch(toupper(wParam)) {
case 'C':
m_Water.CreateWaterDroplet(RandomNumber(0,64), RandomNumber(0,64), 1, 5);
break;
case 'N':
m_Water.SetHardcodeNormals(!m_Water.GetHardcodeNormals());
break;
case 'O':
f = m_Water.GetEnvBlendFactor();
f += 0.1f; if (f > 1.0f) f = 1.0f;
m_Water.SetEnvBlendFactor(f);
break;
case 'I':
f = m_Water.GetEnvBlendFactor();
f -= 0.1f; if (f < 0.0f) f = 0.0f;
m_Water.SetEnvBlendFactor(f);
break;
case 'L':
f = m_Water.GetDepth();
f += 0.1f; if (f > 10.0f) f = 10.0f;
m_Water.SetDepth(f);
break;
case 'K':
f = m_Water.GetDepth();
f -= 0.1f; if (f < 0.0f) f = 0.0f;
m_Water.SetDepth(f);
break;
}
}
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -