PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 1ms

/Rendering/Basics/Drawing/Line.cs

#
C# | 117 lines | 73 code | 11 blank | 33 comment | 3 complexity | b3a756500ba4520436e086a2f96b2fe7 MD5 | raw file
  1using System;
  2using Delta.Rendering.Cameras;
  3using Delta.Utilities.Datatypes;
  4
  5namespace Delta.Rendering.Basics.Drawing
  6{
  7	/// <summary>
  8	/// Draw 2D or 3D lines with help of this class. Rarely used. If you want
  9	/// to render more complex shapes use one of the other classes here or
 10	/// implement one yourself. This has the advantage of optimizing it better.
 11	/// </summary>
 12	public static class Line
 13	{
 14		#region Draw (Static)
 15		/// <summary>
 16		/// Draw line in 2D
 17		/// </summary>
 18		/// <param name="startPosition">Start position in quadratic space.</param>
 19		/// <param name="endPosition">End position in quadratic space.</param>
 20		/// <param name="lineColor">Line color</param>
 21		public static void Draw(Point startPosition, Point endPosition,
 22			Color color)
 23		{
 24			#region Culling check
 25			// Skip if the line is certainly outside of the quadratic space.
 26			// Checking the screen area would work too, but is a little slower and
 27			// won't exclude much more anyway.
 28			float smallestX = startPosition.X < endPosition.X
 29			                  	? startPosition.X
 30			                  	: endPosition.X;
 31			float smallestY = startPosition.Y < endPosition.Y
 32			                  	? startPosition.Y
 33			                  	: endPosition.Y;
 34			float distanceX = Math.Abs(startPosition.X - endPosition.X);
 35			float distanceY = Math.Abs(startPosition.Y - endPosition.Y);
 36			if (smallestX >= 1.0f ||
 37			    smallestY >= 1.0f ||
 38			    smallestX < -distanceX ||
 39			    smallestY < -distanceY)
 40			{
 41				// Skip this line rendering, not visible this frame.
 42				WasLastLineCulled = true;
 43				return;
 44			}
 45
 46			WasLastLineCulled = false;
 47			#endregion
 48
 49			DrawManager.Instance.Draw2DLine(ref startPosition, ref endPosition,
 50				ref color);
 51		}
 52		#endregion
 53
 54		#region DrawCross (Static)
 55		/// <summary>
 56		/// Draws a cross in 2D
 57		/// </summary>
 58		/// <param name="position">Position</param>
 59		/// <param name="radius">Radius</param>
 60		/// <param name="color">Color</param>
 61		public static void DrawCross(Point position, float radius, Color color)
 62		{
 63			Draw(new Point(position.X - radius, position.Y),
 64				new Point(position.X + radius, position.Y),
 65				color);
 66
 67			Draw(new Point(position.X, position.Y - radius),
 68				new Point(position.X, position.Y + radius),
 69				color);
 70		}
 71		#endregion
 72
 73		#region Draw (Static)
 74		/// <summary>
 75		/// Draw line in 3D
 76		/// </summary>
 77		/// <param name="startPosition">Start position</param>
 78		/// <param name="endPosition">End position</param>
 79		/// <param name="color">Line color</param>
 80		public static void Draw(Vector startPosition, Vector endPosition,
 81			Color color)
 82		{
 83			#region Culling check
 84			Plane[] frustum = BaseCamera.Current.ViewFrustum;
 85			// Check all the frustum planes.
 86			for (byte planeId = 0; planeId < 6; planeId++)
 87			{
 88				// is the positiveVertex outside?
 89				if (frustum[planeId].DotCoordinate(startPosition) < -0.001f &&
 90				    frustum[planeId].DotCoordinate(endPosition) < -0.001f)
 91				{
 92					WasLastLineCulled = true;
 93					return;
 94				}
 95			}
 96
 97			WasLastLineCulled = false;
 98			#endregion
 99
100			DrawManager.Instance.Draw3DLine(ref startPosition, ref endPosition,
101				ref color);
102		}
103		#endregion
104
105		#region Internal
106
107		#region WasLastLineCulled (Internal)
108		/// <summary>
109		/// Helper boolean for the Culling Draw Test which counts the
110		/// number of drawn lines.
111		/// </summary>
112		internal static bool WasLastLineCulled;
113		#endregion
114
115		#endregion
116	}
117}