Skip to content

Commit

Permalink
Merge pull request #43 from technologists-team/update-render
Browse files Browse the repository at this point in the history
Update render
  • Loading branch information
Tornado-Technology authored Aug 10, 2024
2 parents da6637d + 06bd423 commit fe195a3
Show file tree
Hide file tree
Showing 48 changed files with 1,225 additions and 337 deletions.
3 changes: 2 additions & 1 deletion Hypercube.Client/Dependencies.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Hypercube.Client.Audio;
using Hypercube.Client.Audio.Loading;
using Hypercube.Client.Audio.Realisations.OpenAL;
using Hypercube.Client.Graphics.ImGui;
using Hypercube.Client.Graphics.Realisation.OpenGL.Rendering;
using Hypercube.Client.Graphics.Realisation.OpenGL.Texturing;
using Hypercube.Client.Graphics.Rendering;
Expand Down Expand Up @@ -40,9 +41,9 @@ public static void Register(DependenciesContainer rootContainer)
// Camera
rootContainer.Register<ICameraManager, CameraManager>();


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

// Runtime
rootContainer.Register<IRuntimeLoop, RuntimeLoop>();
Expand Down
5 changes: 3 additions & 2 deletions Hypercube.Client/Graphics/Events/MainWindowClosedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Hypercube.Client.Graphics.Windows;
using Hypercube.Graphics.Windowing;
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Graphics.Events;

public readonly struct MainWindowClosedEvent(WindowRegistration registration) : IEventArgs
public readonly struct MainWindowClosedEvent(WindowHandle handle) : IEventArgs
{
public readonly WindowRegistration Registration = registration;
public readonly WindowHandle Handle = handle;
}
5 changes: 3 additions & 2 deletions Hypercube.Client/Graphics/Events/WindowClosed.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Hypercube.Client.Graphics.Windows;
using Hypercube.Graphics.Windowing;
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Graphics.Events;

public readonly struct WindowClosedEvent(WindowRegistration registration) : IEventArgs
public readonly struct WindowClosedEvent(WindowHandle handle) : IEventArgs
{
public readonly WindowRegistration Registration = registration;
public readonly WindowHandle Handle = handle;
}
3 changes: 2 additions & 1 deletion Hypercube.Client/Graphics/Events/WindowFocusChangedEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Hypercube.Client.Graphics.Windows;
using Hypercube.Graphics.Windowing;
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Graphics.Events;

public readonly record struct WindowFocusChangedEvent(WindowRegistration Registration, bool Focused) : IEventArgs;
public readonly record struct WindowFocusChangedEvent(WindowHandle Handle, bool Focused) : IEventArgs;
5 changes: 5 additions & 0 deletions Hypercube.Client/Graphics/ImGui/Events/ImGuiRenderEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Hypercube.Shared.EventBus.Events;

namespace Hypercube.Client.Graphics.ImGui.Events;

public readonly record struct ImGuiRenderEvent(IImGui Instance) : IEventArgs;
9 changes: 9 additions & 0 deletions Hypercube.Client/Graphics/ImGui/IImGui.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Hypercube.Client.Graphics.ImGui;

public interface IImGui
{
void Begin(string name);
void Text(string label);
bool Button(string label);
void End();
}
70 changes: 70 additions & 0 deletions Hypercube.Client/Graphics/ImGui/ImGui.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Hypercube.Client.Graphics.Events;
using Hypercube.Client.Graphics.ImGui.Events;
using Hypercube.Client.Graphics.Rendering;
using Hypercube.ImGui;
using Hypercube.Shared.Dependency;
using Hypercube.Shared.EventBus;
using Hypercube.Shared.Logging;
using Hypercube.Shared.Runtimes.Loop.Event;
using JetBrains.Annotations;

namespace Hypercube.Client.Graphics.ImGui;

[PublicAPI]
public sealed class ImGui : IImGui, IEventSubscriber, IPostInject
{
[Dependency] private readonly IEventBus _eventBus = default!;
[Dependency] private readonly IRenderer _renderer = default!;

private readonly Logger _logger = LoggingManager.GetLogger("im_gui");

private IImGuiController _controller = default!;

public void PostInject()
{
_eventBus.Subscribe<GraphicsLibraryInitializedEvent>(this, OnGraphicsInitialized);
_eventBus.Subscribe<UpdateFrameEvent>(this, OnUpdateFrame);
_eventBus.Subscribe<RenderDrawingEvent>(this, OnRenderDrawing);
}

private void OnGraphicsInitialized(ref GraphicsLibraryInitializedEvent args)
{
_controller = ImGuiFactory.Create(_renderer.MainWindow);
_controller.OnErrorHandled += message => _logger.Error(message);

_controller.Initialize();
}

private void OnUpdateFrame(ref UpdateFrameEvent args)
{
_controller.Update(args.DeltaSeconds);
}

private void OnRenderDrawing(ref RenderDrawingEvent args)
{
var ev = new ImGuiRenderEvent(this);
_eventBus.Raise(ev);

_controller.Render();
}

public void Begin(string name)
{
_controller.Begin(name);
}

public void Text(string label)
{
_controller.Text(label);
}

public bool Button(string label)
{
return _controller.Button(label);
}

public void End()
{
_controller.End();
}
}
45 changes: 0 additions & 45 deletions Hypercube.Client/Graphics/Realisation/OpenGL/ArrayObject.cs

