PageRenderTime 52ms CodeModel.GetById 45ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/Rendering/Basics/Drawing/Box.cs

#
C# | 127 lines | 91 code | 8 blank | 28 comment | 1 complexity | fc482d3979b49e155382fad1c6b6dd9a MD5 | raw file
  1using Delta.Utilities.Datatypes;
  2
  3namespace Delta.Rendering.Basics.Drawing
  4{
  5	/// <summary>
  6	/// Static box class to draw 3d boxes either with lines or as solid boxes.
  7	/// </summary>
  8	public static class Box
  9	{
 10		#region DrawOutline (Static)
 11		/// <summary>
 12		/// Draws a 3-dimensional box from start to end using "LineColor" as
 13		/// ambient color. Mostly used for BoundingBoxes.
 14		//// DrawOutline(minimum, maximum, color)
 15		/// </summary>
 16		/// <param name="minimum">Minimum</param>
 17		/// <param name="maximum">Maximum</param>
 18		/// <param name="color">Line color</param>
 19		public static void DrawOutline(Vector minimum, Vector maximum, Color color)
 20		{
 21
 22			Vector difference = maximum - minimum;
 23			Vector minPlusDifference = minimum + difference;
 24			Vector minPlusDiffYZ =
 25				minimum + new Vector(0f, difference.Y, difference.Z);
 26			Vector minPlusDiffX = minimum + new Vector(difference.X, 0f, 0f);
 27			Vector minPlusDiffZ = minimum + new Vector(0f, 0f, difference.Z);
 28			Vector minPlusDiffY = minimum + new Vector(0f, difference.Y, 0f);
 29			Vector minPlusDiffXY =
 30				minimum + new Vector(difference.X, difference.Y, 0f);
 31			Vector minPlusDiffXZ =
 32				minimum + new Vector(difference.X, 0f, difference.Z);
 33
 34			DrawManager drawInstance = DrawManager.Instance;
 35			drawInstance.Draw3DLine(ref minimum, ref minPlusDiffZ, ref color);
 36			drawInstance.Draw3DLine(ref minPlusDiffX, ref minPlusDiffXZ, ref color);
 37			drawInstance.Draw3DLine(ref minPlusDiffXY, ref minPlusDifference,
 38				ref color);
 39			drawInstance.Draw3DLine(ref minPlusDiffY, ref minPlusDiffYZ, ref color);
 40			drawInstance.Draw3DLine(ref minPlusDiffZ, ref minPlusDiffYZ, ref color);
 41			drawInstance.Draw3DLine(ref minPlusDiffZ, ref minPlusDiffXZ, ref color);
 42			drawInstance.Draw3DLine(ref minPlusDiffXZ, ref minPlusDifference,
 43				ref color);
 44			drawInstance.Draw3DLine(ref minPlusDiffYZ, ref minPlusDifference,
 45				ref color);
 46			drawInstance.Draw3DLine(ref minimum, ref minPlusDiffY, ref color);
 47			drawInstance.Draw3DLine(ref minimum, ref minPlusDiffX, ref color);
 48			drawInstance.Draw3DLine(ref minPlusDiffX, ref minPlusDiffXY, ref color);
 49			drawInstance.Draw3DLine(ref minPlusDiffY, ref minPlusDiffXY, ref color);
 50		}
 51
 52		/// <summary>
 53		/// Draws a 3-dimensional box from start to end using "LineColor" as
 54		/// ambient color.
 55		/// </summary>
 56		/// <param name="minimum">Minimum</param>
 57		/// <param name="maximum">Maximum</param>
 58		/// <param name="color">Line color</param>
 59		/// <param name="rotation">Rotation as raw, pitch, roll</param>
 60		public static void DrawOutline(Vector minimum, Vector maximum, Color color,
 61			Vector rotation)
 62		{
 63
 64			Vector difference = (maximum - minimum);
 65			Vector minusHalfDifference = -difference * 0.5f;
 66			// Bunch of vectors for all the lines, see above for details
 67			Vector[] vectors = new[]
 68			{
 69				minusHalfDifference + Vector.Zero,
 70				minusHalfDifference + new Vector(0f, 0f, difference.Z),
 71				minusHalfDifference + new Vector(difference.X, 0f, 0f),
 72				minusHalfDifference + new Vector(difference.X, 0f, difference.Z),
 73				minusHalfDifference + new Vector(difference.X, difference.Y, 0f),
 74				minusHalfDifference + new Vector(difference.X, difference.Y, difference.Z),
 75				minusHalfDifference + new Vector(0f, difference.Y, 0f),
 76				minusHalfDifference + new Vector(0f, difference.Y, difference.Z),
 77				minusHalfDifference + new Vector(0f, 0f, difference.Z),
 78				minusHalfDifference + new Vector(0f, difference.Y, difference.Z),
 79				minusHalfDifference + new Vector(0f, 0f, difference.Z),
 80				minusHalfDifference + new Vector(difference.X, 0f, difference.Z),
 81				minusHalfDifference + new Vector(difference.X, 0f, difference.Z),
 82				minusHalfDifference + new Vector(difference.X, difference.Y, difference.Z),
 83				minusHalfDifference + new Vector(0f, difference.Y, difference.Z),
 84				minusHalfDifference + new Vector(difference.X, difference.Y, difference.Z),
 85				minusHalfDifference + new Vector(0f, 0f, 0f),
 86				minusHalfDifference + new Vector(0f, difference.Y, 0f),
 87				minusHalfDifference + new Vector(0f, 0f, 0f),
 88				minusHalfDifference + new Vector(difference.X, 0f, 0f),
 89				minusHalfDifference + new Vector(difference.X, 0f, 0f),
 90				minusHalfDifference + new Vector(difference.X, difference.Y, 0f),
 91				minusHalfDifference + new Vector(0f, difference.Y, 0f),
 92				minusHalfDifference + new Vector(difference.X, difference.Y, 0f)
 93			};
 94
 95			// Prepare the "raw" rotation
 96			Quaternion rotationQuaternion =
 97				Quaternion.CreateFromYawPitchRoll(rotation.X, rotation.Y, rotation.Z);
 98			Matrix transformMatrix = Matrix.FromQuaternion(rotationQuaternion);
 99
100			// And rotate and draw each vertex point as line pairs
101			DrawManager drawInstance = DrawManager.Instance;
102			for (int i = 0; i < vectors.Length / 2; i++)
103			{
104				drawInstance.Draw3DLine(
105					Vector.TransformNormal(vectors[i * 2 + 0], transformMatrix) +
106					minimum - minusHalfDifference,
107					Vector.TransformNormal(vectors[i * 2 + 1], transformMatrix) +
108					minimum - minusHalfDifference, color);
109			}
110		}
111		#endregion
112
113		#region DrawFilled (Static)
114		/// <summary>
115		/// Draw a filled box in 3d space.
116		/// </summary>
117		/// <param name="minimum">The minimum corner position.</param>
118		/// <param name="maximum">The maximum corner position.</param>
119		/// <param name="color">The fill color of the box.</param>
120		public static void DrawFilled(Vector minimum, Vector maximum, Color color)
121		{
122			Vector size = maximum - minimum;
123			DrawManager.Instance.DrawBox(ref minimum, ref size, ref color);
124		}
125		#endregion
126	}
127}