void SetSceneConstants() { FreeLook freelook = Demo.Freelook; Vector3 up = MathHelper.Convert(freelook.Up); sceneConstants.View = Matrix.LookAtLH(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), up); sceneConstants.Projection = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane); sceneConstants.ViewInverse = Matrix.Invert(sceneConstants.View); Vector3 light = new Vector3(20, 30, 10); Texture2DDescription depthBuffer = lightDepthTexture.Description; Matrix lightView = Matrix.LookAtLH(light, Vector3.Zero, up); Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _nearPlane, FarPlane); sceneConstants.LightViewProjection = lightView * lightProjection; DataStream data; _immediateContext.MapSubresource(sceneConstantsBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out data); Marshal.StructureToPtr(sceneConstants, data.DataPointer, false); _immediateContext.UnmapSubresource(sceneConstantsBuffer, 0); data.Dispose(); inverseProjectionVar.SetMatrix(Matrix.Invert(sceneConstants.Projection)); inverseViewVar.SetMatrix(sceneConstants.ViewInverse); lightInverseViewProjectionVar.SetMatrix(Matrix.Invert(sceneConstants.LightViewProjection)); lightPositionVar.Set(new Vector4(light, 1)); eyePositionVar.Set(new Vector4(MathHelper.Convert(freelook.Eye), 1)); eyeZAxisVar.Set(new Vector4(Vector3.Normalize(MathHelper.Convert(freelook.Target - freelook.Eye)), 1)); float tanHalfFOVY = (float)Math.Tan(FieldOfView * 0.5f); tanHalfFOVXVar.Set(tanHalfFOVY * AspectRatio); tanHalfFOVYVar.Set(tanHalfFOVY); float projectionA = FarPlane / (FarPlane - _nearPlane); float projectionB = -projectionA * _nearPlane; projectionAVar.Set(projectionA); projectionBVar.Set(projectionB); Matrix overlayMatrix = Matrix.Scaling(info.Width / _width, info.Height / _height, 1.0f); overlayMatrix *= Matrix.Translation(-(_width - info.Width) / _width, (_height - info.Height) / _height, 0.0f); overlayViewProjectionVar.SetMatrix(overlayMatrix); }
void SetSceneConstants() { FreeLook freelook = Demo.Freelook; Vector3 up = MathHelper.Convert(freelook.Up); sceneConstants.View = Matrix.LookAtLH(MathHelper.Convert(freelook.Eye), MathHelper.Convert(freelook.Target), up); sceneConstants.Projection = Matrix.PerspectiveFovLH(FieldOfView, AspectRatio, _nearPlane, FarPlane); sceneConstants.ViewInverse = Matrix.Invert(sceneConstants.View); Vector3 light = new Vector3(20, 30, 10); Texture2DDescription depthBuffer = lightDepthTexture.Description; Matrix lightView = Matrix.LookAtLH(light, Vector3.Zero, up); Matrix lightProjection = Matrix.OrthoLH(depthBuffer.Width / 8.0f, depthBuffer.Height / 8.0f, _nearPlane, FarPlane); sceneConstants.LightViewProjection = lightView * lightProjection; using (var data = sceneConstantsBuffer.Map(MapMode.WriteDiscard)) { Marshal.StructureToPtr(sceneConstants, data.DataPointer, false); sceneConstantsBuffer.Unmap(); } effect2.GetVariableByName("InverseProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.Projection)); effect2.GetVariableByName("InverseView").AsMatrix().SetMatrix(sceneConstants.ViewInverse); effect2.GetVariableByName("LightInverseViewProjection").AsMatrix().SetMatrix(Matrix.Invert(sceneConstants.LightViewProjection)); effect2.GetVariableByName("LightPosition").AsVector().Set(new Vector4(light, 1)); effect2.GetVariableByName("EyePosition").AsVector().Set(new Vector4(MathHelper.Convert(freelook.Eye), 1)); effect2.GetVariableByName("EyeZAxis").AsVector().Set(new Vector4(Vector3.Normalize(MathHelper.Convert(freelook.Target - freelook.Eye)), 1)); float tanHalfFOVY = (float)Math.Tan(FieldOfView * 0.5f); effect2.GetVariableByName("TanHalfFOVX").AsScalar().Set(tanHalfFOVY * AspectRatio); effect2.GetVariableByName("TanHalfFOVY").AsScalar().Set(tanHalfFOVY); float projectionA = FarPlane / (FarPlane - _nearPlane); float projectionB = -projectionA * _nearPlane; effect2.GetVariableByName("ProjectionA").AsScalar().Set(projectionA); effect2.GetVariableByName("ProjectionB").AsScalar().Set(projectionB); Matrix overlayMatrix = Matrix.Scaling(2 * info.Width / _width, 2 * info.Height / _height, 1.0f); overlayMatrix *= Matrix.Translation(-(_width - info.Width) / _width, (_height - info.Height) / _height, 0.0f); effect2.GetVariableByName("OverlayViewProjection").AsMatrix().SetMatrix(overlayMatrix); }
public override void Initialize() { Form.SizeChanged += (o, args) => { _width = Form.ClientSize.Width; _height = Form.ClientSize.Height; if (_swapChain == null) { return; } renderView.Dispose(); depthView.Dispose(); _swapChain.ResizeBuffers(_swapChain.Description.BufferCount, 0, 0, Format.Unknown, 0); CreateBuffers(); SetSceneConstants(); }; _width = 1024; _height = 768; _nearPlane = 1.0f; ambient = new Color4(Color.Gray.ToArgb()); try { OnInitializeDevice(); } catch (Exception e) { MessageBox.Show(e.ToString(), "Could not create DirectX 10 device."); return; } // shader.fx const ShaderFlags shaderFlags = ShaderFlags.None; //const ShaderFlags shaderFlags = ShaderFlags.Debug | ShaderFlags.SkipOptimization; ShaderBytecode shaderByteCode = LoadShader("shader.fx", shaderFlags); effect = new Effect(_device, shaderByteCode); EffectTechnique technique = effect.GetTechniqueByIndex(0); shadowGenPass = technique.GetPassByIndex(0); gBufferGenPass = technique.GetPassByIndex(1); debugDrawPass = technique.GetPassByName("debug"); BufferDescription sceneConstantsDesc = new BufferDescription() { SizeInBytes = Marshal.SizeOf(typeof(ShaderSceneConstants)), Usage = ResourceUsage.Dynamic, BindFlags = BindFlags.ConstantBuffer, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None }; sceneConstantsBuffer = new Buffer(_device, sceneConstantsDesc); EffectConstantBuffer effectConstantBuffer = effect.GetConstantBufferByName("scene"); effectConstantBuffer.SetConstantBuffer(sceneConstantsBuffer); RasterizerStateDescription desc = new RasterizerStateDescription() { CullMode = CullMode.None, FillMode = FillMode.Solid, IsFrontCounterClockwise = true, DepthBias = 0, DepthBiasClamp = 0, SlopeScaledDepthBias = 0, IsDepthClipEnabled = true, }; _device.Rasterizer.State = new RasterizerState(_device, desc); DepthStencilStateDescription depthDesc = new DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Less }; depthStencilState = new DepthStencilState(_device, depthDesc); DepthStencilStateDescription lightDepthStateDesc = new DepthStencilStateDescription() { IsDepthEnabled = true, IsStencilEnabled = false, DepthWriteMask = DepthWriteMask.All, DepthComparison = Comparison.Less }; lightDepthStencilState = new DepthStencilState(_device, lightDepthStateDesc); // grender.fx shaderByteCode = LoadShader("grender.fx", shaderFlags); effect2 = new Effect(_device, shaderByteCode); technique = effect2.GetTechniqueByIndex(0); gBufferRenderPass = technique.GetPassByIndex(0); gBufferOverlayPass = technique.GetPassByIndex(1); Buffer quad = DemoFramework.SharpDX.MeshFactory.CreateScreenQuad(_device); quadBinding = new VertexBufferBinding(quad, 20, 0); Matrix quadProjection = Matrix.OrthoLH(1, 1, 0.1f, 1.0f); effect2.GetVariableByName("ViewProjection").AsMatrix().SetMatrix(quadProjection); InputElement[] elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0, InputClassification.PerVertexData, 0), }; quadBufferLayout = new InputLayout(_device, gBufferRenderPass.Description.Signature, elements); info = new InfoText(_device); _meshFactory = new MeshFactory(this); MeshFactory = _meshFactory; CreateBuffers(); LibraryManager.LibraryStarted(); }