PageRenderTime 127ms CodeModel.GetById 40ms app.highlight 27ms RepoModel.GetById 33ms app.codeStats 1ms

/Rendering/BasicTests/DrawTests.cs

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