|
|
|
@ -15,7 +15,10 @@ using static gaemstone.Components.TransformComponents; |
|
|
|
|
namespace gaemstone.Client.Systems; |
|
|
|
|
|
|
|
|
|
[Module] |
|
|
|
|
[DependsOn(typeof(Windowing))] |
|
|
|
|
[DependsOn(typeof(gaemstone.Components.TransformComponents))] |
|
|
|
|
[DependsOn(typeof(gaemstone.Client.Components.CameraComponents))] |
|
|
|
|
[DependsOn(typeof(gaemstone.Client.Components.RenderingComponents))] |
|
|
|
|
[DependsOn(typeof(gaemstone.Client.Systems.Windowing))] |
|
|
|
|
public class Renderer |
|
|
|
|
: IModuleInitializer |
|
|
|
|
{ |
|
|
|
@ -51,28 +54,29 @@ public class Renderer |
|
|
|
|
_modelMatrixUniform = GL.GetUniformLocation(_program, "modelMatrix"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[System(typeof(SystemPhase.OnStore))] |
|
|
|
|
public void Render(Universe universe, GameWindow window, Canvas canvas) |
|
|
|
|
[System(typeof(SystemPhase.PreStore))] |
|
|
|
|
public void Clear(Canvas canvas) |
|
|
|
|
{ |
|
|
|
|
var GL = canvas.GL; |
|
|
|
|
GL.UseProgram(_program); |
|
|
|
|
GL.Viewport(default, canvas.Size); |
|
|
|
|
GL.ClearColor(new Vector4D<float>(0, 0, 0, 255)); |
|
|
|
|
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Filter.RunOnce(universe, (in GlobalTransform cameraTransform, |
|
|
|
|
in Camera camera, CameraViewport? viewport) => { |
|
|
|
|
[System(typeof(SystemPhase.OnStore))] |
|
|
|
|
public void Render(Universe universe, [Game] Canvas canvas, |
|
|
|
|
in GlobalTransform cameraTransform, in Camera camera, CameraViewport? viewport) |
|
|
|
|
{ |
|
|
|
|
var color = viewport?.ClearColor ?? new(0x4B, 0x00, 0x82, 255); |
|
|
|
|
var bounds = viewport?.Viewport ?? new(default, canvas.Size); |
|
|
|
|
|
|
|
|
|
var GL = canvas.GL; |
|
|
|
|
GL.Enable(EnableCap.ScissorTest); |
|
|
|
|
GL.Viewport(bounds); GL.Scissor(bounds.Origin.X, bounds.Origin.Y, (uint)bounds.Size.X, (uint)bounds.Size.Y); |
|
|
|
|
GL.ClearColor(color); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); |
|
|
|
|
GL.Disable(EnableCap.ScissorTest); |
|
|
|
|
|
|
|
|
|
// Get the camera's transform matrix and invert it. |
|
|
|
|
Matrix4X4.Invert<float>(cameraTransform, out var invertedTransform); |
|
|
|
|
|
|
|
|
|
// Create the camera's projection matrix. |
|
|
|
|
var cameraProjection = camera.IsOrthographic |
|
|
|
|
? Matrix4X4.CreateOrthographic( |
|
|
|
@ -83,6 +87,8 @@ public class Renderer |
|
|
|
|
(float)bounds.Size.X / bounds.Size.Y, // Aspect Ratio |
|
|
|
|
camera.NearPlane, camera.FarPlane); |
|
|
|
|
|
|
|
|
|
// Get the camera's transform matrix and invert it. |
|
|
|
|
Matrix4X4.Invert<float>(cameraTransform, out var invertedTransform); |
|
|
|
|
// Set the uniform to the combined transform and projection. |
|
|
|
|
var cameraMatrix = invertedTransform * cameraProjection; |
|
|
|
|
GL.UniformMatrix4(_cameraMatrixUniform, 1, false, in cameraMatrix.Row1.X); |
|
|
|
@ -117,11 +123,12 @@ public class Renderer |
|
|
|
|
if (texture.HasValue) GL.BindTexture(texture.Value.Target, 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
window.Handle.SwapBuffers(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[System(typeof(SystemPhase.PostFrame))] |
|
|
|
|
public static void SwapBuffers(GameWindow window) |
|
|
|
|
=> window.Handle.SwapBuffers(); |
|
|
|
|
|
|
|
|
|
[DebuggerStepThrough] |
|
|
|
|
private static void DebugCallback(GLEnum source, GLEnum _type, int id, GLEnum _severity, |
|
|
|
|
int length, nint _message, nint userParam) |
|
|
|
|