PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Rendering/Basics/Drawing/Line.cs

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