mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Added PerspectiveCameraController
This commit is contained in:
@@ -0,0 +1,127 @@
|
|||||||
|
using System;
|
||||||
|
using GlitchyEngine.Events;
|
||||||
|
using GlitchyEngine.Math;
|
||||||
|
using GlitchyEngine.Renderer;
|
||||||
|
|
||||||
|
namespace GlitchyEngine
|
||||||
|
{
|
||||||
|
/// A simple controller for a perspective camera
|
||||||
|
public class PerspectiveCameraController
|
||||||
|
{
|
||||||
|
private PerspectiveCamera _camera ~ delete _;
|
||||||
|
private float _aspectRatio;
|
||||||
|
private float _fovY = Math.PI_f / 4;
|
||||||
|
|
||||||
|
private Vector3 _cameraPosition;
|
||||||
|
private Vector3 _cameraRotation;
|
||||||
|
|
||||||
|
private float _cameraTranslationSpeed = 1.0f;
|
||||||
|
private float _cameraRotationSpeedX = 0.001f;
|
||||||
|
private float _cameraRotationSpeedY = 0.001f;
|
||||||
|
|
||||||
|
public PerspectiveCamera Camera => _camera;
|
||||||
|
|
||||||
|
public Vector3 CameraPosition
|
||||||
|
{
|
||||||
|
get => _cameraPosition;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_cameraPosition = value;
|
||||||
|
|
||||||
|
UpdateCamera();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public this(float aspectRatio)
|
||||||
|
{
|
||||||
|
_aspectRatio = aspectRatio;
|
||||||
|
|
||||||
|
_camera = new PerspectiveCamera();
|
||||||
|
_camera.ProjectionType = .Infinite;
|
||||||
|
_camera.NearPlane = 0.01f;
|
||||||
|
|
||||||
|
UpdateCamera();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(GameTime gameTime)
|
||||||
|
{
|
||||||
|
if(Application.Get().Window.IsActive)
|
||||||
|
{
|
||||||
|
Vector3 movement = .();
|
||||||
|
|
||||||
|
if(Input.IsKeyPressed(Key.W))
|
||||||
|
{
|
||||||
|
movement.Z += 1;
|
||||||
|
}
|
||||||
|
if(Input.IsKeyPressed(Key.S))
|
||||||
|
{
|
||||||
|
movement.Z -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Input.IsKeyPressed(Key.A))
|
||||||
|
{
|
||||||
|
movement.X -= 1;
|
||||||
|
}
|
||||||
|
if(Input.IsKeyPressed(Key.D))
|
||||||
|
{
|
||||||
|
movement.X += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Input.IsKeyPressed(Key.Space))
|
||||||
|
{
|
||||||
|
movement.Y += 1;
|
||||||
|
}
|
||||||
|
if(Input.IsKeyPressed(Key.Control))
|
||||||
|
{
|
||||||
|
movement.Y -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(movement != .Zero)
|
||||||
|
movement.Normalize();
|
||||||
|
|
||||||
|
movement *= (float)(gameTime.FrameTime.TotalSeconds) * _cameraTranslationSpeed;
|
||||||
|
|
||||||
|
Vector4 delta = Vector4(movement, 1.0f) * _camera.View;
|
||||||
|
|
||||||
|
_cameraPosition += Vector3(delta.X, delta.Y, delta.Z);
|
||||||
|
//_cameraPosition += movement;
|
||||||
|
|
||||||
|
// Camera rotation
|
||||||
|
var mouseDelta = Input.GetMouseMovement();
|
||||||
|
|
||||||
|
float rotY = mouseDelta.X * _cameraRotationSpeedX;
|
||||||
|
float rotX = mouseDelta.Y * _cameraRotationSpeedY;
|
||||||
|
|
||||||
|
_cameraRotation.X += rotX;
|
||||||
|
_cameraRotation.Y += rotY;
|
||||||
|
|
||||||
|
UpdateCamera();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnEvent(Event e)
|
||||||
|
{
|
||||||
|
EventDispatcher dispatcher = EventDispatcher(e);
|
||||||
|
dispatcher.Dispatch<WindowResizeEvent>(scope => OnWindowResized);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool OnWindowResized(WindowResizeEvent e)
|
||||||
|
{
|
||||||
|
_aspectRatio = (float)e.Width / (float)e.Height;
|
||||||
|
|
||||||
|
UpdateCamera();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateCamera()
|
||||||
|
{
|
||||||
|
_camera.AspectRatio = _aspectRatio;
|
||||||
|
_camera.FovY = _fovY;
|
||||||
|
_camera.Rotation = _cameraRotation;
|
||||||
|
_camera.Position = _cameraPosition;
|
||||||
|
|
||||||
|
_camera.Update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -78,7 +78,8 @@ namespace GlitchyEngine.Renderer
|
|||||||
|
|
||||||
protected override void UpdateProjection()
|
protected override void UpdateProjection()
|
||||||
{
|
{
|
||||||
Log.EngineLogger.Assert(_nearPlane < _farPlane, "The near plane must be smaller than the far plane.");
|
if(_projectionType == .Limited || _projectionType == .LimitedReversed)
|
||||||
|
Log.EngineLogger.AssertDebug(_nearPlane < _farPlane, "The near plane must be smaller than the far plane.");
|
||||||
|
|
||||||
switch(_projectionType)
|
switch(_projectionType)
|
||||||
{
|
{
|
||||||
@@ -91,7 +92,7 @@ namespace GlitchyEngine.Renderer
|
|||||||
case .InfiniteReversed:
|
case .InfiniteReversed:
|
||||||
_projection = Matrix.ReversedInfinitePerspectiveProjection(_fovY, _aspect, _nearPlane);
|
_projection = Matrix.ReversedInfinitePerspectiveProjection(_fovY, _aspect, _nearPlane);
|
||||||
default:
|
default:
|
||||||
Log.EngineLogger.Assert(false, "Unknown projection type.");
|
Log.EngineLogger.AssertDebug(false, "Unknown projection type.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,8 @@ namespace Sandbox
|
|||||||
|
|
||||||
EcsWorld _world = new EcsWorld() ~ delete _;
|
EcsWorld _world = new EcsWorld() ~ delete _;
|
||||||
|
|
||||||
OrthographicCameraController _cameraController ~ delete _;
|
// OrthographicCameraController _cameraController ~ delete _;
|
||||||
|
PerspectiveCameraController _cameraController ~ delete _;
|
||||||
|
|
||||||
private Vector3 CircleCoord(float angle)
|
private Vector3 CircleCoord(float angle)
|
||||||
{
|
{
|
||||||
@@ -183,8 +184,12 @@ namespace Sandbox
|
|||||||
|
|
||||||
InitEcs();
|
InitEcs();
|
||||||
|
|
||||||
_cameraController = new OrthographicCameraController(Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
|
//_cameraController = new OrthographicCameraController(Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
|
||||||
|
// Application.Get().Window.Context.SwapChain.BackbufferViewport.Height);
|
||||||
|
|
||||||
|
_cameraController = new .(Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
|
||||||
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height);
|
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height);
|
||||||
|
_cameraController.CameraPosition = .(0, 0, -5);
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitEcs()
|
void InitEcs()
|
||||||
|
|||||||
Reference in New Issue
Block a user