private void UpdateMaterialCBs() { UploadBuffer <MaterialConstants> currMaterialCB = CurrFrameResource.MaterialCB; foreach (Material mat in _materials.Values) { // Only update the cbuffer data if the constants have changed. If the cbuffer // data changes, it needs to be updated for each FrameResource. if (mat.NumFramesDirty > 0) { var matConstants = new MaterialConstants { DiffuseAlbedo = mat.DiffuseAlbedo, FresnelR0 = mat.FresnelR0, Roughness = mat.Roughness, MatTransform = Matrix.Transpose(mat.MatTransform) }; currMaterialCB.CopyData(mat.MatCBIndex, ref matConstants); // Next FrameResource need to be updated too. mat.NumFramesDirty--; } } }
private void UpdateCubeMapFacePassCBs() { for (int i = 0; i < 6; i++) { PassConstants cubeFacePassCB = _mainPassCB; Matrix view = _cubeMapCameras[i].View; Matrix proj = _cubeMapCameras[i].Proj; Matrix viewProj = view * proj; Matrix invView = Matrix.Invert(view); Matrix invProj = Matrix.Invert(proj); Matrix invViewProj = Matrix.Invert(viewProj); cubeFacePassCB.View = Matrix.Transpose(view); cubeFacePassCB.InvView = Matrix.Transpose(invView); cubeFacePassCB.Proj = Matrix.Transpose(proj); cubeFacePassCB.InvProj = Matrix.Transpose(invProj); cubeFacePassCB.ViewProj = Matrix.Transpose(viewProj); cubeFacePassCB.InvViewProj = Matrix.Transpose(invViewProj); cubeFacePassCB.EyePosW = _cubeMapCameras[i].Position; cubeFacePassCB.RenderTargetSize = new Vector2(_dynamicCubeMap.Width, _dynamicCubeMap.Height); cubeFacePassCB.InvRenderTargetSize = new Vector2(1.0f / _dynamicCubeMap.Width, 1.0f / _dynamicCubeMap.Height); UploadBuffer <PassConstants> currPassCB = CurrFrameResource.PassCB; // Cube map pass cbuffers are stored in elements 1-6. currPassCB.CopyData(1 + i, ref cubeFacePassCB); } }
private void UpdateWaves(GameTimer gt) { // Every quarter second, generate a random wave. if ((Timer.TotalTime - _tBase) >= 0.25f) { _tBase += 0.25f; int i = MathHelper.Rand(4, _waves.RowCount - 5); int j = MathHelper.Rand(4, _waves.ColumnCount - 5); float r = MathHelper.Randf(0.2f, 0.5f); _waves.Disturb(i, j, r); } // Update the wave simulation. _waves.Update(gt.DeltaTime); // Update the wave vertex buffer with the new solution. UploadBuffer <Vertex> currWavesVB = CurrFrameResource.WavesVB; for (int i = 0; i < _waves.VertexCount; ++i) { var v = new Vertex { Pos = _waves.Position(i), Color = Color.Blue.ToVector4() }; currWavesVB.CopyData(i, ref v); } // Set the dynamic VB of the wave renderitem to the current frame VB. _wavesRitem.Geo.VertexBufferGPU = currWavesVB.Resource; }
private void UpdateInstanceData() { UploadBuffer <InstanceData> currInstanceBuffer = CurrFrameResource.InstanceBuffer; foreach (RenderItem e in _allRitems) { int visibleInstanceCount = 0; foreach (InstanceData instance in e.Instances) { var box = new BoundingBox( Vector3.TransformCoordinate(e.Bounds.Minimum, instance.World), Vector3.TransformCoordinate(e.Bounds.Maximum, instance.World)); // Perform the box/frustum intersection test in local space. if (!_frustumCullingEnabled || _camera.Frustum.Intersects(ref box)) { var data = new InstanceData { World = Matrix.Transpose(instance.World), TexTransform = Matrix.Transpose(instance.TexTransform), MaterialIndex = instance.MaterialIndex }; // Write the instance data to structured buffer for the visible objects. currInstanceBuffer.CopyData(visibleInstanceCount++, ref data); } } e.InstanceCount = visibleInstanceCount; MainWindowCaption = $"Instancing and Culling {e.InstanceCount} objects visible out of {e.Instances.Length}"; } }
protected override void Update(GameTimer gt) { // Convert Spherical to Cartesian coordinates. float x = _radius * MathHelper.Sinf(_phi) * MathHelper.Cosf(_theta); float z = _radius * MathHelper.Sinf(_phi) * MathHelper.Sinf(_theta); float y = _radius * MathHelper.Cosf(_phi); // Build the view matrix. _view = Matrix.LookAtLH(new Vector3(x, y, z), Vector3.Zero, Vector3.Up); // Simply use identity for world matrix for this demo. Matrix world = Matrix.Identity; var cb = new ObjectConstants { WorldViewProj = Matrix.Transpose(world * _view * _proj) }; // Update the constant buffer with the latest worldViewProj matrix. _objectCB.CopyData(0, ref cb); }
private void UpdateWaves(GameTimer gt) { // Every quarter second, generate a random wave. if ((Timer.TotalTime - _tBase) >= 0.25f) { _tBase += 0.25f; int i = MathHelper.Rand(4, _waves.RowCount - 5); int j = MathHelper.Rand(4, _waves.ColumnCount - 5); float r = MathHelper.Randf(0.2f, 0.5f); _waves.Disturb(i, j, r); } // Update the wave simulation. _waves.Update(gt.DeltaTime); // Update the wave vertex buffer with the new solution. UploadBuffer <Vertex> currWavesVB = CurrFrameResource.WavesVB; for (int i = 0; i < _waves.VertexCount; ++i) { var v = new Vertex { Pos = _waves.Position(i), Normal = _waves.Normal(i), }; // Derive tex-coords from position by // mapping [-w/2,w/2] --> [0,1] v.TexC = new Vector2( 0.5f + v.Pos.X / _waves.Width, 0.5f - v.Pos.Z / _waves.Depth); currWavesVB.CopyData(i, ref v); } // Set the dynamic VB of the wave renderitem to the current frame VB. _wavesRitem.Geo.VertexBufferGPU = currWavesVB.Resource; }