initial commit to new instance
This commit is contained in:
113
src/Camera/Camera.cs
Normal file
113
src/Camera/Camera.cs
Normal file
@ -0,0 +1,113 @@
|
||||
using Silk.NET.Maths;
|
||||
|
||||
namespace Engine_silk.NET;
|
||||
|
||||
public enum MovementDirection
|
||||
{
|
||||
Forward,
|
||||
Backward,
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
Down,
|
||||
}
|
||||
|
||||
public class Camera
|
||||
{
|
||||
private readonly Vector3D<float> _worldUp;
|
||||
private Vector3D<float> _position;
|
||||
private Vector3D<float> _front;
|
||||
private Vector3D<float> _right;
|
||||
private Vector3D<float> _up;
|
||||
private float _yaw;
|
||||
private float _pitch;
|
||||
private float _movementSpeed;
|
||||
|
||||
/// <summary>
|
||||
/// The view matrix according to the current camera position and rotation
|
||||
/// </summary>
|
||||
public Matrix4X4<float> ViewMatrix { get => Matrix4X4.CreateLookAt(_position, _position + _front, _worldUp); }
|
||||
public float MouseSensitivity { get; set; } = 0.1f;
|
||||
/// <summary>
|
||||
/// Controlls how fast the camera moves if a key is pressed
|
||||
/// </summary>
|
||||
public float MovementSpeed { get; set; } = 2.5f;
|
||||
public float Fov { get; set; }
|
||||
|
||||
public Camera(Vector3D<float> position, float yaw = -90.0f, float pitch = 0, float movementSpeed = 2.5f, float fov = 45.0f)
|
||||
{
|
||||
_position = position;
|
||||
_yaw = yaw;
|
||||
_pitch = pitch;
|
||||
_movementSpeed = movementSpeed;
|
||||
Fov = fov;
|
||||
_worldUp = Vector3D<float>.UnitY;
|
||||
|
||||
UpdateCameraVectors();
|
||||
}
|
||||
|
||||
public void ProcessKeyboard(float deltaTime, MovementDirection direction)
|
||||
{
|
||||
float velocity = MovementSpeed * deltaTime;
|
||||
|
||||
switch (direction)
|
||||
{
|
||||
case MovementDirection.Forward:
|
||||
_position += _front * velocity;
|
||||
break;
|
||||
case MovementDirection.Backward:
|
||||
_position -= _front * velocity;
|
||||
break;
|
||||
case MovementDirection.Left:
|
||||
_position -= _right * velocity;
|
||||
break;
|
||||
case MovementDirection.Right:
|
||||
_position += _right * velocity;
|
||||
break;
|
||||
case MovementDirection.Up:
|
||||
_position += _up * velocity;
|
||||
break;
|
||||
case MovementDirection.Down:
|
||||
_position -= _up * velocity;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void ProcessMouseMovement(float xOffset, float yOffset)
|
||||
{
|
||||
xOffset *= MouseSensitivity; yOffset *= MouseSensitivity;
|
||||
|
||||
_yaw += xOffset;
|
||||
_pitch += yOffset;
|
||||
|
||||
if (_pitch > 89.0f)
|
||||
_pitch = 89.0f;
|
||||
else if (_pitch < -89.0f)
|
||||
_pitch = -89.0f;
|
||||
|
||||
UpdateCameraVectors(); // Recalculate front, right and up vectors since the view angle has changed
|
||||
}
|
||||
|
||||
public void ProcessMouseScroll(float yOffset)
|
||||
{
|
||||
Fov -= yOffset;
|
||||
if (Fov < 1.0f)
|
||||
Fov = 1.0f;
|
||||
if (Fov > 90.0f)
|
||||
Fov = 90.0f;
|
||||
}
|
||||
|
||||
private void UpdateCameraVectors()
|
||||
{
|
||||
Vector3D<float> front = new(
|
||||
MathF.Cos(Maths.Convert.ToRadians(_yaw)) * MathF.Cos(Maths.Convert.ToRadians(_pitch)),
|
||||
MathF.Sin(Maths.Convert.ToRadians(_pitch)),
|
||||
MathF.Sin(Maths.Convert.ToRadians(_yaw)) * MathF.Cos(Maths.Convert.ToRadians(_pitch))
|
||||
);
|
||||
_front = Vector3D.Normalize(front);
|
||||
|
||||
_right = Vector3D.Normalize(Vector3D.Cross(_front, _worldUp));
|
||||
_up = Vector3D.Normalize(Vector3D.Cross(_right, _front));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user