Skip to content

Commit

Permalink
Started camera projection implemetation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Jul 10, 2024
1 parent b3eb798 commit 735f943
Show file tree
Hide file tree
Showing 22 changed files with 378 additions and 117 deletions.
4 changes: 3 additions & 1 deletion .idea/.idea.Hypercube/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Hypercube.Client/Dependencies.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Hypercube.Client.Graphics;
using Hypercube.Client.Graphics.Rendering;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Client.Input.Handler;
using Hypercube.Client.Input.Manager;
using Hypercube.Client.Runtimes;
Expand Down Expand Up @@ -34,6 +35,9 @@ public static void Register(DependenciesContainer rootContainer)
// Texturing
rootContainer.Register<ITextureManager, TextureManager>();

// Camera
rootContainer.Register<ICameraManager, CameraManager>();

// Rendering
rootContainer.Register<IRenderer, Renderer>();

Expand Down
8 changes: 6 additions & 2 deletions Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Hypercube.Client.Graphics.Shading;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Shared.Math;
using Hypercube.Shared.Math.Box;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Runtimes.Loop.Event;
using OpenToolkit.Graphics.OpenGL4;

Expand Down Expand Up @@ -35,6 +35,8 @@ private void OnLoad()
_baseTexture = _textureManager.CreateHandler("Resources/Textures/opengl_logo.png");
_baseTexture.Bind();

//_cameraManager.SetMainCamera(_cameraManager.CreateCamera2D(MainWindow.Size));

_vbo = new BufferObject(BufferTarget.ArrayBuffer);
_ebo = new BufferObject(BufferTarget.ElementArrayBuffer);
_vao = new ArrayObject();
Expand All @@ -59,7 +61,7 @@ private void OnLoad()
private void OnFrameUpdate(UpdateFrameEvent args)
{
#if DEBUG
_windowManager.WindowSetTitle(MainWindow, $"FPS: {_timing.Fps} | RealTime: {_timing.RealTime}");
_windowManager.WindowSetTitle(MainWindow, $"FPS: {_timing.Fps} | RealTime: {_timing.RealTime} | cPos: {_cameraManager.MainCamera?.Position ?? null}");
#endif
_windowManager.PollEvents();
}
Expand All @@ -86,6 +88,8 @@ private void OnFrameRender(RenderFrameEvent args)
BatchUpdate();

_baseShader.Use();
_baseShader.SetUniform(_baseShader.GetUniformLocation("model"), Matrix4X4.Identity);
_baseShader.SetUniform(_baseShader.GetUniformLocation("projection"), _cameraManager.Projection);

_vao.Bind();
GL.DrawElements(BeginMode.Triangles, (int) _batchIndexIndex, DrawElementsType.UnsignedInt, 0);
Expand Down
5 changes: 3 additions & 2 deletions Hypercube.Client/Graphics/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Frozen;
using Hypercube.Client.Graphics.Event;
using Hypercube.Client.Graphics.OpenGL;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Client.Graphics.Windows.Manager;
using Hypercube.Shared.Dependency;
Expand All @@ -21,7 +21,8 @@ public sealed partial class Renderer : IRenderer, IPostInject
[Dependency] private readonly IEventBus _eventBus = default!;
[Dependency] private readonly ITextureManager _textureManager = default!;
[Dependency] private readonly ITiming _timing = default!;

[Dependency] private readonly ICameraManager _cameraManager = default!;

private readonly ILogger _logger = LoggingManager.GetLogger("renderer");
private readonly ILogger _loggerOpenGL = LoggingManager.GetLogger("open_gl")!;

Expand Down
7 changes: 6 additions & 1 deletion Hypercube.Client/Graphics/Shading/IShader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
namespace Hypercube.Client.Graphics.Shading;
using Hypercube.Shared.Math.Matrix;

namespace Hypercube.Client.Graphics.Shading;

public interface IShader
{
void Use();
int GetUniformLocation(string name);
void SetUniform(string name, int value);
void SetUniform(string name, Matrix4X4 value, bool transpose = false);
void SetUniform(int index, Matrix4X4 value, bool transpose = false);
}
26 changes: 23 additions & 3 deletions Hypercube.Client/Graphics/Shading/Shader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Math.Vector;
using OpenToolkit.Graphics.OpenGL4;
using Vector2 = Hypercube.Shared.Math.Vector.Vector2;

namespace Hypercube.Client.Graphics.Shading;

Expand Down Expand Up @@ -51,12 +51,17 @@ public void Use()
GL.UseProgram(_handle);
}

public int GetUniformLocation(string name)
{
return GL.GetUniformLocation(_handle, name);
}

public void SetUniform(string name, int value)
{
GL.UseProgram(_handle);
GL.Uniform1(_uniformLocations[name], value);
}

public void SetUniform(string name, Vector2Int value)
{
GL.UseProgram(_handle);
Expand All @@ -74,6 +79,21 @@ public void SetUniform(string name, Vector2 value)
GL.UseProgram(_handle);
GL.Uniform2(_uniformLocations[name], value.X, value.Y);
}

public void SetUniform(string name, Matrix4X4 value, bool transpose = false)
{
SetUniform(GL.GetUniformLocation(_handle, name), value, transpose);
}

public void SetUniform(int index, Matrix4X4 value, bool transpose = false)
{
unsafe
{
var matrix = transpose ? Matrix4X4.Transpose(value) : new Matrix4X4(value);
GL.UseProgram(_handle);
GL.UniformMatrix4(index, 1, false, (float*) &matrix);
}
}

