C
C#8mo ago
Silme94

✅ ImGui.NET and OpenTK

hello, i tried to render a basic imgui window on my opentk window, but it just dont works and crash with an exception
using System;
using ImGuiNET;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace ImGuiWithOpenTK
{
class Program
{
private static GameWindow gameWindow;

public static void Main(string[] args)
{
NativeWindowSettings nativeWindowSettings = new NativeWindowSettings
{
Size = new Vector2i(800, 600),
Title = "ImGui with OpenTK",
API = ContextAPI.OpenGL,
APIVersion = new Version(4, 6),
};

GameWindowSettings gameWindowSettings = new GameWindowSettings
{
UpdateFrequency = 60
};

gameWindow = new GameWindow(gameWindowSettings, nativeWindowSettings);

gameWindow.Load += GameWindow_Load;

gameWindow.RenderFrame += GameWindow_RenderFrame;

gameWindow.UpdateFrame += GameWindow_UpdateFrame;

gameWindow.Run();
}

private static void GameWindow_UpdateFrame(FrameEventArgs obj)
{

}

private static void GameWindow_RenderFrame(FrameEventArgs obj)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

ImGui.NewFrame();
ImGui.Begin("Demo windows");
ImGui.Text("Hello world");
ImGui.End();
ImGui.Render();

gameWindow.SwapBuffers();
}

private static void GameWindow_Load()
{
GL.ClearColor(Color4.CornflowerBlue);

ImGui.CreateContext();
ImGuiIOPtr io = ImGui.GetIO();
ImGui.StyleColorsDark();
}
}
}
using System;
using ImGuiNET;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace ImGuiWithOpenTK
{
class Program
{
private static GameWindow gameWindow;

public static void Main(string[] args)
{
NativeWindowSettings nativeWindowSettings = new NativeWindowSettings
{
Size = new Vector2i(800, 600),
Title = "ImGui with OpenTK",
API = ContextAPI.OpenGL,
APIVersion = new Version(4, 6),
};

GameWindowSettings gameWindowSettings = new GameWindowSettings
{
UpdateFrequency = 60
};

gameWindow = new GameWindow(gameWindowSettings, nativeWindowSettings);

gameWindow.Load += GameWindow_Load;

gameWindow.RenderFrame += GameWindow_RenderFrame;

gameWindow.UpdateFrame += GameWindow_UpdateFrame;

gameWindow.Run();
}

private static void GameWindow_UpdateFrame(FrameEventArgs obj)
{

}

private static void GameWindow_RenderFrame(FrameEventArgs obj)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

ImGui.NewFrame();
ImGui.Begin("Demo windows");
ImGui.Text("Hello world");
ImGui.End();
ImGui.Render();

gameWindow.SwapBuffers();
}

private static void GameWindow_Load()
{
GL.ClearColor(Color4.CornflowerBlue);

ImGui.CreateContext();
ImGuiIOPtr io = ImGui.GetIO();
ImGui.StyleColorsDark();
}
}
}
35 Replies
Silme94
Silme94OP8mo ago
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at ImGuiNET.ImGuiNative.igNewFrame()
--------------------------------
at ImGuiNET.ImGui.NewFrame()
at ImGuiWithOpenTK.Program.GameWindow_RenderFrame(OpenTK.Windowing.Common.FrameEventArgs)
at OpenTK.Windowing.Desktop.GameWindow.OnRenderFrame(OpenTK.Windowing.Common.FrameEventArgs)
at OpenTK.Windowing.Desktop.GameWindow.Run()
at ImGuiWithOpenTK.Program.Main(System.String[])
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
at ImGuiNET.ImGuiNative.igNewFrame()
--------------------------------
at ImGuiNET.ImGui.NewFrame()
at ImGuiWithOpenTK.Program.GameWindow_RenderFrame(OpenTK.Windowing.Common.FrameEventArgs)
at OpenTK.Windowing.Desktop.GameWindow.OnRenderFrame(OpenTK.Windowing.Common.FrameEventArgs)
at OpenTK.Windowing.Desktop.GameWindow.Run()
at ImGuiWithOpenTK.Program.Main(System.String[])
TheRanger
TheRanger8mo ago
ah yes good thing im here cuz i did make it work but that error seems new to me Shouldnt you have an instance of a ImGuiController ?
Silme94
Silme94OP8mo ago
ImGuiController is not definied on ImGui.NET bro
TheRanger
TheRanger8mo ago
never said it does
Silme94
Silme94OP8mo ago
its from github or smth? is there a way to make without?
TheRanger
TheRanger8mo ago
let me check actually imguicontroller is there
TheRanger
TheRanger8mo ago
GitHub
GitHub - ImGuiNET/ImGui.NET: An ImGui wrapper for .NET.
An ImGui wrapper for .NET. Contribute to ImGuiNET/ImGui.NET development by creating an account on GitHub.
TheRanger
TheRanger8mo ago
did you follow the tutorial? there's an example project there called SampleProgram try running it
Silme94
Silme94OP8mo ago
ok yeah but i dont wanna paste it bro i wanna write myself
TheRanger
TheRanger8mo ago
the controller?
Silme94
Silme94OP8mo ago
how do we actually use it cause i see also Memory.cs MemoryEditor
TheRanger
TheRanger8mo ago
this is specialized for opentk https://github.com/aybe/DearImGui
GitHub
GitHub - aybe/DearImGui: imgui & implot for .NET & OpenTK
imgui & implot for .NET & OpenTK. Contribute to aybe/DearImGui development by creating an account on GitHub.
Silme94
Silme94OP8mo ago
there is no way to use the normal ImGui.NET library? how do u do for example on ur projects
TheRanger
TheRanger8mo ago
ah yes sorry this is the one im actually using https://github.com/NogginBops/ImGui.NET_OpenTK_Sample
GitHub
GitHub - NogginBops/ImGui.NET_OpenTK_Sample: A sample project showi...
A sample project showing an ImGui (using ImGui.NET) renderer for OpenTK in C# - NogginBops/ImGui.NET_OpenTK_Sample
Silme94
Silme94OP8mo ago
i copy the controller and how i use it on my code?
TheRanger
TheRanger8mo ago
to be safe use this ^ project because its the one im using so it'd probably be easier to help u the project has an example it should give you an idea how to
Silme94
Silme94OP8mo ago
this is working but that just look rly weird, and i cannot interacte the imgui menu with keys
TheRanger
TheRanger8mo ago
use $paste
MODiX
MODiX8mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Silme94
Silme94OP8mo ago
using System;
using ImGuiNET;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace ImGuiWithOpenTK
{
class Program
{
private static GameWindow gameWindow;
private static ImGuiController _controller;

public static void Main(string[] args)
{
NativeWindowSettings nativeWindowSettings = new NativeWindowSettings
{
Size = new Vector2i(800, 600),
Title = "ImGui with OpenTK",
API = ContextAPI.OpenGL,
APIVersion = new Version(4, 6),
};

GameWindowSettings gameWindowSettings = new GameWindowSettings
{
UpdateFrequency = 60
};

gameWindow = new GameWindow(gameWindowSettings, nativeWindowSettings);

_controller = new ImGuiController(800, 600);

gameWindow.Load += GameWindow_Load;

gameWindow.RenderFrame += GameWindow_RenderFrame;

gameWindow.UpdateFrame += GameWindow_UpdateFrame;

gameWindow.Run();
}

private static void GameWindow_UpdateFrame(FrameEventArgs obj)
{

}

private static void GameWindow_RenderFrame(FrameEventArgs obj)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

_controller.Update(gameWindow, (float)obj.Time);

ImGui.DockSpaceOverViewport();

ImGui.ShowDemoWindow();

_controller.Render();

ImGuiController.CheckGLError("End of frame");

gameWindow.SwapBuffers();
}

private static void GameWindow_Load()
{
GL.ClearColor(Color4.CornflowerBlue);
GL.Viewport(0, 0, 800, 600);
}
}
}
using System;
using ImGuiNET;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;

