Skip to content

Commit

Permalink
Fixed ImGui render
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 10, 2024
1 parent 33200df commit 88b8402
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Hypercube.Client/Graphics/ImGui/ImGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void PostInject()
{
_eventBus.Subscribe<GraphicsLibraryInitializedEvent>(this, OnGraphicsInitialized);
_eventBus.Subscribe<UpdateFrameEvent>(this, OnUpdateFrame);
_eventBus.Subscribe<RenderFrameEvent>(this, OnRenderFrame);
_eventBus.Subscribe<RenderDrawingEvent>(this, OnRenderDrawing);
}

private void OnGraphicsInitialized(ref GraphicsLibraryInitializedEvent args)
Expand All @@ -39,7 +39,7 @@ private void OnUpdateFrame(ref UpdateFrameEvent args)
_controller.Update(args.DeltaSeconds);
}

private void OnRenderFrame(ref RenderFrameEvent args)
private void OnRenderDrawing(ref RenderDrawingEvent args)
{
_controller.Begin("Test");
_controller.End();
Expand Down
2 changes: 2 additions & 0 deletions Hypercube.Graphics/Shaders/IShaderProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ public interface IShaderProgram : IDisposable

void SetUniform(string name, Matrix3X3 value, bool transpose = false);
void SetUniform(string name, Matrix4X4 value, bool transpose = false);

void Label(string name);
}
24 changes: 23 additions & 1 deletion Hypercube.ImGui/Implementations/GlfwImGuiController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Hypercube.Graphics.Shaders;
using Hypercube.Graphics.Windowing;
using Hypercube.OpenGL.Objects;
Expand Down Expand Up @@ -81,10 +82,31 @@ public unsafe void InitializeGlfw()
public void InitializeShaders()
{
_shader = new ShaderProgram(ShaderSource.VertexShader, ShaderSource.FragmentShader);

_shader.Label("ImGui");

_vao = new ArrayObject();
_vao.Label("ImGui");

_vbo = new BufferObject(BufferTarget.ArrayBuffer);
_vbo.Label("ImGui");

_ebo = new BufferObject(BufferTarget.ElementArrayBuffer);
_ebo.Label("ImGui");

var stride = Unsafe.SizeOf<ImDrawVert>();

GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, stride, 0);
GL.EnableVertexAttribArray(0);

GL.VertexAttribPointer(1, 4, VertexAttribPointerType.UnsignedByte, true, stride, 16);
GL.EnableVertexAttribArray(1);

GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, stride, 8);
GL.EnableVertexAttribArray(2);

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

CreateFontsTexture();

Expand Down
9 changes: 8 additions & 1 deletion Hypercube.OpenGL/Objects/ArrayObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using Hypercube.OpenGL.Utilities.Helpers;
using JetBrains.Annotations;
using OpenToolkit.Graphics.OpenGL4;

namespace Hypercube.OpenGL.Objects;
Expand Down Expand Up @@ -35,6 +36,12 @@ public void Delete()
GL.DeleteVertexArray(Handle);
}

public void Label(string name)
{
Bind();
GLHelper.LabelObject(ObjectLabelIdentifier.VertexArray, Handle, name);
}

public void Dispose()
{
Delete();
Expand Down
7 changes: 7 additions & 0 deletions Hypercube.OpenGL/Objects/BufferObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.InteropServices;
using Hypercube.OpenGL.Utilities.Helpers;
using JetBrains.Annotations;
using OpenToolkit.Graphics.OpenGL4;

Expand Down Expand Up @@ -66,6 +67,12 @@ public void SetSubData<T>(T[] data) where T : struct
Bind();
GL.BufferSubData(BufferTarget, nint.Zero, data.Length * Marshal.SizeOf<T>(), data);
}

public void Label(string name)
{
Bind();
GLHelper.LabelObject(ObjectLabelIdentifier.Buffer, Handle, name);
}

