34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using Engine.Renderer.Camera;
|
|
using Engine.Util;
|
|
using OpenTK.Mathematics;
|
|
|
|
namespace Engine.Scene.Component;
|
|
|
|
public class PerspectiveCamera(
|
|
GameObject gameObject,
|
|
float aspectRatio,
|
|
float fieldOfView,
|
|
float nearPlane,
|
|
float farPlane)
|
|
: Component(gameObject), ICamera
|
|
{
|
|
public float AspectRatio { get; set; } = aspectRatio;
|
|
public float FieldOfView { get; set; } = fieldOfView;
|
|
public float NearPlane { get; set; } = nearPlane;
|
|
public float FarPlane { get; set; } = farPlane;
|
|
|
|
public Matrix4 View
|
|
{
|
|
get
|
|
{
|
|
var transformMatrix = GameObject.Transform.TransformMatrix;
|
|
var forward = new Vector4(0, 0, 1, 1).MulProject(transformMatrix);
|
|
var eye = new Vector4(0, 0, 0, 1).MulProject(transformMatrix);
|
|
var up = (new Vector4(0, 1, 0, 1).MulProject(transformMatrix) - eye).Normalized();
|
|
|
|
return Matrix4.LookAt(eye.Xyz, forward.Xyz, up.Xyz);
|
|
}
|
|
}
|
|
|
|
public Matrix4 Projection => Matrix4.CreatePerspectiveFieldOfView(FieldOfView, AspectRatio, NearPlane, FarPlane);
|
|
} |