From d5d0a38ff111dc92885bb9b1bb953d35f2b2dc53 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 9 Dec 2020 17:10:01 -0800 Subject: [PATCH] turn point lights into classes and give them a shadow map --- Effects/DeferredPBREffect.cs | 26 --------------------- Effects/PBREffect.cs | 39 ------------------------------- Effects/PointLightCollection.cs | 14 ----------- Lights/PointLight.cs | 41 +++++++++++++++++++++++++++++---- Renderer.cs | 1 - 5 files changed, 36 insertions(+), 85 deletions(-) diff --git a/Effects/DeferredPBREffect.cs b/Effects/DeferredPBREffect.cs index 181d1ec..a4f4306 100644 --- a/Effects/DeferredPBREffect.cs +++ b/Effects/DeferredPBREffect.cs @@ -74,32 +74,6 @@ namespace Kav ); } - protected DeferredPBREffect(DeferredPBREffect cloneSource) : base(cloneSource) - { - GPosition = cloneSource.GPosition; - GAlbedo = cloneSource.GAlbedo; - GNormal = cloneSource.GNormal; - GMetallicRoughness = cloneSource.GMetallicRoughness; - - EyePosition = cloneSource.EyePosition; - - PointLights = new PointLightCollection( - Parameters["LightPositions"], - Parameters["PositionLightColors"], - MaxPointLights - ); - - for (int i = 0; i < MaxPointLights; i++) - { - PointLights[i] = cloneSource.PointLights[i]; - } - } - - public override Effect Clone() - { - return new DeferredPBREffect(this); - } - protected override void OnApply() { gPositionParam.SetValue(GPosition); diff --git a/Effects/PBREffect.cs b/Effects/PBREffect.cs index 4b80ecb..0f6068f 100644 --- a/Effects/PBREffect.cs +++ b/Effects/PBREffect.cs @@ -197,45 +197,6 @@ namespace Kav ); } - protected PBREffect(PBREffect cloneSource) : base(cloneSource) - { - CacheEffectParameters(); - - World = cloneSource.World; - View = cloneSource.View; - Projection = cloneSource.Projection; - - PointLights = new PointLightCollection( - Parameters["LightPositions"], - Parameters["PositionLightColors"], - MaxPointLights - ); - - for (int i = 0; i < MaxPointLights; i++) - { - PointLights[i] = cloneSource.PointLights[i]; - } - - AlbedoTexture = cloneSource.AlbedoTexture; - NormalTexture = cloneSource.NormalTexture; - EmissionTexture = cloneSource.EmissionTexture; - OcclusionTexture = cloneSource.OcclusionTexture; - MetallicRoughnessTexture = cloneSource.MetallicRoughnessTexture; - EnvDiffuseTexture = cloneSource.EnvDiffuseTexture; - BRDFLutTexture = cloneSource.BRDFLutTexture; - EnvSpecularTexture = cloneSource.EnvSpecularTexture; - - Albedo = cloneSource.Albedo; - Metallic = cloneSource.Metallic; - Roughness = cloneSource.Roughness; - AO = cloneSource.AO; - } - - public override Effect Clone() - { - return new PBREffect(this); - } - protected override void OnApply() { if ((dirtyFlags & EffectDirtyFlags.World) != 0) diff --git a/Effects/PointLightCollection.cs b/Effects/PointLightCollection.cs index 3a53f1c..0ba2f48 100644 --- a/Effects/PointLightCollection.cs +++ b/Effects/PointLightCollection.cs @@ -23,20 +23,6 @@ namespace Kav public PointLight this[int i] { - get - { - var color = colors[i] / intensities[i]; - return new PointLight( - positions[i], - new Color( - color.X, - color.Y, - color.Z, - 1f - ), - intensities[i] - ); - } set { positions[i] = value.Position; diff --git a/Lights/PointLight.cs b/Lights/PointLight.cs index eefe100..647621a 100644 --- a/Lights/PointLight.cs +++ b/Lights/PointLight.cs @@ -1,24 +1,55 @@ using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; namespace Kav { - public struct PointLight + public sealed class PointLight : IDisposable { - public static double ATTENUATION_EPSILON = 0.1; - public Vector3 Position { get; } public Color Color { get; } public float Radius { get; } public BoundingSphere BoundingSphere { get; } - public PointLight(Vector3 position, Color color, float radius) - { + public RenderTargetCube ShadowMap { get; } + + public PointLight( + GraphicsDevice graphicsDevice, + Vector3 position, + Color color, + float radius, + int shadowMapSize + ) { Position = position; Color = color; Radius = radius; BoundingSphere = new BoundingSphere(position, Radius); + + ShadowMap = new RenderTargetCube( + graphicsDevice, + shadowMapSize, + false, + SurfaceFormat.Single, + DepthFormat.Depth24, + 0, + RenderTargetUsage.PreserveContents + ); + + var currentRTs = graphicsDevice.GetRenderTargets(); + foreach (CubeMapFace face in Enum.GetValues(typeof(CubeMapFace))) + { + graphicsDevice.SetRenderTarget(ShadowMap, face); + graphicsDevice.Clear(Color.White); + } + + graphicsDevice.SetRenderTargets(currentRTs); + } + + public void Dispose() + { + ShadowMap.Dispose(); } } } diff --git a/Renderer.cs b/Renderer.cs index ce82881..0bb5f97 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -755,7 +755,6 @@ namespace Kav public void RenderPointShadowMapIndexed( RenderTargetCube pointShadowCubeMap, - PerspectiveCamera camera, IEnumerable<(T, Matrix)> modelTransforms, PointLight pointLight ) where T : ICullable, IIndexDrawable {