Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace UmbraGame.Camera
- {
- using System;
- using Microsoft.Xna.Framework;
- using Utilities;
- public class CameraController : BaseManager
- {
- #region Fields
- private readonly FpsCamera _centerEyeCamera;
- private readonly FpsCamera _leftEyeCamera;
- private readonly FpsCamera _rightEyeCamera;
- #endregion
- #region Properties
- public Joint ShoulderNeck { get; set; }
- public Joint NeckHead { get; set; }
- public Matrix World
- {
- get
- {
- return (game.OculusRiftMode
- ? (Matrix.Invert((_leftEyeCamera.View + _rightEyeCamera.View) / 2))
- : Matrix.Invert(_centerEyeCamera.View));
- }
- }
- #endregion
- public CameraController(MainGame game)
- : base(game)
- {
- NeckHead = new Joint(null, new Vector3(0, 2.2f, 0), 60, -60, 0, 0, 60, -60);
- ShoulderNeck = new Joint(NeckHead, new Vector3(0, 15.3f, 0), 20, -20, 0, 0, 20, -20);
- game.Input.FreeMouse = false;
- _leftEyeCamera = new FpsCamera(game, game.GraphicsManager.LeftEyeViewport);
- _rightEyeCamera = new FpsCamera(game, game.GraphicsManager.RightEyeViewport);
- _centerEyeCamera = new FpsCamera(game, game.GraphicsManager.DefaultViewport);
- }
- /// <summary>
- /// Updates all information regarding the cameras.
- /// </summary>
- /// <param name="gameTime">Elapsed gametime since last update.</param>
- public override void Update(GameTime gameTime)
- {
- Vector3 leftEyePosition = GetEyePosition(Eyes.Left);
- Vector3 rightEyePosition = GetEyePosition(Eyes.Right);
- Vector3 centerEyePosition = NeckHead.Position;
- if (game.Input.OldMouse != game.Input.Mouse)
- {
- NeckHead.Yaw -= game.Input.DeltaY*0.005f;
- NeckHead.Pitch -= game.Input.DeltaX*0.005f;
- }
- if (game.OculusRiftMode)
- {
- _leftEyeCamera.Update(gameTime, leftEyePosition, NeckHead.Yaw, NeckHead.Pitch, NeckHead.Roll);
- _rightEyeCamera.Update(gameTime, rightEyePosition, NeckHead.Yaw, NeckHead.Pitch, NeckHead.Roll);
- }
- else
- {
- _centerEyeCamera.Update(gameTime, centerEyePosition, NeckHead.Yaw, NeckHead.Pitch, NeckHead.Roll);
- }
- base.Update(gameTime);
- }
- /// <summary>
- /// Gets the position of the eye specified
- /// </summary>
- /// <param name="eyes">The eye which to get the position of.</param>
- /// <returns>Returns a vector 3 containing the information specified.</returns>
- private Vector3 GetEyePosition(Eyes eyes)
- {
- Matrix neckHead = GetHeadPositions();
- Vector3 eye = (eyes == Eyes.Left ? neckHead.Left : neckHead.Right);
- Vector3 eyePosition = NeckHead.Position;
- return (eye + eyePosition);
- }
- /// <summary>
- /// Calculates and retrieves the position in the head, which is at the height of the eyes.
- /// About the center of your nose.
- /// </summary>
- /// <returns>Returns a matrix containing said information.</returns>
- private Matrix GetHeadPositions()
- {
- Matrix shoulderNeck = ShoulderNeck.GetMatrix();
- Matrix neckHead = NeckHead.GetMatrix()*shoulderNeck;
- return neckHead;
- }
- /// <summary>
- /// Returns the camera according to the eye specified.
- /// </summary>
- /// <param name="eye">The eye which to retrieve the camera that corrosponds to.</param>
- /// <returns>Returns the camera according to the eye specified.</returns>
- public FpsCamera GetCamera(Eyes eye)
- {
- switch (eye)
- {
- case Eyes.Left:
- return _leftEyeCamera;
- case Eyes.Right:
- return _rightEyeCamera;
- case Eyes.Center:
- return _centerEyeCamera;
- default:
- throw new ArgumentOutOfRangeException("eye");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement