recrownedathenaeum/RecrownedAthenaeum/Render/Camera3D.cs

100 lines
3.2 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace RecrownedAthenaeum.Camera
{
/// <summary>
/// A generic 3D camera.
/// </summary>
2019-02-06 06:12:39 +00:00
public class Camera3D
{
2019-03-10 06:46:28 +00:00
/// <summary>
/// The scale for the world.
2019-03-10 06:46:28 +00:00
/// </summary>
public float worldScale = 1f;
/// <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>
2019-03-10 06:46:28 +00:00
public Matrix ViewMatrix { get; protected set; }
/// <summary>
/// The projection matrix.
/// </summary>
public Matrix projectionMatrix;
/// <summary>
/// The graphics device used
/// </summary>
protected GraphicsDevice graphicsDevice;
2019-02-25 04:44:02 +00:00
/// <summary>
/// The basic effect that contains the transformations.
/// </summary>
2019-03-10 06:46:28 +00:00
public BasicEffect BasicEffect { get; protected set; }
2019-02-25 04:44:02 +00:00
/// <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>
2019-02-06 06:12:39 +00:00
public Camera3D(GraphicsDevice graphicsDevice = null)
{
2019-02-25 04:44:02 +00:00
graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
this.graphicsDevice = graphicsDevice;
worldMatrix = Matrix.Identity;
lookAt = Vector3.Forward;
upDirection = Vector3.Up;
2019-02-11 05:28:48 +00:00
projectionMatrix = Matrix.Identity;
2019-02-25 04:44:02 +00:00
BasicEffect = new BasicEffect(graphicsDevice);
BasicEffect.TextureEnabled = true;
2019-03-06 03:42:28 +00:00
BasicEffect.VertexColorEnabled = 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);
2019-03-10 06:46:28 +00:00
worldMatrix *= Matrix.CreateScale(worldScale);
2019-02-25 04:44:02 +00:00
BasicEffect.World = worldMatrix;
BasicEffect.View = ViewMatrix;
BasicEffect.Projection = projectionMatrix;
}
/// <summary>
2019-02-11 05:28:48 +00:00
/// Moves camera by the given amount.
/// </summary>
2019-02-11 05:28:48 +00:00
/// <param name="move">A <see cref="Vector3"/> that contains how much in each direction to move.</param>
public void MoveCamera(Vector3 move)
{
2019-02-11 05:28:48 +00:00
position += move;
Apply();
}
}
}