72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
using OpenTK.Mathematics;
|
|
|
|
namespace Voxel
|
|
{
|
|
static class Camera
|
|
{
|
|
public static Vector3 Position = new Vector3(-8, 16, -8);
|
|
|
|
public static float Pitch = -22.5f;
|
|
public static float Yaw = 0f;
|
|
public static float FOV = 60f;
|
|
public static float TargetFOV = FOV;
|
|
public static float FOVLerpSpeed = 10f;
|
|
public static float Speed = 5f;
|
|
public static float ShiftSpeed = 20f;
|
|
|
|
private static int _width;
|
|
private static int _height;
|
|
|
|
public static Matrix4 view =>
|
|
Matrix4.LookAt(Position, Position + Front, Vector3.UnitY);
|
|
|
|
public static Matrix4 projection;
|
|
|
|
public static Vector3 Front
|
|
{
|
|
get
|
|
{
|
|
float yawOffset = Yaw - 90f;
|
|
Vector3 front;
|
|
front.X = MathF.Cos(MathHelper.DegreesToRadians(yawOffset)) * MathF.Cos(MathHelper.DegreesToRadians(Pitch));
|
|
front.Y = MathF.Sin(MathHelper.DegreesToRadians(Pitch));
|
|
front.Z = MathF.Sin(MathHelper.DegreesToRadians(yawOffset)) * MathF.Cos(MathHelper.DegreesToRadians(Pitch));
|
|
return front.Normalized();
|
|
}
|
|
}
|
|
|
|
public static void UpdateMouse(Vector2 delta)
|
|
{
|
|
float sensitivity = 0.1f;
|
|
Yaw += delta.X * sensitivity;
|
|
Pitch -= delta.Y * sensitivity;
|
|
|
|
Pitch = MathHelper.Clamp(Pitch, -89f, 89f);
|
|
}
|
|
|
|
public static void UpdateProjection()
|
|
{
|
|
float fov = MathHelper.DegreesToRadians(FOV);
|
|
float aspectRatio = _width / (float)_height;
|
|
float near = 0.1f;
|
|
float far = 1000f;
|
|
|
|
projection = Matrix4.CreatePerspectiveFieldOfView(fov, aspectRatio, near, far);
|
|
}
|
|
|
|
public static void UpdateSize(int width, int height)
|
|
{
|
|
_width = width;
|
|
_height = height;
|
|
UpdateProjection();
|
|
}
|
|
|
|
public static void UpdateFOV(float deltaTime, float alpha)
|
|
{
|
|
float currentFOV = MathHelper.Lerp(FOV, TargetFOV, FOVLerpSpeed * deltaTime);
|
|
Camera.FOV = currentFOV;
|
|
Camera.UpdateProjection();
|
|
}
|
|
}
|
|
}
|