basic rendering structure
parent
7857679666
commit
f0f09115f5
20
Camera.cs
20
Camera.cs
|
@ -6,15 +6,16 @@ namespace Kav
|
||||||
{
|
{
|
||||||
public Matrix Transform { get; set; }
|
public Matrix Transform { get; set; }
|
||||||
public Matrix Projection { get; set; }
|
public Matrix Projection { get; set; }
|
||||||
public Matrix View {
|
public Matrix View
|
||||||
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return Matrix.CreateLookAt(Transform.Translation, Transform.Forward, Transform.Up);
|
return Matrix.CreateLookAt(Transform.Translation, Transform.Forward, Transform.Up);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float FOV { get; set; } = 75f;
|
public float FOV { get; set; } = MathHelper.PiOver4;
|
||||||
public float AspectRatio { get; set; } = 1920 / 1080;
|
public float AspectRatio { get; set; } = 1920f / 1080f;
|
||||||
public float NearPlane { get; set; } = 0.1f;
|
public float NearPlane { get; set; } = 0.1f;
|
||||||
public float FarPlane { get; set; } = 200f;
|
public float FarPlane { get; set; } = 200f;
|
||||||
|
|
||||||
|
@ -24,7 +25,18 @@ namespace Kav
|
||||||
Projection = Matrix.CreatePerspectiveFieldOfView(
|
Projection = Matrix.CreatePerspectiveFieldOfView(
|
||||||
FOV,
|
FOV,
|
||||||
AspectRatio,
|
AspectRatio,
|
||||||
NearPlane,
|
NearPlane,
|
||||||
|
FarPlane
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Camera(Matrix transform)
|
||||||
|
{
|
||||||
|
Transform = transform;
|
||||||
|
Projection = Matrix.CreatePerspectiveFieldOfView(
|
||||||
|
FOV,
|
||||||
|
AspectRatio,
|
||||||
|
NearPlane,
|
||||||
FarPlane
|
FarPlane
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,6 @@ namespace Kav
|
||||||
{
|
{
|
||||||
public interface PointLightEffect
|
public interface PointLightEffect
|
||||||
{
|
{
|
||||||
PointLight[] PointLights { get; } // TODO: should be a collection class?
|
PointLightCollection PointLights { get; } // TODO: should be a collection class?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,19 +3,7 @@ using Microsoft.Xna.Framework.Graphics;
|
||||||
|
|
||||||
namespace Kav
|
namespace Kav
|
||||||
{
|
{
|
||||||
public struct PBRLight
|
public class PointLightCollection
|
||||||
{
|
|
||||||
public Vector3 position;
|
|
||||||
public Vector3 color;
|
|
||||||
|
|
||||||
public PBRLight(Vector3 position, Vector3 colour)
|
|
||||||
{
|
|
||||||
this.position = position;
|
|
||||||
this.color = colour;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class PBRLightCollection
|
|
||||||
{
|
{
|
||||||
private readonly Vector3[] positions = new Vector3[4];
|
private readonly Vector3[] positions = new Vector3[4];
|
||||||
private readonly Vector3[] colors = new Vector3[4];
|
private readonly Vector3[] colors = new Vector3[4];
|
||||||
|
@ -23,26 +11,37 @@ namespace Kav
|
||||||
readonly EffectParameter lightPositionsParam;
|
readonly EffectParameter lightPositionsParam;
|
||||||
readonly EffectParameter lightColorsParam;
|
readonly EffectParameter lightColorsParam;
|
||||||
|
|
||||||
public PBRLightCollection(EffectParameter lightPositionsParam, EffectParameter lightColorsParam)
|
public PointLightCollection(EffectParameter lightPositionsParam, EffectParameter lightColorsParam)
|
||||||
{
|
{
|
||||||
this.lightPositionsParam = lightPositionsParam;
|
this.lightPositionsParam = lightPositionsParam;
|
||||||
this.lightColorsParam = lightColorsParam;
|
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
|
set
|
||||||
{
|
{
|
||||||
positions[i] = value.position;
|
positions[i] = value.Position;
|
||||||
colors[i] = value.color;
|
colors[i] = value.Color.ToVector3() * value.Intensity;
|
||||||
lightPositionsParam.SetValue(positions);
|
lightPositionsParam.SetValue(positions);
|
||||||
lightColorsParam.SetValue(colors);
|
lightColorsParam.SetValue(colors);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PBREffect : Effect
|
public class PBREffect : Effect, TransformEffect, PointLightEffect
|
||||||
{
|
{
|
||||||
EffectParameter worldParam;
|
EffectParameter worldParam;
|
||||||
EffectParameter worldViewProjectionParam;
|
EffectParameter worldViewProjectionParam;
|
||||||
|
@ -69,7 +68,7 @@ namespace Kav
|
||||||
Matrix world = Matrix.Identity;
|
Matrix world = Matrix.Identity;
|
||||||
Matrix view = Matrix.Identity;
|
Matrix view = Matrix.Identity;
|
||||||
Matrix projection = Matrix.Identity;
|
Matrix projection = Matrix.Identity;
|
||||||
PBRLightCollection pbrLightCollection;
|
PointLightCollection pointLightCollection;
|
||||||
|
|
||||||
Vector3 albedo;
|
Vector3 albedo;
|
||||||
float metallic;
|
float metallic;
|
||||||
|
@ -114,10 +113,10 @@ namespace Kav
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PBRLightCollection Lights
|
public PointLightCollection PointLights
|
||||||
{
|
{
|
||||||
get { return pbrLightCollection; }
|
get { return pointLightCollection; }
|
||||||
internal set { pbrLightCollection = value; }
|
internal set { pointLightCollection = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector3 Albedo
|
public Vector3 Albedo
|
||||||
|
@ -227,7 +226,7 @@ namespace Kav
|
||||||
{
|
{
|
||||||
CacheEffectParameters();
|
CacheEffectParameters();
|
||||||
|
|
||||||
pbrLightCollection = new PBRLightCollection(
|
pointLightCollection = new PointLightCollection(
|
||||||
Parameters["LightPositions"],
|
Parameters["LightPositions"],
|
||||||
Parameters["LightColors"]
|
Parameters["LightColors"]
|
||||||
);
|
);
|
||||||
|
@ -241,14 +240,14 @@ namespace Kav
|
||||||
View = cloneSource.View;
|
View = cloneSource.View;
|
||||||
Projection = cloneSource.Projection;
|
Projection = cloneSource.Projection;
|
||||||
|
|
||||||
Lights = new PBRLightCollection(
|
PointLights = new PointLightCollection(
|
||||||
Parameters["LightPositions"],
|
Parameters["LightPositions"],
|
||||||
Parameters["LightColors"]
|
Parameters["LightColors"]
|
||||||
);
|
);
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
Lights[i] = cloneSource.Lights[i];
|
PointLights[i] = cloneSource.PointLights[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
AlbedoTexture = cloneSource.AlbedoTexture;
|
AlbedoTexture = cloneSource.AlbedoTexture;
|
||||||
|
|
|
@ -5,11 +5,24 @@ namespace Kav
|
||||||
public class Model
|
public class Model
|
||||||
{
|
{
|
||||||
public Mesh[] Meshes { get; }
|
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;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,10 @@ namespace Kav
|
||||||
public class PointLight
|
public class PointLight
|
||||||
{
|
{
|
||||||
public Vector3 Position { get; set; }
|
public Vector3 Position { get; set; }
|
||||||
public Vector4 Color { get; set; }
|
public Color Color { get; set; }
|
||||||
public float Intensity { 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;
|
Position = position;
|
||||||
Color = color;
|
Color = color;
|
||||||
|
|
|
@ -26,7 +26,6 @@ namespace Kav
|
||||||
|
|
||||||
if (meshPart.Effect is TransformEffect transformEffect)
|
if (meshPart.Effect is TransformEffect transformEffect)
|
||||||
{
|
{
|
||||||
transformEffect.World = model.Transform;
|
|
||||||
transformEffect.View = view;
|
transformEffect.View = view;
|
||||||
transformEffect.Projection = projection;
|
transformEffect.Projection = projection;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue