recrownedathenaeum/RecrownedAthenaeum/View/Camera3D.cs

104 lines
3.3 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 Camera3D
{
/// <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>
/// The basic effect that contains the transformations.
/// </summary>
public BasicEffect BasicEffect { get; private set; }
/// <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 Camera3D(GraphicsDevice graphicsDevice = null)
{
graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
this.graphicsDevice = graphicsDevice;
worldMatrix = Matrix.Identity;
lookAt = Vector3.Forward;
upDirection = Vector3.Up;
projectionMatrix = Matrix.Identity;
BasicEffect = new BasicEffect(graphicsDevice);
BasicEffect.TextureEnabled = true;
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 = projectionMatrix * ViewMatrix * worldMatrix;
BasicEffect.World = worldMatrix;
BasicEffect.View = ViewMatrix;
BasicEffect.Projection = projectionMatrix;
}
/// <summary>
/// Moves camera by the given amount.
/// </summary>
/// <param name="move">A <see cref="Vector3"/> that contains how much in each direction to move.</param>
public void MoveCamera(Vector3 move)
{
position += move;
Apply();
}
}
}