Kav/Scene.cs

63 lines
2.3 KiB
C#

using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
public static class Renderer
{
public static void Render(GraphicsDevice graphicsDevice, SceneData sceneData)
{
Render(graphicsDevice, sceneData.Camera, sceneData.Models, sceneData.PointLights);
}
public static void Render(GraphicsDevice graphicsDevice, Camera camera, IEnumerable<Model> models, IEnumerable<PointLight> pointLights)
{
var view = camera.View;
var projection = camera.Projection;
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;
if (meshPart.Effect is TransformEffect transformEffect)
{
transformEffect.World = model.Transform;
transformEffect.View = view;
transformEffect.Projection = projection;
}
if (meshPart.Effect is PointLightEffect pointLightEffect)
{
int i = 0;
foreach (var pointLight in pointLights)
{
pointLightEffect.PointLights[i] = pointLight;
i++;
}
}
foreach (var pass in meshPart.Effect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
meshPart.VertexBuffer.VertexCount,
0,
meshPart.Triangles.Length
);
}
}
}
}
}
}
}