recrownedgtk/RecrownedAthenaeum/Camera/Camera.cs

92 lines
3.0 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RecrownedAthenaeum.Camera
{
/// <summary>
/// A generic 3D camera.
/// </summary>
public class Camera
{
/// <summary>
/// Current position in the world.
/// </summary>
public Vector3 position;
/// <summary>
/// The place the 3D camera is looking at.
/// </summary>
public Vector3 lookAt;
/// <summary>
/// The direction up is for the 3D camera.
/// </summary>
public Vector3 upDirection;
/// <summary>
/// The transform matrix representing the world (rotation and translations of the original world).
/// </summary>
public Matrix worldMatrix;
/// <summary>
/// The view matrix that describes where the camera looks.
/// </summary>
public Matrix ViewMatrix { get; private set; }
/// <summary>
/// The projection matrix.
/// </summary>
public Matrix projectionMatrix;
/// <summary>
/// The final transformation matrix.
/// </summary>
public Matrix TransformationMatrix { get; private set; }
/// <summary>
/// The graphics device used
/// </summary>
protected GraphicsDevice graphicsDevice;
/// <summary>
/// Constructs 3D camera with an orthographic projection matrix with dimensions of graphics devices viewport. All changes to matrices should have apply called after changes.
/// </summary>
/// <param name="graphicsDevice">The graphics device to use. Will use graphics device from <see cref="Configuration"/>'s graphics device manager if this is null which it is by default.</param>
public Camera(GraphicsDevice graphicsDevice = null)
{
this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
position.Z = 0;
worldMatrix = Matrix.Identity;
lookAt = Vector3.Forward;
upDirection = Vector3.Up;
Center();
projectionMatrix = Matrix.CreateOrthographic(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 1);
Apply();
}
/// <summary>
/// Applies the changes to the fields and properties of the camera.
/// </summary>
public virtual void Apply()
{
ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection);
TransformationMatrix = worldMatrix * ViewMatrix * projectionMatrix;
}
/// <summary>
/// Centers the camera to middle of width and height of game window.
/// </summary>
public void Center()
{
position.X = this.graphicsDevice.Viewport.Width * 0.5f;
position.Y = this.graphicsDevice.Viewport.Height * 0.5f;
}
}
}