/SolarSystem.Wpf/CameraController.cs
C# | 168 lines | 135 code | 33 blank | 0 comment | 29 complexity | cf17a30ae25947cec88e3b594f81127f MD5 | raw file
Possible License(s): BSD-3-Clause
- using SharpGL;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media.Media3D;
-
- namespace SolarSystem.Wpf
- {
- public class CameraController
- {
- private const Int32 CameraMoveSensitivity = 10;
- private const Int32 CameraRotationSensitivity = 10;
-
- private Camera camera;
- private Int32 followBodyIndex = -1;
- private System system;
- private Normalizer normalizer;
- private Double height;
- private Double width;
- private Point? lastMousePosition;
- private Boolean mouseControlEnabled;
- private Boolean hudEnabled;
-
- public CameraController(System system, Normalizer normalizer, Double height, Double width)
- {
- camera = new Camera(new Vector3D(0, 100, 100), new Vector3D(0, 0, 0), new Vector3D(0, Math.Sqrt(2) / 2, -Math.Sqrt(2) / 2));
- this.system = system;
- this.normalizer = normalizer;
- this.height = height;
- this.width = width;
- }
-
- public void Render(OpenGL gl)
- {
- if (followBodyIndex != -1)
- FollowBody(gl);
-
- gl.MatrixMode(OpenGL.GL_PROJECTION);
- gl.LoadIdentity();
- gl.Perspective(60.0f, width / height, 0.01d, 1000000d);
-
- gl.LookAt(
- camera.Position.X,
- camera.Position.Y,
- camera.Position.Z,
- camera.Target.X,
- camera.Target.Y,
- camera.Target.Z,
- camera.Up.X,
- camera.Up.Y,
- camera.Up.Z);
-
- gl.MatrixMode(OpenGL.GL_MODELVIEW);
- }
-
- private void FollowBody(OpenGL gl)
- {
- var body = system.Bodies.ElementAt(followBodyIndex);
- var position = new Vector3D(normalizer.NormalizePosition(body.Position[0]), 0, normalizer.NormalizePosition(body.Position[1]));
- camera.FollowBody(position, normalizer.NormalizeRadius(body.Radius));
-
- if (hudEnabled)
- {
- var planetDisplay = "Planet: " + body.Name;
- var positionDisplay = String.Format(
- "Position: X={0} Z={1} ",
- GetRoundedVectorValue(body.Position[0]),
- GetRoundedVectorValue(body.Position[1]));
-
- var velocityDisplay = String.Format(
- "Velocity: X={0} Z={1} Magnitude={2}",
- GetRoundedVectorValue(body.Velocity[0]),
- GetRoundedVectorValue(body.Velocity[1]),
- GetRoundedVectorValue(Math.Sqrt(Math.Pow(body.Velocity[0], 2) + Math.Pow(body.Velocity[1], 2))));
-
- DrawHudText(gl, planetDisplay, 20);
- DrawHudText(gl, positionDisplay, 40);
- DrawHudText(gl, velocityDisplay, 60);
- }
- }
-
- private static string GetRoundedVectorValue(Double value)
- {
- return Math.Round(value, 2).ToString("N");
- }
-
- private static void DrawHudText(OpenGL gl, String text, Int32 topOffset)
- {
- gl.DrawText(10, gl.RenderContextProvider.Height - topOffset, 255, 255, 255, String.Empty, 12f, text);
- }
-
- public void KeyPress(Key key)
- {
- if (Keyboard.IsKeyDown(Key.W))
- camera.MoveInDirection(CameraMoveSensitivity);
-
- if (Keyboard.IsKeyDown(Key.S))
- camera.MoveInDirection(-CameraMoveSensitivity);
-
- if (Keyboard.IsKeyDown(Key.A))
- camera.MoveHorizontally(-CameraMoveSensitivity);
-
- if (Keyboard.IsKeyDown(Key.D))
- camera.MoveHorizontally(CameraMoveSensitivity);
-
- if (Keyboard.IsKeyDown(Key.Q))
- camera.MoveVertically(CameraMoveSensitivity);
-
- if (Keyboard.IsKeyDown(Key.E))
- camera.MoveVertically(-CameraMoveSensitivity);
-
- if (Keyboard.IsKeyDown(Key.Left))
- camera.Rotate(new Vector3D(CameraRotationSensitivity, 0, 0));
-
- if (Keyboard.IsKeyDown(Key.Right))
- camera.Rotate(new Vector3D(-CameraRotationSensitivity, 0, 0));
-
- if (Keyboard.IsKeyDown(Key.Up))
- camera.Rotate(new Vector3D(0, CameraRotationSensitivity, 0));
-
- if (Keyboard.IsKeyDown(Key.Down))
- camera.Rotate(new Vector3D(0, -CameraRotationSensitivity, 0));
-
- if (Keyboard.IsKeyDown(Key.M))
- mouseControlEnabled = !mouseControlEnabled;
-
- if (Keyboard.IsKeyDown(Key.H))
- hudEnabled = !hudEnabled;
-
- var keyValue = Convert.ToInt32(key);
-
- if (keyValue == 34)
- followBodyIndex = -1;
- else if (keyValue >= 35 && keyValue <= 42)
- followBodyIndex = keyValue - 34;
- }
-
- public void MouseMove(Point mousePosition)
- {
- if (mouseControlEnabled == false)
- return;
-
- if (lastMousePosition.HasValue)
- {
- var movement = new Vector3D(lastMousePosition.Value.X - mousePosition.X, lastMousePosition.Value.Y - mousePosition.Y, 0);
-
- if (Math.Abs(movement.X) > CameraRotationSensitivity)
- camera.Rotate(new Vector3D(movement.X / width * 360, 0, 0));
-
- if (Math.Abs(movement.Y) > CameraRotationSensitivity)
- camera.Rotate(new Vector3D(0, movement.Y / height * 360, 0));
- }
-
- lastMousePosition = mousePosition;
- }
-
- public void RightMouseButtonPressed()
- {
- if (followBodyIndex == 8 || followBodyIndex == -1)
- followBodyIndex = 1;
- else
- followBodyIndex++;
- }
- }
- }