From 86cc23b0adb7bf712a941e3e044985b4ba143992 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Aug 2020 17:58:50 -0700 Subject: [PATCH] start on depth rendering --- Effects/FXB/SimpleDepthEffect.fxb | Bin 0 -> 624 bytes Effects/HLSL/SimpleDepthEffect.fx | 36 +++++++++++++ Effects/SimpleDepthEffect.cs | 68 +++++++++++++++++++++++++ Renderer.cs | 82 +++++++++++++++++++++++++++--- Resources.cs | 13 +++++ 5 files changed, 191 insertions(+), 8 deletions(-) create mode 100644 Effects/FXB/SimpleDepthEffect.fxb create mode 100644 Effects/HLSL/SimpleDepthEffect.fx create mode 100644 Effects/SimpleDepthEffect.cs diff --git a/Effects/FXB/SimpleDepthEffect.fxb b/Effects/FXB/SimpleDepthEffect.fxb new file mode 100644 index 0000000000000000000000000000000000000000..e2baafe615d1a37379d63f36ed61dd4b2d97395b GIT binary patch literal 624 zcmZSN{QqwS0|YPwDJCFJfN;Si3y=h1N`VN_0N?zS)SR%))bfC${H)aElFa-(21cN` z01%_w0TKrR9v}|R%q_@CbxAEK$pEVd$${Jn!XSMLKnw#jpbU_j2}tZoP<9QFu0Rrp zG8z5@0gN31(#-tt|G)o649+2rPBK95Y>*^S3?v2O1A)R6AYlN+5O**!xB%H9K#a|w z%nZyxx3YlU4^%=iC@YRPjyGWN%}g%JFV0UZQP2p|RPgW#_E8AVNK8pBQgF`CEy&CP zG7Js$3>j7jGBB{RFz_#6U}&&Mwp models, IEnumerable pointLights) - { - var view = camera.View; - var projection = camera.Projection; + private GraphicsDevice GraphicsDevice { get; } + private int RenderDimensionsX { get; } + private int RenderDimensionsY { get; } + private RenderTarget2D DepthRenderTarget { get; } + private SimpleDepthEffect SimpleDepthEffect { get; } + + public Renderer(GraphicsDevice graphicsDevice, int renderDimensionsX, int renderDimensionsY) + { + GraphicsDevice = graphicsDevice; + RenderDimensionsX = renderDimensionsX; + RenderDimensionsY = renderDimensionsY; + + DepthRenderTarget = new RenderTarget2D( + GraphicsDevice, + renderDimensionsX, + renderDimensionsY, + false, + SurfaceFormat.Color, + DepthFormat.Depth24 + ); + + SimpleDepthEffect = new SimpleDepthEffect(GraphicsDevice); + } + + public void Render(Camera camera, IEnumerable models, IEnumerable pointLights) + { + Render(camera.View, camera.Projection, models, pointLights); + } + + // for shadow mapping + public void DepthRender(Matrix view, Matrix projection, IEnumerable<(Model, Matrix)> modelTransforms, IEnumerable pointLights) + { + GraphicsDevice.SetRenderTarget(DepthRenderTarget); + GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 1, 0); + + SimpleDepthEffect.View = view; + SimpleDepthEffect.Projection = projection; + + foreach (var (model, transform) in modelTransforms) + { + foreach (var modelMesh in model.Meshes) + { + foreach (var meshPart in modelMesh.MeshParts) + { + GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); + GraphicsDevice.Indices = meshPart.IndexBuffer; + + SimpleDepthEffect.Model = transform; + + foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes) + { + pass.Apply(); + + GraphicsDevice.DrawIndexedPrimitives( + PrimitiveType.TriangleList, + 0, + 0, + meshPart.VertexBuffer.VertexCount, + 0, + meshPart.Triangles.Length + ); + } + } + } + } + } + + private void Render(Matrix view, Matrix projection, IEnumerable models, IEnumerable pointLights) + { foreach (var model in models) { foreach (var modelMesh in model.Meshes) { foreach (var meshPart in modelMesh.MeshParts) { - graphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); - graphicsDevice.Indices = meshPart.IndexBuffer; + GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); + GraphicsDevice.Indices = meshPart.IndexBuffer; if (meshPart.Effect is TransformEffect transformEffect) { @@ -40,7 +106,7 @@ namespace Kav { pass.Apply(); - graphicsDevice.DrawIndexedPrimitives( + GraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, diff --git a/Resources.cs b/Resources.cs index e20f87b..4964b4a 100644 --- a/Resources.cs +++ b/Resources.cs @@ -16,7 +16,20 @@ namespace Kav } } + public static byte[] SimpleDepthEffect + { + get + { + if (simpleDepthEffect == null) + { + simpleDepthEffect = GetResource("SimpleDepthEffecT"); + } + return simpleDepthEffect; + } + } + private static byte[] pbrEffect; + private static byte[] simpleDepthEffect; private static byte[] GetResource(string name) {