Advertisement
Guest User

CameraController.cs

a guest
Oct 14th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.33 KB | None | 0 0
  1. namespace UmbraGame.Camera
  2. {
  3.     using System;
  4.     using Microsoft.Xna.Framework;
  5.     using Utilities;
  6.  
  7.     public class CameraController : BaseManager
  8.     {
  9.         #region Fields
  10.  
  11.         private readonly FpsCamera _centerEyeCamera;
  12.         private readonly FpsCamera _leftEyeCamera;
  13.         private readonly FpsCamera _rightEyeCamera;
  14.  
  15.         #endregion
  16.  
  17.         #region Properties
  18.  
  19.         public Joint ShoulderNeck { get; set; }
  20.         public Joint NeckHead { get; set; }
  21.  
  22.         public Matrix World
  23.         {
  24.             get
  25.             {
  26.                 return (game.OculusRiftMode
  27.                             ? (Matrix.Invert((_leftEyeCamera.View + _rightEyeCamera.View) / 2))
  28.                             : Matrix.Invert(_centerEyeCamera.View));
  29.             }
  30.         }
  31.  
  32.         #endregion
  33.  
  34.         public CameraController(MainGame game)
  35.             : base(game)
  36.         {
  37.             NeckHead = new Joint(null, new Vector3(0, 2.2f, 0), 60, -60, 0, 0, 60, -60);
  38.             ShoulderNeck = new Joint(NeckHead, new Vector3(0, 15.3f, 0), 20, -20, 0, 0, 20, -20);
  39.  
  40.             game.Input.FreeMouse = false;
  41.  
  42.             _leftEyeCamera = new FpsCamera(game, game.GraphicsManager.LeftEyeViewport);
  43.             _rightEyeCamera = new FpsCamera(game, game.GraphicsManager.RightEyeViewport);
  44.  
  45.             _centerEyeCamera = new FpsCamera(game, game.GraphicsManager.DefaultViewport);
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Updates all information regarding the cameras.
  50.         /// </summary>
  51.         /// <param name="gameTime">Elapsed gametime since last update.</param>
  52.         public override void Update(GameTime gameTime)
  53.         {
  54.             Vector3 leftEyePosition = GetEyePosition(Eyes.Left);
  55.             Vector3 rightEyePosition = GetEyePosition(Eyes.Right);
  56.  
  57.             Vector3 centerEyePosition = NeckHead.Position;
  58.  
  59.             if (game.Input.OldMouse != game.Input.Mouse)
  60.             {
  61.                 NeckHead.Yaw -= game.Input.DeltaY*0.005f;
  62.                 NeckHead.Pitch -= game.Input.DeltaX*0.005f;
  63.             }
  64.  
  65.             if (game.OculusRiftMode)
  66.             {
  67.                 _leftEyeCamera.Update(gameTime, leftEyePosition, NeckHead.Yaw, NeckHead.Pitch, NeckHead.Roll);
  68.                 _rightEyeCamera.Update(gameTime, rightEyePosition, NeckHead.Yaw, NeckHead.Pitch, NeckHead.Roll);
  69.             }
  70.             else
  71.             {
  72.                 _centerEyeCamera.Update(gameTime, centerEyePosition, NeckHead.Yaw, NeckHead.Pitch, NeckHead.Roll);
  73.             }
  74.  
  75.             base.Update(gameTime);
  76.         }
  77.  
  78.         /// <summary>
  79.         /// Gets the position of the eye specified
  80.         /// </summary>
  81.         /// <param name="eyes">The eye which to get the position of.</param>
  82.         /// <returns>Returns a vector 3 containing the information specified.</returns>
  83.         private Vector3 GetEyePosition(Eyes eyes)
  84.         {
  85.             Matrix neckHead = GetHeadPositions();
  86.  
  87.             Vector3 eye = (eyes == Eyes.Left ? neckHead.Left : neckHead.Right);
  88.             Vector3 eyePosition = NeckHead.Position;
  89.             return (eye + eyePosition);
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Calculates and retrieves the position in the head, which is at the height of the eyes.
  94.         /// About the center of your nose.
  95.         /// </summary>
  96.         /// <returns>Returns a matrix containing said information.</returns>
  97.         private Matrix GetHeadPositions()
  98.         {
  99.             Matrix shoulderNeck = ShoulderNeck.GetMatrix();
  100.             Matrix neckHead = NeckHead.GetMatrix()*shoulderNeck;
  101.  
  102.             return neckHead;
  103.         }
  104.  
  105.         /// <summary>
  106.         /// Returns the camera according to the eye specified.
  107.         /// </summary>
  108.         /// <param name="eye">The eye which to retrieve the camera that corrosponds to.</param>
  109.         /// <returns>Returns the camera according to the eye specified.</returns>
  110.         public FpsCamera GetCamera(Eyes eye)
  111.         {
  112.             switch (eye)
  113.             {
  114.                 case Eyes.Left:
  115.                     return _leftEyeCamera;
  116.                 case Eyes.Right:
  117.                     return _rightEyeCamera;
  118.                 case Eyes.Center:
  119.                     return _centerEyeCamera;
  120.                 default:
  121.                     throw new ArgumentOutOfRangeException("eye");
  122.             }
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement