def __enter__(self): # Save viewport for final rendering self._viewport = gl.glGetIntegerv(gl.GL_VIEWPORT) # Prepare framebuffer for "original" rendering gl.glViewport(0, 0, self.width, self.height) self._framebuffers[0].activate()
def on_mouse_scroll(x, y, dx, dy): global translate, scale _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) y = h - y s = min(max(0.25, scale + 0.01 * dy * scale), 200) translate[0] = x - s * (x - translate[0]) / scale translate[1] = y - s * (y - translate[1]) / scale translate = [translate[0], translate[1]] scale = s program["u_translate"] = translate program["u_scale"] = scale
def on_mouse_scroll(x, y, dx, dy): global translate, scale _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) y = h - y s = min(max(0.25, scale + .01 * dy * scale), 200) translate[0] = x - s * (x - translate[0]) / scale translate[1] = y - s * (y - translate[1]) / scale translate = [translate[0], translate[1]] scale = s program['u_translate'] = translate program['u_scale'] = scale
def mouse_cursor_pos_callback(self, window, xpos, ypos): io = imgui.get_io() mouse_pos = np.array([xpos, ypos]).astype('float') if io.mouse_down[0]: _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) if not np.all(self.ui.prev_mouse == np.array([-1, -1])): dmouse = self.ui.prev_mouse - mouse_pos self.ui.mouse_pos -= np.array([dmouse[0] / w, -dmouse[1] / h ]) / self.ui.zoom * 2 self.quad["mouse_pos"] = self.ui.mouse_pos print("mouse_pos: {}, dmouse: {}".format(mouse_pos, dmouse)) self.ui.prev_mouse = mouse_pos
def on_mouse_drag(x, y, dx, dy, button): global translate, scale _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) translate = [translate[0] + dx, translate[1] - dy] program["u_translate"] = translate
def on_mouse_drag(x, y, dx, dy, button): _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) program["iMouse"] = 2 * x / w - 1, 1 - 2 * y / h
def render(self, draw_data): # perf: local for faster access io = self.io display_width, display_height = io.display_size fb_width = int(display_width * io.display_fb_scale[0]) fb_height = int(display_height * io.display_fb_scale[1]) if fb_width == 0 or fb_height == 0: return draw_data.scale_clip_rects(*io.display_fb_scale) # backup GL state # todo: provide cleaner version of this backup-restore code last_program = gl.glGetIntegerv(gl.GL_CURRENT_PROGRAM) last_texture = gl.glGetIntegerv(gl.GL_TEXTURE_BINDING_2D) last_active_texture = gl.glGetIntegerv(gl.GL_ACTIVE_TEXTURE) last_array_buffer = gl.glGetIntegerv(gl.GL_ARRAY_BUFFER_BINDING) last_element_array_buffer = gl.glGetIntegerv( gl.GL_ELEMENT_ARRAY_BUFFER_BINDING) last_vertex_array = gl.glGetIntegerv(gl.GL_VERTEX_ARRAY_BINDING) last_blend_src = gl.glGetIntegerv(gl.GL_BLEND_SRC) last_blend_dst = gl.glGetIntegerv(gl.GL_BLEND_DST) last_blend_equation_rgb = gl.glGetIntegerv(gl.GL_BLEND_EQUATION_RGB) last_blend_equation_alpha = gl.glGetIntegerv( gl.GL_BLEND_EQUATION_ALPHA) last_viewport = gl.glGetIntegerv(gl.GL_VIEWPORT) last_scissor_box = gl.glGetIntegerv(gl.GL_SCISSOR_BOX) last_enable_blend = gl.glIsEnabled(gl.GL_BLEND) last_enable_cull_face = gl.glIsEnabled(gl.GL_CULL_FACE) last_enable_depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST) last_enable_scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendEquation(gl.GL_FUNC_ADD) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glDisable(gl.GL_CULL_FACE) gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_SCISSOR_TEST) #gl.glActiveTexture(gl.GL_TEXTURE0) gl.glViewport(0, 0, int(fb_width), int(fb_height)) ortho_projection = (ctypes.c_float * 16)(2.0 / display_width, 0.0, 0.0, 0.0, 0.0, 2.0 / -display_height, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0) self.prog["ProjMtx"] = ortho_projection for commands in draw_data.commands_lists: idx_buffer_offset = 0 array_type = c_ubyte * commands.vtx_buffer_size * imgui.VERTEX_SIZE data_carray = array_type.from_address(commands.vtx_buffer_data) if not ( imgui.VERTEX_BUFFER_POS_OFFSET == 0 and \ imgui.VERTEX_BUFFER_UV_OFFSET == 8 and \ imgui.VERTEX_BUFFER_COL_OFFSET == 16 ): log.error( "GlumpyRenderer.render(): imgui vertex buffer layout has changed ! notify the developers ..." ) return #TODO: this is a bit convoluted; Imgui delivers uint8 colors, but glumpy wants float32's dtype = [('Position', np.float32, 2), ('UV', np.float32, 2), ('Color', np.uint8, 4)] vao_content = np.frombuffer(data_carray, dtype=dtype) dtype2 = [('Position', np.float32, 2), ('UV', np.float32, 2), ('Color', np.float32, 4)] vao_content_f = np.zeros(vao_content.shape, dtype=dtype2) for i, val in enumerate(vao_content): vao_content_f[i] = vao_content[i] vao_content_f[i]['Color'] /= 255 v_array = vao_content_f.view(gloo.VertexArray) self.prog.bind(v_array) if imgui.INDEX_SIZE == 1: dtype = np.uint8 if imgui.INDEX_SIZE == 2: dtype = np.uint16 if imgui.INDEX_SIZE == 4: dtype = np.uint32 # gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, self._elements_handle) # # todo: check this (sizes) # gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER, commands.idx_buffer_size * imgui.INDEX_SIZE, ctypes.c_void_p(commands.idx_buffer_data), gl.GL_STREAM_DRAW) array_type = c_ubyte * commands.idx_buffer_size * imgui.INDEX_SIZE data_carray = array_type.from_address(commands.idx_buffer_data) idx_content = np.frombuffer(data_carray, dtype=dtype) for command in commands.commands: # TODO: ImGui Images will not work yet, homogenizing texture id # allocation by imgui/glumpy is likely a larger issue # #accessing command.texture_id crashes the prog # #gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id ) x, y, z, w = command.clip_rect gl.glScissor(int(x), int(fb_height - w), int(z - x), int(w - y)) idx_array = idx_content[idx_buffer_offset:( idx_buffer_offset + command.elem_count)].view( gloo.IndexBuffer) self.prog.draw(mode=gl.GL_TRIANGLES, indices=idx_array) idx_buffer_offset += command.elem_count # restore modified GL state gl.glUseProgram(last_program) gl.glActiveTexture(last_active_texture) gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture) gl.glBindVertexArray(last_vertex_array) gl.glBindBuffer(gl.GL_ARRAY_BUFFER, last_array_buffer) gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer) gl.glBlendEquationSeparate(last_blend_equation_rgb, last_blend_equation_alpha) gl.glBlendFunc(last_blend_src, last_blend_dst) if last_enable_blend: gl.glEnable(gl.GL_BLEND) else: gl.glDisable(gl.GL_BLEND) if last_enable_cull_face: gl.glEnable(gl.GL_CULL_FACE) else: gl.glDisable(gl.GL_CULL_FACE) if last_enable_depth_test: gl.glEnable(gl.GL_DEPTH_TEST) else: gl.glDisable(gl.GL_DEPTH_TEST) if last_enable_scissor_test: gl.glEnable(gl.GL_SCISSOR_TEST) else: gl.glDisable(gl.GL_SCISSOR_TEST) gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2], last_viewport[3]) gl.glScissor(last_scissor_box[0], last_scissor_box[1], last_scissor_box[2], last_scissor_box[3]) log.debug("----------------------end---------------------------------")
def on_mouse_press(x, y, button): _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) program["iMouse"] = 2 * x / w - 1, 1 - 2 * y / h
def on_draw(dt): window.clear() _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) program.draw(gl.GL_TRIANGLE_STRIP) program['iGlobalTime'] += dt
def on_mouse_drag(x, y, dx, dy, button): global translate, scale _, _, w, h = gl.glGetIntegerv(gl.GL_VIEWPORT) translate = [translate[0] + dx, translate[1] - dy] program['u_translate'] = translate