def _test_enabling_disabling(): # Enabling/disabling gl.glEnable(gl.GL_DEPTH_TEST) assert_equal(gl.glIsEnabled(gl.GL_DEPTH_TEST), True) assert_equal(gl.glGetParameter(gl.GL_DEPTH_TEST), 1) gl.glDisable(gl.GL_DEPTH_TEST) assert_equal(gl.glIsEnabled(gl.GL_DEPTH_TEST), False) assert_equal(gl.glGetParameter(gl.GL_DEPTH_TEST), 0) # gl.glEnable(gl.GL_BLEND) assert_equal(gl.glIsEnabled(gl.GL_BLEND), True) assert_equal(gl.glGetParameter(gl.GL_BLEND), 1) gl.glDisable(gl.GL_BLEND) assert_equal(gl.glIsEnabled(gl.GL_BLEND), False) assert_equal(gl.glGetParameter(gl.GL_BLEND), 0) gl.check_error()
def render_with_gloo(draw_data): """draw ImGui's drawlists with vispy's gloo interface Parameters ---------- draw_data: ImDrawData """ global device_objects # adapted from imgui_impl_ios.cpp in imgui, to target the minimal GLES API d = pymgui.ImDrawData(draw_data) io = pymgui.get_io() fb_scale = io.display_framebuffer_scale fb_width, fb_height = io.display_size if fb_width == 0 or fb_height == 0: return conf = gloo.get_gl_configuration() # backup GL state last_program = gl.glGetParameter(gl.GL_CURRENT_PROGRAM) last_texture = gl.glGetParameter(gl.GL_TEXTURE_BINDING_2D) last_array_buffer = gl.glGetParameter(gl.GL_ARRAY_BUFFER_BINDING) last_element_array_buffer = gl.glGetParameter(gl.GL_ELEMENT_ARRAY_BUFFER_BINDING) last_viewport = gl.glGetParameter(gl.GL_VIEWPORT) last_enable_blend = gl.glIsEnabled(gl.GL_BLEND), last_blend_equation = gl.glGetParameter(gl.GL_BLEND_EQUATION_RGB), last_blend_func = dict( last_blend_equation_alpha=gl.glGetParameter(gl.GL_BLEND_EQUATION_ALPHA), ) last_gl_states = dict( cull_face = gl.glIsEnabled(gl.GL_CULL_FACE), depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST), scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST)) gloo.set_state(blend=True) gloo.set_blend_equation('func_add') gloo.set_blend_func(salpha='src_alpha', dalpha='one_minus_src_alpha') gloo.set_state(cull_face=False, depth_test=False, scissor_test=True) gl.glActiveTexture(gl.GL_TEXTURE0) # Setup viewport, orthographic projection matrix gloo.set_viewport(0, 0, int(fb_width), int(fb_height)) p = device_objects['program'] # type: gloo.Program ortho_projection = np.array([ [2./fb_width, 0, 0, 0], [0., 2./-fb_height, 0, 0], [0., 0., -1., 0], [-1., 1., 0, 0] ]) vtype = [('a_position', np.float32, 2), ('a_uv', np.float32, 2), # ('a_color', np.uint8, 4)] ('a_color', np.uint32, 1)] # pos_dtype = [('a_position', np.float32, 2)] # uv_dtype = [('a_uv', np.float32, 2)] # color_dtype = [('a_color', np.uint8, 4)] itype = np.uint16 display_scale = 1.0 p['u_mvp'] = ortho_projection # print("draw {} cmd_list".format(len(d.cmd_lists))) for cmd_list in d.cmd_lists: vertex_count =C.ImDrawList_GetVertexBufferSize(cmd_list) index_count = C.ImDrawList_GetIndexBufferSize(cmd_list) vertex_buffer_ptr = C.ImDrawList_GetVertexPtr(cmd_list, 0) index_buffer_ptr = C.ImDrawList_GetIndexPtr(cmd_list, 0) def to_nparray(ptr, item_count, dtype): byte_count = item_count * dtype.itemsize as_bytes = ffi.buffer(ptr, byte_count) return np.frombuffer(as_bytes, dtype=dtype) ig_vertices = to_nparray(vertex_buffer_ptr, vertex_count, np.dtype(vtype)) ig_indices = to_nparray(index_buffer_ptr, index_count, np.dtype(itype)) # print("{} vertices to draw".format(len(ig_vertices))) # p['a_position'] = ig_vertices[:] # p['a_uv'] = ig_vertices['a_uv'] # p['a_color'] = ig_vertices['a_color'] # device_objects['geometry'] = ig_vertices # # gl.glBufferData(gl.GL_ARRAY_BUFFER, ig_vertices, gl.GL_STREAM_DRAW) # gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, indices, gl.GL_STREAM_DRAW) # # index_buffer_offset = 0 # cmd_size = C.ImDrawList_GetCmdSize(cmd_list) # for cmd_index in range(cmd_size): # draw_cmd_ptr = C.ImDrawList_GetCmdPtr(cmd_list, cmd_index) # draw_cmd = pymgui.ImDrawCmd(draw_cmd_ptr) # # if draw_cmd.texture_id: # gl.glBindTexture(gl.GL_TEXTURE_2D, draw_cmd.texture_id) # x1, y1, x2, y2 = draw_cmd.clip_rect # # gl.glScissor( # x=int(x1 * display_scale), # y=int(y1 * display_scale), # width=int((x2 - x1) * display_scale), # height=int((y2 - y1) * display_scale) # ) # # start, stop = index_buffer_offset, draw_cmd.elem_count # # gl.glDrawElements(gl.GL_TRIANGLES, draw_cmd.elem_count, gl.GL_UNSIGNED_SHORT, indices[index_buffer_offset:]) # p.draw('triangles', gloo.IndexBuffer(ig_indices[start:stop])) # index_buffer_offset += draw_cmd.elem_count # # # Restore modified state gloo.set_state(**last_gl_states) gloo.set_state(blend=last_enable_blend) # gloo.set_blend_equation(last_blend_equation) # # gl.glBindVertexArray(0) # gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) gl.glUseProgram(last_program)
def render_with_gloo(draw_data): """draw ImGui's drawlists with vispy's gloo interface Parameters ---------- draw_data: ImDrawData """ global device_objects # adapted from imgui_impl_ios.cpp in imgui, to target the minimal GLES API d = pymgui.ImDrawData(draw_data) io = pymgui.get_io() fb_scale = io.display_framebuffer_scale fb_width, fb_height = io.display_size if fb_width == 0 or fb_height == 0: return conf = gloo.get_gl_configuration() # backup GL state last_program = gl.glGetParameter(gl.GL_CURRENT_PROGRAM) last_texture = gl.glGetParameter(gl.GL_TEXTURE_BINDING_2D) last_array_buffer = gl.glGetParameter(gl.GL_ARRAY_BUFFER_BINDING) last_element_array_buffer = gl.glGetParameter( gl.GL_ELEMENT_ARRAY_BUFFER_BINDING) last_viewport = gl.glGetParameter(gl.GL_VIEWPORT) last_enable_blend = gl.glIsEnabled(gl.GL_BLEND), last_blend_equation = gl.glGetParameter(gl.GL_BLEND_EQUATION_RGB), last_blend_func = dict(last_blend_equation_alpha=gl.glGetParameter( gl.GL_BLEND_EQUATION_ALPHA), ) last_gl_states = dict(cull_face=gl.glIsEnabled(gl.GL_CULL_FACE), depth_test=gl.glIsEnabled(gl.GL_DEPTH_TEST), scissor_test=gl.glIsEnabled(gl.GL_SCISSOR_TEST)) gloo.set_state(blend=True) gloo.set_blend_equation('func_add') gloo.set_blend_func(salpha='src_alpha', dalpha='one_minus_src_alpha') gloo.set_state(cull_face=False, depth_test=False, scissor_test=True) gl.glActiveTexture(gl.GL_TEXTURE0) # Setup viewport, orthographic projection matrix gloo.set_viewport(0, 0, int(fb_width), int(fb_height)) p = device_objects['program'] # type: gloo.Program ortho_projection = np.array([[2. / fb_width, 0, 0, 0], [0., 2. / -fb_height, 0, 0], [0., 0., -1., 0], [-1., 1., 0, 0]]) vtype = [ ('a_position', np.float32, 2), ('a_uv', np.float32, 2), # ('a_color', np.uint8, 4)] ('a_color', np.uint32, 1) ] # pos_dtype = [('a_position', np.float32, 2)] # uv_dtype = [('a_uv', np.float32, 2)] # color_dtype = [('a_color', np.uint8, 4)] itype = np.uint16 display_scale = 1.0 p['u_mvp'] = ortho_projection # print("draw {} cmd_list".format(len(d.cmd_lists))) for cmd_list in d.cmd_lists: vertex_count = C.ImDrawList_GetVertexBufferSize(cmd_list) index_count = C.ImDrawList_GetIndexBufferSize(cmd_list) vertex_buffer_ptr = C.ImDrawList_GetVertexPtr(cmd_list, 0) index_buffer_ptr = C.ImDrawList_GetIndexPtr(cmd_list, 0) def to_nparray(ptr, item_count, dtype): byte_count = item_count * dtype.itemsize as_bytes = ffi.buffer(ptr, byte_count) return np.frombuffer(as_bytes, dtype=dtype) ig_vertices = to_nparray(vertex_buffer_ptr, vertex_count, np.dtype(vtype)) ig_indices = to_nparray(index_buffer_ptr, index_count, np.dtype(itype)) # print("{} vertices to draw".format(len(ig_vertices))) # p['a_position'] = ig_vertices[:] # p['a_uv'] = ig_vertices['a_uv'] # p['a_color'] = ig_vertices['a_color'] # device_objects['geometry'] = ig_vertices # # gl.glBufferData(gl.GL_ARRAY_BUFFER, ig_vertices, gl.GL_STREAM_DRAW) # gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, indices, gl.GL_STREAM_DRAW) # # index_buffer_offset = 0 # cmd_size = C.ImDrawList_GetCmdSize(cmd_list) # for cmd_index in range(cmd_size): # draw_cmd_ptr = C.ImDrawList_GetCmdPtr(cmd_list, cmd_index) # draw_cmd = pymgui.ImDrawCmd(draw_cmd_ptr) # # if draw_cmd.texture_id: # gl.glBindTexture(gl.GL_TEXTURE_2D, draw_cmd.texture_id) # x1, y1, x2, y2 = draw_cmd.clip_rect # # gl.glScissor( # x=int(x1 * display_scale), # y=int(y1 * display_scale), # width=int((x2 - x1) * display_scale), # height=int((y2 - y1) * display_scale) # ) # # start, stop = index_buffer_offset, draw_cmd.elem_count # # gl.glDrawElements(gl.GL_TRIANGLES, draw_cmd.elem_count, gl.GL_UNSIGNED_SHORT, indices[index_buffer_offset:]) # p.draw('triangles', gloo.IndexBuffer(ig_indices[start:stop])) # index_buffer_offset += draw_cmd.elem_count # # # Restore modified state gloo.set_state(**last_gl_states) gloo.set_state(blend=last_enable_blend) # gloo.set_blend_equation(last_blend_equation) # # gl.glBindVertexArray(0) # gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0) gl.glUseProgram(last_program)