Rendering Bounding Frustums
From XNAWiki
This is a basic static class that allows easy rendering of BoundingFrustums.
/// <summary>
/// Provides a set of methods for rendering BoundingFrustums
/// </summary>
public static class BoundingFrustumRenderer
{
#region Fields
static VertexPositionColor[] verts = new VertexPositionColor[8];
static int[] indices = new int[]
{
0, 1,
1, 2,
2, 3,
3, 0,
0, 4,
1, 5,
2, 6,
3, 7,
4, 5,
5, 6,
6, 7,
7, 4,
};
static BasicEffect effect;
static VertexDeclaration vertDecl;
#endregion
/// <summary>
/// Renders the bounding frustum for debugging purposes.
/// </summary>
/// <param name="frustum">The frustum to render.</param>
/// <param name="graphicsDevice">The graphics device to use when rendering.</param>
/// <param name="view">The current view matrix.</param>
/// <param name="projection">The current projection matrix.</param>
/// <param name="color">The color to use drawing the lines of the frustum.</param>
public static void Render(
BoundingFrustum frustum,
GraphicsDevice graphicsDevice,
Matrix view,
Matrix projection,
Color color)
{
if (effect == null)
{
effect = new BasicEffect(graphicsDevice, null);
effect.VertexColorEnabled = true;
effect.LightingEnabled = false;
vertDecl = new VertexDeclaration(graphicsDevice, VertexPositionColor.VertexElements);
}
Vector3[] corners = frustum.GetCorners();
for (int i = 0; i < 8; i++)
{
verts[i].Position = corners[i];
verts[i].Color = color;
}
graphicsDevice.VertexDeclaration = vertDecl;
effect.View = view;
effect.Projection = projection;
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
graphicsDevice.DrawUserIndexedPrimitives(
PrimitiveType.LineList,
verts,
0,
8,
indices,
0,
indices.Length / 2);
pass.End();
}
effect.End();
}
}