def glEnableBackfaceCulling(enable=True): if enable: bgl.glDisable(bgl.GL_CULL_FACE) bgl.glDepthFunc(bgl.GL_GEQUAL) else: bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glEnable(bgl.GL_CULL_FACE)
def draw_scene(self, context, projection_matrix): bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LESS) # Get List of Mesh Objects objs = [] deps = bpy.context.view_layer.depsgraph for obj_int in deps.object_instances: obj = obj_int.object if obj.type == 'MESH' and obj.hide_render == False: mat = obj_int.matrix_world obj_eval = obj.evaluated_get(deps) mesh = obj_eval.to_mesh(preserve_all_data_layers=True, depsgraph=bpy.context.view_layer.depsgraph) mesh.calc_loop_triangles() tris = mesh.loop_triangles vertices = [] indices = [] for vert in mesh.vertices: # Multipy vertex Position by Object Transform Matrix vertices.append(mat @ vert.co) for tri in tris: indices.append(tri.vertices) #shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') shader = gpu.types.GPUShader(Base_Shader_3D.vertex_shader, DepthOnlyFrag.fragment_shader) batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices) batch.program_set(shader) batch.draw() gpu.shader.unbind() obj_eval.to_mesh_clear() #Write to Image for Debug debug = False if debug: scene = context.scene render_scale = scene.render.resolution_percentage / 100 width = int(scene.render.resolution_x * render_scale) height = int(scene.render.resolution_y * render_scale) buffer = bgl.Buffer(bgl.GL_BYTE, width * height * 4) bgl.glReadBuffer(bgl.GL_COLOR_ATTACHMENT0) bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer) image_name = "measureit_arch_depth" if image_name not in bpy.data.images: bpy.data.images.new(image_name, width, height) image = bpy.data.images[image_name] image.scale(width, height) image.pixels = [v / 255 for v in buffer] bgl.glDisable(bgl.GL_DEPTH_TEST)
def draw_callback_px(self, context): kha_SystemImpl.frame() bgl.glUseProgram(0) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0) bgl.glBindBuffer(bgl.GL_ELEMENT_ARRAY_BUFFER, 0) bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_ALWAYS)
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(): shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') shader.bind() shader.uniform_float("color", (*color, alpha)) if bpy.app.version >= (2, 91, 0) and not xray: bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glEnable(bgl.GL_BLEND) if alpha < 1 else bgl.glDisable( bgl.GL_BLEND) bgl.glDisable(bgl.GL_DEPTH_TEST) if xray else bgl.glEnable( bgl.GL_DEPTH_TEST) if alpha < 1: bgl.glEnable(bgl.GL_LINE_SMOOTH) if mx != Matrix(): batch = batch_for_shader(shader, 'TRIS', {"pos": [mx @ co for co in coords]}, indices=indices) else: batch = batch_for_shader(shader, 'TRIS', {"pos": coords}, indices=indices) batch.draw(shader)
def draw_viewport_2d(self, context): bgl.glPushAttrib( bgl.GL_DEPTH_BUFFER_BIT | bgl.GL_LINE_BIT | bgl.GL_COLOR_BUFFER_BIT | bgl.GL_CURRENT_BIT) bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) bgl.glLineWidth(1) if self.started: ax, ay = self.mouse_a bx, by = self.mouse_b bgl.glBegin(bgl.GL_LINES) bgl.glColor4f(0, 0, 0, 1) bgl.glVertex2f(ax + 1, ay) bgl.glVertex2f(bx + 1, by) bgl.glVertex2f(ax - 1, ay) bgl.glVertex2f(bx - 1, by) bgl.glVertex2f(ax, ay + 1) bgl.glVertex2f(bx, by + 1) bgl.glVertex2f(ax, ay - 1) bgl.glVertex2f(bx, by - 1) bgl.glColor4f(1, 1, 1, 1) bgl.glVertex2f(ax, ay) bgl.glVertex2f(bx, by) bgl.glEnd() mx, my = self.mouse_now my -= 16 bgl.glBegin(bgl.GL_QUADS) bgl.glColor3f(0, 0, 0) bgl.glVertex2f(mx - 9, my - 8) bgl.glVertex2f(mx, my + 1) bgl.glVertex2f(mx + 9, my - 8) bgl.glVertex2f(mx, my - 17) bgl.glEnd() bgl.glBegin(bgl.GL_TRIANGLES) bgl.glColor3f(*self.color_a) bgl.glVertex2f(mx - 8, my - 8) bgl.glVertex2f(mx, my) bgl.glVertex2f(mx, my - 16) bgl.glEnd() bgl.glBegin(bgl.GL_TRIANGLES) bgl.glColor3f(*self.color_b) bgl.glVertex2f(mx, my) bgl.glVertex2f(mx + 8, my - 8) bgl.glVertex2f(mx, my - 16) bgl.glEnd() bgl.glPopAttrib();
def draw(coords): glEnable(GL_BLEND) glPointSize(12) glDepthFunc(GL_ALWAYS) shader.bind() batch = batch_for_shader(shader, 'POINTS', {"pos": coords}) batch.draw(shader) glDisable(GL_BLEND)
def draw(): shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') shader.bind() # bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) shader.uniform_float("color", (*color, 1)) batch = batch_for_shader(shader, 'POINTS', {"pos": [coords]}) batch.draw(shader)
def draw(): coords = [origin, origin + vector] shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') shader.bind() # bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) shader.uniform_float("color", (*color, 1)) batch = batch_for_shader(shader, 'LINES', {"pos": coords}) batch.draw(shader)
def draw_callback_bezier_3d(self, context): bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) bezier = self.beziers[self.at[0]][self.at[1]] split = bezier.split(self.at[2]) points = bezier.points bgl.glColor4f(1, 1, 1, 0.5) bgl.glLineWidth(1.0) bgl.glBegin(bgl.GL_LINES) bgl.glVertex3f(*points[0]) bgl.glVertex3f(*points[1]) bgl.glVertex3f(*points[2]) bgl.glVertex3f(*points[3]) bgl.glEnd() bgl.glColor4f(1, 1, 1, 1) bgl.glPointSize(6) bgl.glBegin(bgl.GL_POINTS) bgl.glVertex3f(*points[0]) bgl.glVertex3f(*points[3]) bgl.glEnd() bgl.glPointSize(2) bgl.glBegin(bgl.GL_POINTS) bgl.glVertex3f(*points[1]) bgl.glVertex3f(*points[2]) bgl.glEnd() # draw new bezier anchor bgl.glColor4f(0.8, 1.0, 0.0, 0.5) bgl.glLineWidth(2) bgl.glBegin(bgl.GL_LINE_STRIP) bgl.glVertex3f(*split[2]) bgl.glVertex3f(*split[3]) bgl.glVertex3f(*split[4]) bgl.glEnd() bgl.glColor4f(0.2, 1, 0.0, 1.0) bgl.glPointSize(10) bgl.glBegin(bgl.GL_POINTS) bgl.glVertex3f(*split[3]) bgl.glEnd() bgl.glPointSize(6) bgl.glBegin(bgl.GL_POINTS) bgl.glVertex3f(*split[2]) bgl.glVertex3f(*split[4]) bgl.glEnd() gl_end_and_restore()
def draw_VIEW3D(self, args): shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') shader.bind() bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) bgl.glLineWidth(3) shader.uniform_float("color", (0.5, 1, 0.5, 0.5)) batch = batch_for_shader(shader, 'LINES', {"pos": self.coords}, indices=self.edge_indices) batch.draw(shader) 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 draw_callback(self): #bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) shader, batch = self.shader, self.batch bgl.glLineWidth(2) shader.bind() shader.uniform_float("color", (0, 2, 1, 1)) batch.draw(shader) bgl.glLineWidth(1) bgl.glDisable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) return
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_callback_view(): settings = bpy.context.window_manager.MathVisProp prop_states = bpy.context.window_manager.MathVisStatePropList scale = settings.bbox_scale with_bounding_box = not settings.bbox_hide if settings.in_front: bgl.glDepthFunc(bgl.GL_ALWAYS) else: bgl.glDepthFunc(bgl.GL_LESS) data_matrix, data_quat, data_euler, data_vector, data_vector_array = utils.console_math_data( ) if settings.index in range(0, len(prop_states)): active_index = settings.index active_key = prop_states[active_index].name else: active_index = -1 active_key = None if data_vector: coords = [tuple(vec.to_3d()) for vec in data_vector.values()] draw_points(coords) if data_vector_array: for key, line in data_vector_array.items(): coords = [tuple(vec.to_3d()) for vec in line] if key == active_key: draw_line(coords, COLOR_LINE_ACTIVE) else: draw_line(coords, COLOR_LINE) if data_matrix: draw_matrices(data_matrix, scale, with_bounding_box, active_key) if data_euler or data_quat: cursor = bpy.context.scene.cursor.location.copy() derived_matrices = [] for key, quat in data_quat.values(): matrix = quat.to_matrix().to_4x4() matrix.translation = cursor derived_matrices[key] = matrix for key, eul in data_euler.values(): matrix = eul.to_matrix().to_4x4() matrix.translation = cursor derived_matrices[key] = matrix draw_matrices(derived_matrices, scale, with_bounding_box, active_key)
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 draw(self, op, context): if self.batch is None: return view_projection_matrix = bpy.context.region_data.perspective_matrix normal_matrix = bpy.context.region_data.view_matrix.inverted( ).transposed() self.shader.bind() bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LESS) self.shader.uniform_float("color", self.color) self.shader.uniform_float("modelviewprojection_mat", view_projection_matrix) self.shader.uniform_float("normal_mat", normal_matrix) self.batch.draw(self.shader) bgl.glDisable(bgl.GL_DEPTH_TEST)
def draw_scene(self, context, projection_matrix): # Get List of Mesh Objects objs = [] for obj in context.view_layer.objects: if obj.type == 'MESH': objs.append(obj) # get 3D view matrix view_matrix_3d = context.scene.camera.matrix_world.inverted() # Get Vertices and Indices of Objects for obj in objs: mesh = obj.data bm = bmesh.new() bm.from_object(obj, context.view_layer.depsgraph, deform=True) mesh.calc_loop_triangles() vertices = [] indices = np.empty((len(mesh.loop_triangles), 3), 'i') for vert in bm.verts: # Multipy vertex Position by Object Transform Matrix vertices.append(obj.matrix_world @ vert.co) # get Indices mesh.loop_triangles.foreach_get( "vertices", np.reshape(indices, len(mesh.loop_triangles) * 3)) #--------------- # Draw #--------------- bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LESS) shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') #shader = gpu.types.GPUShader(Base_Shader_3D.vertex_shader, DepthOnlyFrag.fragment_shader) batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices) gpu.matrix.reset() gpu.matrix.load_matrix(view_matrix_3d) gpu.matrix.load_projection_matrix(projection_matrix) batch.draw(shader) bgl.glDisable(bgl.GL_DEPTH_TEST)
def draw_callback_bezier_3d(self, context): bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) bezier = self.beziers[self.spline_id][self.bezier_id] split = bezier.split(self.at) points = bezier.points bgl.glLineWidth(1.0) shader.bind() shader.uniform_float("color", (1, 1, 1, 0.5)) batch = batch_for_shader(shader, 'LINES', {"pos": points}) batch.draw(shader) bgl.glPointSize(6) shader.uniform_float("color", (1, 1, 1, 1)) batch = batch_for_shader(shader, 'POINTS', {"pos": [points[0], points[3]]}) batch.draw(shader) bgl.glPointSize(2) batch = batch_for_shader(shader, 'POINTS', {"pos": [points[1], points[2]]}) batch.draw(shader) # draw new bezier anchor bgl.glLineWidth(2) shader.uniform_float("color", (0.8, 1.0, 0.0, 0.5)) batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": [split[2], split[3], split[4]]}) batch.draw(shader) bgl.glPointSize(10) shader.uniform_float("color", (0.2, 1, 0.0, 1.0)) batch = batch_for_shader(shader, 'POINTS', {"pos": [split[3]]}) batch.draw(shader) bgl.glPointSize(6) batch = batch_for_shader(shader, 'POINTS', {"pos": [split[2], split[4]]}) batch.draw(shader) # gl end and restore bgl.glLineWidth(1) bgl.glDisable(bgl.GL_BLEND) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glPointSize(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_viewport_2d(self, context): data = context.active_object.data vps = data.vertex_paint_settings bgl.glPushAttrib( bgl.GL_DEPTH_BUFFER_BIT | bgl.GL_LINE_BIT | bgl.GL_COLOR_BUFFER_BIT | bgl.GL_CURRENT_BIT) bgl.glEnable(bgl.GL_BLEND) bgl.glDepthFunc(bgl.GL_ALWAYS) bgl.glLineWidth(1) bgl.glBegin(bgl.GL_LINES) mx, my = self.mouse_pos radius = vps.radius ticks = radius if ticks % 2 != 0: ticks -= 1 ticks = min(512, max(16, ticks)) bgl.glColor4f(0, 0, 0, 1) for i in range(ticks): v = i / float(ticks) x = mx + math.cos(v * math.pi * 2) * radius y = my + math.sin(v * math.pi * 2) * radius bgl.glVertex2f(x, y) bgl.glVertex2f(x, y) bgl.glColor4f(1, 1, 1, 1) for i in range(ticks): v = i / float(ticks) x = mx + math.cos(v * math.pi * 2) * radius y = my + math.sin(v * math.pi * 2) * radius bgl.glVertex2f(x, y) bgl.glEnd() bgl.glPopAttrib();
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_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 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_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(self, context): """ Draws the preview based on the current configuration Args: _op ([type]): [description] context ([type]): [description] """ if self.batch is None: return view_projection_matrix = context.region_data.perspective_matrix normal_matrix = context.region_data.view_matrix.inverted().transposed() self.shader.bind() bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_LESS) self.shader.uniform_float("color", self.color) self.shader.uniform_float("modelviewprojection_mat", view_projection_matrix) self.shader.uniform_float("normal_mat", normal_matrix) self.batch.draw(self.shader) bgl.glDisable(bgl.GL_DEPTH_TEST)
def draw_callback_view(): settings = bpy.context.window_manager.MathVisProp scale = settings.bbox_scale with_bounding_box = not settings.bbox_hide if settings.in_front: bgl.glDepthFunc(bgl.GL_ALWAYS) else: bgl.glDepthFunc(bgl.GL_LESS) data_matrix, data_quat, data_euler, data_vector, data_vector_array = utils.console_math_data( ) if data_vector: coords = [tuple(vec.to_3d()) for vec in data_vector.values()] draw_points(coords) if data_vector_array: for line in data_vector_array.values(): coords = [tuple(vec.to_3d()) for vec in line] draw_line(coords) if data_matrix: draw_matrices(list(data_matrix.values()), scale, with_bounding_box) if data_euler or data_quat: cursor = bpy.context.scene.cursor.location.copy() derived_matrices = [] for quat in data_quat.values(): matrix = quat.to_matrix().to_4x4() matrix.translation = cursor derived_matrices.append(matrix) for eul in data_euler.values(): matrix = eul.to_matrix().to_4x4() matrix.translation = cursor derived_matrices.append(matrix) draw_matrices(derived_matrices, scale, with_bounding_box)
def draw_callback_3d(self, op, context): bgl.glPointSize(self.vertex_size) bgl.glLineWidth(self.edge_width) bgl.glEnable(bgl.GL_MULTISAMPLE) 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_BLEND) bgl.glEnable(bgl.GL_POLYGON_SMOOTH) bgl.glHint(bgl.GL_POLYGON_SMOOTH_HINT, bgl.GL_NICEST) bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glDepthFunc(bgl.GL_ALWAYS) self.shader.bind() if self.batch_path: self.batch_path.draw(self.shader) if self.batch_cp_faces: self.batch_cp_faces.draw(self.shader) if self.batch_cp_verts: self.batch_cp_verts.draw(self.shader)
def draw_brush(self): xy = self.rfcontext.actions.mouse p, n, _, _ = self.rfcontext.raycast_sources_mouse() if not p: return depth = self.rfcontext.Point_to_depth(p) if not depth: return self.scale = self.rfcontext.size2D_to_size(1.0, xy, depth) r = self.radius * self.scale co = self.outer_color ci = self.inner_color cc = self.fill_color * self.fill_color_scale ff = math.pow(0.5, 1.0 / self.falloff) fs = (1 - ff) * r # draw below bgl.glDepthFunc(bgl.GL_GREATER) bgl.glDepthRange(0.0, 0.99995) Globals.drawing.draw3D_circle(p, r, co * self.color_mult_below, n=n, width=2 * self.scale) Globals.drawing.draw3D_circle(p, r * ff, ci * self.color_mult_below, n=n, width=2 * self.scale) bgl.glDepthRange(0.0, 0.99996) Globals.drawing.draw3D_circle(p, r - fs, cc * self.color_mult_below, n=n, width=fs) # draw above bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0.0, 0.99996) Globals.drawing.draw3D_circle(p, r - fs, cc, n=n, width=fs) bgl.glDepthRange(0.0, 0.99995) Globals.drawing.draw3D_circle(p, r, co, n=n, width=2 * self.scale) Globals.drawing.draw3D_circle(p, r * ff, ci, n=n, width=2 * self.scale) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0.0, 1.0)
def draw_brush(self): xy = self.rfcontext.actions.mouse p, n, _, _ = self.rfcontext.raycast_sources_mouse() if not p: return depth = self.rfcontext.Point_to_depth(p) if not depth: return self.scale = self.rfcontext.size2D_to_size(1.0, xy, depth) # draw below bgl.glDepthFunc(bgl.GL_GREATER) bgl.glDepthRange(0.0, 0.99995) Globals.drawing.draw3D_circle(p, self.radius * self.scale * 1.0, self.outer_color * self.color_mult_below, n=n, width=2 * self.scale) Globals.drawing.draw3D_circle(p, self.radius * self.scale * 0.5, self.inner_color * self.color_mult_below, n=n, width=2 * self.scale) bgl.glDepthRange(0.0, 0.99996) Globals.drawing.draw3D_circle( p, (self.radius - 3) * self.scale * 1.0, self.outer_border_color * self.color_mult_below, n=n, width=8 * self.scale) # draw above bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0.0, 0.99996) Globals.drawing.draw3D_circle(p, (self.radius - 3) * self.scale * 1.0, self.outer_border_color, n=n, width=8 * self.scale) bgl.glDepthRange(0.0, 0.99995) Globals.drawing.draw3D_circle(p, self.radius * self.scale * 1.0, self.outer_color, n=n, width=2 * self.scale) Globals.drawing.draw3D_circle(p, self.radius * self.scale * 0.5, self.inner_color, n=n, width=2 * self.scale) bgl.glDepthFunc(bgl.GL_LEQUAL) bgl.glDepthRange(0.0, 1.0)
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_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 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_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 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)