def h_clip_begin(bounds, padding=[0, 0, 0, 0]): vp = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_VIEWPORT, vp) B = [bounds[0]+padding[0], bounds[1]+padding[1], bounds[2]-padding[2]*2, bounds[3]-padding[3]*2] # Do some math to invert the coords scp = [0, 0, int(B[2]), int(B[3])] scp[0] = int(B[0] + vp[0]) scp[1] = int(vp[1] + (vp[3] - B[1] - B[3])) bgl.glEnable(bgl.GL_SCISSOR_TEST) bgl.glClearColor(0.0, 0.0, 0.0, 0.0) bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_SCISSOR_BIT) bgl.glScissor(*scp)
def draw(self, position=[0, 0]): if self.visible: super().draw() if self.scroll_bar and self.scrolling: self.scroll_bar.draw() if self.scrolling and self.type == 'PANEL': # cur_scissor = None # if bgl.glIsEnabled(bgl.GL_SCISSOR_TEST) == 1: # cur_scissor = bgl.Buffer(bgl.GL_INT, 4) # bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, cur_scissor) # bgl.glDisable(bgl.GL_SCISSOR_TEST) pos = [position[0]+self.scale_pos_offset[0], position[1]+self.scale_pos_offset[1]] clip_pos = [pos[0]+self.horizontal_margin, pos[1]-self.vertical_margin] offset = 0 # if pos[1]+self.scroll_offset > position[1]: # offset = pos[1]+self.scroll_offset-position[1] bgl.glEnable(bgl.GL_SCISSOR_TEST) bgl.glScissor( round(pos[0]+self.horizontal_margin*self.scale), round(pos[1]-self.height*self.scale + self.vertical_margin*self.scale), round((self.width-self.horizontal_margin*2)*self.scale), round((self.height-self.vertical_margin*2-offset)*self.scale) ) for cont in self.containers: if self.collapse == False or cont == self.header_box: cont.draw() if self.scrolling and self.type == 'PANEL': bgl.glDisable(bgl.GL_SCISSOR_TEST) # if cur_scissor: # bgl.glEnable(bgl.GL_SCISSOR_TEST) # bgl.glScissor(cur_scissor[0],cur_scissor[1],cur_scissor[2],cur_scissor[3]) return
def gen_screenshot_texture(x, y, w, h, mode=None): scissor_is_enabled = bgl.Buffer(bgl.GL_BYTE, 1) bgl.glGetIntegerv(bgl.GL_SCISSOR_TEST, scissor_is_enabled) scissor_box = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, scissor_box) bgl.glEnable(bgl.GL_SCISSOR_TEST) bgl.glScissor(x, y, w, h) mode_bak = bgl.Buffer(bgl.GL_INT, 1) bgl.glGetIntegerv(bgl.GL_READ_BUFFER, mode_bak) if mode is not None: bgl.glReadBuffer(mode) pixels = bgl.Buffer(bgl.GL_BYTE, 4 * w * h) # RGBAにしないと斜めになる # GL_UNSIGNED_BYTEでないと色が僅かにずれる bgl.glReadPixels(x, y, w, h, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, pixels) bgl.glFinish() if mode is not None: bgl.glReadBuffer(mode_bak[0]) # 反転。確認用 # for i in range(4 * w * h): # if (i % 4) != 3: # pixels[i] = 255 - pixels[i] tex = gen_texture() bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex) bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, w, h, 0, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, pixels) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) if not scissor_is_enabled[0]: bgl.glDisable(bgl.GL_SCISSOR_TEST) bgl.glScissor(*scissor_box) return tex
def gen_screenshot_texture(x, y, w, h): """window全体のスクショを撮る :rtype: int """ def gen_texture(): textures = bgl.Buffer(bgl.GL_INT, 1) bgl.glGenTextures(1, textures) tex = textures[0] bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_LINEAR) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_LINEAR) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) return tex with vagl.GLSettings.push_attrib(): bgl.glEnable(bgl.GL_SCISSOR_TEST) bgl.glScissor(x, y, w, h) tex = gen_texture() bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex) bgl.glCopyTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, 0, 0, w, h, 0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0) return tex
def draw_to_viewport(view_min, view_max, show_extra, label_counter, tilegrid, sprytile_data, cursor_loc, region, rv3d, middle_btn, context): """Draw the offscreen texture into the viewport""" # Prepare some data that will be used for drawing grid_size = SprytileGui.loaded_grid.grid tile_sel = SprytileGui.loaded_grid.tile_selection padding = SprytileGui.loaded_grid.padding margin = SprytileGui.loaded_grid.margin is_pixel = sprytile_utils.grid_is_single_pixel(SprytileGui.loaded_grid) # Draw work plane SprytileGui.draw_work_plane(grid_size, sprytile_data, cursor_loc, region, rv3d, middle_btn) # Setup GL for drawing the offscreen texture bgl.glColor4f(1.0, 1.0, 1.0, 1.0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, SprytileGui.texture) # Backup texture settings old_mag_filter = Buffer(bgl.GL_INT, 1) glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, old_mag_filter) old_wrap_S = Buffer(GL_INT, 1) old_wrap_T = Buffer(GL_INT, 1) glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, old_wrap_S) glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, old_wrap_T) # Set texture filter bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) bgl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glEnable(bgl.GL_BLEND) # Draw the preview tile if middle_btn is False: SprytileGui.draw_preview_tile(context, region, rv3d) # Calculate actual view size view_size = int(view_max.x - view_min.x), int(view_max.y - view_min.y) # Save the original scissor box, and then set new scissor setting scissor_box = bgl.Buffer(bgl.GL_INT, [4]) bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, scissor_box) bgl.glScissor( int(view_min.x) + scissor_box[0], int(view_min.y) + scissor_box[1], view_size[0], view_size[1]) # Draw the tile select UI SprytileGui.draw_tile_select_ui(view_min, view_max, view_size, SprytileGui.tex_size, grid_size, tile_sel, padding, margin, show_extra, is_pixel) # restore opengl defaults bgl.glScissor(scissor_box[0], scissor_box[1], scissor_box[2], scissor_box[3]) bgl.glLineWidth(1) bgl.glTexParameteriv(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, old_mag_filter) bgl.glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, old_wrap_S) bgl.glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, old_wrap_T) # Draw label font_id = 0 font_size = 16 pad = 5 if label_counter > 0: import math def ease_out_circ(t, b, c, d): t /= d t -= 1 return c * math.sqrt(1 - t * t) + b box_pad = font_size + (pad * 2) fade = label_counter fade = ease_out_circ(fade, 0, SprytileGui.label_frames, SprytileGui.label_frames) fade /= SprytileGui.label_frames bgl.glColor4f(0.0, 0.0, 0.0, 0.75 * fade) bgl.glBegin(bgl.GL_QUADS) uv = [(0, 0), (0, 1), (1, 1), (1, 0)] vtx = [(view_min.x, view_max.y), (view_min.x, view_max.y + box_pad), (view_max.x, view_max.y + +box_pad), (view_max.x, view_max.y)] for i in range(4): glTexCoord2f(uv[i][0], uv[i][1]) glVertex2f(vtx[i][0], vtx[i][1]) bgl.glEnd() bgl.glColor4f(1.0, 1.0, 1.0, 1.0 * fade) blf.size(font_id, font_size, 72) x_pos = view_min.x + pad y_pos = view_max.y + pad label_string = "%dx%d" % (tilegrid.grid[0], tilegrid.grid[1]) if tilegrid.name != "": label_string = "%s - %s" % (label_string, tilegrid.name) blf.position(font_id, x_pos, y_pos, 0) blf.draw(font_id, label_string) if tilegrid.grid[0] == 1 and tilegrid.grid[1] == 1: size_text = "%dx%d" % (tile_sel[2], tile_sel[3]) blf.size(font_id, font_size, 72) size = blf.dimensions(font_id, size_text) x_pos = view_max.x - size[0] - pad y_pos = view_max.y + pad blf.position(font_id, x_pos, y_pos, 0) blf.draw(font_id, size_text) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_TEXTURE_2D) bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
def draw_callback_px(self, context): use_texture = self.use_texture if not use_texture: if context.region != self.region: return U = context.user_preferences prefs = MouseGesturePreferences.get_instance() dpi = U.system.dpi widget_unit = int((PIXEL_SIZE * dpi * 20 + 36) / 72) font_id = 0 theme_style = U.ui_styles[0] blf.size(font_id, theme_style.widget.points, dpi) bgl.glEnable(bgl.GL_BLEND) bgl.glColor3f(*U.themes['Default'].view_3d.space.text_hi) win = context.window w, h = win.width, win.height if use_texture: bgl.glDisable(bgl.GL_SCISSOR_TEST) with window_space(win): if use_texture: draw_texture(0, 0, w, h, self.texture_back) # draw origin coords = self.coords + [self.mco] bgl.glLineWidth(2) r1 = prefs.threshold r2 = r1 + 5 x, y = coords[0] bgl.glBegin(bgl.GL_LINES) for i in range(4): a = math.pi / 2 * i + math.pi / 4 bgl.glVertex2f(x + r1 * math.cos(a), y + r1 * math.sin(a)) bgl.glVertex2f(x + r2 * math.cos(a), y + r2 * math.sin(a)) bgl.glEnd() bgl.glLineWidth(1) # draw lines bgl.glEnable(bgl.GL_LINE_STIPPLE) bgl.glLineStipple(1, int(0b101010101010101)) # (factor, pattern) bgl.glBegin(bgl.GL_LINE_STRIP) for v in coords: bgl.glVertex2f(v[0], v[1]) bgl.glEnd() bgl.glLineStipple(1, 1) bgl.glDisable(bgl.GL_LINE_STIPPLE) if use_texture: bgl.glEnable(bgl.GL_SCISSOR_TEST) # draw txt xmin, ymin, xmax, ymax = self.region_drawing_rectangle( context, self.area, self.region) xmin += self.region.x ymin += self.region.y xmax += self.region.x ymax += self.region.y if self.area.type == 'VIEW_3D': if U.view.show_mini_axis: # view3d_draw.c: 1019: draw_selected_name() posx = xmin + widget_unit + U.view.mini_axis_size * 2 else: # view3d_draw.c: 928: draw_selected_name() posx = xmin + 1.5 * widget_unit posy = ymin + widget_unit * 1.5 else: posx = xmin + widget_unit * 0.5 posy = ymin + widget_unit * 0.5 char_space = 5 group = prefs.gesture_groups.get(self.group) use_relative = group and group.use_relative gesture = self.gesture_rel if use_relative else self.gesture_abs # 文字はregionに収まるように(はみ出すと見た目が悪いから) scissor_box = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, scissor_box) bgl.glScissor(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1) with window_space(win): if gesture: x = posx for txt in gesture: blf.position(font_id, x, posy, 0) blf.draw(font_id, txt) text_width, text_height = blf.dimensions(font_id, txt) x += text_width + char_space if self.item: blf.position(font_id, posx, posy + widget_unit, 0) blf.draw(font_id, self.item.name) bgl.glScissor(*scissor_box) # restore opengl defaults bgl.glLineWidth(1) bgl.glDisable(bgl.GL_BLEND) bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
def glScissor(x, y, width, height): bgl.glScissor(x, y, width, height)
def _set(self, instance, value): glScissor(int(value[0]), int(value[1]), int(value[2]), int(value[3]))
def drawCallback(self, context): # use ImageTexture.evaluate / get Image.pixels in case of View2D? # seems unnecessary to restore afterwards. bgl.glViewport(*self.viewport) bgl.glScissor(*self.viewport) b = bgl.Buffer(bgl.GL_FLOAT, 3) for p in itertools.chain(self.points, self.current_mouse_points): bgl.glReadPixels(p[0], p[1], 1, 1, bgl.GL_RGB, bgl.GL_FLOAT, b) p[-1] = self.convertColorspace(context, b.to_list()) points_drawn = 0 max_points_to_draw = PTS_LIMIT - self.remaining last_point_drawn = None for p in self.points: if points_drawn == max_points_to_draw: break self.drawPoint(*p[2:5]) last_point_drawn = p points_drawn += 1 # under cursor -> probably not wanted for p in self.current_mouse_points[:-1]: if points_drawn == max_points_to_draw: break self.drawPoint(*p[2:5]) last_point_drawn = p points_drawn += 1 segments_drawn = 0 if len(self.points) > 1: ps = iter(self.points) next(ps) for p1, p2 in zip(self.points, ps): if segments_drawn == max_points_to_draw - 1: break self.drawSegment(p1[2], p1[3], p2[2], p2[3], False) segments_drawn += 1 if self.current_mouse_points: p1 = self.points[-1] p2 = self.current_mouse_points[0] if segments_drawn < max_points_to_draw - 1: self.drawSegment(p1[2], p1[3], p2[2], p2[3], True) segments_drawn += 1 if len(self.current_mouse_points) > 1: ps = iter(self.current_mouse_points) next(ps) for p1, p2 in zip(self.current_mouse_points, ps): if segments_drawn == max_points_to_draw - 1: break self.drawSegment(p1[2], p1[3], p2[2], p2[3], True) segments_drawn += 1 if points_drawn == max_points_to_draw: # this means even the point under the cursor _is_ already # surnumerary, thus we have to draw the last segment in red if self.current_mouse_points: p1 = last_point_drawn p2 = self.current_mouse_points[-1] self.drawSegment(p1[2], p1[3], p2[2], p2[3], True, True) if context.window_manager.crd_show_values: for p, i in zip(itertools.chain(self.points, self.current_mouse_points), range(points_drawn)): blf.position(0, p[2] + 10, p[3] - 10, 0) bgl.glColor4f(0.8, 0.8, 0.8, 1.0) blf.size(0, 10, context.user_preferences.system.dpi) blf.draw(0, "(%.3f, %.3f, %.3f)" % tuple(p[-1])) if points_drawn <= max_points_to_draw: # we want to draw the current color anyways if self.current_mouse_points: p = self.current_mouse_points[-1] blf.position(0, p[2] + 10, p[3] - 10, 0) bgl.glColor4f(0.8, 0.8, 0.8, 1.0) blf.size(0, 10, context.user_preferences.system.dpi) blf.draw(0, "(%.3f, %.3f, %.3f)" % tuple(p[-1])) bgl.glColor4f(0.8, 0.8, 0.8, 1.0) blf.size(0, 20, context.user_preferences.system.dpi) if context.window_manager.crd_use_intermediate: blf.position(0, 10, 60, 0) blf.draw(0, "Oversamples:") blf.position(0, 160, 60, 0) blf.draw(0, str(context.window_manager.crd_intermediate_amount)) blf.position(0, 10, 30, 0) blf.draw(0, "Path Type:") blf.position(0, 160, 30, 0) blf.draw(0, {"POLYLINE": "Polyline", "CUBIC_SPLINE": "Cubic Spline"}[context.window_manager.crd_path_type])
def draw_callback(cls, context): prefs = compat.get_user_preferences(context).addons[__package__].preferences if context.window.as_pointer() != cls.origin["window"]: return # Not match target window. rect = cls.calc_draw_area_rect(context) if not rect: return # No draw target. draw_area_min_x, draw_area_min_y, draw_area_max_x, draw_area_max_y = rect _, _, _, origin_x, origin_y = cls.get_origin(context) width = draw_area_max_x - origin_x height = draw_area_max_y - origin_y if width == height == 0: return region = context.region area = context.area if region.type == 'WINDOW': region_min_x, region_min_y, region_max_x, region_max_y = get_window_region_rect(area) else: region_min_x = region.x region_min_y = region.y region_max_x = region.x + region.width - 1 region_max_y = region.y + region.height - 1 if not intersect_aabb( [region_min_x, region_min_y], [region_max_x, region_max_y], [draw_area_min_x + 1, draw_area_min_y + 1], [draw_area_max_x - 1, draw_area_max_x - 1]): # We don't need to draw if draw area is not overlapped with region. return current_time = time.time() region_drawn = False font_size = prefs.font_size font_id = 0 dpi = compat.get_user_preferences(context).system.dpi blf.size(font_id, font_size, dpi) scissor_box = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, scissor_box) # Clip 'TOOLS' and 'UI' region from 'WINDOW' region if need. # This prevents from drawing multiple time when # user_preferences.system.use_region_overlap is True. if context.area.type == 'VIEW_3D' and region.type == 'WINDOW': x_min, y_min, x_max, y_max = get_region_rect_on_v3d(context) bgl.glScissor(x_min, y_min, x_max - x_min + 1, y_max - y_min + 1) # Get string height in draw area. sh = blf.dimensions(0, string.printable)[1] x = origin_x - region.x y = origin_y - region.y # Draw last operator. operator_history = cls.removed_old_operator_history() if prefs.show_last_operator and operator_history: time_, bl_label, idname_py, _ = operator_history[-1] if current_time - time_ <= prefs.display_time: compat.set_blf_font_color(font_id, *prefs.color, 1.0) # Draw operator text. text = bpy.app.translations.pgettext_iface(bl_label, "Operator") text += " ('{}')".format(idname_py) offset_x = cls.get_text_offset_for_alignment(font_id, text, context) blf.position(font_id, x + offset_x, y, 0) if prefs.background: draw_text_background(text, font_id, x + offset_x, y, prefs.color_background) draw_text(text, font_id, prefs.color, prefs.shadow, prefs.color_shadow) y += sh + sh * cls.HEIGHT_RATIO_FOR_SEPARATOR * 0.2 # Draw separator. sw = blf.dimensions(font_id, "Left Mouse")[0] offset_x = cls.get_offset_for_alignment(sw, context) draw_line([x + offset_x, y], [x + sw + offset_x, y], prefs.color, prefs.shadow, prefs.color_shadow) y += sh * cls.HEIGHT_RATIO_FOR_SEPARATOR * 0.8 region_drawn = True else: y += sh + sh * cls.HEIGHT_RATIO_FOR_SEPARATOR # Draw hold modifier keys. drawing = False # TODO: Need to check if drawing is now on progress. compat.set_blf_font_color(font_id, *prefs.color, 1.0) margin = sh * 0.2 if cls.hold_modifier_keys or drawing: mod_keys = cls.sorted_modifier_keys(cls.hold_modifier_keys) if drawing: text = "" else: text = " + ".join(mod_keys) offset_x = cls.get_text_offset_for_alignment(font_id, text, context) # Draw rounded box. box_height = sh + margin * 2 box_width = blf.dimensions(font_id, text)[0] + margin * 2 draw_rounded_box(x - margin + offset_x, y - margin, box_width, box_height, box_height * 0.2, prefs.background, prefs.color_background) # Draw key text. blf.position(font_id, x + offset_x, y + margin, 0) draw_text(text, font_id, prefs.color, prefs.shadow, prefs.color_shadow) bgl.glColor4f(*prefs.color, 1.0) region_drawn = True y += sh + margin * 2 # Draw event history. event_history = cls.removed_old_event_history() y += sh * cls.HEIGHT_RATIO_FOR_SEPARATOR for _, event_type, modifiers, repeat_count in event_history[::-1]: color = prefs.color compat.set_blf_font_color(font_id, *color, 1.0) text = get_display_event_text(event_type.name) if modifiers: mod_keys = cls.sorted_modifier_keys(modifiers) text = "{} + {}".format(" + ".join(mod_keys), text) if repeat_count > 1: text += " x{}".format(repeat_count) offset_x = cls.get_text_offset_for_alignment(font_id, text, context) blf.position(font_id, x + offset_x, y, 0) if prefs.background: draw_text_background(text, font_id, x + offset_x, y, prefs.color_background) draw_text(text, font_id, prefs.color, prefs.shadow, prefs.color_shadow) y += sh region_drawn = True bgl.glDisable(bgl.GL_BLEND) bgl.glScissor(*scissor_box) bgl.glLineWidth(1.0) if region_drawn: cls.draw_regions_prev.add(region.as_pointer())
def render(self, draw_data): io = self.io shader = self._bl_shader 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 ( last_program, last_texture, last_active_texture, last_array_buffer, last_element_array_buffer, last_vertex_array, last_blend_src, last_blend_dst, last_blend_equation_rgb, last_blend_equation_alpha, last_viewport, last_scissor_box, ) = self._backup_integers( gl.GL_CURRENT_PROGRAM, 1, gl.GL_TEXTURE_BINDING_2D, 1, gl.GL_ACTIVE_TEXTURE, 1, gl.GL_ARRAY_BUFFER_BINDING, 1, gl.GL_ELEMENT_ARRAY_BUFFER_BINDING, 1, gl.GL_VERTEX_ARRAY_BINDING, 1, gl.GL_BLEND_SRC, 1, gl.GL_BLEND_DST, 1, gl.GL_BLEND_EQUATION_RGB, 1, gl.GL_BLEND_EQUATION_ALPHA, 1, gl.GL_VIEWPORT, 4, gl.GL_SCISSOR_BOX, 4, ) 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 = (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) shader.bind() shader.uniform_float("ProjMtx", ortho_projection) shader.uniform_int("Texture", 0) for commands in draw_data.commands_lists: size = commands.idx_buffer_size * imgui.INDEX_SIZE // 4 address = commands.idx_buffer_data ptr = C.cast(address, C.POINTER(C.c_int)) idx_buffer_np = np.ctypeslib.as_array(ptr, shape=(size, )) size = commands.vtx_buffer_size * imgui.VERTEX_SIZE // 4 address = commands.vtx_buffer_data ptr = C.cast(address, C.POINTER(C.c_float)) vtx_buffer_np = np.ctypeslib.as_array(ptr, shape=(size, )) vtx_buffer_shaped = vtx_buffer_np.reshape(-1, imgui.VERTEX_SIZE // 4) idx_buffer_offset = 0 for command in commands.commands: x, y, z, w = command.clip_rect gl.glScissor(int(x), int(fb_height - w), int(z - x), int(w - y)) vertices = vtx_buffer_shaped[:, :2] uvs = vtx_buffer_shaped[:, 2:4] colors = vtx_buffer_shaped.view(np.uint8)[:, 4 * 4:] colors = colors.astype('f') / 255.0 indices = idx_buffer_np[idx_buffer_offset:idx_buffer_offset + command.elem_count] gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id) batch = batch_for_shader(shader, 'TRIS', { "Position": vertices, "UV": uvs, "Color": colors, }, indices=indices) batch.draw(shader) 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])
def _set_scissor(): assert ScissorStack.started and ScissorStack.stack bgl.glScissor(*ScissorStack.stack[-1])
def draw_callback(cls, context): prefs = ScreenCastKeysPreferences.get_instance() """:type: ScreenCastKeysPreferences""" if context.window.as_pointer() != cls.origin['window']: return rect = cls.calc_draw_rectangle(context) if not rect: return xmin, ymin, xmax, ymax = rect win, _area, _region, x, y = cls.get_origin(context) w = xmax - x h = ymax - y if w == h == 0: return region = context.region area = context.area if region.type == 'WINDOW': r_xmin, r_ymin, r_xmax, r_ymax = region_window_rectangle(area) else: r_xmin, r_ymin, r_xmax, r_ymax = ( region.x, region.y, region.x + region.width - 1, region.y + region.height - 1) if not intersect_aabb( (r_xmin, r_ymin), (r_xmax, r_ymax), (xmin + 1, ymin + 1), (xmax - 1, ymax - 1)): return current_time = time.time() draw_any = False font_size = prefs.font_size font_id = 0 dpi = context.user_preferences.system.dpi blf.size(font_id, font_size, dpi) def draw_text(text): col = prefs.color_shadow bgl.glColor4f(*col[:3], col[3] * 20) blf.blur(font_id, 5) blf.draw(font_id, text) blf.blur(font_id, 0) bgl.glColor3f(*prefs.color) blf.draw(font_id, text) def draw_line(p1, p2): bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glLineWidth(3.0) bgl.glColor4f(*prefs.color_shadow) bgl.glBegin(bgl.GL_LINES) bgl.glVertex2f(*p1) bgl.glVertex2f(*p2) bgl.glEnd() bgl.glLineWidth(1.0 if prefs.color_shadow[-1] == 0.0 else 1.5) bgl.glColor3f(*prefs.color) bgl.glBegin(bgl.GL_LINES) bgl.glVertex2f(*p1) bgl.glVertex2f(*p2) bgl.glEnd() bgl.glLineWidth(1.0) bgl.glDisable(bgl.GL_LINE_SMOOTH) # user_preferences.system.use_region_overlapが真の場合に、 # 二重に描画されるのを防ぐ glscissorbox = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, glscissorbox) if context.area.type == 'VIEW_3D' and region.type == 'WINDOW': xmin, ymin, xmax, ymax = region_rectangle_v3d(context) bgl.glScissor(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1) th = blf.dimensions(0, string.printable)[1] px = x - region.x py = y - region.y operator_log = cls.removed_old_operator_log() if prefs.show_last_operator and operator_log: t, name, idname_py, addr = operator_log[-1] if current_time - t <= prefs.display_time: color = prefs.color bgl.glColor3f(*color) text = bpy.app.translations.pgettext_iface(name, 'Operator') text += " ('{}')".format(idname_py) blf.position(font_id, px, py, 0) draw_text(text) py += th + th * cls.SEPARATOR_HEIGHT * 0.2 tw = blf.dimensions(font_id, 'Left Mouse')[0] # 適当 draw_line((px, py), (px + tw, py)) py += th * cls.SEPARATOR_HEIGHT * 0.8 draw_any = True else: py += th + th * cls.SEPARATOR_HEIGHT bgl.glColor3f(*prefs.color) if cls.hold_keys or mhm.is_rendering(): col = prefs.color_shadow[:3] + (prefs.color_shadow[3] * 2,) mod_names = cls.sorted_modifiers(cls.hold_keys) if mhm.is_rendering(): if 0: text = '- - -' else: text = '' else: text = ' + '.join(mod_names) blf.position(font_id, px, py, 0) # blf.draw(font_id, text) draw_text(text) py += th draw_any = True else: py += th event_log = cls.removed_old_event_log() if cls.hold_keys or event_log: py += th * cls.SEPARATOR_HEIGHT * 0.2 tw = blf.dimensions(font_id, 'Left Mouse')[0] # 適当 draw_line((px, py), (px + tw, py)) py += th * cls.SEPARATOR_HEIGHT * 0.8 draw_any = True else: py += th * cls.SEPARATOR_HEIGHT for event_time, event_type, modifiers, count in event_log[::-1]: color = prefs.color bgl.glColor3f(*color) text = event_type.names[event_type.name] if modifiers: mod_names = cls.sorted_modifiers(modifiers) text = ' + '.join(mod_names) + ' + ' + text if count > 1: text += ' x' + str(count) blf.position(font_id, px, py, 0) # blf.draw(font_id, text) draw_text(text) py += th draw_any = True bgl.glDisable(bgl.GL_BLEND) bgl.glScissor(*glscissorbox) bgl.glLineWidth(1.0) # blf.disable(font_id, blf.SHADOW) if draw_any: cls.draw_regions_prev.add(region.as_pointer())
def drawCallback(self, context): # use ImageTexture.evaluate / get Image.pixels in case of View2D? # seems unnecessary to restore afterwards. bgl.glViewport(*self.viewport) bgl.glScissor(*self.viewport) b = bgl.Buffer(bgl.GL_FLOAT, 3) for p in itertools.chain(self.points, self.current_mouse_points): bgl.glReadPixels(p[0], p[1], 1, 1, bgl.GL_RGB, bgl.GL_FLOAT, b) p[-1] = self.convertColorspace(context, b.to_list()) points_drawn = 0 max_points_to_draw = PTS_LIMIT - self.remaining last_point_drawn = None for p in self.points: if points_drawn == max_points_to_draw: break self.drawPoint(*p[2:5]) last_point_drawn = p points_drawn += 1 # under cursor -> probably not wanted for p in self.current_mouse_points[:-1]: if points_drawn == max_points_to_draw: break self.drawPoint(*p[2:5]) last_point_drawn = p points_drawn += 1 segments_drawn = 0 if len(self.points) > 1: ps = iter(self.points) next(ps) for p1, p2 in zip(self.points, ps): if segments_drawn == max_points_to_draw - 1: break self.drawSegment(p1[2], p1[3], p2[2], p2[3], False) segments_drawn += 1 if self.current_mouse_points: p1 = self.points[-1] p2 = self.current_mouse_points[0] if segments_drawn < max_points_to_draw - 1: self.drawSegment(p1[2], p1[3], p2[2], p2[3], True) segments_drawn += 1 if len(self.current_mouse_points) > 1: ps = iter(self.current_mouse_points) next(ps) for p1, p2 in zip(self.current_mouse_points, ps): if segments_drawn == max_points_to_draw - 1: break self.drawSegment(p1[2], p1[3], p2[2], p2[3], True) segments_drawn += 1 if points_drawn == max_points_to_draw: # this means even the point under the cursor _is_ already # surnumerary, thus we have to draw the last segment in red if self.current_mouse_points: p1 = last_point_drawn p2 = self.current_mouse_points[-1] self.drawSegment(p1[2], p1[3], p2[2], p2[3], True, True) if context.window_manager.crd_show_values: for p, i in zip( itertools.chain(self.points, self.current_mouse_points), range(points_drawn)): blf.position(0, p[2] + 10, p[3] - 10, 0) bgl.glColor4f(0.8, 0.8, 0.8, 1.0) blf.size(0, 10, context.user_preferences.system.dpi) blf.draw(0, "(%.3f, %.3f, %.3f)" % tuple(p[-1])) if points_drawn <= max_points_to_draw: # we want to draw the current color anyways if self.current_mouse_points: p = self.current_mouse_points[-1] blf.position(0, p[2] + 10, p[3] - 10, 0) bgl.glColor4f(0.8, 0.8, 0.8, 1.0) blf.size(0, 10, context.user_preferences.system.dpi) blf.draw(0, "(%.3f, %.3f, %.3f)" % tuple(p[-1])) bgl.glColor4f(0.8, 0.8, 0.8, 1.0) blf.size(0, 20, context.user_preferences.system.dpi) if context.window_manager.crd_use_intermediate: blf.position(0, 10, 60, 0) blf.draw(0, "Oversamples:") blf.position(0, 160, 60, 0) blf.draw(0, str(context.window_manager.crd_intermediate_amount)) blf.position(0, 10, 30, 0) blf.draw(0, "Path Type:") blf.position(0, 160, 30, 0) blf.draw(0, { 'POLYLINE': "Polyline", 'CUBIC_SPLINE': "Cubic Spline" }[context.window_manager.crd_path_type])
def draw_callback(cls, context): prefs = ScreenCastKeysPreferences.get_instance() """:type: ScreenCastKeysPreferences""" if context.window.as_pointer() != cls.origin['window']: return rect = cls.calc_draw_rectangle(context) if not rect: return xmin, ymin, xmax, ymax = rect win, _area, _region, x, y = cls.get_origin(context) w = xmax - x h = ymax - y if w == h == 0: return region = context.region area = context.area if region.type == 'WINDOW': r_xmin, r_ymin, r_xmax, r_ymax = region_window_rectangle(area) else: r_xmin, r_ymin, r_xmax, r_ymax = (region.x, region.y, region.x + region.width - 1, region.y + region.height - 1) if not intersect_aabb((r_xmin, r_ymin), (r_xmax, r_ymax), (xmin + 1, ymin + 1), (xmax - 1, ymax - 1)): return current_time = time.time() draw_any = False font_size = prefs.font_size font_id = 0 dpi = context.user_preferences.system.dpi blf.size(font_id, font_size, dpi) def draw_text(text): col = prefs.color_shadow bgl.glColor4f(*col[:3], col[3] * 20) blf.blur(font_id, 5) blf.draw(font_id, text) blf.blur(font_id, 0) bgl.glColor3f(*prefs.color) blf.draw(font_id, text) def draw_line(p1, p2): bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glLineWidth(3.0) bgl.glColor4f(*prefs.color_shadow) bgl.glBegin(bgl.GL_LINES) bgl.glVertex2f(*p1) bgl.glVertex2f(*p2) bgl.glEnd() bgl.glLineWidth(1.0 if prefs.color_shadow[-1] == 0.0 else 1.5) bgl.glColor3f(*prefs.color) bgl.glBegin(bgl.GL_LINES) bgl.glVertex2f(*p1) bgl.glVertex2f(*p2) bgl.glEnd() bgl.glLineWidth(1.0) bgl.glDisable(bgl.GL_LINE_SMOOTH) # user_preferences.system.use_region_overlapが真の場合に、 # 二重に描画されるのを防ぐ glscissorbox = bgl.Buffer(bgl.GL_INT, 4) bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, glscissorbox) if context.area.type == 'VIEW_3D' and region.type == 'WINDOW': xmin, ymin, xmax, ymax = region_rectangle_v3d(context) bgl.glScissor(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1) th = blf.dimensions(0, string.printable)[1] px = x - region.x py = y - region.y operator_log = cls.removed_old_operator_log() if prefs.show_last_operator and operator_log: t, name, idname_py, addr = operator_log[-1] if current_time - t <= prefs.display_time: color = prefs.color bgl.glColor3f(*color) text = bpy.app.translations.pgettext_iface(name, 'Operator') text += " ('{}')".format(idname_py) blf.position(font_id, px, py, 0) draw_text(text) py += th + th * cls.SEPARATOR_HEIGHT * 0.2 tw = blf.dimensions(font_id, 'Left Mouse')[0] # 適当 draw_line((px, py), (px + tw, py)) py += th * cls.SEPARATOR_HEIGHT * 0.8 draw_any = True else: py += th + th * cls.SEPARATOR_HEIGHT bgl.glColor3f(*prefs.color) if cls.hold_keys or mhm.is_rendering(): col = prefs.color_shadow[:3] + (prefs.color_shadow[3] * 2, ) mod_names = cls.sorted_modifiers(cls.hold_keys) if mhm.is_rendering(): if 0: text = '- - -' else: text = '' else: text = ' + '.join(mod_names) blf.position(font_id, px, py, 0) # blf.draw(font_id, text) draw_text(text) py += th draw_any = True else: py += th event_log = cls.removed_old_event_log() if cls.hold_keys or event_log: py += th * cls.SEPARATOR_HEIGHT * 0.2 tw = blf.dimensions(font_id, 'Left Mouse')[0] # 適当 draw_line((px, py), (px + tw, py)) py += th * cls.SEPARATOR_HEIGHT * 0.8 draw_any = True else: py += th * cls.SEPARATOR_HEIGHT for event_time, event_type, modifiers, count in event_log[::-1]: color = prefs.color bgl.glColor3f(*color) text = event_type.names[event_type.name] if modifiers: mod_names = cls.sorted_modifiers(modifiers) text = ' + '.join(mod_names) + ' + ' + text if count > 1: text += ' x' + str(count) blf.position(font_id, px, py, 0) # blf.draw(font_id, text) draw_text(text) py += th draw_any = True bgl.glDisable(bgl.GL_BLEND) bgl.glScissor(*glscissorbox) bgl.glLineWidth(1.0) # blf.disable(font_id, blf.SHADOW) if draw_any: cls.draw_regions_prev.add(region.as_pointer())
def draw_to_viewport(min, max, show_extra, label_counter, tilegrid, sprytile_data, cursor_loc, region, rv3d, middle_btn): """Draw the offscreen texture into the viewport""" # Prepare some data that will be used for drawing grid_size = SprytileGui.loaded_grid.grid # Draw world space axis plane SprytileGui.draw_plane_grid(grid_size, sprytile_data, cursor_loc, region, rv3d, middle_btn) # Setup GL for drawing offscreen texture bgl.glColor4f(1.0, 1.0, 1.0, 1.0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, SprytileGui.texture) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glEnable(bgl.GL_BLEND) view_size = int(max.x - min.x), int(max.y - min.y) # Save the original scissor box, and then set new scissor setting scissor_box = bgl.Buffer(bgl.GL_INT, [4]) bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, scissor_box) bgl.glScissor( int(min.x) + scissor_box[0], int(min.y) + scissor_box[1], view_size[0], view_size[1]) # Draw the texture in first bgl.glBegin(bgl.GL_QUADS) uv = [(0, 0), (0, 1), (1, 1), (1, 0)] vtx = [(min.x, min.y), (min.x, max.y), (max.x, max.y), (max.x, min.y)] for i in range(4): glTexCoord2f(uv[i][0], uv[i][1]) glVertex2f(vtx[i][0], vtx[i][1]) bgl.glEnd() if show_extra: # Draw the tile grid overlay # Translate the gl context by grid matrix tex_size = SprytileGui.tex_size scale_factor = (view_size[0] / tex_size[0], view_size[1] / tex_size[1]) offset_matrix = Matrix.Translation((min.x, min.y, 0)) grid_matrix = sprytile_utils.get_grid_matrix( SprytileGui.loaded_grid) grid_matrix = Matrix.Scale(scale_factor[0], 4, Vector( (1, 0, 0))) * Matrix.Scale(scale_factor[1], 4, Vector( (0, 1, 0))) * grid_matrix calc_matrix = offset_matrix * grid_matrix matrix_vals = [ calc_matrix[j][i] for i in range(4) for j in range(4) ] grid_buff = bgl.Buffer(bgl.GL_FLOAT, 16, matrix_vals) glPushMatrix() glLoadIdentity() glLoadMatrixf(grid_buff) glDisable(GL_TEXTURE_2D) glColor4f(0.0, 0.0, 0.0, 0.5) glLineWidth(1) # Draw the grid x_divs = ceil(tex_size[0] / grid_size[0]) y_divs = ceil(tex_size[1] / grid_size[1]) # x_size = (grid_size[0] / tex_size[0]) * (max.x - min.x) # y_size = (grid_size[1] / tex_size[1]) * (max.y - min.y) x_end = x_divs * grid_size[0] y_end = y_divs * grid_size[1] for x in range(x_divs + 1): x_pos = (x * grid_size[0]) glBegin(GL_LINES) glVertex2f(x_pos, 0) glVertex2f(x_pos, y_end) glEnd() for y in range(y_divs + 1): y_pos = (y * grid_size[1]) glBegin(GL_LINES) glVertex2f(0, y_pos) glVertex2f(x_end, y_pos) glEnd() glPopMatrix() # restore opengl defaults bgl.glScissor(scissor_box[0], scissor_box[1], scissor_box[2], scissor_box[3]) bgl.glLineWidth(1) if label_counter > 0: import math def ease_out_circ(t, b, c, d): t /= d t -= 1 return c * math.sqrt(1 - t * t) + b font_id = 0 font_size = 16 pad = 5 box_pad = font_size + (pad * 2) fade = label_counter fade = ease_out_circ(fade, 0, SprytileGui.label_frames, SprytileGui.label_frames) fade /= SprytileGui.label_frames bgl.glColor4f(0.0, 0.0, 0.0, 0.75 * fade) bgl.glBegin(bgl.GL_QUADS) uv = [(0, 0), (0, 1), (1, 1), (1, 0)] vtx = [(min.x, max.y), (min.x, max.y + box_pad), (max.x, max.y + +box_pad), (max.x, max.y)] for i in range(4): glTexCoord2f(uv[i][0], uv[i][1]) glVertex2f(vtx[i][0], vtx[i][1]) bgl.glEnd() bgl.glColor4f(1.0, 1.0, 1.0, 1.0 * fade) blf.size(font_id, font_size, 72) x_pos = min.x + pad y_pos = max.y + pad label_string = "%dx%d" % (tilegrid.grid[0], tilegrid.grid[1]) if tilegrid.name != "": label_string = "%s - %s" % (label_string, tilegrid.name) blf.position(font_id, x_pos, y_pos, 0) blf.draw(font_id, label_string) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_TEXTURE_2D) bgl.glColor4f(0.0, 0.0, 0.0, 1.0)