📄 ch15p1_simpleclouds.cpp
字号:
m_pd3dDevice = pDev;
m_pvbQuad = pvbQuad;
// create our blending vertex buffer... a quad with
// 0.5 alpha components on all vertices.
// Create vertex buffer
if (FAILED(hr = CreateQuad(&m_pvbAlphaBlendingQuad, D3DPOOL_MANAGED, 2.0f,
D3DCOLOR_ARGB(128,128,128,128), m_pd3dDevice))) return hr;
// new up the appropriate number of cloud layers
int iThisSize = GetLowestPowerOf2(iTextureSize);
m_iTextureSize = iThisSize;
try {
for (int loop=0; loop < iNumOctaves; loop++) {
CCloudOctave *octave = new CCloudOctave(iThisSize, pDev, pvbQuad);
m_vOctaves.push_back(octave);
iThisSize /= 2;
}
// create the final and composite texture
if (FAILED(hr = D3DXCreateTexture(pDev, iTextureSize, iTextureSize,
1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_ptexFinal))) throw(hr);
if (FAILED(hr = D3DXCreateTexture(pDev, iTextureSize, iTextureSize,
1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_ptexComposite))) throw(hr);
} catch(HRESULT hr) { return hr; }
// now that the layers are constructed, generate some noise for each.
for (CloudOctaveVector::iterator i = m_vOctaves.begin(); i != m_vOctaves.end(); ++i) {
if (FAILED(hr = (*i)->GenerateRegularNoise())) return(hr);
// use bilinear filtering to smooth out the generated noise.
if (FAILED(hr = (*i)->SmoothLayer())) return(hr);
}
return S_OK;
}
HRESULT CCloud::InvalidateDeviceObjects()
{
// delete the cloud layers
for (CloudOctaveVector::iterator i = m_vOctaves.begin(); i != m_vOctaves.end(); ++i) {
delete (*i);
}
m_vOctaves.clear();
// release textures
SAFE_RELEASE(m_ptexFinal);
SAFE_RELEASE(m_ptexComposite);
// release alpha blending quad
SAFE_RELEASE(m_pvbAlphaBlendingQuad);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: class CMyD3DApplication
// Desc: Application class. The base class (CD3DApplication) provides the
// generic functionality needed in all Direct3D samples. CMyD3DApplication
// adds functionality specific to this sample program.
//-----------------------------------------------------------------------------
class CMyD3DApplication : public CD3DApplication
{
// Font for drawing text
CD3DFont* m_pFont;
CD3DFont* m_pFontSmall;
// quad for rendering our textures
LPDIRECT3DVERTEXBUFFER8 m_pVB;
DWORD m_dwNumVertices;
// Our Cloud
CCloud m_Cloud;
protected:
HRESULT OneTimeSceneInit();
HRESULT InitDeviceObjects();
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
HRESULT FinalCleanup();
HRESULT Render();
HRESULT FrameMove();
HRESULT ConfirmDevice( D3DCAPS8* pCaps, DWORD dwBehavior, D3DFORMAT Format );
LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
public:
CMyD3DApplication();
};
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: Entry point to the program. Initializes everything, and goes into a
// message-processing loop. Idle time is used to render the scene.
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
CMyD3DApplication d3dApp;
if( FAILED( d3dApp.Create( hInst ) ) )
return 0;
return d3dApp.Run();
}
//-----------------------------------------------------------------------------
// Name: CMyD3DApplication()
// Desc: Application constructor. Sets attributes for the app.
//-----------------------------------------------------------------------------
CMyD3DApplication::CMyD3DApplication()
{
m_strWindowTitle = _T("Ch15p1_SimpleClouds");
m_bUseDepthBuffer = TRUE;
m_pFont = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
m_pFontSmall = new CD3DFont( _T("Arial"), 9, D3DFONT_BOLD );
m_pVB = NULL;
m_dwNumVertices = 6;
}
//-----------------------------------------------------------------------------
// Name: OneTimeSceneInit()
// Desc: Called during initial app startup, this function performs all the
// permanent initialization.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::OneTimeSceneInit()
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FrameMove()
// Desc: Called once per frame, the call is the entry point for animating
// the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::FrameMove()
{
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Called once per frame, the call is the entry point for 3d
// rendering. This function sets up render states, clears the
// viewport, and renders the scene.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
// Clear the backbuffer
m_pd3dDevice->Clear(0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
0x000000, 1.0f, 0L);
// Begin rendering the scene
if( SUCCEEDED(m_pd3dDevice->BeginScene()))
{
// set up an orthogonal projection of 1050x1050 units.
D3DXMATRIX projmat;
D3DXMatrixOrthoLH(&projmat, 1000.0f, 1000.0f, 0.0, 100.0);
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &projmat );
// draw all of the cloud octaves individually across the top of the screen.
CloudOctaveVector &vOctaves = m_Cloud.m_vOctaves;
m_pd3dDevice->SetStreamSource(0, m_pVB, sizeof(VERTEX_XYZ_DIFFUSE_TEX1));
m_pd3dDevice->SetVertexShader(D3DFVF_XYZ_DIFFUSE_TEX1);
m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
m_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
// set up texture stages for simple texture stage copy
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
for (int q=0; q < vOctaves.size(); q++) {
CCloudOctave *oct = vOctaves[q];
int fOctaveWidth = 900.0f/(float)vOctaves.size();
int fOctavePos = -450+(q*fOctaveWidth)+(fOctaveWidth/2.0f);
// set up our world transform for this quad
D3DXMATRIX transmat, scalemat,worldmat;
D3DXMatrixScaling(&scalemat, (fOctaveWidth-50)/2, (fOctaveWidth-50)/2, 1.0f);
D3DXMatrixMultiply(&worldmat, &scalemat, D3DXMatrixTranslation(&transmat, fOctavePos, 400.0f, 0.0));
m_pd3dDevice->SetTransform( D3DTS_WORLD, &worldmat );
// make the octave's texture active...
m_pd3dDevice->SetTexture(0, oct->m_ptexSmoothOctave);
// draw our quad
m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
}
// now we draw the final composite cloud texture in a big square
D3DXMATRIX transmat, scalemat,worldmat;
D3DXMatrixScaling(&scalemat, 350.0f, 350.0f, 1.0f);
D3DXMatrixMultiply(&worldmat, &scalemat, D3DXMatrixTranslation(&transmat, 0.0f, -100.0f, 0.0f));
m_pd3dDevice->SetTransform( D3DTS_WORLD, &worldmat );
// make the final cloud texture active...
m_pd3dDevice->SetTexture(0, m_Cloud.GetFinalTexture());
// draw our quad
m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
// Output statistics
m_pFont->DrawText(2, 0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats);
m_pFont->DrawText(2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats);
// 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 );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
HRESULT hr;
m_pFont->RestoreDeviceObjects();
m_pFontSmall->RestoreDeviceObjects();
// Create vertex buffer
if (FAILED( hr = CreateQuad(&m_pVB, D3DPOOL_MANAGED, 2.0f, 0xffffffff, m_pd3dDevice)))
return hr;
// init the cloud - this sets up all our textures and everything
if (FAILED(hr = m_Cloud.RestoreDeviceObjects(m_pd3dDevice, m_pVB, NUMOCTAVES, 256)))
return(hr);
// since we're not animating, we can tell the cloud to render itself to
// its texture now.
m_Cloud.RenderToFinalTexture();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InvalidateDeviceObjects()
{
m_pFont->InvalidateDeviceObjects();
m_pFontSmall->InvalidateDeviceObjects();
m_Cloud.InvalidateDeviceObjects();
SAFE_RELEASE(m_pVB);
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();
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 )
{
return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -