Skip to content

Instantly share code, notes, and snippets.

@kylewlacy
Last active December 15, 2015 04:20
Show Gist options
  • Save kylewlacy/5201179 to your computer and use it in GitHub Desktop.
Save kylewlacy/5201179 to your computer and use it in GitHub Desktop.
A `Block` class for use with MonoGame/XNA. Provides the verticies and indicies for drawing a BoundingBox using `GraphicsDevice.DrawIndexedPrimitives()`
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Graphics;
public class Block {
public BoundingBox Box;
public Color Color;
public List<Vector3> Points {
get {
return new List<Vector3> {
new Vector3(Box.Min.X, Box.Min.Y, Box.Min.Z),
new Vector3(Box.Max.X, Box.Min.Y, Box.Min.Z),
new Vector3(Box.Min.X, Box.Max.Y, Box.Min.Z),
new Vector3(Box.Max.X, Box.Max.Y, Box.Min.Z),
new Vector3(Box.Min.X, Box.Min.Y, Box.Max.Z),
new Vector3(Box.Max.X, Box.Min.Y, Box.Max.Z),
new Vector3(Box.Min.X, Box.Max.Y, Box.Max.Z),
new Vector3(Box.Max.X, Box.Max.Y, Box.Max.Z)
};
}
}
public List<short> Indicies {
get {
return new List<short> {
0, 2, 1,
1, 2, 3,
0, 1, 4,
1, 5, 4,
0, 4, 2,
2, 4, 6,
1, 3, 5,
3, 7, 5,
2, 6, 3,
6, 7, 3,
4, 5, 6,
5, 7, 6
};
}
}
public List<VertexPositionColor> Verticies {
get { return Points.Select(p => new VertexPositionColor(p, Color)).ToList<VertexPositionColor>(); }
}
public Block(BoundingBox box, Color color) {
Box = box;
Color = color;
}
}
// You can draw a `Block` by doing something along the lines of:
// VertexBuffer vertexBuffer = new VertexBuffer(graphicsDevice, block.Verticies.GetType().GetGenericArguments()[0], block.Verticies.Count, BufferUsage.WriteOnly);
// IndexBuffer indexBuffer = new IndexBuffer(graphicsDevice, block.Indicies.GetType().GetGenericArguments()[0], block.Indicies.Count, BufferUsage.WriteOnly);
// graphicsDevice.Indicies = indexBuffer;
// graphicsDevice.SetVertexBuffer(vertexBuffer);
//
// graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, block.Verticies.Count, 0, block.Indicies.Count / 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment