11module NeuralGraphicsGL
22
33using CImGui
4- using CImGui. ImGuiGLFWBackend. LibGLFW
54using FileIO
65using ImageCore
76using ImageIO
87using LinearAlgebra
9- using ModernGL
108using StaticArrays
119
10+ using GLFW
11+ using ModernGL
12+
13+ import CImGui. lib as lib
14+
1215"""
1316Replaces:
1417
@@ -147,11 +150,11 @@ include("line.jl")
147150include (" frustum.jl" )
148151include (" widget.jl" )
149152
150- const GLSL_VERSION = 410
153+ const GLSL_VERSION = 130
151154
152155function init (version_major:: Integer = 3 , version_minor:: Integer = 0 )
153- glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR , version_major)
154- glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR , version_minor)
156+ GLFW . WindowHint (GLFW . CONTEXT_VERSION_MAJOR , version_major)
157+ GLFW . WindowHint (GLFW . CONTEXT_VERSION_MINOR , version_minor)
155158end
156159
157160function get_gl_version ()
@@ -161,10 +164,8 @@ function get_gl_version()
161164end
162165
163166struct Context
164- window:: Ptr{GLFWwindow}
167+ window:: GLFW.Window
165168 imgui_ctx:: Ptr{CImGui.ImGuiContext}
166- glfw_ctx:: CImGui.ImGuiGLFWBackend.Context
167- gl_ctx:: CImGui.ImGuiOpenGLBackend.Context
168169
169170 width:: Int64
170171 height:: Int64
@@ -181,45 +182,52 @@ function Context(
181182 error (" You need to specify either `fullscreen` or `width` & `height` parameters." )
182183 end
183184
184- glfwWindowHint (GLFW_VISIBLE, visible)
185- if fullscreen
186- glfwWindowHint (GLFW_RESIZABLE, false )
187- monitor = glfwGetPrimaryMonitor ()
188- mode = unsafe_load (glfwGetVideoMode (monitor))
189- window = glfwCreateWindow (mode. width, mode. height, title, monitor, C_NULL )
185+ imgui_ctx = CImGui. CreateContext ()
186+
187+ GLFW. Init ()
188+ GLFW. WindowHint (GLFW. VISIBLE, visible)
189+
190+ window = if fullscreen
191+ GLFW. WindowHint (GLFW. RESIZABLE, false )
192+ monitor = GLFW. GetPrimaryMonitor ()
193+ mode = unsafe_load (GLFW. GetVideoMode (monitor))
194+
190195 width, height = mode. width, mode. height
196+ GLFW. CreateWindow (width, height, title, monitor)
191197 else
192- glfwWindowHint (GLFW_RESIZABLE , resizable)
193- window = glfwCreateWindow (width, height, title, C_NULL , C_NULL )
198+ GLFW . WindowHint (GLFW . RESIZABLE , resizable)
199+ GLFW . CreateWindow (width, height, title)
194200 end
195- glfwMakeContextCurrent (window)
196- glfwSwapInterval (vsync ? 1 : 0 )
201+ @assert window != C_NULL
202+
203+ GLFW. MakeContextCurrent (window)
204+ GLFW. SwapInterval (vsync ? 1 : 0 )
205+
206+ # Setup Platform/Renderer bindings.
207+ lib. ImGui_ImplGlfw_InitForOpenGL (Ptr {lib.GLFWwindow} (window. handle), true )
208+ lib. ImGui_ImplOpenGL3_Init (" #version $GLSL_VERSION " )
209+
197210 # You need this for RGB textures that their width is not a multiple of 4.
198211 glPixelStorei (GL_UNPACK_ALIGNMENT, 1 )
199212
200- # enable depth buffer
213+ # Enable depth buffer.
201214 glEnable (GL_DEPTH_TEST)
202215 glDepthMask (GL_TRUE)
203216 glClearDepth (1.0f0 )
204217
205218 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
206219
207- imgui_ctx = CImGui . CreateContext ()
220+ # Set ImGui syle.
208221 CImGui. StyleColorsDark ()
209222 style = CImGui. GetStyle ()
210223 style. FrameRounding = 0f0
211224 style. WindowRounding = 0f0
212225 style. ScrollbarRounding = 0f0
213226
214- glfw_ctx = CImGui. ImGuiGLFWBackend. create_context (window)
215- gl_ctx = CImGui. ImGuiOpenGLBackend. create_context (GLSL_VERSION)
216-
217227 io = CImGui. GetIO ()
218228 io. ConfigFlags = unsafe_load (io. ConfigFlags) | CImGui. ImGuiConfigFlags_DockingEnable
219229
220- CImGui. ImGuiGLFWBackend. init (glfw_ctx)
221- CImGui. ImGuiOpenGLBackend. init (gl_ctx)
222- Context (window, imgui_ctx, glfw_ctx, gl_ctx, width, height)
230+ Context (window, imgui_ctx, width, height)
223231end
224232
225233enable_blend () = glEnable (GL_BLEND)
@@ -236,32 +244,31 @@ disable_wireframe() = glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
236244
237245function delete! (c:: Context )
238246 imgui_shutdown! (c)
239- glfwDestroyWindow (c. window)
247+ GLFW . DestroyWindow (c. window)
240248end
241249
242250function set_resizable_window! (c:: Context , resizable:: Bool )
243- glfwSetWindowAttrib (c. window, GLFW_RESIZABLE , resizable)
251+ GLFW . SetWindowAttrib (c. window, GLFW . RESIZABLE , resizable)
244252end
245253
246254function set_resize_callback! (c:: Context , callback)
247- glfwSetWindowSizeCallback (
248- c. window, @cfunction ($ callback, Cvoid, (Ptr{GLFWwindow}, Cint, Cint)))
255+ GLFW. SetWindowSizeCallback (c. window, callback)
249256end
250257
251- function imgui_begin (c :: Context )
252- CImGui . ImGuiOpenGLBackend . new_frame (c . gl_ctx )
253- CImGui . ImGuiGLFWBackend . new_frame (c . glfw_ctx )
258+ function imgui_begin ()
259+ lib . ImGui_ImplOpenGL3_NewFrame ( )
260+ lib . ImGui_ImplGlfw_NewFrame ( )
254261 CImGui. NewFrame ()
255262end
256263
257- function imgui_end (c :: Context )
264+ function imgui_end ()
258265 CImGui. Render ()
259- CImGui . ImGuiOpenGLBackend . render (c . gl_ctx )
266+ lib . ImGui_ImplOpenGL3_RenderDrawData ( Ptr {Cint} (CImGui . GetDrawData ()) )
260267end
261268
262269function imgui_shutdown! (c:: Context )
263- CImGui . ImGuiOpenGLBackend . shutdown (c . gl_ctx )
264- CImGui . ImGuiGLFWBackend . shutdown (c . glfw_ctx )
270+ lib . ImGui_ImplOpenGL3_Shutdown ( )
271+ lib . ImGui_ImplGlfw_Shutdown ( )
265272 CImGui. DestroyContext (c. imgui_ctx)
266273end
267274
@@ -271,13 +278,13 @@ set_clear_color(r, g, b, a) = glClearColor(r, g, b, a)
271278
272279set_viewport (width, height) = glViewport (0 , 0 , width, height)
273280
274- hide_cursor (w:: GLFWwindow ) = glfwSetInputMode (w, GLFW_CURSOR, GLFW_CURSOR_DISABLED )
281+ hide_cursor (w:: GLFW.Window ) = GLFW . SetInputMode (w, GLFW . CURSOR, GLFW . CURSOR_DISABLED )
275282
276- show_cursor (w:: GLFWwindow ) = glfwSetInputMode (w, GLFW_CURSOR, GLFW_CURSOR_NORMAL )
283+ show_cursor (w:: GLFW.Window ) = GLFW . SetInputMode (w, GLFW . CURSOR, GLFW . CURSOR_NORMAL )
277284
278285function render_loop (draw_function, c:: Context ; destroy_context:: Bool = true )
279286 try
280- while glfwWindowShouldClose (c. window) == 0
287+ while GLFW . WindowShouldClose (c. window) == 0
281288 is_running = draw_function ()
282289 is_running || break
283290 end
0 commit comments