This file was deleted.

54 changes: 0 additions & 54 deletions Hypercube.Client/Graphics/Realisation/OpenGL/BufferObject.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Hypercube.Client.Graphics.Events;
using Hypercube.Shared.Logging;
using OpenTK.Windowing.GraphicsLibraryFramework;
Expand All @@ -16,7 +17,7 @@ public sealed partial class Renderer
/// </summary>
private DebugProc? _debugProc;

private void InitOpenGL()
private unsafe void InitOpenGL()
{
GL.LoadBindings(_bindingsContext);

Expand All @@ -27,43 +28,42 @@ private void InitOpenGL()
var version = GL.GetString(StringName.Version);
var shading = GL.GetString(StringName.ShadingLanguageVersion);

GLFW.SwapInterval(SwapInterval);

_loggerOpenGL.EngineInfo($"Vendor: {vendor}");
_loggerOpenGL.EngineInfo($"Renderer: {renderer}");
_loggerOpenGL.EngineInfo($"Version: {version}, Shading: {shading}");

GLFW.SwapInterval(SwapInterval);
_loggerOpenGL.EngineInfo($"Thread: {Thread.CurrentThread.Name ?? "unnamed"} ({Environment.CurrentManagedThreadId})");
_loggerOpenGL.EngineInfo($"Swap interval: {SwapInterval}");

_debugProc = DebugMessageCallback;
GL.DebugMessageCallback(_debugProc, IntPtr.Zero);

GL.DebugMessageCallback(_debugProc, nint.Zero);

GL.Enable(EnableCap.Blend);
GL.Enable(EnableCap.DebugOutput);
GL.Enable(EnableCap.DebugOutputSynchronous);

GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.ClearColor(0, 0, 0, 0);

_loggerOpenGL.EngineInfo("Initialized");
_eventBus.Raise(new GraphicsLibraryInitializedEvent());
}

private void DebugMessageCallback(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, IntPtr messagePointer, IntPtr userparam)
private unsafe void DebugMessageCallback(DebugSource source, DebugType type, int id, DebugSeverity severity, int length, nint messagePointer, nint @params)
{
// In order to access the string pointed to by pMessage, you can use Marshal
// class to copy its contents to a C# string without unsafe code. You can
// also use the new function Marshal.PtrToStringUTF8 since .NET Core 1.1.
string message = Marshal.PtrToStringAnsi(messagePointer, length);

// The rest of the function is up to you to implement, however a debug output
// is always useful.
var message = Marshal.PtrToStringAnsi(messagePointer, length);
var logger = LoggingManager.GetLogger("open_gl_debug");
logger.EngineInfo($"{severity} source={source} type={id} {message}");

// Potentially, you may want to throw from the function for certain severity
// messages.
var loggingMessage = $"[{type}] [{severity}] [{source}] {message} ({id})";

if (type == DebugType.DebugTypeError)
{
logger.Fatal(loggingMessage);
throw new Exception(message);
}

logger.EngineInfo(loggingMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Hypercube.Client.Graphics.Drawing;
using Hypercube.Client.Graphics.Events;
using Hypercube.Client.Graphics.Shaders;
using Hypercube.Client.Graphics.Windows;
using Hypercube.Client.Resources.Caching;
using Hypercube.Graphics.Shaders;
using Hypercube.Graphics.Windowing;
using Hypercube.Math;
using Hypercube.Math.Matrices;
using Hypercube.OpenGL.Objects;
using Hypercube.Shared.Runtimes.Loop.Event;
using OpenToolkit.Graphics.OpenGL4;

Expand Down Expand Up @@ -88,7 +90,7 @@ private void OnFrameRender(ref RenderFrameEvent args)
Render(MainWindow);
}

public void Render(WindowRegistration window)
public void Render(WindowHandle window)
{
Clear();

Expand All @@ -111,6 +113,9 @@ public void Render(WindowRegistration window)
}

_vao.Unbind();
_vbo.Unbind();
_ebo.Unbind();

_windowManager.WindowSwapBuffers(window);
}

Expand Down
Loading

0 comments on commit fe195a3

Please sign in to comment.