2020-08-04 09:32:02 +00:00
|
|
|
|
using System.Collections.Generic;
|
2020-08-07 00:58:50 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
2020-08-04 09:32:02 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace Kav
|
|
|
|
|
{
|
2020-08-07 00:58:50 +00:00
|
|
|
|
public class Renderer
|
2020-08-04 09:32:02 +00:00
|
|
|
|
{
|
2020-08-07 00:58:50 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
|
public void Render(
|
|
|
|
|
Camera camera,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
|
|
|
|
IEnumerable<PointLight> pointLights,
|
|
|
|
|
IEnumerable<DirectionalLight> directionalLights
|
|
|
|
|
) {
|
|
|
|
|
Render(camera.View, camera.Projection, modelTransforms, pointLights, directionalLights);
|
2020-08-07 00:58:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for shadow mapping
|
2020-08-07 08:12:46 +00:00
|
|
|
|
public void DepthRender(IEnumerable<(Model, Matrix)> modelTransforms, DirectionalLight directionalLight)
|
2020-08-04 09:32:02 +00:00
|
|
|
|
{
|
2020-08-07 00:58:50 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(DepthRenderTarget);
|
|
|
|
|
GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 1, 0);
|
|
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
|
SimpleDepthEffect.View = directionalLight.View;
|
|
|
|
|
SimpleDepthEffect.Projection = directionalLight.Projection;
|
2020-08-07 00:58:50 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2020-08-04 09:32:02 +00:00
|
|
|
|
|
2020-08-07 00:58:50 +00:00
|
|
|
|
foreach (var pass in SimpleDepthEffect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
|
|
|
|
GraphicsDevice.DrawIndexedPrimitives(
|
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.Triangles.Length
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
|
private void Render(
|
|
|
|
|
Matrix view,
|
|
|
|
|
Matrix projection,
|
|
|
|
|
IEnumerable<(Model, Matrix)> modelTransforms,
|
|
|
|
|
IEnumerable<PointLight> pointLights,
|
|
|
|
|
IEnumerable<DirectionalLight> directionalLights
|
|
|
|
|
) {
|
|
|
|
|
foreach (var (model, transform) in modelTransforms)
|
2020-08-04 09:32:02 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var modelMesh in model.Meshes)
|
|
|
|
|
{
|
|
|
|
|
foreach (var meshPart in modelMesh.MeshParts)
|
|
|
|
|
{
|
2020-08-07 00:58:50 +00:00
|
|
|
|
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
|
|
|
|
|
GraphicsDevice.Indices = meshPart.IndexBuffer;
|
2020-08-04 09:32:02 +00:00
|
|
|
|
|
|
|
|
|
if (meshPart.Effect is TransformEffect transformEffect)
|
|
|
|
|
{
|
2020-08-07 08:12:46 +00:00
|
|
|
|
transformEffect.World = transform;
|
2020-08-04 09:32:02 +00:00
|
|
|
|
transformEffect.View = view;
|
|
|
|
|
transformEffect.Projection = projection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (meshPart.Effect is PointLightEffect pointLightEffect)
|
|
|
|
|
{
|
2020-08-05 03:50:44 +00:00
|
|
|
|
int i = 0;
|
|
|
|
|
foreach (var pointLight in pointLights)
|
2020-08-04 09:32:02 +00:00
|
|
|
|
{
|
2020-08-06 08:12:15 +00:00
|
|
|
|
if (i > pointLightEffect.MaxPointLights) { break; }
|
2020-08-05 03:50:44 +00:00
|
|
|
|
pointLightEffect.PointLights[i] = pointLight;
|
|
|
|
|
i++;
|
2020-08-04 09:32:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 08:12:46 +00:00
|
|
|
|
if (meshPart.Effect is DirectionalLightEffect directionalLightEffect)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
foreach (var directionalLight in directionalLights)
|
|
|
|
|
{
|
|
|
|
|
if (i > directionalLightEffect.MaxDirectionalLights) { break; }
|
|
|
|
|
directionalLightEffect.DirectionalLights[i] = directionalLight;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 09:32:02 +00:00
|
|
|
|
foreach (var pass in meshPart.Effect.CurrentTechnique.Passes)
|
|
|
|
|
{
|
|
|
|
|
pass.Apply();
|
|
|
|
|
|
2020-08-07 00:58:50 +00:00
|
|
|
|
GraphicsDevice.DrawIndexedPrimitives(
|
2020-08-04 09:32:02 +00:00
|
|
|
|
PrimitiveType.TriangleList,
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.VertexBuffer.VertexCount,
|
|
|
|
|
0,
|
|
|
|
|
meshPart.Triangles.Length
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|