namespace ImGuiWithOpenTK
{
class Program
{
private static GameWindow gameWindow;
private static ImGuiController _controller;

public static void Main(string[] args)
{
NativeWindowSettings nativeWindowSettings = new NativeWindowSettings
{
Size = new Vector2i(800, 600),
Title = "ImGui with OpenTK",
API = ContextAPI.OpenGL,
APIVersion = new Version(4, 6),
};

GameWindowSettings gameWindowSettings = new GameWindowSettings
{
UpdateFrequency = 60
};

gameWindow = new GameWindow(gameWindowSettings, nativeWindowSettings);

_controller = new ImGuiController(800, 600);

gameWindow.Load += GameWindow_Load;

gameWindow.RenderFrame += GameWindow_RenderFrame;

gameWindow.UpdateFrame += GameWindow_UpdateFrame;

gameWindow.Run();
}

private static void GameWindow_UpdateFrame(FrameEventArgs obj)
{

}

private static void GameWindow_RenderFrame(FrameEventArgs obj)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

_controller.Update(gameWindow, (float)obj.Time);

ImGui.DockSpaceOverViewport();

ImGui.ShowDemoWindow();

_controller.Render();

ImGuiController.CheckGLError("End of frame");

gameWindow.SwapBuffers();
}

private static void GameWindow_Load()
{
GL.ClearColor(Color4.CornflowerBlue);
GL.Viewport(0, 0, 800, 600);
}
}
}
No description
Silme94
Silme94OP8mo ago
the color of my windows does not infect the imgui menu why this is grey and dont resize
TheRanger
TheRanger8mo ago
bro did you check the sample? where is this method
protected override void OnResize(ResizeEventArgs e)
{
base.OnResize(e);

// Update the opengl viewport
GL.Viewport(0, 0, ClientSize.X, ClientSize.Y);

// Tell ImGui of the new size
_controller.WindowResized(ClientSize.X, ClientSize.Y);
}
protected override void OnResize(ResizeEventArgs e)
{
base.OnResize(e);

// Update the opengl viewport
GL.Viewport(0, 0, ClientSize.X, ClientSize.Y);

// Tell ImGui of the new size
_controller.WindowResized(ClientSize.X, ClientSize.Y);
}
Silme94
Silme94OP8mo ago
yes fr but what is ClientSize where did he got that variable form
TheRanger
TheRanger8mo ago
its in the sample its a property defined in the GameWindow class in your case it would be gameWindow.ClientSize.X
Silme94
Silme94OP8mo ago
oh ok sorry thats works but i cannot change the background color and its always grey
Silme94
Silme94OP8mo ago
MODiX
MODiX8mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Silme94
Silme94OP8mo ago
BlazeBin - ahsxjyzdlcjj
A tool for sharing your source code with the world!
TheRanger
TheRanger8mo ago
show image?
Silme94
Silme94OP8mo ago
@ @R like the screen here the same color but it scale well i just want to change color
TheRanger
TheRanger8mo ago
so its all grey?
Silme94
Silme94OP8mo ago
yes but imgui windows work well
TheRanger
TheRanger8mo ago
what color is this? GL.ClearColor(new Color4(0.0f, 0.3f, 0.5f, 1.0f)); also wow this code
gameWindowSettings = new GameWindowSettings
{
UpdateFrequency = 60
};
gameWindowSettings = new GameWindowSettings
{
UpdateFrequency = 60
};
super lags my game i just use GameWindowSettings.Default like the sample does hmm ur right i ran the sample, it doesnt change the color lol it does change in my game, ill see why found the culprit comment this line ImGui.DockSpaceOverViewport();
Silme94
Silme94OP8mo ago
ok that work well thanks !close
Accord
Accord8mo ago
Closed!
Want results from more Discord servers?
Add your server