private int CreateShader(string path, ShaderType type)
{
Expand Down
37 changes: 28 additions & 9 deletions Hypercube.Client/Graphics/Viewports/Camera2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@ namespace Hypercube.Client.Graphics.Viewports;

public class Camera2D : ICamera
{
public float ZFar { get; private set; }
public float ZNear { get; private set; }
public Vector2 Position { get; private set; }
public float Zoom { get; private set; }
public Vector3 Position { get; private set; }

private readonly float _zFar;
private readonly float _zNear;
private Vector2Int Size { get; set; }
private float Zoom { get; set; } = 1f;

private Vector2 HalfSize => Size / 2f;
public Matrix4X4 Projection { get; private set; }

public Matrix4X4 Projection;
public Camera2D(Vector2Int size, Vector2 position, float zNear, float zFar)
{
Size = size;
Position = new Vector3(position);
_zNear = zNear;
_zFar = zFar;

UpdateProjection();
}

public void SetPosition(Vector2 position)
public void SetSize(Vector2Int size)
{
Size = size;
UpdateProjection();
}

public void SetPosition(Vector3 position)
{
Position = position;
UpdateProjection();
Expand All @@ -27,9 +46,9 @@ public void SetZoom(float zoom)

private void UpdateProjection()
{
var size = new Box2();

var projection = Matrix4X4.CreateOrthographic(size, ZNear, ZFar);
var position = (Vector2)Position;
var box2 = new Box2(position - HalfSize, position + HalfSize);
var projection = Matrix4X4.CreateOrthographic(box2, _zNear, _zFar);
var scale = Matrix4X4.CreateScale(Zoom);

Projection = projection * scale;
Expand Down
50 changes: 49 additions & 1 deletion Hypercube.Client/Graphics/Viewports/CameraManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
using Hypercube.Shared.Dependency;
using Hypercube.Client.Input;
using Hypercube.Client.Input.Handler;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Runtimes.Loop.Event;

namespace Hypercube.Client.Graphics.Viewports;

public class CameraManager : ICameraManager, IPostInject
{
[Dependency] private readonly IEventBus _eventBus = default!;
[Dependency] private readonly IInputHandler _inputHandler = default!;

public ICamera? MainCamera { get; private set; }

public Matrix4X4 Projection => MainCamera?.Projection ?? Matrix4X4.Identity;

public void PostInject()
{
Expand All @@ -15,6 +24,45 @@ public void PostInject()

private void OnUpdate(UpdateFrameEvent args)
{
if (MainCamera is null)
return;

// Debug camera controls
var position = MainCamera.Position;

if (_inputHandler.IsKeyDown(Key.W))
position += Vector3.Forward;

if (_inputHandler.IsKeyDown(Key.S))
position -= Vector3.Back;

if (_inputHandler.IsKeyDown(Key.A))
position -= Vector3.Forward.Cross(Vector3.Up).Normalized;

if (_inputHandler.IsKeyDown(Key.D))
position += Vector3.Forward.Cross(Vector3.Up).Normalized;

if (_inputHandler.IsKeyDown(Key.Space))
position += Vector3.Up;

if (_inputHandler.IsKeyDown(Key.LeftShift))
position -= Vector3.Up;

MainCamera.SetPosition(position);
}

public void SetMainCamera(ICamera camera)
{
MainCamera = camera;
}

public ICamera CreateCamera2D(Vector2Int size)
{
return CreateCamera2D(size, Vector2.Zero);
}

public ICamera CreateCamera2D(Vector2Int size, Vector2 position, float zNear = 0.01f, float zFar = 100f)
{
return new Camera2D(size, position, zNear, zFar);
}
}
9 changes: 7 additions & 2 deletions Hypercube.Client/Graphics/Viewports/ICamera.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
namespace Hypercube.Client.Graphics.Viewports;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Math.Vector;

namespace Hypercube.Client.Graphics.Viewports;

public interface ICamera
{

Matrix4X4 Projection { get; }
Vector3 Position { get; }
void SetPosition(Vector3 position);
}
10 changes: 8 additions & 2 deletions Hypercube.Client/Graphics/Viewports/ICameraManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
namespace Hypercube.Client.Graphics.Viewports;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Math.Vector;

namespace Hypercube.Client.Graphics.Viewports;

public interface ICameraManager
{

ICamera? MainCamera { get; }
Matrix4X4 Projection { get; }
void SetMainCamera(ICamera camera);
ICamera CreateCamera2D(Vector2Int size);
}
1 change: 1 addition & 0 deletions Hypercube.Client/Input/Handler/IInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface IInputHandler

// TODO: Create Analyzer to allow access only for IWindowManager implementation
void SendKeyState(KeyStateChangedArgs changedArgs);
bool IsKeyDown(Key key);
}
14 changes: 11 additions & 3 deletions Hypercube.Client/Input/Handler/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public sealed class InputHandler : IInputHandler
{
public event Action<KeyStateChangedArgs>? KeyUp;
public event Action<KeyStateChangedArgs>? KeyDown;


private readonly HashSet<Key> _keysDown = new();
private readonly ILogger _logger = LoggingManager.GetLogger("input_handler");

public void SendKeyState(KeyStateChangedArgs changedArgs)
Expand All @@ -23,10 +24,17 @@ public void SendKeyState(KeyStateChangedArgs changedArgs)

if (changedArgs.Pressed)
{
KeyUp?.Invoke(changedArgs);
KeyDown?.Invoke(changedArgs);
_keysDown.Add(changedArgs.Key);
return;
}

KeyDown?.Invoke(changedArgs);
_keysDown.Remove(changedArgs.Key);
KeyUp?.Invoke(changedArgs);
}

public bool IsKeyDown(Key key)
{
return _keysDown.Contains(key);
}
}
Loading

0 comments on commit 735f943

Please sign in to comment.