public void Dispose()
{
Expand Down
36 changes: 18 additions & 18 deletions Hypercube.OpenGL/Shaders/ShaderProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hypercube.Graphics.Shaders;
using Hypercube.Math.Matrices;
using Hypercube.Math.Vectors;
using Hypercube.OpenGL.Utilities.Helpers;
using JetBrains.Annotations;
using OpenToolkit.Graphics.OpenGL4;

Expand All @@ -11,24 +12,24 @@ namespace Hypercube.OpenGL.Shaders;
public class ShaderProgram : IShaderProgram
{
public int Handle { get; private set; }

private readonly FrozenDictionary<string, int> _uniformLocations;

public ShaderProgram(string vertSource, string fragSource)
{
var shaders = new HashSet<IShader>
{
CreateShader(vertSource, ShaderType.VertexShader),
CreateShader(fragSource, ShaderType.FragmentShader)
};

Handle = GL.CreateProgram();

foreach (var shader in shaders)
{
Attach(shader);
}

LinkProgram();

// When the shader program is linked, it no longer needs the individual shaders attached to it; the compiled code is copied into the shader program.
Expand Down Expand Up @@ -90,7 +91,7 @@ public void SetUniform(string name, double value)
{
GL.Uniform1(_uniformLocations[name], value);
}

public void SetUniform(string name, Vector2 value)
{
GL.Uniform2(_uniformLocations[name], value.X, value.Y);
Expand All @@ -106,24 +107,23 @@ public void SetUniform(string name, Vector3 value)
GL.Uniform3(_uniformLocations[name], value.X, value.Y, value.Z);
}

public void SetUniform(string name, Matrix3X3 value, bool transpose = false)
public unsafe void SetUniform(string name, Matrix3X3 value, bool transpose = false)
{
unsafe
{
var matrix = transpose ? Matrix3X3.Transpose(value) : new Matrix3X3(value);
GL.UniformMatrix3(GL.GetUniformLocation(Handle, name), 1, false, (float*) &matrix);
}
var matrix = transpose ? Matrix3X3.Transpose(value) : new Matrix3X3(value);
GL.UniformMatrix3(GL.GetUniformLocation(Handle, name), 1, false, (float*)&matrix);
}

public void SetUniform(string name, Matrix4X4 value, bool transpose = false)
public unsafe void SetUniform(string name, Matrix4X4 value, bool transpose = false)
{
unsafe
{
var matrix = transpose ? Matrix4X4.Transpose(value) : new Matrix4X4(value);
GL.UniformMatrix4(GL.GetUniformLocation(Handle, name), 1, false, (float*) &matrix);
}
var matrix = transpose ? Matrix4X4.Transpose(value) : new Matrix4X4(value);
GL.UniformMatrix4(GL.GetUniformLocation(Handle, name), 1, false, (float*)&matrix);
}


public void Label(string name)
{
GLHelper.LabelObject(ObjectLabelIdentifier.Program, Handle, name);
}

public void Dispose()
{
GL.DeleteProgram(Handle);
Expand Down
5 changes: 5 additions & 0 deletions Hypercube.OpenGL/Utilities/Helpers/GLHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ namespace Hypercube.OpenGL.Utilities.Helpers;

public static class GLHelper
{
public static void LabelObject(ObjectLabelIdentifier objLabelIdent, int glObject, string name)
{
GL.ObjectLabel(objLabelIdent, glObject, name.Length, name);
}

public static string CheckErrors(string title)
{
var error = GL.GetError();
Expand Down
28 changes: 28 additions & 0 deletions settings.cap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"rdocCaptureSettings": 1,
"settings": {
"autoStart": false,
"commandLine": "",
"environment": [
],
"executable": "E:\\Projects\\СS\\Hypercube\\Hypercube.Example.Client\\bin\\Debug\\net8.0\\Hypercube.Example.Client.exe",
"inject": false,
"numQueuedFrames": 0,
"options": {
"allowFullscreen": true,
"allowVSync": true,
"apiValidation": false,
"captureAllCmdLists": false,
"captureCallstacks": false,
"captureCallstacksOnlyDraws": false,
"debugOutputMute": true,
"delayForDebugger": 0,
"hookIntoChildren": false,
"refAllResources": false,
"softMemoryLimit": 0,
"verifyBufferAccess": false
},
"queuedFrameCap": 0,
"workingDir": ""
}
}

0 comments on commit 88b8402

Please sign in to comment.