void AdvanceFrame() { RebuildGrid(); ComputeForces(); ProcessCollisions(); AdvanceParticles(); #if defined(USE_ImpeneratableWall) // N.B. The integration of the position can place the particle // outside the domain. We now make a pass on the perimiter cells // to account for particle migration beyond domain. ProcessCollisions2(); #endif #ifdef ENABLE_STATISTICS float mean, stddev; int i; mean = (float)numParticles/(float)numCells; stddev = 0.0; for(i=0; i<numCells; i++) { stddev += (mean-cnumPars[i])*(mean-cnumPars[i]); } stddev = sqrtf(stddev); std::cout << "Cell statistics: mean=" << mean << " particles, stddev=" << stddev << " particles." << std::endl; #endif }
//-------------------------------------------------------------------------------------- void SoftParticles::OnD3D11FrameRender( ID3D11DeviceContext* context, D3DXMATRIX viewMatrix, D3DXMATRIX projectionMatrix,D3DXVECTOR3 spaceshipPos) { ////////////////////////////////////////////////////////////////////////// //On Frame Move ////////////////////////////////////////////////////////////////////////// CameraClass* cc = CameraManager::getInstance()->getCamera("mainCamera"); const float dt = 1.0f/60.0f; m_time += dt; D3DXVECTOR3 vEye = cc->GetPosition(); D3DXVECTOR3 vDir = - vEye; D3DXVec3Normalize( &vDir, &vDir ); AdvanceParticles( m_time, dt ); SortParticleBuffer( vEye, vDir ); UpdateParticleBuffers( context ); // Update the movement of the noise octaves D3DXVECTOR4 OctaveOffsets[4]; for( int i=0; i<4; i++ ) { OctaveOffsets[i].x = -(float)(m_time*0.05); OctaveOffsets[i].y = 0; OctaveOffsets[i].z = 0; OctaveOffsets[i].w = 0; } g_pvOctaveOffsets->SetFloatVectorArray( (float*)OctaveOffsets, 0, 4 ); ////////////////////////////////////////////////////////////////////////// //On Frame Move End ////////////////////////////////////////////////////////////////////////// ID3D11RenderTargetView* pRTV; ID3D11DepthStencilView* pDSV; context->OMGetRenderTargets(1,&pRTV,&pDSV); // Get the projection & view matrix from the camera class D3DXMATRIX mWorld; D3DXMATRIX mView; D3DXMATRIX mProj; D3DXMATRIX mInvView; D3DXMATRIX mInvProj; D3DXMatrixIdentity( &mWorld ); //D3DXMATRIX mScale; //float mScaleValue = 1.0f; //D3DXMatrixScaling(&mScale,mScaleValue,mScaleValue,mScaleValue); D3DXMatrixTranslation(&mWorld,spaceshipPos.x - 15.0f,spaceshipPos.y + 3.5f,spaceshipPos.z); //mWorld = mScale * mWorld; mProj = projectionMatrix; mView = viewMatrix; 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 = - cc->GetPosition(); D3DXVec3Normalize( &viewDir, &viewDir ); D3DXVECTOR3 vec3 = cc->GetPosition(); D3DXVECTOR4 vEyePt; vEyePt.x = vec3.x; vEyePt.y = vec3.y; vEyePt.z = vec3.z; FLOAT fScreenSize[ 2 ] = { (FLOAT)g_iWidth, (FLOAT)g_iHeight }; 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 ); g_pvScreenSize->SetFloatVector( fScreenSize ); ID3DX11EffectTechnique* pParticleTech = NULL; if( 1 == g_iSampleCount ) { 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; }; } else { 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_pRenderBillboardParticlesSoftMSAA; break; case PT_BILLBOARD_ODEPTHSOFT: pParticleTech = g_pRenderBillboardParticlesODepthSoftMSAA; break; case PT_VOLUME_HARD: pParticleTech = g_pRenderVolumeParticlesHardMSAA; break; case PT_VOLUME_SOFT: pParticleTech = g_pRenderVolumeParticlesSoftMSAA; break; }; } if( PT_BILLBOARD_HARD != g_ParticleTechnique && PT_BILLBOARD_ODEPTH != g_ParticleTechnique ) { // Unbind the depth stencil texture from the device context->OMSetRenderTargets( 1, &pRTV, NULL ); // Bind it instead as a shader resource view if( 1 == g_iSampleCount ) { g_pDepthTex->SetResource( g_pDepthStencilSRV ); } else { g_pDepthMSAATex->SetResource( g_pDepthStencilSRV ); } } // Render the particles context->IASetInputLayout( g_pParticleVertexLayout ); ID3D11Buffer *pBuffers[1] = { g_pParticleVB }; UINT stride[1] = { sizeof(PARTICLE_VERTEX) }; UINT offset[1] = { 0 }; context->IASetVertexBuffers( 0, 1, pBuffers, stride, offset ); context->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_POINTLIST ); context->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 ); D3DX11_TECHNIQUE_DESC techDesc; pParticleTech->GetDesc( &techDesc ); for( UINT p = 0; p < techDesc.Passes; ++p ) { pParticleTech->GetPassByIndex( p )->Apply(0,context); context->DrawIndexed( MAX_PARTICLES, 0, 0 ); } // unbind the depth from the resource so we can set it as depth next time around ID3D11ShaderResourceView* Nulls[2] = {NULL,NULL}; context->PSSetShaderResources( 0, 2, Nulls ); }