Skip to content

Commit

Permalink
Updated matrix usage in rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Jul 11, 2024
1 parent e3b5517 commit 5e74241
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 22 deletions.
19 changes: 10 additions & 9 deletions Hypercube.Client/Graphics/Rendering/Renderer.Render.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Numerics;
using Hypercube.Client.Graphics.Shading;
using Hypercube.Client.Graphics.Shading;
using Hypercube.Client.Graphics.Texturing;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Shared.Math;
using Hypercube.Shared.Math.Box;
using Hypercube.Shared.Math.Matrix;
using Hypercube.Shared.Math.Vector;
using Hypercube.Shared.Runtimes.Loop.Event;
using OpenToolkit.Graphics.OpenGL4;

Expand Down Expand Up @@ -62,7 +62,7 @@ private void OnLoad()
private void OnFrameUpdate(UpdateFrameEvent args)
{
#if DEBUG
_windowManager.WindowSetTitle(MainWindow, $"FPS: {_timing.Fps} | RealTime: {_timing.RealTime} | cPos: {_cameraManager.MainCamera?.Position ?? null}");
_windowManager.WindowSetTitle(MainWindow, $"FPS: {_timing.Fps} | RealTime: {_timing.RealTime} | cPos: {_cameraManager.MainCamera?.Position ?? null} | cRot: {_cameraManager.MainCamera?.Rotation ?? null}");
#endif
_windowManager.PollEvents();
_cameraManager.UpdateInput(_cameraManager.MainCamera, args.DeltaSeconds);
Expand All @@ -82,17 +82,18 @@ private void OnFrameRender(RenderFrameEvent args)
var colorG = new Color(0f, sin, 0f);
var colorB = new Color(0f, 0f, sin);

DrawTexture(_baseTexture, _baseTexture.Texture.QuadCrateTranslated(-Vector2.UnitY), new Box2(0.0f, 1.0f, 1.0f, 0.0f), Color.White);
DrawTexture(_baseTexture, _baseTexture.Texture.QuadCrateTranslated(Vector2.UnitX), new Box2(0.0f, 1.0f, 1.0f, 0.0f), colorR);
DrawTexture(_baseTexture, _baseTexture.Texture.QuadCrateTranslated(-Vector2.UnitX), new Box2(0.0f, 1.0f, 1.0f, 0.0f), colorG);
DrawTexture(_baseTexture, _baseTexture.Texture.QuadCrateTranslated(Vector2.UnitY), new Box2(0.0f, 1.0f, 1.0f, 0.0f), colorB);
DrawTexture(_baseTexture, _baseTexture.Texture.QuadCrateTranslated(-Vector2.UnitY * 60f), new Box2(0.0f, 1.0f, 1.0f, 0.0f), Color.White);
DrawTexture(_baseTexture, _baseTexture.Texture.QuadCrateTranslated(Vector2.UnitX * 60f), new Box2(0.0f, 1.0f, 1.0f, 0.0f), colorR);
DrawTexture(_baseTexture, _baseTexture.Texture.QuadCrateTranslated(-Vector2.UnitX * 60f), new Box2(0.0f, 1.0f, 1.0f, 0.0f), colorG);
DrawTexture(_baseTexture, _baseTexture.Texture.QuadCrateTranslated(Vector2.UnitY * 60f), new Box2(0.0f, 1.0f, 1.0f, 0.0f), colorB);

BatchUpdate();


var model = Matrix4X4.CreateTranslation(Vector2.Zero) * Matrix4X4.CreateRotationZ(0) * Matrix4X4.CreateScale(Vector2.One);
var view = Matrix4X4.CreateTranslation(0.0f, 0.0f, -3.0f);

_baseShader.Use();
_baseShader.SetUniform("model", Matrix4X4.CreateScale(1f));
_baseShader.SetUniform("model", model);
_baseShader.SetUniform("view", view);
_baseShader.SetUniform("projection", _cameraManager.Projection);

Expand Down
15 changes: 12 additions & 3 deletions Hypercube.Client/Graphics/Viewports/Camera2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Hypercube.Client.Graphics.Viewports;
public class Camera2D : ICamera
{
public Vector3 Position { get; private set; }
public Vector3 Rotation { get; private set; }

private readonly float _zFar;
private readonly float _zNear;
Expand Down Expand Up @@ -43,6 +44,12 @@ public void SetPosition(Vector3 position)
UpdateProjection();
}

public void SetRotation(Vector3 rotation)
{
Rotation = Rotation.WithZ(rotation.Z);
UpdateProjection();
}

public void SetZoom(float zoom)
{
Zoom = zoom;
Expand All @@ -51,10 +58,12 @@ public void SetZoom(float zoom)

private void UpdateProjection()
{
var projection = Matrix4X4.CreateOrthographic(HalfSize, _zNear, _zFar);
var scale = Matrix4X4.CreateScale(Zoom);
var projection = Matrix4X4.CreateOrthographic(Size, _zNear, _zFar);

var translate = Matrix4X4.CreateTranslation(Position);
var rotation = Matrix4X4.CreateRotationZ(Rotation.Z);
var scale = Matrix4X4.CreateScale(Zoom);

Projection = projection * scale * translate;
Projection = projection * translate * rotation * scale;
}
}
20 changes: 11 additions & 9 deletions Hypercube.Client/Graphics/Viewports/CameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ public void UpdateInput(ICamera? camera, float delta)

// Debug camera controls
var position = camera.Position;
var rotation = camera.Rotation;

if (_inputHandler.IsKeyDown(Key.W))
position += Vector3.Forward * delta;
position -= Vector3.UnitY * delta;

if (_inputHandler.IsKeyDown(Key.S))
position -= Vector3.Forward * delta;
position += Vector3.UnitY * delta;

if (_inputHandler.IsKeyDown(Key.A))
position -= Vector3.Right * delta;
position -= Vector3.UnitX * delta;

if (_inputHandler.IsKeyDown(Key.D))
position += Vector3.Right * delta;

if (_inputHandler.IsKeyDown(Key.Space))
position += Vector3.Up * delta;
position += Vector3.UnitX * delta;
if (_inputHandler.IsKeyDown(Key.Q))
rotation -= Vector3.UnitZ * delta;

if (_inputHandler.IsKeyDown(Key.LeftShift))
position -= Vector3.Up * delta;
if (_inputHandler.IsKeyDown(Key.E))
rotation += Vector3.UnitZ * delta;

camera.SetPosition(position);
camera.SetRotation(rotation);
}

