Last active
June 19, 2024 16:33
-
-
Save SteffenPL/88f65222bf786a6434bc2da3ae02ac74 to your computer and use it in GitHub Desktop.
Modification of Gnimuc's example: https://gist.github.com/Gnimuc/5a1e4942784cfc3558e2ee50b7bee84d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CImGui | |
using CImGui.GLFWBackend | |
using CImGui.OpenGLBackend | |
using CImGui.GLFWBackend.GLFW | |
using CImGui.OpenGLBackend.ModernGL | |
using AbstractPlotting | |
using GLMakie | |
import GLMakie: make_context_current, render_frame, to_native, Screen | |
# add ImGui rendering pass to GLMakie's rendering loop | |
function imgui_renderloop(screen::Screen; framerate = 1/30, prerender = () -> nothing) | |
try | |
while isopen(screen) | |
# Somehow errors get sometimes ignored, so we at least print them here | |
try | |
t = time() | |
GLFW.PollEvents() # GLFW poll | |
# create new cimgui frame | |
ImGui_ImplOpenGL3_NewFrame() | |
ImGui_ImplGlfw_NewFrame(to_native(screen)) | |
CImGui.NewFrame() | |
# create UIs | |
CImGui.ShowDemoWindow(Ref(true)) | |
# produce GUI "meshes" | |
CImGui.Render() | |
prerender() | |
make_context_current(screen) | |
render_frame(screen) | |
# render imgui over plots | |
ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData()) | |
make_context_current(screen) | |
GLFW.SwapBuffers(to_native(screen)) | |
diff = framerate - (time() - t) | |
if diff > 0 | |
sleep(diff) | |
else # if we don't sleep, we need to yield explicitely | |
yield() | |
end | |
catch e | |
@error "Error in renderloop!" exception=e | |
rethrow(e) | |
end | |
end | |
catch e | |
@error "Error in renderloop!" exception=e | |
rethrow(e) | |
finally | |
destroy!(screen) | |
end | |
return | |
end | |
# init CImGui | |
ctx = CImGui.CreateContext() | |
CImGui.StyleColorsDark() | |
ImGui_ImplGlfw_InitForOpenGL(GLMakie.to_native(GLMakie.global_gl_screen()), true) | |
ImGui_ImplOpenGL3_Init(150) | |
x = rand(10); | |
y = rand(10); | |
colors = rand(10); | |
scene = scatter(x, y, color = colors) | |
# replace renderloop | |
#GLMakie.opengl_renderloop[] = imgui_renderloop | |
# new version | |
screen = AbstractPlotting.backend_display(GLMakie.GLBackend(), scene) | |
set_window_config!(renderloop=imgui_renderloop) | |
screen |
I updated the code snippet.
What's a good way to pass my custom prerender
in the draw loop? E.g. something like
https://github.com/Gnimuc/CImGui.jl/blob/master/examples/Renderer.jl
This code is being discussed at Gnimuc/CImGui.jl#114
Any idea about integrating a GLMakie plot in a CImGui window (or conversely) with latest version of both CImGui.jl and Makie.jl
Pinging @SimonDanisch and @jkrumbiegel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better to free resources here: