PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/SolarSystem.Wpf/CameraController.cs

https://bitbucket.org/efouts/solarsystem
C# | 168 lines | 135 code | 33 blank | 0 comment | 29 complexity | cf17a30ae25947cec88e3b594f81127f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using SharpGL;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Input;
  8. using System.Windows.Media.Media3D;
  9. namespace SolarSystem.Wpf
  10. {
  11. public class CameraController
  12. {
  13. private const Int32 CameraMoveSensitivity = 10;
  14. private const Int32 CameraRotationSensitivity = 10;
  15. private Camera camera;
  16. private Int32 followBodyIndex = -1;
  17. private System system;
  18. private Normalizer normalizer;
  19. private Double height;
  20. private Double width;
  21. private Point? lastMousePosition;
  22. private Boolean mouseControlEnabled;
  23. private Boolean hudEnabled;
  24. public CameraController(System system, Normalizer normalizer, Double height, Double width)
  25. {
  26. camera = new Camera(new Vector3D(0, 100, 100), new Vector3D(0, 0, 0), new Vector3D(0, Math.Sqrt(2) / 2, -Math.Sqrt(2) / 2));
  27. this.system = system;
  28. this.normalizer = normalizer;
  29. this.height = height;
  30. this.width = width;
  31. }
  32. public void Render(OpenGL gl)
  33. {
  34. if (followBodyIndex != -1)
  35. FollowBody(gl);
  36. gl.MatrixMode(OpenGL.GL_PROJECTION);
  37. gl.LoadIdentity();
  38. gl.Perspective(60.0f, width / height, 0.01d, 1000000d);
  39. gl.LookAt(
  40. camera.Position.X,
  41. camera.Position.Y,
  42. camera.Position.Z,
  43. camera.Target.X,
  44. camera.Target.Y,
  45. camera.Target.Z,
  46. camera.Up.X,
  47. camera.Up.Y,
  48. camera.Up.Z);
  49. gl.MatrixMode(OpenGL.GL_MODELVIEW);
  50. }
  51. private void FollowBody(OpenGL gl)
  52. {
  53. var body = system.Bodies.ElementAt(followBodyIndex);
  54. var position = new Vector3D(normalizer.NormalizePosition(body.Position[0]), 0, normalizer.NormalizePosition(body.Position[1]));
  55. camera.FollowBody(position, normalizer.NormalizeRadius(body.Radius));
  56. if (hudEnabled)
  57. {
  58. var planetDisplay = "Planet: " + body.Name;
  59. var positionDisplay = String.Format(
  60. "Position: X={0} Z={1} ",
  61. GetRoundedVectorValue(body.Position[0]),
  62. GetRoundedVectorValue(body.Position[1]));
  63. var velocityDisplay = String.Format(
  64. "Velocity: X={0} Z={1} Magnitude={2}",
  65. GetRoundedVectorValue(body.Velocity[0]),
  66. GetRoundedVectorValue(body.Velocity[1]),
  67. GetRoundedVectorValue(Math.Sqrt(Math.Pow(body.Velocity[0], 2) + Math.Pow(body.Velocity[1], 2))));
  68. DrawHudText(gl, planetDisplay, 20);
  69. DrawHudText(gl, positionDisplay, 40);
  70. DrawHudText(gl, velocityDisplay, 60);
  71. }
  72. }
  73. private static string GetRoundedVectorValue(Double value)
  74. {
  75. return Math.Round(value, 2).ToString("N");
  76. }
  77. private static void DrawHudText(OpenGL gl, String text, Int32 topOffset)
  78. {
  79. gl.DrawText(10, gl.RenderContextProvider.Height - topOffset, 255, 255, 255, String.Empty, 12f, text);
  80. }
  81. public void KeyPress(Key key)
  82. {
  83. if (Keyboard.IsKeyDown(Key.W))
  84. camera.MoveInDirection(CameraMoveSensitivity);
  85. if (Keyboard.IsKeyDown(Key.S))
  86. camera.MoveInDirection(-CameraMoveSensitivity);
  87. if (Keyboard.IsKeyDown(Key.A))
  88. camera.MoveHorizontally(-CameraMoveSensitivity);
  89. if (Keyboard.IsKeyDown(Key.D))
  90. camera.MoveHorizontally(CameraMoveSensitivity);
  91. if (Keyboard.IsKeyDown(Key.Q))
  92. camera.MoveVertically(CameraMoveSensitivity);
  93. if (Keyboard.IsKeyDown(Key.E))
  94. camera.MoveVertically(-CameraMoveSensitivity);
  95. if (Keyboard.IsKeyDown(Key.Left))
  96. camera.Rotate(new Vector3D(CameraRotationSensitivity, 0, 0));
  97. if (Keyboard.IsKeyDown(Key.Right))
  98. camera.Rotate(new Vector3D(-CameraRotationSensitivity, 0, 0));
  99. if (Keyboard.IsKeyDown(Key.Up))
  100. camera.Rotate(new Vector3D(0, CameraRotationSensitivity, 0));
  101. if (Keyboard.IsKeyDown(Key.Down))
  102. camera.Rotate(new Vector3D(0, -CameraRotationSensitivity, 0));
  103. if (Keyboard.IsKeyDown(Key.M))
  104. mouseControlEnabled = !mouseControlEnabled;
  105. if (Keyboard.IsKeyDown(Key.H))
  106. hudEnabled = !hudEnabled;
  107. var keyValue = Convert.ToInt32(key);
  108. if (keyValue == 34)
  109. followBodyIndex = -1;
  110. else if (keyValue >= 35 && keyValue <= 42)
  111. followBodyIndex = keyValue - 34;
  112. }
  113. public void MouseMove(Point mousePosition)
  114. {
  115. if (mouseControlEnabled == false)
  116. return;
  117. if (lastMousePosition.HasValue)
  118. {
  119. var movement = new Vector3D(lastMousePosition.Value.X - mousePosition.X, lastMousePosition.Value.Y - mousePosition.Y, 0);
  120. if (Math.Abs(movement.X) > CameraRotationSensitivity)
  121. camera.Rotate(new Vector3D(movement.X / width * 360, 0, 0));
  122. if (Math.Abs(movement.Y) > CameraRotationSensitivity)
  123. camera.Rotate(new Vector3D(0, movement.Y / height * 360, 0));
  124. }
  125. lastMousePosition = mousePosition;
  126. }
  127. public void RightMouseButtonPressed()
  128. {
  129. if (followBodyIndex == 8 || followBodyIndex == -1)
  130. followBodyIndex = 1;
  131. else
  132. followBodyIndex++;
  133. }
  134. }
  135. }