PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Rendering/BasicTests/DrawTests.cs

#
C# | 627 lines | 424 code | 60 blank | 143 comment | 15 complexity | e5029e144d889a3b4224ed662cb44639 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using Delta.ContentSystem.Rendering;
  3. using Delta.Engine;
  4. using Delta.Graphics.Basics;
  5. using Delta.InputSystem;
  6. using Delta.Rendering.Basics.Drawing;
  7. using Delta.Rendering.Basics.Fonts;
  8. using Delta.Rendering.Basics.Materials;
  9. using Delta.Rendering.Cameras;
  10. using Delta.Utilities.Datatypes;
  11. using Delta.Utilities.Graphics;
  12. using Delta.Utilities.Helpers;
  13. using NUnit.Framework;
  14. namespace Delta.Rendering.BasicTests
  15. {
  16. /// <summary>
  17. /// Basic rendering tests, will mostly just test the Ma manager!
  18. /// </summary>
  19. [Category("Visual")]
  20. public class DrawTests
  21. {
  22. #region DrawLine (Static)
  23. /// <summary>
  24. /// Very simple draw line unit test, which renders a single line from
  25. /// the top left to the bottom right at amazing speed (15k+ fps, without
  26. /// SwapBuffer its 300k+ fps, see OpenTKGraphics.Show).
  27. /// </summary>
  28. [Test]
  29. public static void DrawLine()
  30. {
  31. Application.Start(delegate
  32. {
  33. // Line from top left up to bottom right in quadratic space
  34. Line.Draw(new Point(0, 0), new Point(1, 1), Color.Red);
  35. });
  36. }
  37. #endregion
  38. #region DrawLineAndTexture (Static)
  39. /// <summary>
  40. /// Test rendering both a material with the default texture and a line.
  41. /// </summary>
  42. [Test]
  43. public static void DrawLineAndTexture()
  44. {
  45. Application.Start(delegate
  46. {
  47. Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  48. // Line from top left up to bottom right in quadratic space
  49. Line.Draw(new Point(0, 0), new Point(1, 1), Color.Red);
  50. });
  51. }
  52. #endregion
  53. #region AddLinesAndTexturesDynamically (Static)
  54. /// <summary>
  55. /// Basically the same test as DrawLineAndTexture, but allows you to add
  56. /// more stuff by clicking (left = new line, right = new material).
  57. /// </summary>
  58. [Test]
  59. public static void AddLinesAndTexturesDynamically()
  60. {
  61. List<Point> materialPositions = new List<Point>();
  62. materialPositions.Add(Point.Half);
  63. List<Point> lineEndPoints = new List<Point>();
  64. lineEndPoints.Add(Point.One);
  65. Application.Start(delegate
  66. {
  67. if (Input.Mouse.LeftButtonReleased)
  68. {
  69. lineEndPoints.Add(Input.Mouse.Position);
  70. }
  71. if (Input.Mouse.RightButtonReleased)
  72. {
  73. materialPositions.Add(Input.Mouse.Position);
  74. }
  75. foreach (Point pos in materialPositions)
  76. {
  77. Material2D.Default.Draw(Rectangle.FromCenter(pos, 0.01f));
  78. }
  79. foreach (Point pos in lineEndPoints)
  80. {
  81. Line.Draw(new Point(0, 0), pos, Color.Red);
  82. }
  83. });
  84. }
  85. #endregion
  86. #region DrawAlternatingLines (Static)
  87. /// <summary>
  88. /// DrawAlternatingLines
  89. /// </summary>
  90. [Test]
  91. public static void DrawAlternatingLines()
  92. {
  93. Application.Start(delegate
  94. {
  95. // We draw here just a cross over the whole screen
  96. // Horizontal line in the middle of the screen in quadratic space
  97. Line.Draw(new Point(0, 0.5f), new Point(1, 0.5f),
  98. Time.Seconds % 2 == 0
  99. ? Color.Red
  100. : Color.Green);
  101. // Vertical line in the middle of the screen in quadratic space
  102. Line.Draw(new Point(0.5f, 0), new Point(0.5f, 1),
  103. Time.Seconds % 2 == 0
  104. ? Color.Yellow
  105. : Color.Black);
  106. // Line from top left up to bottom right in quadratic space
  107. Line.Draw(new Point(0, 0), new Point(1, 1),
  108. Time.Seconds % 2 == 0
  109. ? Color.Red
  110. : Color.Green);
  111. });
  112. }
  113. #endregion
  114. #region DrawTransparentLines (Static)
  115. /// <summary>
  116. /// Draw transparent lines, this is to test if the alpha component of
  117. /// line drawing works with all graphic modules (was not working on ES20).
  118. /// </summary>
  119. [Test]
  120. public static void DrawTransparentLines()
  121. {
  122. Application.Start(delegate
  123. {
  124. // Solid red line at top
  125. Line.Draw(new Point(0.2f, 0.3f), new Point(0.8f, 0.3f),
  126. Color.Red);
  127. // 50% faded red line in middle
  128. Line.Draw(new Point(0.2f, 0.5f), new Point(0.8f, 0.5f),
  129. new Color(Color.Red, 0.5f));
  130. // And finally 25% faded red line at bottom
  131. Line.Draw(new Point(0.2f, 0.7f), new Point(0.8f, 0.7f),
  132. new Color(Color.Red, 0.25f));
  133. });
  134. }
  135. #endregion
  136. #region DrawLotsOfRotatedLines (Static)
  137. /// <summary>
  138. /// </summary>
  139. [Test]
  140. public static void DrawLotsOfRotatedLines()
  141. {
  142. // Draw lots of lines to test performance (1000 is currently the limit,
  143. // we only can store 2000 vertices in the VertexPool for Draw)!
  144. // Note: 200 lines (400 vertices) is the sweet spot on my netbook, it
  145. // can still be rendered at 300 fps (same as with less lines, 1 line has
  146. // 330 fps), but more lines like 400 will bring it down to 150fps.
  147. const int NumberOfLines = 2000; //50;//100;//1000;
  148. // Obviously we only need to draw 180 degrees of lines because we see
  149. // both sides, which will fill a full 360 degree circle.
  150. float rotationStep = 180.0f / NumberOfLines;
  151. Point center = Point.Half;
  152. Application.Start(delegate
  153. {
  154. for (int num = 0; num < NumberOfLines; num++)
  155. {
  156. Point rotatedPos = new Point(0, 0.4f).Rotate(num * rotationStep);
  157. Color color = num % 3 == 0
  158. ? Color.Red
  159. : num % 3 == 1
  160. ? Color.Orange
  161. : Color.Yellow;
  162. Line.Draw(center - rotatedPos, center + rotatedPos, color);
  163. }
  164. });
  165. }
  166. #endregion
  167. #region DrawDynamicLines (Static)
  168. /// <summary>
  169. /// Draw dynamic lines
  170. /// </summary>
  171. [Test]
  172. public static void DrawDynamicLines()
  173. {
  174. Application.Start(delegate
  175. {
  176. float sinValue = MathHelper.Sin(Time.CurrentExactTime * 15f);
  177. Line.Draw(new Point(0.5f + (sinValue * 0.5f), ScreenSpace.DrawArea.Top),
  178. new Point(0.5f, ScreenSpace.DrawArea.Bottom), Color.Red);
  179. });
  180. }
  181. #endregion
  182. #region DrawCircleSimple (Static)
  183. /// <summary>
  184. /// Draw circle simple
  185. /// </summary>
  186. [Test]
  187. public static void DrawCircleSimple()
  188. {
  189. Point circlePosition = new Point(0.5f, 0.5f);
  190. Application.Start(delegate
  191. {
  192. Circle.DrawOutline(circlePosition, 0.1f, Color.Red);
  193. });
  194. }
  195. #endregion
  196. #region DrawCircles (Static)
  197. /// <summary>
  198. /// Draw circles
  199. /// </summary>
  200. [Test]
  201. public static void DrawCircles()
  202. {
  203. Point circlePosition = new Point(0.5f, 0.5f);
  204. Application.Start(delegate
  205. {
  206. // Draw a few circles in the middle of the screen
  207. Circle.DrawOutline(circlePosition, 0.1f, Color.Red);
  208. Circle.DrawOutline(circlePosition, 0.25f, Color.Red);
  209. Circle.DrawOutline(circlePosition, 0.5f, Color.Red);
  210. });
  211. }
  212. #endregion
  213. #region DrawCircleAtMousePos (Static)
  214. /// <summary>
  215. /// Draw circle at mouse position
  216. /// </summary>
  217. [Test]
  218. public static void DrawCircleAtMousePos()
  219. {
  220. Application.Start(delegate
  221. {
  222. Material2D.Default.Draw(new Rectangle(0.2f, 0.2f, 0.6f, 0.6f));
  223. Circle.DrawOutline(Input.Mouse.Position, 0.05f, Color.Red);
  224. });
  225. }
  226. #endregion
  227. #region DrawFilledCircle (Static)
  228. /// <summary>
  229. /// DrawFilledCircle
  230. /// </summary>
  231. [Test]
  232. public static void DrawFilledCircle()
  233. {
  234. Application.Start(delegate
  235. {
  236. // Draw a filled circle in the middle of the screen
  237. Circle.DrawFilled(Point.Half, 0.25f, Color.Red);
  238. });
  239. }
  240. #endregion
  241. #region DrawCirclePerformance (Static)
  242. /// <summary>
  243. /// Draw circle performance test to make sure it is fast. Performance is
  244. /// pretty amazing, we can draw 1000 circles with reaching up to 3000 fps!
  245. /// </summary>
  246. [Test]
  247. public static void DrawCirclePerformance()
  248. {
  249. Application.Start(delegate
  250. {
  251. // Test a case with 10 different circles each being drawn 100 times,
  252. // which is quite a bit of circles (each can have up to 64 segments).
  253. // Each of these circles is drawn with different sizes and colors.
  254. for (int i = 0; i < 10; i++)
  255. {
  256. for (int x = 0; x < 10; x++)
  257. {
  258. for (int y = 0; y < 10; y++)
  259. {
  260. Circle.DrawOutline(
  261. new Point(0.05f + x / 10.0f, 0.05f + y / 10.0f),
  262. 0.025f + i * 0.008f,
  263. new Color(i / 10.0f, 1.0f - (i / 10.0f), x / 10.0f));
  264. }
  265. } // for
  266. } // for
  267. });
  268. }
  269. #endregion
  270. #region DrawRectangle (Static)
  271. /// <summary>
  272. /// Draw rectangle
  273. /// </summary>
  274. [Test]
  275. public static void DrawRectangle()
  276. {
  277. Application.Start(delegate
  278. {
  279. Rect.DrawOutline(new Rectangle(0.5f, 0.5f, 0.25f, 0.25f),
  280. Color.Red);
  281. Rect.DrawOutline(new Rectangle(0.5f, 0.5f, 0.25f, 0.25f), Color.Red,
  282. 45f);
  283. // Draw a rectangle located in the middle of the screen
  284. Rect.DrawOutline(new Rectangle(0.5f, 0.5f, 0.15f, 0.1f),
  285. Color.Red);
  286. // And show the whole screen area in green
  287. //This is 0, 800 for 800x600, but we need 0, 799: ScreenSpace.Area
  288. Rectangle pixelScreenRect = new Rectangle(0, 0,
  289. Application.Window.ViewportPixelWidth - 1,
  290. Application.Window.ViewportPixelHeight - 1);
  291. Rect.DrawOutline(ScreenSpace.ToQuadraticSpace(pixelScreenRect),
  292. Color.Green);
  293. });
  294. }
  295. #endregion
  296. #region DrawRectangleAutoRotated (Static)
  297. /// <summary>
  298. /// Draw rectangle auto rotated
  299. /// </summary>
  300. [Test]
  301. public static void DrawRectangleAutoRotated()
  302. {
  303. float rotation = 0.0f;
  304. Application.Start(delegate
  305. {
  306. rotation += Time.Delta * 10.0f;
  307. Rect.DrawOutline(Rectangle.FromCenter(0.5f, 0.5f, 0.5f, 0.25f),
  308. Color.Red, rotation);
  309. });
  310. }
  311. #endregion
  312. #region DrawFilledRectangle (Static)
  313. /// <summary>
  314. /// Draw filled rectangle
  315. /// </summary>
  316. [Test]
  317. public static void DrawFilledRectangle()
  318. {
  319. Application.Start(delegate
  320. {
  321. Rect.DrawFilled(new Rectangle(0.5f, 0.5f, 0.25f, 0.25f),
  322. Color.Red);
  323. Rect.DrawFilled(new Rectangle(0.25f, 0.35f, 0.2f, 0.2f),
  324. Color.Green, 45);
  325. });
  326. }
  327. #endregion
  328. #region DrawLines3D (Static)
  329. /// <summary>
  330. /// Draw lines 3d
  331. /// </summary>
  332. [Test]
  333. public static void DrawLines3D()
  334. {
  335. BaseCamera cam = new LookAtCamera(new Vector(4, -4, 4));
  336. Application.Start(delegate
  337. {
  338. // Draw a simple 3d axis for basic line testing.
  339. Line.Draw(Vector.Zero, Vector.UnitX, Color.Red);
  340. Line.Draw(Vector.Zero, Vector.UnitY, Color.Blue);
  341. Line.Draw(Vector.Zero, Vector.UnitZ, Color.Green);
  342. });
  343. }
  344. #endregion
  345. #region DrawBox3D (Static)
  346. /// <summary>
  347. /// Draw box 3D
  348. /// </summary>
  349. [Test]
  350. public static void DrawBox3D()
  351. {
  352. BaseCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  353. Vector halfSize = new Vector(3, 3, 3);
  354. Application.Start(delegate
  355. {
  356. Grid.Draw();
  357. // draw a simple cube from (-3, -3, -3) to (3, 3, 3)
  358. Box.DrawOutline(-halfSize, halfSize, Color.Red);
  359. });
  360. }
  361. #endregion
  362. #region GetRayFromScreenPoint (Static)
  363. /// <summary>
  364. /// This tests ScreenSpace.GetRayFromScreenPoint. It is here because in
  365. /// Display or Delta.Engine.Tests there is no 3D visualization.
  366. /// Its easier to test here.
  367. /// </summary>
  368. [Test]
  369. public static void GetRayFromScreenPoint()
  370. {
  371. BaseCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  372. Vector halfSize = new Vector(3, 3, 3);
  373. Application.Start(delegate
  374. {
  375. Grid.Draw();
  376. // Get a ray from the middle of the screen
  377. Point input = Input.Mouse.Position;
  378. Ray screenRay = ScreenSpace.GetRayFromScreenPoint(input);
  379. // And convert it back to 2D to make sure it works.
  380. Vector resultingPosition =
  381. ScreenSpace.Project(screenRay.Position + screenRay.Direction);
  382. bool isIntersecting = false;
  383. // draw a simple cube from (-3, -3, -3) to (3, 3, 3)
  384. Box.DrawOutline(-halfSize, halfSize,
  385. isIntersecting
  386. ? Color.Green
  387. : Color.Red);
  388. Font.Default.Draw(
  389. "input=" + input + "\n" +
  390. "screenRay=" + screenRay + "\n" +
  391. "resultingPosition=" + resultingPosition,
  392. Rectangle.FromCenter(new Point(0.5f, 0.3f), Size.Half));
  393. });
  394. }
  395. #endregion
  396. #region DrawRotatedBox3D (Static)
  397. /// <summary>
  398. /// Draw rotated box 3D
  399. /// </summary>
  400. [Test]
  401. public static void DrawRotatedBox3D()
  402. {
  403. BaseCamera cam = new LookAtCamera(new Vector(7, -10, 8));
  404. Vector rotation = new Vector();
  405. Vector halfSize = new Vector(3, 3, 3);
  406. Application.Start(delegate
  407. {
  408. rotation.X += Time.Delta * 10.0f;
  409. Box.DrawOutline(-halfSize, halfSize, Color.Red, rotation);
  410. });
  411. }
  412. #endregion
  413. #region DrawSphere (Static)
  414. /// <summary>
  415. /// Draw sphere
  416. /// </summary>
  417. [Test]
  418. public static void DrawSphere()
  419. {
  420. BaseCamera cam = new LookAtCamera(new Vector(10, 10, 10));
  421. Vector center = Vector.Zero;
  422. Application.Start(delegate
  423. {
  424. // draw some sime spheres around each other
  425. //Sphere.DrawOutline(center, 1f, Color.Red);
  426. Sphere.DrawOutline(center, 5f, Color.Yellow); //.Blue);
  427. //Sphere.DrawOutline(center, 15f, Color.Yellow);
  428. //Sphere.DrawOutline(center, 25f, Color.Red);
  429. //Sphere.DrawOutline(center, 50f, Color.Blue);
  430. Sphere.DrawFilled(center, 4f, Color.Red);
  431. Sphere.DrawFilled(new Vector(10f, 0f, 0f), 4f, Color.Green);
  432. });
  433. }
  434. #endregion
  435. #region DrawSimpleQuad3D (Static)
  436. /// <summary>
  437. /// Draw simple quad 3d
  438. /// </summary>
  439. [Test]
  440. public static void DrawSimpleQuad3D()
  441. {
  442. // We create a 3d quad, so we need a camera.
  443. BaseCamera cam = new LookAtCamera(new Vector(4, -4, 4));
  444. // Create a geometry data instance which will hold the actual data.
  445. GeometryData geometryData = new GeometryData("<SimpleQuad3D>", 4,
  446. VertexFormat.Position3DTextured, 6, true, false);
  447. // Fill the geometry with actual data.
  448. geometryData.SetVertexData(0, new Vector(1, 0, 0), Point.One);
  449. geometryData.SetVertexData(1, new Vector(-1, 0, 0), Point.UnitY);
  450. geometryData.SetVertexData(2, new Vector(-1.25f, 0, 1), Point.Zero);
  451. geometryData.SetVertexData(3, new Vector(1.25f, 0, 1), Point.UnitX);
  452. geometryData.Indices = new ushort[]
  453. {
  454. 0, 2, 1,
  455. 0, 3, 2,
  456. };
  457. // Now create a geometry from the data.
  458. Geometry geometry = Geometry.Create(geometryData);
  459. // We need a material to draw the geometry. Simply use the default.
  460. MaterialColored material = MaterialColored.Default;
  461. // Now start the application...
  462. Application.Start(delegate
  463. {
  464. // ...and draw the geometry.
  465. material.Draw(geometry);
  466. });
  467. }
  468. #endregion
  469. #region DrawGrid (Static)
  470. /// <summary>
  471. /// Draw grid
  472. /// </summary>
  473. [Test]
  474. public static void DrawGrid()
  475. {
  476. LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 3));
  477. Application.Start(delegate
  478. {
  479. // Draw a grid.
  480. Grid.Draw();
  481. });
  482. }
  483. #endregion
  484. #region DrawCulledLines3D (Static)
  485. /// <summary>
  486. /// Draw culled lines 3d.
  487. /// </summary>
  488. [Test]
  489. public static void DrawCulledLines3D()
  490. {
  491. BaseCamera cam = new LookAtCamera(new Vector(4, -4, 4));
  492. Application.Start(delegate
  493. {
  494. int numberOfTotalLines = 0;
  495. int numberOfDrawnLines = 0;
  496. for (float posX = -100f; posX <= 100f; posX += 5f)
  497. {
  498. for (float posY = -100f; posY <= 100f; posY += 5f)
  499. {
  500. numberOfTotalLines++;
  501. Vector pos = new Vector(posX, posY, 0f);
  502. Line.Draw(pos, pos + Vector.UnitZ, Color.Green);
  503. if (Line.WasLastLineCulled == false)
  504. {
  505. numberOfDrawnLines++;
  506. }
  507. }
  508. }
  509. Application.Window.Title = "Number of lines drawn: " +
  510. numberOfDrawnLines + "/" + numberOfTotalLines +
  511. " | FPS=" + Time.Fps;
  512. });
  513. }
  514. #endregion
  515. #region DrawFilledRectangleWithFont (Static)
  516. /// <summary>
  517. /// Draw filled rectangle with font. bug #3616
  518. /// </summary>
  519. [Test]
  520. public static void DrawFilledRectangleWithFont()
  521. {
  522. Font drawFont = Font.Default;
  523. Rectangle drawRectangle = new Rectangle(0.5f, 0.5f, 0.25f, 0.25f);
  524. Application.Start(delegate
  525. {
  526. Rect.DrawFilled(drawRectangle, Color.Red);
  527. drawFont.Draw("Hello World", drawRectangle);
  528. });
  529. }
  530. #endregion
  531. #region LoadDrawManager (Static)
  532. /// <summary>
  533. /// Load draw manager
  534. /// </summary>
  535. [Test]
  536. public static void LoadDrawManager()
  537. {
  538. }
  539. #endregion
  540. #region DrawMeasure (Static)
  541. /// <summary>
  542. /// Draw measure
  543. /// </summary>
  544. [Test]
  545. public static void DrawMeasure()
  546. {
  547. //Point startPos = Point.Half;
  548. Application.Start(delegate
  549. {
  550. /*obs, we don't have input here.
  551. if (Input.Mouse.GetState(InputButton.MouseLeft) == InputState.Pressed)
  552. {
  553. startPos = Input.Mouse.Position;
  554. }
  555. if (Input.Mouse.LeftButtonIsPressed)
  556. {
  557. Graphic.DrawManager.Measure(startPos, Input.Mouse.Position, Color.Red);
  558. } // if
  559. */
  560. //DrawManager.Measure(ScreenSpace.Area.TopLeft, ScreenSpace.Area.Center,
  561. // Color.Red);
  562. });
  563. }
  564. #endregion
  565. }
  566. }