Skip to content

Instantly share code, notes, and snippets.

@Gnimuc
Last active April 16, 2022 03:46
Show Gist options
  • Save Gnimuc/5a1e4942784cfc3558e2ee50b7bee84d to your computer and use it in GitHub Desktop.
Save Gnimuc/5a1e4942784cfc3558e2ee50b7bee84d to your computer and use it in GitHub Desktop.
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)

# replace renderloop
GLMakie.opengl_renderloop[] = imgui_renderloop

x = rand(10);
y = rand(10);
colors = rand(10);
scene = scatter(x, y, color = colors)
@Gnimuc
Copy link
Author

Gnimuc commented Aug 12, 2019

environment info:

(v1.1) pkg> st
    Status `~/.julia/environments/v1.1/Project.toml`
  [537997a7] AbstractPlotting v0.9.8
  [079381af] Assimp v0.1.0 [`~/.julia/dev/Assimp`]
  [c52e3926] Atom v0.9.1
  [6e4b80f9] BenchmarkTools v0.4.2
  [12aac903] BinaryBuilder v0.1.4
  [b99e7846] BinaryProvider v0.5.6
  [163b9779] Blobs v0.3.0
  [f735cfa6] C2Julia v0.1.0 [`~/.julia/dev/C2Julia`]
  [fa961155] CEnum v0.2.0
  [5d785b6c] CImGui v1.69.2
  [538df835] CSFML v0.1.1 [`~/.julia/dev/LibCSFML`]
  [ea656a56] CSyntax v0.2.0
  [40e3b903] Clang v0.9.1
  [861a8166] Combinatorics v0.7.0
  [a0b5b9ef] Cxx v0.3.2 [`~/.julia/dev/Cxx`]
  [864edb3b] DataStructures v0.17.0
  [31a5f54b] Debugger v0.5.0
  [e30172f5] Documenter v0.23.2
  [35a29f4d] DocumenterTools v0.1.1
  [e9467ef8] GLMakie v0.0.7 [`~/.julia/dev/GLMakie`]
  [aeeaf58c] GLTF v0.1.0 [`~/.julia/dev/GLTF`]
  [d9be37ee] Homebrew v0.7.1
  [682c06a0] JSON v0.21.0
  [0f8b85d8] JSON3 v0.1.7
  [aa1ae85d] JuliaInterpreter v0.6.1
  [e5e0dc1b] Juno v0.7.2
  [ae8d54c2] Luxor v1.5.0 #master (https://github.com/JuliaGraphics/Luxor.jl.git)
  [ee78f7c6] Makie v0.9.4
  [66fc600b] ModernGL v1.0.0

@Gnimuc
Copy link
Author

Gnimuc commented Aug 12, 2019

Screen Shot 2019-08-12 at 23 54 38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment