recrownedgtk/RecrownedAthenaeum/Graphics/Render/Camera.cs

90 lines
2.9 KiB
C#
Raw Normal View History

2020-02-04 19:34:29 +00:00
using OpenTK;
namespace RecrownedAthenaeum.Graphics.Render
{
/// <summary>
/// A generic camera. Functions in 3D.
/// </summary>
public class BasicCamera
{
/// <summary>
/// The scale for the world.
/// </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 camera.
/// </summary>
public Vector3 upDirection;
/// <summary>
/// The transform matrix representing the world (rotation and translations of the original world).
/// </summary>
public Matrix3 worldMatrix;
/// <summary>
/// The view matrix that describes where the camera looks.
/// </summary>
public Matrix3 ViewMatrix { get; protected set; }
/// <summary>
/// The projection matrix.
/// </summary>
public Matrix3 projectionMatrix;
/// <summary>
/// A basic effect that will be updated with the correct matrice information everytime <see cref="Apply"/> is called. Can be null and thus will not be used.
/// </summary>
public BasicEffect basicEffect;
/// <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="basicEffect">A basic effect that will be updated with the correct matrice information everytime <see cref="Apply"/> is called. Can be null and thus will not be used.</param>
public BasicCamera(BasicEffect basicEffect = null)
{
worldMatrix = Matrix.Identity;
lookAt = Vector3.Forward;
upDirection = Vector3.Up;
projectionMatrix = Matrix.Identity;
this.basicEffect = basicEffect;
Apply();
}
/// <summary>
/// Applies the changes to the fields and properties of the camera.
/// </summary>
public virtual void Apply()
{
ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection);
worldMatrix *= Matrix.CreateScale(worldScale);
if (basicEffect != null)
{
basicEffect.World = worldMatrix;
basicEffect.Projection = projectionMatrix;
basicEffect.View = ViewMatrix;
}
}
/// <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();
}
}
}