From f0f09115f5dfbe34179bab33f60d07230aed9dd7 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 5 Aug 2020 12:15:22 -0700 Subject: [PATCH] basic rendering structure --- Camera.cs | 20 ++++++++--- EffectInterfaces/PointLightEffect.cs | 2 +- Effects/PBREffect.cs | 51 ++++++++++++++-------------- Geometry/Model.cs | 17 ++++++++-- Lights/PointLight.cs | 4 +-- Renderer.cs | 1 - 6 files changed, 59 insertions(+), 36 deletions(-) diff --git a/Camera.cs b/Camera.cs index 292f2c9..64a82d2 100644 --- a/Camera.cs +++ b/Camera.cs @@ -6,15 +6,16 @@ namespace Kav { public Matrix Transform { get; set; } public Matrix Projection { get; set; } - public Matrix View { + public Matrix View + { get { return Matrix.CreateLookAt(Transform.Translation, Transform.Forward, Transform.Up); } } - public float FOV { get; set; } = 75f; - public float AspectRatio { get; set; } = 1920 / 1080; + public float FOV { get; set; } = MathHelper.PiOver4; + public float AspectRatio { get; set; } = 1920f / 1080f; public float NearPlane { get; set; } = 0.1f; public float FarPlane { get; set; } = 200f; @@ -24,7 +25,18 @@ namespace Kav Projection = Matrix.CreatePerspectiveFieldOfView( FOV, AspectRatio, - NearPlane, + NearPlane, + FarPlane + ); + } + + public Camera(Matrix transform) + { + Transform = transform; + Projection = Matrix.CreatePerspectiveFieldOfView( + FOV, + AspectRatio, + NearPlane, FarPlane ); } diff --git a/EffectInterfaces/PointLightEffect.cs b/EffectInterfaces/PointLightEffect.cs index 7cc16f1..bca763a 100644 --- a/EffectInterfaces/PointLightEffect.cs +++ b/EffectInterfaces/PointLightEffect.cs @@ -2,6 +2,6 @@ namespace Kav { public interface PointLightEffect { - PointLight[] PointLights { get; } // TODO: should be a collection class? + PointLightCollection PointLights { get; } // TODO: should be a collection class? } } diff --git a/Effects/PBREffect.cs b/Effects/PBREffect.cs index fb321d8..daf85b1 100644 --- a/Effects/PBREffect.cs +++ b/Effects/PBREffect.cs @@ -3,19 +3,7 @@ using Microsoft.Xna.Framework.Graphics; namespace Kav { - public struct PBRLight - { - public Vector3 position; - public Vector3 color; - - public PBRLight(Vector3 position, Vector3 colour) - { - this.position = position; - this.color = colour; - } - } - - public class PBRLightCollection + public class PointLightCollection { private readonly Vector3[] positions = new Vector3[4]; private readonly Vector3[] colors = new Vector3[4]; @@ -23,26 +11,37 @@ namespace Kav readonly EffectParameter lightPositionsParam; readonly EffectParameter lightColorsParam; - public PBRLightCollection(EffectParameter lightPositionsParam, EffectParameter lightColorsParam) + public PointLightCollection(EffectParameter lightPositionsParam, EffectParameter lightColorsParam) { this.lightPositionsParam = lightPositionsParam; this.lightColorsParam = lightColorsParam; } - public PBRLight this[int i] + public PointLight this[int i] { - get { return new PBRLight(positions[i], colors[i]); } + get + { + return new PointLight( + positions[i], + new Color( + colors[i].X, + colors[i].Y, + colors[i].Z, + 1f + ) + ); + } set { - positions[i] = value.position; - colors[i] = value.color; + positions[i] = value.Position; + colors[i] = value.Color.ToVector3() * value.Intensity; lightPositionsParam.SetValue(positions); lightColorsParam.SetValue(colors); } } } - public class PBREffect : Effect + public class PBREffect : Effect, TransformEffect, PointLightEffect { EffectParameter worldParam; EffectParameter worldViewProjectionParam; @@ -69,7 +68,7 @@ namespace Kav Matrix world = Matrix.Identity; Matrix view = Matrix.Identity; Matrix projection = Matrix.Identity; - PBRLightCollection pbrLightCollection; + PointLightCollection pointLightCollection; Vector3 albedo; float metallic; @@ -114,10 +113,10 @@ namespace Kav } } - public PBRLightCollection Lights + public PointLightCollection PointLights { - get { return pbrLightCollection; } - internal set { pbrLightCollection = value; } + get { return pointLightCollection; } + internal set { pointLightCollection = value; } } public Vector3 Albedo @@ -227,7 +226,7 @@ namespace Kav { CacheEffectParameters(); - pbrLightCollection = new PBRLightCollection( + pointLightCollection = new PointLightCollection( Parameters["LightPositions"], Parameters["LightColors"] ); @@ -241,14 +240,14 @@ namespace Kav View = cloneSource.View; Projection = cloneSource.Projection; - Lights = new PBRLightCollection( + PointLights = new PointLightCollection( Parameters["LightPositions"], Parameters["LightColors"] ); for (int i = 0; i < 4; i++) { - Lights[i] = cloneSource.Lights[i]; + PointLights[i] = cloneSource.PointLights[i]; } AlbedoTexture = cloneSource.AlbedoTexture; diff --git a/Geometry/Model.cs b/Geometry/Model.cs index 9238586..049532f 100644 --- a/Geometry/Model.cs +++ b/Geometry/Model.cs @@ -5,11 +5,24 @@ namespace Kav public class Model { public Mesh[] Meshes { get; } - public Matrix Transform { get; set; } // TODO: this should probably be a wrapper class - public Model(Mesh[] meshes, Matrix transform) + public Model(Mesh[] meshes) { Meshes = meshes; } + + public void ApplyTransform(Matrix transform) + { + foreach (var mesh in Meshes) + { + foreach (var meshPart in mesh.MeshParts) + { + if (meshPart.Effect is TransformEffect transformEffect) + { + transformEffect.World = transform; + } + } + } + } } } diff --git a/Lights/PointLight.cs b/Lights/PointLight.cs index ea89278..657f7ea 100644 --- a/Lights/PointLight.cs +++ b/Lights/PointLight.cs @@ -5,10 +5,10 @@ namespace Kav public class PointLight { public Vector3 Position { get; set; } - public Vector4 Color { get; set; } + public Color Color { get; set; } public float Intensity { get; set; } - public PointLight(Vector3 position, Vector4 color, float intensity = 1f) + public PointLight(Vector3 position, Color color, float intensity = 1f) { Position = position; Color = color; diff --git a/Renderer.cs b/Renderer.cs index 303a582..6bc9705 100644 --- a/Renderer.cs +++ b/Renderer.cs @@ -26,7 +26,6 @@ namespace Kav if (meshPart.Effect is TransformEffect transformEffect) { - transformEffect.World = model.Transform; transformEffect.View = view; transformEffect.Projection = projection; }