Split Render system into 3

wip/source-generators
copygirl 2 years ago
parent 8177ea24ef
commit bb0ab53c06
  1. 29
      src/gaemstone.Client/Systems/Renderer.cs

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

Loading…
Cancel
Save