📄 ch22p2_inscenelensflare.cpp
字号:
{
// 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 );
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());
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_Camera.GetViewMatrix());
m_pd3dDevice->SetVertexShader( D3DFVF_XYZ_DIFFUSE_TEX1 );
// draw Object
bool bLensFlareObscured = IsLensFlareObscured();
if (bLensFlareObscured) {
// the lens flare is obscured, so draw the only halo first then draw
// the planet.
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
m_LensFlare.Render(m_Camera, m_matProj, m_vSunPos,
m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, true);
}
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pd3dDevice->SetVertexShader( D3DFVF_XYZ_DIFFUSE_TEX1 );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_Camera.GetViewMatrix());
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_matProj);
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE);
D3DXMatrixRotationYawPitchRoll(&matWorld, -m_fPlanetRotation/16.0f, 0.0f, 0.0f);
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE);
m_pd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f));
m_pObject->Render( m_pd3dDevice );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
if (!bLensFlareObscured) {
// the lens flare isn't obscured, so draw it last, and draw the whole
// thing
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
m_LensFlare.Render(m_Camera, m_matProj, m_vSunPos,
m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, false);
}
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, 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, "Screen Pos of Sun: (%d,%d)", m_iSunScreenPosX, m_iSunScreenPosY);
m_pFontSmall->DrawText( 2, 15, D3DCOLOR_ARGB(255,255,255,0), buf );
_snprintf(buf, 256, "NumObjectsAtPoint: %d", m_iNumObjectsAtPoint);
m_pFontSmall->DrawText( 2, 30, D3DCOLOR_ARGB(255,255,255,0), 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);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
m_fPlanetRotation = 0.0f;
m_vSunPos = D3DXVECTOR3(10, 5, 50);
m_InputManager.CreateDevices(m_hWnd, false, true);
m_SkyBox.RestoreDeviceObjects(m_pd3dDevice,
"Ch22p2_SkyBox_Top.bmp",
"Ch22p2_SkyBox_Bottom.bmp",
"Ch22p2_SkyBox_Front.bmp",
"Ch22p2_SkyBox_Back.bmp",
"Ch22p2_SkyBox_Left.bmp",
"Ch22p2_SkyBox_Right.bmp"
);
m_LensFlare.RestoreDeviceObjects(m_pd3dDevice);
LPDIRECT3DTEXTURE8 texHalo = m_LensFlare.AddTexture("Ch22p2_LensFlare_Halo.dds");
LPDIRECT3DTEXTURE8 tex1 = m_LensFlare.AddTexture("Ch22p2_LensFlare_01.dds");
LPDIRECT3DTEXTURE8 tex2 = m_LensFlare.AddTexture("Ch22p2_LensFlare_02.dds");
LPDIRECT3DTEXTURE8 tex3 = m_LensFlare.AddTexture("Ch22p2_LensFlare_03.dds");
m_LensFlare.AddSpot(CLensFlareSpot(texHalo, 1.20f, 1.0f, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.10f, 0.8f, D3DXCOLOR(0.7f, 0.5f, 0.0f, 0.2f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.01f, 0.7f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 0.7f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f, 0.6f, D3DXCOLOR(1.0f, 1.0f, 0.0f, 0.5f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.13f, 0.5f, D3DXCOLOR(1.0f, 1.0f, 0.0f, 0.5f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f, 0.4f, D3DXCOLOR(1.0f, 0.5f, 0.0f, 1.0f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.05f, 0.1f, D3DXCOLOR(1.0f, 1.0f, 0.5f, 0.5f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.05f, -0.2f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 1.0f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.09f, -0.3f, D3DXCOLOR(1.0f, 1.0f, 0.6f, 0.5f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex1, 0.15f, -0.4f, D3DXCOLOR(1.0f, 0.7f, 0.0f, 0.3f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.15f, -0.7f, D3DXCOLOR(1.0f, 0.5f, 0.0f, 0.2f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex2, 0.32f, -1.0f, D3DXCOLOR(1.0f, 0.7f, 0.0f, 0.4f)));
m_LensFlare.AddSpot(CLensFlareSpot(tex3, 0.40f, -1.3f, D3DXCOLOR(1.0f, 0.0f, 0.0f, 0.7f)));
m_pFont->RestoreDeviceObjects();
m_pFontSmall->RestoreDeviceObjects();
m_Camera.SetPosition(D3DXVECTOR3(0.0f, 7.0f, -20.0f));
if( FAILED( m_pObject->Create(m_pd3dDevice, _T("Ch22p2_Planet.x") ) ) )
return E_FAIL;
m_pObject->RestoreDeviceObjects(m_pd3dDevice);
// Set the world matrix
D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Set projection matrix
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixPerspectiveFovLH( &m_matProj, D3DX_PI/4, fAspect, 0.1f, 100.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &m_matProj );
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; ::ZeroMemory(&light, sizeof(D3DLIGHT8));
light.Ambient.a = light.Ambient.g = light.Ambient.b = light.Ambient.r = 0.0;
light.Diffuse.a = light.Diffuse.g = light.Diffuse.b = light.Diffuse.r = 1.0;
light.Position = m_vSunPos;
light.Range = 500.0f;
light.Type = D3DLIGHT_POINT;
light.Phi = 0.0;
light.Attenuation0 = 1.0f;
//light.Attenuation1 = 1.0f;
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();
m_LensFlare.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()
{
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 )
{
// Pass remaining messages to default handler
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -