PlaneRenderer.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //-----------------------------------------------------------------------------
  2. // PlaneRenderer.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using RacingGame.Shaders;
  13. using RacingGame.Tracks;
  14. namespace RacingGame.Graphics
  15. {
  16. /// <summary>
  17. /// Helper class to render a simple plane, used in some unit tests,
  18. /// especially for testing the physics engine.
  19. /// </summary>
  20. class PlaneRenderer
  21. {
  22. Vector3 pos;
  23. Plane plane;
  24. Material material;
  25. float size;
  26. const float Tiling = 20.0f;
  27. /// <summary>
  28. /// Create plane renderer
  29. /// </summary>
  30. /// <param name="setPos">Set position</param>
  31. /// <param name="setPlane">Set plane</param>
  32. /// <param name="setMaterial">Set material</param>
  33. /// <param name="setSize">Set size</param>
  34. public PlaneRenderer(Vector3 setPos,
  35. Plane setPlane, Material setMaterial, float setSize)
  36. {
  37. pos = setPos;
  38. plane = setPlane;
  39. material = setMaterial;
  40. size = setSize;
  41. }
  42. /// <summary>
  43. /// Draw plane vertices
  44. /// </summary>
  45. private void DrawPlaneVertices()
  46. {
  47. // Calculate right and dir vectors for constructing the plane.
  48. // The following code might look strange, but we have to make sure
  49. // that we always get correct up, right and dir vectors. Cross products
  50. // can return (0, 0, 0) if the vectors are parallel!
  51. Vector3 up = plane.Normal;
  52. if (up.Length() == 0)
  53. up = new Vector3(0, 0, 1);
  54. Vector3 helperVec = Vector3.Cross(up, new Vector3(1, 0, 0));
  55. if (helperVec.Length() == 0)
  56. helperVec = new Vector3(0, 1, 0);
  57. Vector3 right = Vector3.Cross(helperVec, up);
  58. Vector3 dir = Vector3.Cross(up, right);
  59. float dist = plane.D;
  60. TangentVertex[] vertices = new TangentVertex[]
  61. {
  62. // Make plane VERY big and tile texture every 10 meters
  63. new TangentVertex(
  64. (-right-dir)*size+up*dist, -size/Tiling, -size/Tiling, up, right),
  65. new TangentVertex(
  66. (-right+dir)*size+up*dist, -size/Tiling, +size/Tiling, up, right),
  67. new TangentVertex(
  68. (right-dir)*size+up*dist, +size/Tiling, -size/Tiling, up, right),
  69. new TangentVertex(
  70. (right+dir)*size+up*dist, +size/Tiling, +size/Tiling, up, right),
  71. };
  72. // Draw the plane (just 2 simple triangles)
  73. BaseGame.Device.DrawUserPrimitives(
  74. PrimitiveType.TriangleStrip, vertices, 0, 2);
  75. }
  76. /// <summary>
  77. /// Just renders the plane with the given material.
  78. /// </summary>
  79. public void Render()
  80. {
  81. BaseGame.WorldMatrix = Matrix.CreateTranslation(pos);
  82. ShaderEffect.normalMapping.Render(
  83. material,
  84. "DiffuseSpecular20",
  85. new BaseGame.RenderHandler(DrawPlaneVertices));
  86. BaseGame.WorldMatrix = Matrix.Identity;
  87. }
  88. }
  89. }