recrownedgtk/RecrownedAthenaeum/Graphics/Render/BasicCamera.cs

91 lines
2.9 KiB
C#
Raw Normal View History

using OpenTK;
namespace RecrownedAthenaeum.Graphics.Render
{
/// <summary>
/// A generic camera. Functions in 3D.
/// </summary>
public class BasicCamera
{
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 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>
/// 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;
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="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;
2019-02-11 05:28:48 +00:00
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);
2019-03-10 06:46:28 +00:00
worldMatrix *= Matrix.CreateScale(worldScale);
2019-02-25 04:44:02 +00:00
if (basicEffect != null)
{
basicEffect.World = worldMatrix;
basicEffect.Projection = projectionMatrix;
basicEffect.View = ViewMatrix;
}
}
/// <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();
}
}
}