Kav/Scene.cs

69 lines
2.3 KiB
C#

using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
namespace Kav
{
public class Scene
{
private PointLight[] pointLights = new PointLight[4];
private List<Model> models = new List<Model>();
public void AddPointLight(int index, PointLight light)
{
pointLights[index] = light;
}
public void AddModel(Model model)
{
models.Add(model);
}
public void Render(GraphicsDevice graphicsDevice, Camera camera)
{
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)
{
for (int i = 0; i < pointLights.Length; i++)
{
pointLightEffect.PointLights[i] = pointLights[i];
}
}
foreach (var pass in meshPart.Effect.CurrentTechnique.Passes)
{
pass.Apply();
graphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
0,
0,
meshPart.VertexBuffer.VertexCount,
0,
meshPart.Triangles.Length
);
}
}
}
}
}
}
}