softparticles.cpp
来自「声音和图片的加载功能,不过运行起来有点卡」· C++ 代码 · 共 990 行 · 第 1/3 页
CPP
990 行
return true;
}
//--------------------------------------------------------------------------------------
// Create any D3D10 resources that aren't dependant on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D10CreateDevice( ID3D10Device* pd3dDevice, const DXGI_SURFACE_DESC *pBackBufferSurfaceDesc, void* pUserContext )
{
HRESULT hr;
V_RETURN( g_DialogResourceManager.OnD3D10CreateDevice( pd3dDevice ) );
V_RETURN( g_D3DSettingsDlg.OnD3D10CreateDevice( pd3dDevice ) );
V_RETURN( D3DX10CreateFont( pd3dDevice, 15, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE,
L"Arial", &g_pFont10 ) );
V_RETURN( D3DX10CreateSprite( pd3dDevice, 512, &g_pSprite10 ) );
g_pTxtHelper = new CDXUTTextHelper( NULL, NULL, g_pFont10, g_pSprite10, 15 );
// Read the D3DX effect file
WCHAR str[MAX_PATH];
V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"SoftParticles.fx" ) );
DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
// Set the D3D10_SHADER_DEBUG flag to embed debug information in the shaders.
// Setting this flag improves the shader debugging experience, but still allows
// the shaders to be optimized and to run exactly the way they will run in
// the release configuration of this program.
dwShaderFlags |= D3D10_SHADER_DEBUG;
#endif
V_RETURN( D3DX10CreateEffectFromFile( str, NULL, NULL, "fx_4_0", dwShaderFlags, 0, pd3dDevice, NULL, NULL, &g_pEffect10, NULL, NULL ) );
// Obtain the technique handles
g_pRenderScene = g_pEffect10->GetTechniqueByName( "RenderScene" );
g_pRenderSky = g_pEffect10->GetTechniqueByName( "RenderSky" );
g_pRenderBillboardParticlesHard = g_pEffect10->GetTechniqueByName( "RenderBillboardParticles_Hard" );
g_pRenderBillboardParticlesODepth = g_pEffect10->GetTechniqueByName( "RenderBillboardParticles_ODepth" );
g_pRenderBillboardParticlesSoft = g_pEffect10->GetTechniqueByName( "RenderBillboardParticles_Soft" );
g_pRenderBillboardParticlesODepthSoft = g_pEffect10->GetTechniqueByName( "RenderBillboardParticles_ODepthSoft" );
g_pRenderVolumeParticlesHard = g_pEffect10->GetTechniqueByName( "RenderVolumeParticles_Hard" );
g_pRenderVolumeParticlesSoft = g_pEffect10->GetTechniqueByName( "RenderVolumeParticles_Soft" );
// Obtain the parameter handles
g_pmWorldViewProj = g_pEffect10->GetVariableByName( "g_mWorldViewProj" )->AsMatrix();
g_pmWorldView = g_pEffect10->GetVariableByName( "g_mWorldView" )->AsMatrix();
g_pmWorld = g_pEffect10->GetVariableByName( "g_mWorld" )->AsMatrix();
g_pmInvView = g_pEffect10->GetVariableByName( "g_mInvView" )->AsMatrix();
g_pmInvProj = g_pEffect10->GetVariableByName( "g_mInvProj" )->AsMatrix();
g_pfFadeDistance = g_pEffect10->GetVariableByName( "g_fFadeDistance" )->AsScalar();
g_pfSizeZScale = g_pEffect10->GetVariableByName( "g_fSizeZScale" )->AsScalar();
g_pvViewLightDir1 = g_pEffect10->GetVariableByName( "g_vViewLightDir1" )->AsVector();
g_pvViewLightDir2 = g_pEffect10->GetVariableByName( "g_vViewLightDir2" )->AsVector();
g_pvWorldLightDir1 = g_pEffect10->GetVariableByName( "g_vWorldLightDir1" )->AsVector();
g_pvWorldLightDir2 = g_pEffect10->GetVariableByName( "g_vWorldLightDir2" )->AsVector();
g_pvEyePt = g_pEffect10->GetVariableByName( "g_vEyePt" )->AsVector();
g_pvViewDir = g_pEffect10->GetVariableByName( "g_vViewDir" )->AsVector();
g_pvOctaveOffsets = g_pEffect10->GetVariableByName( "g_OctaveOffsets" )->AsVector();
g_pDiffuseTex = g_pEffect10->GetVariableByName( "g_txDiffuse" )->AsShaderResource();
g_pNormalTex = g_pEffect10->GetVariableByName( "g_txNormal" )->AsShaderResource();
g_pColorGradient = g_pEffect10->GetVariableByName( "g_txColorGradient" )->AsShaderResource();
g_pVolumeDiffTex = g_pEffect10->GetVariableByName( "g_txVolumeDiff" )->AsShaderResource();
g_pVolumeNormTex = g_pEffect10->GetVariableByName( "g_txVolumeNorm" )->AsShaderResource();
g_pDepthTex = g_pEffect10->GetVariableByName( "g_txDepth" )->AsShaderResource();
// Create our vertex input layouts
const D3D10_INPUT_ELEMENT_DESC scenelayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 32, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};
D3D10_PASS_DESC PassDesc;
g_pRenderScene->GetPassByIndex( 0 )->GetDesc( &PassDesc );
V_RETURN( pd3dDevice->CreateInputLayout( scenelayout, sizeof(scenelayout)/sizeof(scenelayout[0]), PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pSceneVertexLayout ) );
const D3D10_INPUT_ELEMENT_DESC particlelayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "VELOCITY", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "LIFE", 0, DXGI_FORMAT_R32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0 },
{ "SIZE", 0, DXGI_FORMAT_R32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0 },
};
g_pRenderBillboardParticlesHard->GetPassByIndex( 0 )->GetDesc( &PassDesc );
V_RETURN( pd3dDevice->CreateInputLayout( particlelayout, sizeof(particlelayout)/sizeof(scenelayout[0]), PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &g_pParticleVertexLayout ) );
// Load meshes
V_RETURN( g_ObjectMesh.Create( pd3dDevice, L"SoftParticles\\TankScene.sdkmesh", true ) );
V_RETURN( g_SkyMesh.Create( pd3dDevice, L"SoftParticles\\desertsky.sdkmesh", true ) );
// Create the particles
V_RETURN( CreateParticleBuffers( pd3dDevice ) );
// Create the noise volume
V_RETURN( CreateNoiseVolume( pd3dDevice, 32 ) );
// Load the Particle Texture
V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"SoftParticles\\smokevol1.dds" ) );
V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pParticleTexRV, NULL ) );
V_RETURN( DXUTFindDXSDKMediaFileCch( str, MAX_PATH, L"SoftParticles\\colorgradient.dds" ) );
V_RETURN( D3DX10CreateShaderResourceViewFromFile( pd3dDevice, str, NULL, NULL, &g_pColorGradTexRV, NULL ) );
// Setup the camera's view parameters
D3DXVECTOR3 vecEye(2,1,-10);
D3DXVECTOR3 vecAt (2,1,0);
g_Camera.SetViewParams( &vecEye, &vecAt );
g_Camera.SetRadius( 10.0f, 1.0f, 20.0f );
g_pfFadeDistance->SetFloat( g_fFadeDistance );
return S_OK;
}
//--------------------------------------------------------------------------------------
// Create any D3D10 resources that depend on the back buffer
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D10SwapChainResized( ID3D10Device* pd3dDevice, IDXGISwapChain *pSwapChain, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc, void* pUserContext )
{
HRESULT hr = S_OK;
V_RETURN( g_DialogResourceManager.OnD3D10ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );
V_RETURN( g_D3DSettingsDlg.OnD3D10ResizedSwapChain( pd3dDevice, pBackBufferSurfaceDesc ) );
// Setup the camera's projection parameters
float fNearPlane = 0.1f;
float fFarPlane = 150.0f;
float fAspectRatio = pBackBufferSurfaceDesc->Width / (FLOAT)pBackBufferSurfaceDesc->Height;
g_Camera.SetProjParams( 54.43f*(D3DX_PI/180.0f), fAspectRatio, fNearPlane, fFarPlane );
g_Camera.SetWindow( pBackBufferSurfaceDesc->Width, pBackBufferSurfaceDesc->Height );
g_Camera.SetButtonMasks( 0, MOUSE_WHEEL, MOUSE_RIGHT_BUTTON | MOUSE_LEFT_BUTTON );
// Set the effect variable
g_pfSizeZScale->SetFloat( 1.0f/(fFarPlane - fNearPlane) );
g_HUD.SetLocation( pBackBufferSurfaceDesc->Width-170, 0 );
g_HUD.SetSize( 170, 170 );
g_SampleUI.SetLocation( pBackBufferSurfaceDesc->Width-170, pBackBufferSurfaceDesc->Height-200 );
g_SampleUI.SetSize( 170, 200 );
// Create a new Depth-Stencil texture to replace the DXUT created one
D3D10_TEXTURE2D_DESC descDepth;
descDepth.Width = pBackBufferSurfaceDesc->Width;
descDepth.Height = pBackBufferSurfaceDesc->Height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_R32_TYPELESS; // Use a typeless type here so that it can be both depth-stencil and shader resource.
// This will fail if we try a format like D32_FLOAT
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL | D3D10_BIND_SHADER_RESOURCE;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
V_RETURN( pd3dDevice->CreateTexture2D( &descDepth, NULL, &g_pDepthStencilTexture ) );
// Create the depth stencil view
D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
descDSV.Format = DXGI_FORMAT_D32_FLOAT; // Make the view see this as D32_FLOAT instead of typeless
descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
V_RETURN( pd3dDevice->CreateDepthStencilView( g_pDepthStencilTexture, &descDSV, &g_pDepthStencilDSV ) );
// Create the shader resource view
D3D10_SHADER_RESOURCE_VIEW_DESC descSRV;
descSRV.Format = DXGI_FORMAT_R32_FLOAT; // Make the shaders see this as R32_FLOAT instead of typeless
descSRV.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
descSRV.Texture2D.MipLevels = 1;
descSRV.Texture2D.MostDetailedMip = 0;
V_RETURN( pd3dDevice->CreateShaderResourceView( g_pDepthStencilTexture, &descSRV, &g_pDepthStencilSRV ) );
return hr;
}
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D10FrameRender( ID3D10Device* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
float ClearColor[4] = { 0.0, 0.0, 0.0, 0.0 };
ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView();
pd3dDevice->ClearRenderTargetView( pRTV, ClearColor);
pd3dDevice->ClearDepthStencilView( g_pDepthStencilDSV, D3D10_CLEAR_DEPTH, 1.0, 0);
pd3dDevice->OMSetRenderTargets( 1, &pRTV, g_pDepthStencilDSV );
// If the settings dialog is being shown, then
// render it instead of rendering the app's scene
if( g_D3DSettingsDlg.IsActive() )
{
g_D3DSettingsDlg.OnRender( fElapsedTime );
return;
}
// Get the projection & view matrix from the camera class
D3DXMATRIX mWorld;
D3DXMATRIX mView;
D3DXMATRIX mProj;
D3DXMATRIX mInvView;
D3DXMATRIX mInvProj;
D3DXMatrixIdentity( &mWorld );
mProj = *g_Camera.GetProjMatrix();
mView = *g_Camera.GetViewMatrix();
D3DXMATRIX mWorldViewProj = mWorld*mView*mProj;
D3DXMATRIX mWorldView = mWorld*mView;
D3DXMatrixInverse( &mInvView, NULL, &mView );
D3DXMatrixInverse( &mInvProj, NULL, &mProj);
D3DXVECTOR4 vViewLightDir1;
D3DXVECTOR4 vWorldLightDir1;
D3DXVECTOR4 vViewLightDir2;
D3DXVECTOR4 vWorldLightDir2;
D3DXVec3Normalize( (D3DXVECTOR3*)&vWorldLightDir1, &g_vLightDir1 );
D3DXVec3TransformNormal( (D3DXVECTOR3*)&vViewLightDir1, &g_vLightDir1, &mView );
D3DXVec3Normalize( (D3DXVECTOR3*)&vViewLightDir1, (D3DXVECTOR3*)&vViewLightDir1 );
D3DXVec3Normalize( (D3DXVECTOR3*)&vWorldLightDir2, &g_vLightDir2 );
D3DXVec3TransformNormal( (D3DXVECTOR3*)&vViewLightDir2, &g_vLightDir2, &mView );
D3DXVec3Normalize( (D3DXVECTOR3*)&vViewLightDir2, (D3DXVECTOR3*)&vViewLightDir2 );
D3DXVECTOR3 viewDir = *g_Camera.GetLookAtPt() - *g_Camera.GetEyePt();
D3DXVec3Normalize( &viewDir, &viewDir );
D3DXVECTOR3 vec3 = *g_Camera.GetEyePt();
D3DXVECTOR4 vEyePt;
vEyePt.x = vec3.x;
vEyePt.y = vec3.y;
vEyePt.z = vec3.z;
g_pmWorldViewProj->SetMatrix( (float*)&mWorldViewProj );
g_pmWorldView->SetMatrix( (float*)&mWorldView );
g_pmWorld->SetMatrix( (float*)&mWorld );
g_pmInvView->SetMatrix( (float*)&mInvView );
g_pmInvProj->SetMatrix( (float*)&mInvProj );
g_pvViewLightDir1->SetFloatVector( (float*)&vViewLightDir1 );
g_pvWorldLightDir1->SetFloatVector( (float*)&vWorldLightDir1);
g_pvViewLightDir2->SetFloatVector( (float*)&vViewLightDir2 );
g_pvWorldLightDir2->SetFloatVector( (float*)&vWorldLightDir2);
g_pvViewDir->SetFloatVector( (float*)&viewDir );
g_pvEyePt->SetFloatVector( (float*)&vEyePt );
// Render the scene
pd3dDevice->IASetInputLayout( g_pSceneVertexLayout );
g_SkyMesh.Render( pd3dDevice, g_pRenderSky, g_pDiffuseTex );
g_ObjectMesh.Render( pd3dDevice, g_pRenderScene, g_pDiffuseTex, g_pNormalTex );
ID3D10EffectTechnique* pParticleTech = NULL;
switch( g_ParticleTechnique )
{
case PT_BILLBOARD_HARD:
pParticleTech = g_pRenderBillboardParticlesHard;
break;
case PT_BILLBOARD_ODEPTH:
pParticleTech = g_pRenderBillboardParticlesODepth;
break;
case PT_BILLBOARD_SOFT:
pParticleTech = g_pRenderBillboardParticlesSoft;
break;
case PT_BILLBOARD_ODEPTHSOFT:
pParticleTech = g_pRenderBillboardParticlesODepthSoft;
break;
case PT_VOLUME_HARD:
pParticleTech = g_pRenderVolumeParticlesHard;
break;
case PT_VOLUME_SOFT:
pParticleTech = g_pRenderVolumeParticlesSoft;
break;
};
if( PT_BILLBOARD_HARD != g_ParticleTechnique &&
PT_BILLBOARD_ODEPTH != g_ParticleTechnique )
{
// Unbind the depth stencil texture from the device
pd3dDevice->OMSetRenderTargets( 1, &pRTV, NULL );
// Bind it instead as a shader resource view
g_pDepthTex->SetResource( g_pDepthStencilSRV );
}
// Render the particles
pd3dDevice->IASetInputLayout( g_pParticleVertexLayout );
ID3D10Buffer *pBuffers[1] = { g_pParticleVB };
UINT stride[1] = { sizeof(PARTICLE_VERTEX) };
UINT offset[1] = { 0 };
pd3dDevice->IASetVertexBuffers( 0, 1, pBuffers, stride, offset );
pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_POINTLIST );
pd3dDevice->IASetIndexBuffer( g_pParticleIB, DXGI_FORMAT_R32_UINT, 0 );
if( PT_VOLUME_HARD == g_ParticleTechnique ||
PT_VOLUME_SOFT == g_ParticleTechnique )
{
g_pVolumeDiffTex->SetResource( g_pNoiseVolumeRV );
g_pVolumeNormTex->SetResource( NULL );
}
else
{
g_pVolumeDiffTex->SetResource( g_pParticleTexRV );
}
g_pColorGradient->SetResource( g_pColorGradTexRV );
D3D10_TECHNIQUE_DESC techDesc;
pParticleTech->GetDesc( &techDesc );
for( UINT p = 0; p < techDesc.Passes; ++p )
{
pParticleTech->GetPassByIndex( p )->Apply(0);
pd3dDevice->DrawIndexed( MAX_PARTICLES, 0, 0 );
}
// unbind the depth from the resource so we can set it as depth next time around
ID3D10ShaderResourceView* Nulls[2] = {NULL,NULL};
pd3dDevice->PSSetShaderResources( 0, 2, Nulls );
DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" );
RenderText();
g_HUD.OnRender( fElapsedTime );
g_SampleUI.OnRender( fElapsedTime );
DXUT_EndPerfEvent();
}
//--------------------------------------------------------------------------------------
// Render the help and statistics text
//--------------------------------------------------------------------------------------
void RenderText()
{
g_pTxtHelper->Begin();
g_pTxtHelper->SetInsertionPos( 2, 0 );
g_pTxtHelper->SetForegroundColor( D3DXCOLOR( 1.0f, 1.0f, 0.0f, 1.0f ) );
g_pTxtHelper->DrawTextLine( DXUTGetFrameStats( DXUTIsVsyncEnabled() ) );
g_pTxtHelper->DrawTextLine( DXUTGetDeviceStats() );
g_pTxtHelper->End();
}
//--------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?