def draw_callback(self, op, context): # Force Stop if self.is_handler_list_empty(): self.unregister_handler() return bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glColorMask(bgl.GL_FALSE, bgl.GL_FALSE, bgl.GL_FALSE, bgl.GL_FALSE) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) self.fill_batch.draw(self.fill_shader) bgl.glColorMask(bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glDepthMask(bgl.GL_FALSE) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) self.line_batch.draw(self.line_shader) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDisable(bgl.GL_DEPTH_TEST)
def draw_circle_2d(position, color, radius, segments=32, batch_type='TRI_FAN'): from math import sin, cos, pi import gpu from gpu.types import ( GPUBatch, GPUVertBuf, GPUVertFormat, ) if segments <= 0: raise ValueError("Amount of segments must be greater than 0.") set_line_smooth() bgl.glDepthMask(False) with gpu.matrix.push_pop(): gpu.matrix.translate(position) gpu.matrix.scale_uniform(radius) mul = (1.0 / (segments - 1)) * (pi * 2) verts = [(sin(i * mul), cos(i * mul)) for i in range(segments)] fmt = GPUVertFormat() pos_id = fmt.attr_add(id="pos", comp_type='F32', len=2, fetch_mode='FLOAT') vbo = GPUVertBuf(len=len(verts), format=fmt) vbo.attr_fill(id=pos_id, data=verts) batch = GPUBatch(type=batch_type, buf=vbo) shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') batch.program_set(shader) shader.uniform_float("color", color) batch.draw() set_line_smooth(False)
def draw_3d_points(context, points, size, color=(1, 0, 0, 1)): region = context.region rv3d = context.space_data.region_3d bgl.glEnable(bgl.GL_POINT_SMOOTH) bgl.glPointSize(size) # bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glDepthRange(0, 0.9990) # squeeze depth just a bit bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthMask(bgl.GL_FALSE) # do not overwrite depth bgl.glBegin(bgl.GL_POINTS) # draw red bgl.glColor4f(*color) for coord in points: vector3d = (coord.x, coord.y, coord.z) bgl.glVertex3f(*vector3d) # vector2d = view3d_utils.location_3d_to_region_2d(region, rv3d, vector3d) # bgl.glVertex2f(*vector2d) bgl.glEnd() bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0.0, 1.0) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDisable(bgl.GL_POINT_SMOOTH) bgl.glDisable(bgl.GL_POINTS) return
def _draw_points_callback(self, draw_manager, object_anchor, positions, colors): """A callback function to draw a point cloud in Blender's 3D view.""" handle_is_valid = True try: # Check if object still exists object_anchor_name = object_anchor.name except: handle_is_valid = False if handle_is_valid: if object_anchor_name in bpy.data.objects: # Use the visibility of the object to enable / # disable the drawing of the point cloud if bpy.data.objects[object_anchor_name].visible_get(): # Update the batch depending on the anchor pose (only if # necessary) object_anchor_has_changed = not np.array_equal( self._object_anchor_pose_previous, object_anchor.matrix_world, ) if self._batch_cached is None or object_anchor_has_changed: self._object_anchor_pose_previous = np.copy( object_anchor.matrix_world) transf_pos_list = _compute_transformed_coords( object_anchor.matrix_world, positions) self._batch_cached = batch_for_shader( self._shader, "POINTS", { "pos": transf_pos_list, "color": colors }, ) self._shader.bind() bgl.glPointSize(self._point_size) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_TRUE) self._batch_cached.draw(self._shader) else: log_report("INFO", "Removing draw handler of deleted point cloud handle") if self._draw_handler_handle is not None: bpy.types.SpaceView3D.draw_handler_remove( self._draw_handler_handle, "WINDOW") self._draw_handler_handle = None self._batch_cached = None draw_manager.delete_anchor(object_anchor)
def draw_callback(self, op, context): if not self.is_visible(): return # Force Stop wireframe_image = find_bpy_image_by_name(Config.coloring_texture_name) if self.is_handler_list_empty() or \ not self._check_coloring_image(wireframe_image): self.unregister_handler() return bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) bgl.glColorMask(bgl.GL_FALSE, bgl.GL_FALSE, bgl.GL_FALSE, bgl.GL_FALSE) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) self.fill_batch.draw(self.fill_shader) bgl.glColorMask(bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glDepthMask(bgl.GL_FALSE) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_LINE) bgl.glEnable(bgl.GL_DEPTH_TEST) if not self._use_simple_shader: # coloring_image.bindcode should not be zero # if we don't want to destroy video driver in Blender if not wireframe_image or wireframe_image.bindcode == 0: self.switch_to_simple_shader() else: bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, wireframe_image.bindcode) self.line_shader.bind() self.line_shader.uniform_int('image', 0) self.line_shader.uniform_float('opacity', self._opacity) self.line_batch.draw(self.line_shader) if self._use_simple_shader: self.simple_line_shader.bind() self.simple_line_shader.uniform_float( 'color', ((*self._colors[0][:3], self._opacity))) self.simple_line_batch.draw(self.simple_line_shader) bgl.glPolygonMode(bgl.GL_FRONT_AND_BACK, bgl.GL_FILL) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDisable(bgl.GL_DEPTH_TEST)
def draw_matrices_batches(batches): uniform_color_shader.bind() uniform_color_shader.uniform_float("color", (0.4, 0.4, 1.0, 0.3)) bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_FALSE) for batch in batches: batch.draw(uniform_color_shader) bgl.glDisable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_TRUE)
def draw(): bgl.glEnable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) shader3D.bind() shader3D.uniform_float( "color", (color[0], color[1], color[2], color[3] * alpha)) batch_draw(shader3D, 'TRIS', {"pos": vs}, indices=polys) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE)
def draw_grid(self, context): settings = context.scene.ps_set_ shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') shader.bind() # Color props = context.preferences.addons[__package__].preferences line_color = props.lines_props_grid box_color = props.box_props_grid unit_grid_color = props.unit_grid lineWidth = 3.0 lineSmooth = True xray = False bgl.glEnable(bgl.GL_BLEND) bgl.glLineWidth(lineWidth) bgl.glDepthMask(False) if xray == False: bgl.glEnable(bgl.GL_DEPTH_TEST) if lineSmooth: bgl.glEnable(bgl.GL_LINE_SMOOTH) if settings.box: faceCo, faces_indices = box() FACES = batch_for_shader(shader, 'TRIS', {"pos": faceCo}, indices=faces_indices) shader.uniform_float("color", box_color) FACES.draw(shader) if settings.draw_unit_grid: gridCo = generate_grid() GRID_EDGES = batch_for_shader(shader, 'LINES', {"pos": gridCo}) shader.uniform_float("color", unit_grid_color) GRID_EDGES.draw(shader) linesCo = lines() EDGES = batch_for_shader(shader, 'LINES', {"pos": linesCo}) shader.uniform_float("color", line_color) EDGES.draw(shader) bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glDisable(bgl.GL_DEPTH_TEST) #bgl.glDisable(bgl.GL_CULL_FACE) bgl.glLineWidth(1) bgl.glDisable(bgl.GL_BLEND)
def draw_batch_face_triangles(batch, color=(1, 0, 0, 0.5)): bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) shader3D.bind() shader3D.uniform_float("color", color) batch.draw(shader3D) bgl.glDisable(bgl.GL_BLEND)
def draw_postview(self): if self.rfcontext._nav or not self.nearest_edge: return if self._fsm.state != 'quick': if not (self.rfcontext.actions.ctrl and not self.rfcontext.actions.shift): return # draw new edge strip/loop Point_to_Point2D = self.rfcontext.Point_to_Point2D def draw(color): if not self.edges_: return if self.edge_loop: with Globals.drawing.draw(CC_2D_LINE_LOOP) as draw: draw.color(color) for _, c0, c1 in self.edges_: c = c0 + (c1 - c0) * self.percent draw.vertex(Point_to_Point2D(c)) else: with Globals.drawing.draw(CC_2D_LINE_STRIP) as draw: draw.color(color) for _, c0, c1 in self.edges_: c = c0 + (c1 - c0) * self.percent draw.vertex(Point_to_Point2D(c)) CC_DRAW.stipple(pattern=[4, 4]) CC_DRAW.point_size(5) CC_DRAW.line_width(2) #self.drawing.point_size(5.0) #self.drawing.line_width(2.0) # bgl.glDisable(bgl.GL_CULL_FACE) bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthRange(0, 0.9990) # squeeze depth just a bit # draw below # NOTE: THERE IS NO "BELOW" WHEN DRAWING IN POST2D! # need to implement 3D line drawing first # bgl.glDepthFunc(bgl.GL_GREATER) # draw(Color((0.15, 1.00, 0.15, 0.25))) # draw above bgl.glDepthFunc(bgl.GL_LEQUAL) draw(Color((0.15, 1.00, 0.15, 1.00))) # bgl.glEnable(bgl.GL_CULL_FACE) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0, 1)
def clear(self): is_bound = self is _SnapOffscreen.bound if not is_bound: self.bind() bgl.glColorMask(bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE, bgl.GL_TRUE) bgl.glClearColor(0.0, 0.0, 0.0, 0.0) bgl.glDepthMask(bgl.GL_TRUE) bgl.glClearDepth(1.0); bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT) if not is_bound: self.unbind()
def draw(): bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glLineWidth(width) bgl.glDepthFunc(bgl.GL_ALWAYS) bgl.glDepthMask(bgl.GL_FALSE) shader3D.bind() shader3D.uniform_float("color", color) batch = batch_for_shader(shader3D, 'LINES', {"pos": verts}) batch.draw(shader3D) bgl.glLineWidth(1) bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glDisable(bgl.GL_BLEND)
def draw_pivots3D(poss, radius, color=(1, 1, 1, 1)): bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glEnable(bgl.GL_BLEND) bgl.glPointSize(radius * dpm() * 2) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_POINT) bgl.glPolygonOffset(1.0, 1.0) shader3D.bind() shader3D.uniform_float("color", color) batch_draw(shader3D, 'POINTS', {"pos": poss}) bgl.glPointSize(1) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_POLYGON_OFFSET_POINT)
def draw_Edge3D(obj, edge: bmesh.types.BMEdge, color=(1, 1, 1, 1), width=1): bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glLineWidth(width) bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) bgl.glDepthMask(bgl.GL_FALSE) verts = (obj.matrix_world @ edge.verts[0].co, obj.matrix_world @ edge.verts[1].co) shader3D.bind() shader3D.uniform_float("color", color) batch = batch_for_shader(shader3D, 'LINES', {"pos": verts}) batch.draw(shader3D) bgl.glLineWidth(1) bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glDisable(bgl.GL_BLEND)
def h_mask_begin(bounds, radius=0): bgl.glEnable(bgl.GL_STENCIL_TEST) bgl.glColorMask(False, False, False, False) bgl.glDepthMask(False) bgl.glStencilFunc(bgl.GL_NEVER, 1, 0xFF) bgl.glStencilOp(bgl.GL_REPLACE, bgl.GL_KEEP, bgl.GL_KEEP) bgl.glStencilMask(0xFF) bgl.glClear(bgl.GL_STENCIL_BUFFER_BIT) h_round_rect(bounds, radius) bgl.glColorMask(True, True, True, True) bgl.glDepthMask(True) bgl.glStencilMask(0x00); bgl.glStencilFunc(bgl.GL_EQUAL, 0, 0xFF) bgl.glStencilFunc(bgl.GL_EQUAL, 1, 0xFF)
def drawElementHilight(obj, element, radius, color=(1, 1, 1, 1)): bgl.glEnable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) if isinstance(element, bmesh.types.BMVert): v = obj.matrix_world @ element.co pos = location_3d_to_region_2d(v) draw_pivot2D(pos, radius, color) elif isinstance(element, bmesh.types.BMFace): draw_Face2D(obj, element, (color[0], color[1], color[2], color[3] * 0.25)) elif isinstance(element, bmesh.types.BMEdge): draw_Edge2D(obj, element, color) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDisable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_FALSE)
def _draw_buffered(self, alpha_above, alpha_below, cull_backfaces, alpha_backface): opts = dict(self.opts) for xyz in self.rfmesh.symmetry: opts['mirror %s' % xyz] = True opts['cull backfaces'] = cull_backfaces opts['alpha backface'] = alpha_backface opts['dpi mult'] = self.drawing.get_dpi_mult() # do not change attribs if they're not set bmegl.glSetDefaultOptions(opts=self.opts) bgl.glDepthMask(bgl.GL_FALSE) # do not overwrite the depth buffer pr = profiler.start('geometry above') bgl.glDepthFunc(bgl.GL_LEQUAL) opts['poly hidden'] = 1 - alpha_above opts['poly mirror hidden'] = 1 - alpha_above opts['line hidden'] = 1 - alpha_above opts['line mirror hidden'] = 1 - alpha_above opts['point hidden'] = 1 - alpha_above opts['point mirror hidden'] = 1 - alpha_above for buffered_render in self.buffered_renders: buffered_render.draw(opts) pr.done() if not opts.get('no below', False): # draw geometry hidden behind pr = profiler.start('geometry below') bgl.glDepthFunc(bgl.GL_GREATER) opts['poly hidden'] = 1 - alpha_below opts['poly mirror hidden'] = 1 - alpha_below opts['line hidden'] = 1 - alpha_below opts['line mirror hidden'] = 1 - alpha_below opts['point hidden'] = 1 - alpha_below opts['point mirror hidden'] = 1 - alpha_below for buffered_render in self.buffered_renders: buffered_render.draw(opts) pr.done() bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDepthRange(0, 1)
def draw_postview(self): if not self.points_shader: return bgl.glDepthMask(bgl.GL_TRUE) bgl.glPointSize(8) bgl.glDepthFunc(bgl.GL_LEQUAL) self.points_shader.bind() self.points_shader.uniform_float("color", (1, 1, 1, 1)) self.points_batch.draw(self.points_shader) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDepthRange(0, 1) #bgl.glDisable(bgl.GL_POINT_SMOOTH) #bgl.glDisable(bgl.GL_POINTS) bgl.glPointSize(1)
def draw_postview(self): if self.rfcontext.nav: return if not self.nearest_edge: return if self.rfcontext.actions.ctrl and not self.rfcontext.actions.shift and self.mode == 'main': # draw new edge strip/loop def draw(): if not self.edges_: return self.drawing.enable_stipple() if self.edge_loop: bgl.glBegin(bgl.GL_LINE_LOOP) else: bgl.glBegin(bgl.GL_LINE_STRIP) for _,c0,c1 in self.edges_: c = c0 + (c1 - c0) * self.percent bgl.glVertex3f(*c) bgl.glEnd() self.drawing.disable_stipple() self.drawing.point_size(5.0) self.drawing.line_width(2.0) bgl.glDisable(bgl.GL_CULL_FACE) bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_FALSE) bgl.glDepthRange(0, 0.9990) # squeeze depth just a bit # draw above bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glColor4f(0.15, 1.00, 0.15, 1.00) draw() # draw below bgl.glDepthFunc(bgl.GL_GREATER) bgl.glColor4f(0.15, 1.00, 0.15, 0.25) draw() bgl.glEnable(bgl.GL_CULL_FACE) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0, 1)
def draw_postview(self): if self.rfcontext.nav: return if not self.nearest_edge: return if self.rfcontext.actions.ctrl and not self.rfcontext.actions.shift and self.mode == 'main': # draw new edge strip/loop def draw(): if not self.edges_: return self.drawing.enable_stipple() if self.edge_loop: bgl.glBegin(bgl.GL_LINE_LOOP) else: bgl.glBegin(bgl.GL_LINE_STRIP) for _, c0, c1 in self.edges_: c = c0 + (c1 - c0) * self.percent bgl.glVertex3f(*c) bgl.glEnd() self.drawing.disable_stipple() self.drawing.point_size(5.0) self.drawing.line_width(2.0) bgl.glDisable(bgl.GL_CULL_FACE) bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_FALSE) bgl.glDepthRange(0, 0.9990) # squeeze depth just a bit # draw above bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glColor4f(0.15, 1.00, 0.15, 1.00) draw() # draw below bgl.glDepthFunc(bgl.GL_GREATER) bgl.glColor4f(0.15, 1.00, 0.15, 0.25) draw() bgl.glEnable(bgl.GL_CULL_FACE) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0, 1)
def draw_Poly3D(context, verts, color=(1, 1, 1, 1), hide_alpha=0.5): bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthMask(bgl.GL_FALSE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) polys = mathutils.geometry.tessellate_polygon((verts, )) shader3D.bind() shader3D.uniform_float("color", color) batch = batch_draw(shader3D, 'TRIS', {"pos": verts}, indices=polys) if hide_alpha > 0.0: bgl.glDepthFunc(bgl.GL_GREATER) shader3D.uniform_float( "color", (color[0], color[1], color[2], color[3] * hide_alpha)) batch.draw(shader3D) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
def draw_lines3D(context, verts, color=(1, 1, 1, 1), width: float = 1.0, hide_alpha: float = 1.0, primitiveType='LINE_STRIP'): bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glLineWidth(width) bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL) bgl.glPolygonOffset(1.0, 1.0) if hide_alpha < 0.99: bgl.glDepthFunc(bgl.GL_LESS) else: bgl.glDepthFunc(bgl.GL_ALWAYS) # shader3D.uniform_float("modelMatrix", Matrix.Identity(4) ) shader3D.bind() matrix = context.region_data.perspective_matrix # shader3D.uniform_float("viewProjectionMatrix", matrix) shader3D.uniform_float("color", color) batch = batch_draw(shader3D, primitiveType, {"pos": verts}) if hide_alpha < 0.99: bgl.glDepthFunc(bgl.GL_GREATER) shader3D.uniform_float( "color", (color[0], color[1], color[2], color[3] * hide_alpha)) batch.draw(shader3D) bgl.glLineWidth(1) bgl.glDisable(bgl.GL_LINE_SMOOTH) bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_POLYGON_OFFSET_LINE) bgl.glDisable(bgl.GL_POLYGON_OFFSET_FILL)
def _draw_viewport(self): """ Callback function from blender to draw our particles into the viewport. Don't call by yourself! """ if not self.is_sim_active(): return self.update_preview_uniforms() # Need to use bgl here, since blender hangs, if we change state using moderngl bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_BLEND) if self.preview_mode == "particles": bgl.glDepthMask(False) bgl.glEnable(bgl.GL_PROGRAM_POINT_SIZE) # We somehow need to recreate the vao every time, since otherwise the vertex buffers are not going to be # activated and some other one from blender is the active buffer. preview_vertex_array = self.vao_definition_particles( self.preview_shader) preview_vertex_array.render(moderngl.vertex_array.POINTS) elif self.preview_mode == "texture_overlay": self.paintbuffer_sampler.use() self.mesh_buffer.draw(self.preview_shader) else: raise Error("Unknown preview_mode!")
def DrawNorth_callback(self, context): if not Sun.SP.ShowNorth and North.isActive: North.deactivate() return # ------------------------------------------------------------------ # Set up the compass needle using the current north offset angle # less 90 degrees. This forces the unit circle to begin at the # 12 O'clock instead of 3 O'clock position. # ------------------------------------------------------------------ color = (0.2, 0.6, 1.0, 1) radius = 100 angle = -(Sun.NorthOffset - math.pi / 2) x = math.cos(angle) * radius y = math.sin(angle) * radius bgl.glEnable(bgl.GL_MULTISAMPLE) bgl.glEnable(bgl.GL_LINE_SMOOTH) bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(False) bgl.glLineWidth(2) p1 = (0, 0, 0) p2 = (x / 20, y / 20, 0) coords = [p1, p2] # Start & end of needle arclengths = [0, (Vector(p1) - Vector(p2)).length] batch = batch_for_shader(dashedLineShader, 'LINES', { "pos": coords, "arcLength": arclengths }) dashedLineShader.bind() dashedLineShader.uniform_float("finalColor", color) dashedLineShader.uniform_float("u_Scale", 10) batch.draw(dashedLineShader)
def draw_rotation_line(): v1 = rotation_helper_params.c if rotation_helper_params.mouse_world: v2 = rotation_helper_params.mouse_world else: v2 = mathutils.Vector() bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_STIPPLE) bgl.glLineWidth(1) bgl.glLineStipple(3, 0xAAAA) bgl.glDepthMask(bgl.GL_FALSE) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glColor4f(0, 0, 0, 1) bgl.glBegin(bgl.GL_LINES) bgl.glVertex3f(v1.x, v1.y, v1.z) bgl.glVertex3f(v2.x, v2.y, v2.z) bgl.glEnd() bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDisable(bgl.GL_LINE_STIPPLE) bgl.glDisable(bgl.GL_BLEND)
def drawElementHilight3D(obj, element, radius, width, alpha, color=(1, 1, 1, 1)): bgl.glEnable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) if isinstance(element, bmesh.types.BMVert): v = obj.matrix_world @ element.co draw_pivots3D((v, ), radius, color) elif isinstance(element, bmesh.types.BMFace): draw_Face3D(obj, element, (color[0], color[1], color[2], color[3] * alpha)) elif isinstance(element, bmesh.types.BMEdge): draw_Edge3D(obj, element, color, width) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDisable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_FALSE)
def draw_rotation_line(): v1 = rotation_helper_params.c_world if rotation_helper_params.mouse_world: v2 = rotation_helper_params.mouse_world else: v2 = mathutils.Vector() bgl.glEnable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_LINE_STIPPLE) bgl.glLineWidth(1) bgl.glLineStipple(3, 0xAAAA) bgl.glDepthMask(bgl.GL_FALSE) bgl.glDisable(bgl.GL_DEPTH_TEST); bgl.glColor4f(0, 0, 0, 1) bgl.glBegin(bgl.GL_LINES) bgl.glVertex3f(v1.x,v1.y,v1.z) bgl.glVertex3f(v2.x,v2.y,v2.z) bgl.glEnd() bgl.glEnable(bgl.GL_DEPTH_TEST); bgl.glDepthMask(bgl.GL_TRUE) bgl.glDisable(bgl.GL_LINE_STIPPLE) bgl.glDisable(bgl.GL_BLEND)
def draw_circle(self, world_loc, radius, inner_ratio, color_outside, color_inside): bgl.glDepthRange(0, 0.9999) # squeeze depth just a bit bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_FALSE) # do not overwrite depth bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LEQUAL) # draw in front of geometry circleShader.enable() self.drawing.point_size(2.0 * radius) circleShader['uMVPMatrix'] = self.drawing.get_view_matrix_buffer() circleShader['uInOut'] = inner_ratio bgl.glBegin(bgl.GL_POINTS) circleShader['vOutColor'] = color_outside circleShader['vInColor'] = color_inside bgl.glVertex3f(*world_loc) bgl.glEnd() circleShader.disable() bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0.0, 1.0) bgl.glDepthMask(bgl.GL_TRUE)
def draw_callback_3d(self, context): def draw(ps, cs): for p, c in zip(ps, cs): bgl.glColor4f(*c) bgl.glVertex3f(*p) bgl.glColor4f(*cs[0]) bgl.glVertex3f(*ps[0]) #bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS) ## #debug - stack context try: frcurrent = context.scene.frame_current for amset in State.data.amsets: frprevlimit = amset.dissolve_length for fr in range(frcurrent - frprevlimit, frcurrent + 1): for aseq in amset.get_ampseqs(): amp = aseq.get(fr) if not amp: continue viewvec = State.current_viewvec if Pref.use_billboard else amp.viewvec dfr = frcurrent - fr tfr = 1.0 - dfr / frprevlimit if frprevlimit != 0 else 1.0 ## u0 = viewvec.cross(Vector(( 0, 0, 1))).normalized() if abs(viewvec.z) != 1.0 else Vector( (1, 0, 0)) v0 = u0.cross(viewvec) size = amp.size u = u0 * size v = v0 * size z_offset = fr / 1000 #z_offset = fr/100 #z_offset = -dfr/50 #z_offset = -dfr/1000 #q = amp.location q = amp.location - viewvec * z_offset alpha, quadscale = 1, 1 if amset.dissolve_method == AMDissolveMethod.Opacity: #alpha = tfr alpha = tfr**2.0 elif amset.dissolve_method == AMDissolveMethod.Size: quadscale = tfr**2.0 elif amset.dissolve_method == AMDissolveMethod.OpacitySize: alpha = tfr**2.0 quadscale = tfr**2.0 elif amset.dissolve_method == AMDissolveMethod.Const: pass u *= quadscale v *= quadscale cs = [(1, 1, 1, alpha), (1, 1, 1, .0), (1, 1, 1, .0), (1, 1, 1, .0)] bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_FALSE) if amset.blendmode == AMBlendMode.AlphaOver: ## alpha over bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) #bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE) bgl.glBlendEquation(bgl.GL_FUNC_ADD) elif amset.blendmode == AMBlendMode.Additive: ## additive bgl.glBlendEquation(bgl.GL_FUNC_ADD) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE) else: print('<!> invalid blendmode: ' + amset.blendmode) if amset.image_texture and amset.image_texture.bindcode[0]: amset.image_texture.gl_touch(0) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glColor4f(1.0, 1.0, 1.0, alpha) bgl.glBindTexture(bgl.GL_TEXTURE_2D, amset.image_texture.bindcode[0]) bgl.glBegin(bgl.GL_QUADS) ps = [q - u - v, q + u - v, q + u + v, q - u + v] bgl.glTexCoord2f(0.0, 0.0) bgl.glVertex3f(*ps[0]) bgl.glTexCoord2f(1.0, 0.0) bgl.glVertex3f(*ps[1]) bgl.glTexCoord2f(1.0, 1.0) bgl.glVertex3f(*ps[2]) bgl.glTexCoord2f(0.0, 1.0) bgl.glVertex3f(*ps[3]) bgl.glEnd() bgl.glDisable(bgl.GL_TEXTURE_2D) else: bgl.glBegin(bgl.GL_POLYGON) draw([q, q + u, q + u + v, q + v], cs) draw([q, q - u, q - u + v, q + v], cs) draw([q, q + u, q + u - v, q - v], cs) draw([q, q - u, q - u - v, q - v], cs) bgl.glEnd() bgl.glDisable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_TRUE) # restore opengl defaults bgl.glLineWidth(1) bgl.glDisable(bgl.GL_BLEND) bgl.glColor4f(0.0, 0.0, 0.0, 1.0) except Exception as e: print('error in draw callback') print(e)
def draw_post3d_spline(self): if not self.strips: return strips = self.strips hov_strips = self.hovering_strips Point_to_Point2D = self.rfcontext.Point_to_Point2D def is_visible(v): return True # self.rfcontext.is_visible(v, None) def draw(alphamult, hov_alphamult, hover): nonlocal strips if not hover: hov_alphamult = alphamult size_outer = options['polystrips handle outer size'] size_inner = options['polystrips handle inner size'] border_outer = options['polystrips handle border'] border_inner = options['polystrips handle border'] bgl.glEnable(bgl.GL_BLEND) # draw outer-inner lines pts = [ Point_to_Point2D(p) for strip in strips for p in strip.curve.points() ] self.rfcontext.drawing.draw2D_lines(pts, (1, 1, 1, 0.45), width=2) # draw junction handles (outer control points of curve) faces_drawn = set( ) # keep track of faces, so don't draw same handles 2+ times pts_outer, pts_inner = [], [] for strip in strips: bmf0, bmf1 = strip.end_faces() p0, p1, p2, p3 = strip.curve.points() if bmf0 not in faces_drawn: if is_visible(p0): pts_outer += [Point_to_Point2D(p0)] faces_drawn.add(bmf0) if bmf1 not in faces_drawn: if is_visible(p3): pts_outer += [Point_to_Point2D(p3)] faces_drawn.add(bmf1) if is_visible(p1): pts_inner += [Point_to_Point2D(p1)] if is_visible(p2): pts_inner += [Point_to_Point2D(p2)] self.rfcontext.drawing.draw2D_points(pts_outer, (1.00, 1.00, 1.00, 1.0), radius=size_outer, border=border_outer, borderColor=(0.00, 0.00, 0.00, 0.5)) self.rfcontext.drawing.draw2D_points(pts_inner, (0.25, 0.25, 0.25, 0.8), radius=size_inner, border=border_inner, borderColor=(0.75, 0.75, 0.75, 0.4)) if True: # always draw on top! bgl.glEnable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_FALSE) draw(1.0, 1.0, False) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthMask(bgl.GL_TRUE) else: # allow handles to go under surface bgl.glDepthRange(0, 0.9999) # squeeze depth just a bit bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_FALSE) # do not overwrite depth bgl.glEnable(bgl.GL_DEPTH_TEST) # draw in front of geometry bgl.glDepthFunc(bgl.GL_LEQUAL) draw( options['target alpha'], options['target alpha'], # hover False, #options['polystrips handle hover'] ) # draw behind geometry bgl.glDepthFunc(bgl.GL_GREATER) draw( options['target hidden alpha'], options['target hidden alpha'], # hover False, #options['polystrips handle hover'] ) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0.0, 1.0) bgl.glDepthMask(bgl.GL_TRUE)
def glsl_draw(self): if GlslDrawObj.myinstance is None and GlslDrawObj.draw_func is None: glsl_draw_obj = GlslDrawObj() glsl_draw_obj.build_scene() else: glsl_draw_obj = GlslDrawObj.myinstance model_offset = Matrix.Translation((glsl_draw_obj.draw_x_offset, 0, 0)) light_pos = [ i + n for i, n in zip(glsl_draw_obj.light.location, [-glsl_draw_obj.draw_x_offset, 0, 0]) ] batches = glsl_draw_obj.batches depth_shader = glsl_draw_obj.depth_shader toon_shader = glsl_draw_obj.toon_shader offscreen = glsl_draw_obj.offscreen # need bone etc changed only update depth_matrix = None light = glsl_draw_obj.light light_lookat = light.rotation_euler.to_quaternion() @ Vector( (0, 0, -1)) # TODO このへん tar = light_lookat.normalized() up = light.rotation_euler.to_quaternion() @ Vector((0, 1, 0)) tmp_bound_len = Vector(glsl_draw_obj.bounding_center).length camera_bias = 0.2 loc = Vector([ glsl_draw_obj.bounding_center[i] + tar[i] * (tmp_bound_len + camera_bias) for i in range(3) ]) loc = model_offset @ loc v_matrix = lookat_cross(loc, tar, up) const_proj = 2 * max(glsl_draw_obj.bounding_size) / 2 p_matrix = ortho_proj_mat(-const_proj, const_proj, -const_proj, const_proj, -const_proj, const_proj) depth_matrix = v_matrix @ p_matrix # reuse in main shader depth_matrix.transpose() # region shader depth path with offscreen.bind(): bgl.glClearColor(10, 10, 10, 1) bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT) for bat in batches: mat = bat[0] mat.update() depth_bat = bat[2] depth_shader.bind() bgl.glEnable(bgl.GL_BLEND) if mat.alpha_method == "TRANSPARENT": bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) elif mat.alpha_method == "OPAQUE": bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) elif mat.alpha_method == "CLIP": bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) if mat.cull_mode == "BACK": bgl.glEnable(bgl.GL_CULL_FACE) bgl.glCullFace(bgl.GL_BACK) else: bgl.glDisable(bgl.GL_CULL_FACE) bgl.glEnable(bgl.GL_CULL_FACE) # そも輪郭線がの影は落ちる? bgl.glCullFace(bgl.GL_BACK) depth_shader.uniform_float("obj_matrix", model_offset) # obj.matrix_world) depth_shader.uniform_float("depthMVP", depth_matrix) depth_bat.draw(depth_shader) # endregion shader depth path # region shader main vp_mat = bpy.context.region_data.perspective_matrix projection_mat = bpy.context.region_data.window_matrix view_dir = bpy.context.region_data.view_matrix[2][:3] view_up = bpy.context.region_data.view_matrix[1][:3] normal_world_to_view_matrix = ( bpy.context.region_data.view_matrix.inverted_safe().transposed()) aspect = bpy.context.area.width / bpy.context.area.height for is_outline in [0, 1]: for bat in batches: toon_bat = bat[1] toon_shader.bind() mat = bat[0] if is_outline == 1 and mat.float_dic["OutlineWidthMode"] == 0: continue # mat.update() #already in depth path bgl.glEnable(bgl.GL_BLEND) bgl.glDepthMask(bgl.GL_TRUE) bgl.glEnable(bgl.GL_DEPTH_TEST) if mat.alpha_method == "TRANSPARENT": bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) elif mat.alpha_method == "OPAQUE": bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO) elif mat.alpha_method == "CLIP": bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO) if is_outline == 0: if mat.cull_mode == "BACK": bgl.glEnable(bgl.GL_CULL_FACE) bgl.glCullFace(bgl.GL_BACK) else: bgl.glDisable(bgl.GL_CULL_FACE) else: bgl.glEnable(bgl.GL_CULL_FACE) bgl.glCullFace(bgl.GL_BACK) toon_shader.uniform_float("obj_matrix", model_offset) # obj.matrix_world) toon_shader.uniform_float("projectionMatrix", projection_mat) toon_shader.uniform_float("viewProjectionMatrix", vp_mat) toon_shader.uniform_float("viewDirection", view_dir) toon_shader.uniform_float("viewUpDirection", view_up) toon_shader.uniform_float("normalWorldToViewMatrix", normal_world_to_view_matrix) toon_shader.uniform_float("depthMVP", depth_matrix) toon_shader.uniform_float("lightpos", light_pos) toon_shader.uniform_float("aspect", aspect) toon_shader.uniform_float("is_outline", is_outline) toon_shader.uniform_float("isDebug", 0.0) toon_shader.uniform_float( "is_cutout", 1.0 if mat.alpha_method == "CLIP" else 0.0) float_keys = [ "CutoffRate", "BumpScale", "ReceiveShadowRate", "ShadeShift", "ShadeToony", "RimLightingMix", "RimFresnelPower", "RimLift", "ShadingGradeRate", "LightColorAttenuation", "IndirectLightIntensity", "OutlineWidth", "OutlineScaleMaxDistance", "OutlineLightingMix", "UV_Scroll_X", "UV_Scroll_Y", "UV_Scroll_Rotation", "OutlineWidthMode", "OutlineColorMode", ] for k in float_keys: toon_shader.uniform_float(k, mat.float_dic[k]) for k, v in mat.vector_dic.items(): toon_shader.uniform_float(k, v) bgl.glActiveTexture(bgl.GL_TEXTURE0) bgl.glBindTexture(bgl.GL_TEXTURE_2D, offscreen.color_texture) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) # TODO bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) toon_shader.uniform_int("depth_image", 0) for i, k in enumerate(mat.texture_dic.keys()): bgl.glActiveTexture(bgl.GL_TEXTURE1 + i) texture = mat.texture_dic[k] bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.bindcode) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S, bgl.GL_CLAMP_TO_EDGE) # TODO bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T, bgl.GL_CLAMP_TO_EDGE) toon_shader.uniform_int(k, 1 + i) toon_bat.draw(toon_shader)
def draw_callback(self, context): """ :type context: bpy.types.Context """ prefs = QuickBooleanPreferences.get_instance() color = prefs.color snap_color = prefs.snap_color region = context.region glsettings = vagl.GLSettings(context) glsettings.push() bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(*color) show_reversed = False if self.reverse: if self.mode == 'POLYGON': if len(self.mouse_coords) >= 3: show_reversed = True else: if len(self.mouse_coords) >= 2: if self.mouse_coords[0] != self.mouse_coords[1]: show_reversed = True if show_reversed: bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glClearDepth(1.0) bgl.glClear(bgl.GL_DEPTH_BUFFER_BIT) bgl.glDepthMask(1) bgl.glColorMask(0, 0, 0, 0) lines = [] if self.mouse_coords: if self.mode == 'LINE': w = region.width h = region.height p1, p2 = self.mouse_coords line = (p2 - p1).normalized() normal = Vector([-line[1], line[0]]) corners = [ Vector([0, 0]), Vector([w, 0]), Vector([w, h]), Vector([0, h]) ] corners_ofs = [v - p1 for v in corners] dists = [v.project(line).dot(line) for v in corners_ofs] i = dists.index(min(dists)) line_min = corners_ofs[i].project(line) + p1 i = dists.index(max(dists)) line_max = corners_ofs[i].project(line) + p1 dists = [v.project(normal).dot(normal) for v in corners_ofs] i = dists.index(max(dists)) normal_max_f = corners_ofs[i].project(normal).dot(normal) vec = normal * normal_max_f coords = [line_min, line_max, line_max + vec, line_min + vec] bgl.glBegin(bgl.GL_QUADS) for co in coords: bgl.glVertex2f(*co) bgl.glEnd() lines = self.mouse_coords elif self.mode == 'BOX': p1, p2 = self.mouse_coords bgl.glRectf(p1[0], p1[1], p2[0], p2[1]) lines = [ p1, Vector((p2[0], p1[1])), Vector((p2[0], p2[1])), Vector((p1[0], p2[1])), p1 ] elif self.mode == 'CIRCLE': p1, p2 = self.mouse_coords bgl.glBegin(bgl.GL_TRIANGLE_FAN) bgl.glVertex2f(*p1) r = (p2 - p1).length coords = calc_circle_coords(p1, r, self.circle_segments, self.circle_direction) for co in coords: bgl.glVertex2f(*co) bgl.glVertex2f(*coords[0]) bgl.glEnd() lines = coords + [coords[0]] elif self.mode == 'POLYGON': if len(self.mouse_coords) >= 3: tris = mathutils.geometry.tessellate_polygon( [[co.to_3d() for co in self.mouse_coords]]) bgl.glBegin(bgl.GL_TRIANGLES) for tri in tris: for i in tri: bgl.glVertex2f(*self.mouse_coords[i]) bgl.glEnd() if len(self.mouse_coords) > 1: lines = self.mouse_coords + [self.mouse_coords[0]] if show_reversed: bgl.glColorMask(1, 1, 1, 1) bgl.glBegin(bgl.GL_QUADS) bgl.glVertex3f(0, 0, -1) bgl.glVertex3f(region.width, 0, -1) bgl.glVertex3f(region.width, region.height, -1) bgl.glVertex3f(0, region.height, -1) bgl.glEnd() bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glColor4f(*color[:3], 1.0) bgl.glPointSize(1) bgl.glLineWidth(1) if len(lines) > 1: bgl.glBegin(bgl.GL_LINE_STRIP) for co in lines: bgl.glVertex2f(*co) bgl.glEnd() if self.mode == 'POLYGON': if len(self.mouse_coords) == 1: bgl.glPointSize(5) bgl.glBegin(bgl.GL_POINTS) for co in self.mouse_coords: bgl.glVertex2f(*co) bgl.glEnd() bgl.glPointSize(1) bgl.glLineWidth(1) if self.mco_ctrl: SIZE = 12 bgl.glColor4f(*snap_color) bgl.glBegin(bgl.GL_LINE_LOOP) v = self.mco_mod x = v[0] - SIZE / 2 y = v[1] - SIZE / 2 bgl.glVertex2f(x, y) bgl.glVertex2f(x + SIZE, y) bgl.glVertex2f(x + SIZE, y + SIZE) bgl.glVertex2f(x, y + SIZE) bgl.glEnd() glsettings.pop() glsettings.font_size()
def draw_3D_stuff(self): context = self.context region,r3d = context.region,context.space_data.region_3d view_dir = r3d.view_rotation * Vector((0,0,-1)) view_loc = r3d.view_location - view_dir * r3d.view_distance view_ortho = (r3d.view_perspective == 'ORTHO') if view_ortho: view_loc -= view_dir * 1000.0 bgl.glEnable(bgl.GL_POINT_SMOOTH) bgl.glDepthRange(0.0, 1.0) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glLineWidth(1) bgl.glDepthRange(0.0, 1.0) if self._state != 'segmentation': #CurveNetwork, BezierSegments for seg in self.spline_net.segments: if len(seg.draw_tessellation) == 0: continue #has not been successfully converted to InputPoints and InputSegments if seg.is_inet_dirty: draw3d_polyline(seg.draw_tessellation, orange2, 4, view_loc, view_ortho) #if len(seg.ip_tesselation): # draw3d_polyline(seg.ip_tesselation, blue, 2, view_loc, view_ortho) # draw3d_points(seg.ip_tesselation, green2, 4, view_loc, view_ortho) draw3d_points(self.spline_net.point_world_locs, green2, 6, view_loc, view_ortho) # Polylines...InputSegments for seg in self.input_net.segments: #bad segment with a preview path provided by geodesic if seg.bad_segment and not len(seg.path) > 2: draw3d_polyline([seg.ip0.world_loc, seg.ip1.world_loc], pink, 2, view_loc, view_ortho) #s elif len(seg.path) >= 2 and not seg.bad_segment and seg not in self.network_cutter.completed_segments: draw3d_polyline(seg.path, blue, 2, view_loc, view_ortho) elif len(seg.path) >= 2 and not seg.bad_segment and seg in self.network_cutter.completed_segments: draw3d_polyline(seg.path, green2, 2, view_loc, view_ortho) elif len(seg.path) >= 2 and seg.bad_segment: draw3d_polyline(seg.path, orange2, 2, view_loc, view_ortho) draw3d_polyline([seg.ip0.world_loc, seg.ip1.world_loc], orange2, 2, view_loc, view_ortho) elif seg.calculation_complete == False: draw3d_polyline([seg.ip0.world_loc, seg.ip1.world_loc], orange2, 2, view_loc, view_ortho) else: draw3d_polyline([seg.ip0.world_loc, seg.ip1.world_loc], blue2, 2, view_loc, view_ortho) if self.network_cutter.the_bad_segment: seg = self.network_cutter.the_bad_segment draw3d_polyline([seg.ip0.world_loc, seg.ip1.world_loc], red, 4, view_loc, view_ortho) if self._state == 'segmentation': #draw the hovered patch #TODO, segmentation only happens AFTER CUtting #So it would be MUCH easier to just draw the damn edges of the patch if self.net_ui_context.hovered_near[0] == 'PATCH': p = self.net_ui_context.hovered_near[1] if p != self.network_cutter.active_patch: for spline_seg in p.spline_net_segments: for iseg in spline_seg.input_segments: draw3d_polyline([iseg.ip0.world_loc] + iseg.path + [iseg.ip1.world_loc], orange2, 4, view_loc, view_ortho) if self.network_cutter.active_patch: for spline_seg in self.network_cutter.active_patch.spline_net_segments: for iseg in spline_seg.input_segments: draw3d_polyline([iseg.ip0.world_loc] + iseg.path + [iseg.ip1.world_loc], orange2, 4, view_loc, view_ortho) if self._state == 'spline': draw3d_points(self.input_net.point_world_locs, blue, 2, view_loc, view_ortho) elif self._state != 'segmentation': draw3d_points(self.input_net.point_world_locs, blue, 6, view_loc, view_ortho) #draw the seed/face patch points draw3d_points([p.world_loc for p in self.network_cutter.face_patches], orange2, 6, view_loc, view_ortho) #draw the actively processing Input Point (IP Steper Debug) for debug stepper cutting if self.network_cutter.active_ip: draw3d_points([self.network_cutter.active_ip.world_loc], purple, 20, view_loc, view_ortho) draw3d_points([ip.world_loc for ip in self.network_cutter.ip_chain], purple, 12, view_loc, view_ortho) if self.network_cutter.seg_enter: draw3d_polyline(self.network_cutter.seg_enter.path, green2, 4, view_loc, view_ortho) if self.network_cutter.seg_exit: draw3d_polyline(self.network_cutter.seg_exit.path, red, 4, view_loc, view_ortho) bgl.glLineWidth(1) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0.0, 1.0) bgl.glDepthMask(bgl.GL_TRUE)
def draw_callback(self, context): """ :type context: bpy.types.Context """ prefs = QuickBooleanPreferences.get_instance() color = prefs.color snap_color = prefs.snap_color region = context.region glsettings = vagl.GLSettings(context) glsettings.push() bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(*color) show_reversed = False if self.reverse: if self.mode == 'POLYGON': if len(self.mouse_coords) >= 3: show_reversed = True else: if len(self.mouse_coords) >= 2: if self.mouse_coords[0] != self.mouse_coords[1]: show_reversed = True if show_reversed: bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glClearDepth(1.0) bgl.glClear(bgl.GL_DEPTH_BUFFER_BIT) bgl.glDepthMask(1) bgl.glColorMask(0, 0, 0, 0) lines = [] if self.mouse_coords: if self.mode == 'LINE': w = region.width h = region.height p1, p2 = self.mouse_coords line = (p2 - p1).normalized() normal = Vector([-line[1], line[0]]) corners = [Vector([0, 0]), Vector([w, 0]), Vector([w, h]), Vector([0, h])] corners_ofs = [v - p1 for v in corners] dists = [v.project(line).dot(line) for v in corners_ofs] i = dists.index(min(dists)) line_min = corners_ofs[i].project(line) + p1 i = dists.index(max(dists)) line_max = corners_ofs[i].project(line) + p1 dists = [v.project(normal).dot(normal) for v in corners_ofs] i = dists.index(max(dists)) normal_max_f = corners_ofs[i].project(normal).dot(normal) vec = normal * normal_max_f coords = [line_min, line_max, line_max + vec, line_min + vec] bgl.glBegin(bgl.GL_QUADS) for co in coords: bgl.glVertex2f(*co) bgl.glEnd() lines = self.mouse_coords elif self.mode == 'BOX': p1, p2 = self.mouse_coords bgl.glRectf(p1[0], p1[1], p2[0], p2[1]) lines = [p1, Vector((p2[0], p1[1])), Vector((p2[0], p2[1])), Vector((p1[0], p2[1])), p1] elif self.mode == 'CIRCLE': p1, p2 = self.mouse_coords bgl.glBegin(bgl.GL_TRIANGLE_FAN) bgl.glVertex2f(*p1) r = (p2 - p1).length coords = calc_circle_coords(p1, r, self.circle_segments, self.circle_direction) for co in coords: bgl.glVertex2f(*co) bgl.glVertex2f(*coords[0]) bgl.glEnd() lines = coords + [coords[0]] elif self.mode == 'POLYGON': if len(self.mouse_coords) >= 3: tris = mathutils.geometry.tessellate_polygon( [[co.to_3d() for co in self.mouse_coords]]) bgl.glBegin(bgl.GL_TRIANGLES) for tri in tris: for i in tri: bgl.glVertex2f(*self.mouse_coords[i]) bgl.glEnd() if len(self.mouse_coords) > 1: lines = self.mouse_coords + [self.mouse_coords[0]] if show_reversed: bgl.glColorMask(1, 1, 1, 1) bgl.glBegin(bgl.GL_QUADS) bgl.glVertex3f(0, 0, -1) bgl.glVertex3f(region.width, 0, -1) bgl.glVertex3f(region.width, region.height, -1) bgl.glVertex3f(0, region.height, -1) bgl.glEnd() bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glColor4f(*color[:3], 1.0) bgl.glPointSize(1) bgl.glLineWidth(1) if len(lines) > 1: bgl.glBegin(bgl.GL_LINE_STRIP) for co in lines: bgl.glVertex2f(*co) bgl.glEnd() if self.mode == 'POLYGON': if len(self.mouse_coords) == 1: bgl.glPointSize(5) bgl.glBegin(bgl.GL_POINTS) for co in self.mouse_coords: bgl.glVertex2f(*co) bgl.glEnd() bgl.glPointSize(1) bgl.glLineWidth(1) if self.mco_ctrl: SIZE = 12 bgl.glColor4f(*snap_color) bgl.glBegin(bgl.GL_LINE_LOOP) v = self.mco_mod x = v[0] - SIZE / 2 y = v[1] - SIZE / 2 bgl.glVertex2f(x, y) bgl.glVertex2f(x + SIZE, y) bgl.glVertex2f(x + SIZE, y + SIZE) bgl.glVertex2f(x, y + SIZE) bgl.glEnd() glsettings.pop() glsettings.font_size()
def brushstroke_postview(self): if self.mode not in {'main','brushstroke'}: return if not self.hit: return cx,cy,cp = self.hit_x,self.hit_y,self.hit_p cs_outer = self.scale * self.size cs_inner = self.scale * self.size * 0.5 cr,cg,cb = self.color bgl.glDepthRange(0, 0.999) # squeeze depth just a bit bgl.glEnable(bgl.GL_BLEND) self.drawing.line_width(2.0) self.drawing.point_size(3.0) ###################################### # draw in front of geometry bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthMask(bgl.GL_FALSE) # do not overwrite depth bgl.glColor4f(cr, cg, cb, 1.0) # outer ring bgl.glBegin(bgl.GL_LINE_STRIP) for x,y in self.points: p = (cs_outer * ((cx * x) + (cy * y))) + cp bgl.glVertex3f(*p) bgl.glEnd() bgl.glColor4f(cr, cg, cb, 0.1) # inner ring bgl.glBegin(bgl.GL_LINE_STRIP) for x,y in self.points: p = (cs_inner * ((cx * x) + (cy * y))) + cp bgl.glVertex3f(*p) bgl.glEnd() bgl.glColor4f(1, 1, 1, 0.25) # center point bgl.glBegin(bgl.GL_POINTS) bgl.glVertex3f(*cp) bgl.glEnd() ###################################### # draw behind geometry (hidden below) bgl.glDepthFunc(bgl.GL_GREATER) bgl.glDepthMask(bgl.GL_FALSE) # do not overwrite depth bgl.glColor4f(cr, cg, cb, 0.05) # outer ring bgl.glBegin(bgl.GL_LINE_STRIP) for x,y in self.points: p = (cs_outer * ((cx * x) + (cy * y))) + cp bgl.glVertex3f(*p) bgl.glEnd() bgl.glColor4f(cr, cg, cb, 0.01) # inner ring bgl.glBegin(bgl.GL_LINE_STRIP) for x,y in self.points: p = (cs_inner * ((cx * x) + (cy * y))) + cp bgl.glVertex3f(*p) bgl.glEnd() ###################################### # reset to defaults bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthMask(bgl.GL_TRUE) bgl.glDepthRange(0, 1)