public void SetMainCamera(ICamera camera)
Expand Down
2 changes: 2 additions & 0 deletions Hypercube.Client/Graphics/Viewports/ICamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public interface ICamera
Matrix4X4 Projection { get; }

Vector3 Position { get; }
Vector3 Rotation { get; }

void SetPosition(Vector2 position);
void SetPosition(Vector3 position);
void SetRotation(Vector3 rotation);
}
10 changes: 9 additions & 1 deletion Hypercube.Shared.Math/Angle.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
namespace Hypercube.Shared.Math;
using System.Runtime.CompilerServices;

namespace Hypercube.Shared.Math;

public readonly struct Angle(double theta)
{
public static readonly Angle Zero = new(0);

public readonly double Theta = theta;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator float(Angle angle)
{
return (float)angle.Theta;
}
}
112 changes: 112 additions & 0 deletions Hypercube.Shared.Math/Matrix/Matrix4X4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,118 @@ public static Matrix4X4 CreateScale(float x, float y, float z)
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix4X4 CreateRotation(Vector3 direction, float angle)
{
var cos = MathF.Cos(-angle);
var sin = MathF.Sin(-angle);
var t = 1.0f - cos;

direction = direction.Normalized;

return new Matrix4X4(
t * direction.X * direction.X + cos,
t * direction.X * direction.Y - sin * direction.Z,
t * direction.X * direction.Z + sin * direction.Y,
0,
t * direction.X * direction.Y + sin * direction.Z,
t * direction.Y * direction.Y + cos,
t * direction.Y * direction.Z - sin * direction.X,
0,
t * direction.X * direction.Z - sin * direction.Y,
t * direction.Y * direction.Z + sin * direction.X,
t * direction.Z * direction.Z + cos,
0,
0,
0,
0,
1
);
}

/// <summary>
/// Creating rotation axis X matrix
/// <code>
/// 1 | 0 | 0 | 0
/// 0 | cos | sin | 0
/// 0 | -sin | cos | 0
/// 0 | 0 | 0 | 1
/// </code>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix4X4 CreateRotationX(float angle)
{
var cos = MathF.Cos(angle);
var sin = MathF.Sin(angle);

return new Matrix4X4(
Vector4.UnitX,
new Vector4(0, cos, sin, 0),
new Vector4(0, -sin, cos, 0),
Vector4.UnitW
);
}

/// <summary>
/// Creating rotation axis Y matrix
/// <code>
/// cos | 0 | -sin | 0
/// 0 | 1 | 0 | 0
/// sin | 0 | cos | 0
/// 0 | 0 | 0 | 1
/// </code>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix4X4 CreateRotationY(float angle)
{
var cos = MathF.Cos(angle);
var sin = MathF.Sin(angle);

return new Matrix4X4(
new Vector4(cos, 0, -sin, 0),
Vector4.UnitY,
new Vector4(sin, 0, cos, 0),
Vector4.UnitW
);
}

/// <summary>
/// Creating rotation axis Z matrix
/// <code>
/// cos | sin | 0 | 0
/// -sin | cos | 0 | 0
/// 0 | 0 | 1 | 0
/// 0 | 0 | 0 | 1
/// </code>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Matrix4X4 CreateRotationZ(float angle)
{
var cos = MathF.Cos(angle);
var sin = MathF.Sin(angle);

return new Matrix4X4(
new Vector4(cos, sin, 0, 0),
new Vector4(-sin, cos, 0, 0),
Vector4.UnitZ,
Vector4.UnitW
);
}

/// <summary>
/// Creating translate matrix
/// <code>
/// 1 | 0 | 0 | v
/// 0 | 1 | 0 | v
/// 0 | 0 | 1 | v
/// 0 | 0 | 0 | 1
/// </code>
/// </summary>
public static Matrix4X4 CreateTranslation(float value)
{
return CreateTranslation(value, value, value);
}

/// <summary>
/// Creating translate matrix
/// <code>
Expand Down

0 comments on commit 5e74241

Please sign in to comment.