예제 #1
0
    def draw(self, context):
        if self._area != context.area: return
        Globals.drawing.glCheckError('UI_Document.draw: start')

        time_start = time.time()

        self.force_clean(context)

        Globals.drawing.glCheckError('UI_Document.draw: setting options')
        ScissorStack.start(context)
        bgl.glClearColor(0, 0, 0, 0)
        bgl.glBlendColor(0, 0, 0, 0)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glEnable(bgl.GL_SCISSOR_TEST)
        bgl.glDisable(bgl.GL_DEPTH_TEST)
        bgl.glClear(bgl.GL_DEPTH_BUFFER_BIT)

        Globals.drawing.glCheckError('UI_Document.draw: drawing')
        self._body.draw()
        ScissorStack.end()

        self._draw_count += 1
        self._draw_time += time.time() - time_start
        if self._draw_count % 100 == 0:
            fps = (self._draw_count /
                   self._draw_time) if self._draw_time > 0 else float('inf')
            self._draw_fps = fps
            # print('~%f fps  (%f / %d = %f)' % (self._draw_fps, self._draw_time, self._draw_count, self._draw_time / self._draw_count))
            self._draw_count = 0
            self._draw_time = 0

        Globals.drawing.glCheckError('UI_Document.draw: done')
예제 #2
0
    def draw(self, highlighted: bool):
        bgl.glEnable(bgl.GL_BLEND)
        if highlighted:
            color = (0.555, 0.555, 0.555, 0.8)
        else:
            color = (0.447, 0.447, 0.447, 0.8)

        draw.aabox((self.x, self.y), (self.x + self.width, self.y + self.height), color)

        texture = self.icon
        if texture:
            err = draw.load_texture(texture)
            assert not err, 'OpenGL error: %i' % err

        # ------ TEXTURE ---------#
        if texture:
            draw.bind_texture(texture)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

        draw.aabox_with_texture(
            (self.x + self.icon_margin_x, self.y),
            (self.x + self.icon_margin_x + ICON_WIDTH, self.y + ICON_HEIGHT),
        )
        bgl.glDisable(bgl.GL_BLEND)

        if texture:
            texture.gl_free()

        # draw some text
        text_x = self.x + self.icon_margin_x + ICON_WIDTH + self.text_margin_x
        text_y = self.y + ICON_HEIGHT * 0.5 - 0.25 * self.text_size
        draw.text((text_x, text_y), self.label_text, fsize=self.text_size)
        draw.text((text_x, self.y + 0.5 * self.text_size_small), self.small_text,
                  fsize=self.text_size_small, rgba=(1.0, 1.0, 1.0, 0.5))
예제 #3
0
def drawLine(a,
             b,
             *,
             linethickness=5,
             linecolor=(1, 0, 0, 1),
             enable_depth=False):
    if (enable_depth):
        bgl.glEnable(bgl.GL_DEPTH_TEST)

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    bgl.glEnable(bgl.GL_LINE_SMOOTH)
    bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)

    bgl.glColor4f(*linecolor)
    bgl.glLineWidth(linethickness)
    bgl.glBegin(bgl.GL_LINE_STRIP)

    bgl.glVertex3f(*a)
    bgl.glVertex3f(*b)

    bgl.glEnd()

    if (enable_depth):
        bgl.glDisable(bgl.GL_DEPTH_TEST)
예제 #4
0
    def _start_drawing(self):
        """Starts the drawing.
        """

        # This handles all the settings of the renderer before starting the draw stuff
        matrix = bpy.context.region_data.perspective_matrix
        self._line_shader.uniform_float("ModelViewProjectionMatrix", matrix)

        # GL_MULTISAMPLE
        bgl.glEnable(bgl.GL_MULTISAMPLE)

        # Blender mode
        if self.blend_mode == nmv.consts.Drawing.BLEND:
            bgl.glEnable(bgl.GL_BLEND)

        elif self.blend_mode == nmv.consts.Drawing.MULTIPLY_BLEND:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glBlendFunc(bgl.GL_DST_COLOR, bgl.GL_ZERO)

        elif self.blend_mode == nmv.consts.Drawing.ADDITIVE_BLEND:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE)

        if self.draw_on_top:
            bgl.glDisable(bgl.GL_DEPTH_TEST)

        # Line width
        bgl.glLineWidth(self.line_width)

        # Point size
        bgl.glPointSize(self.point_size)
예제 #5
0
def h_draw_arc(cx, cy, r, start_angle, arc_angle, color=(1,1,1,1), segs=12):
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    
    start_angle = math.radians(start_angle)
    arc_angle = math.radians(arc_angle)
    
    theta = arc_angle / (segs-1)
    
    tangencial_factor = math.tan(theta)
    radial_factor = math.cos(theta)
    
    x = r * math.cos(start_angle)
    y = r * math.sin(start_angle)
    
    bgl.glColor4f(*color)
    bgl.glBegin(bgl.GL_POLYGON)
    
    bgl.glVertex2f(cx, cy)
    for i in range(segs):
        bgl.glVertex2f(x + cx, y + cy)
        
        tx = -y
        ty = x
        
        x += tx * tangencial_factor
        y += ty * tangencial_factor
        x *= radial_factor
        y *= radial_factor

    bgl.glEnd()
    
    bgl.glDisable(bgl.GL_BLEND)
예제 #6
0
파일: PManager.py 프로젝트: jdpillon/pgui
 def draw(self):
     width = render.getWindowWidth()
     height = render.getWindowHeight()
     
     # 2D Projection
     bgl.glMatrixMode(bgl.GL_PROJECTION)
     bgl.glLoadIdentity()
     bgl.glOrtho(0, width, height, 0, -1, 1)
     bgl.glMatrixMode(bgl.GL_MODELVIEW)
     bgl.glLoadIdentity()
     
     # 2D Shading
     bgl.glDisable(bgl.GL_CULL_FACE)
     bgl.glDisable(bgl.GL_LIGHTING)
     bgl.glDisable(bgl.GL_DEPTH_TEST)
     bgl.glShadeModel(bgl.GL_SMOOTH)
     
     # Line antialias
     bgl.glEnable(bgl.GL_LINE_SMOOTH)
     bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
     
     # 2D Blending (Alpha)
     bgl.glEnable(bgl.GL_BLEND)
     bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
     
     if len(self.controls.values()) <= 0: return
     
     ctrls = sorted(self.controls.values(), key=lambda x: x.zorder)
     for c in ctrls:
         c.draw()
def crosshairs():
    """
    Show crosshais in Manipulation Mode
    Use the OpenGL-Wrapper to draw the image
    """

    imageHeight = windowHeight * 0.05
    imageWidth = imageHeight

    x = windowWidth * 0.5 - imageWidth / 2
    y = windowHeight * 0.5 - imageHeight / 2

    gl_position = [[x, y], [x + imageWidth, y],
                   [x + imageWidth, y + imageHeight], [x, y + imageHeight]]

    view_buf = bgl.Buffer(bgl.GL_INT, 4)
    bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf)
    view = view_buf.to_list() if hasattr(view_buf,
                                         "to_list") else view_buf.list
    # Save the state
    bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS)
    # Disable depth test so we always draw over things
    bgl.glDisable(bgl.GL_DEPTH_TEST)
    # Disable lighting so everything is shadless
    bgl.glDisable(bgl.GL_LIGHTING)
    # Unbinding the texture prevents BGUI frames from somehow picking up on
    # color of the last used texture
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
    # Make sure we're using smooth shading instead of flat
    bgl.glShadeModel(bgl.GL_SMOOTH)
    # Setup the matrices
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()
    bgl.gluOrtho2D(0, view[2], 0, view[3])
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()

    bgl.glEnable(bgl.GL_TEXTURE_2D)
    # Enable alpha blending
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    # Bind the texture
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, crosshairs_id)
    # Draw the textured quad
    bgl.glColor4f(1, 1, 1, 1)
    bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL)
    bgl.glPolygonOffset(1.0, 1.0)
    bgl.glBegin(bgl.GL_QUADS)
    for i in range(4):
        bgl.glTexCoord2f(texco[i][0], texco[i][1])
        bgl.glVertex2f(gl_position[i][0], gl_position[i][1])
    bgl.glEnd()
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

    bgl.glPopMatrix()
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPopMatrix()
    bgl.glPopAttrib()
예제 #8
0
    def view_draw(self, context, depsgraph):
        region = context.region
        scene = depsgraph.scene

        # Serialize the scene
        file = os.path.join(os.path.dirname(__file__), "openGL/scene.json")
        srl.scene_to_json(resolution=(self.size_x, self.size_y),
                          scene=scene,
                          path_to_json=file,
                          context=context)

        # Get viewport dimensions
        dimensions = (self.size_x, self.size_y)

        # Bind shader that converts from scene linear to display space,
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA)
        self.bind_display_space_shader(scene)

        if not self.draw_data or self.draw_data.dimensions != dimensions:
            self.draw_data = LiquidknotDrawData(dimensions)

        self.draw_data.draw()

        self.unbind_display_space_shader()
        bgl.glDisable(bgl.GL_BLEND)
예제 #9
0
def h_draw_gradient_rect(bounds, gradient, border_width=1, border_radius=0, border_color=(0, 0, 0, 1), wire=False):
    if len(gradient.colors) != len(gradient.offsets): return
    
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    
    h_mask_begin(bounds, border_radius)
    
    mode = bgl.GL_LINE_STRIP if wire else bgl.GL_TRIANGLE_STRIP
    
    bgl.glBegin(mode)
    if gradient.orientation == 0: # HOR
        for i in range(len(gradient.colors)):
            bgl.glColor4f(*gradient.colors[i])
            x = (bounds[2] * clamp(gradient.offsets[i], 0.0, 1.0)) + bounds[0]
            bgl.glVertex2f(x, bounds[1])
            bgl.glVertex2f(x, bounds[1]+bounds[3])
    elif gradient.orientation == 1: # VER
        for i in range(len(gradient.colors)):
            bgl.glColor4f(*gradient.colors[i])
            y = (bounds[3] * clamp(gradient.offsets[i], 0.0, 1.0)) + bounds[1]
            bgl.glVertex2f(bounds[0], y)
            bgl.glVertex2f(bounds[0]+bounds[2], y)
    bgl.glEnd()
        
    h_mask_end()
    
    if border_width > 0:
        h_round_rect_wire(bounds, border_radius, color=border_color, width=border_width)
    
    bgl.glDisable(bgl.GL_BLEND)
예제 #10
0
def drawTriangle(triangle, linethickness, linecolor, fillcolor, drawpoints=False, pointcolor=None):
    bgl.glEnable(bgl.GL_BLEND);
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA);
    bgl.glEnable(bgl.GL_LINE_SMOOTH);
    bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST);
    
#     for co in triangle:
    bgl.glColor4f(*fillcolor);
    bgl.glBegin(bgl.GL_TRIANGLES);
    
    for coord in triangle:
        bgl.glVertex3f(*coord); 
    bgl.glEnd();
    
    bgl.glColor4f(*linecolor);
    bgl.glLineWidth(linethickness);
    bgl.glBegin(bgl.GL_LINE_STRIP);
    
    for coord in triangle:
        bgl.glVertex3f(*coord);    
    
    bgl.glVertex3f(*triangle[0]);
    
    bgl.glEnd();
        
    if(drawpoints):        
        bgl.glColor4f(*pointcolor);
        bgl.glPointSize(5.0);
        bgl.glBegin(bgl.GL_POINTS);
        
        for coord in triangle:
            bgl.glVertex3f(*coord);
            
        bgl.glEnd();
예제 #11
0
 def draw(self, context, render=False):
     if self.image is None:
         return
     bgl.glPushAttrib(bgl.GL_ENABLE_BIT)
     p0 = self.pts[0]
     p1 = self.pts[1]
     bgl.glEnable(bgl.GL_BLEND)
     bgl.glColor4f(*self.colour)
     bgl.glRectf(p0.x, p0.y, p1.x, p1.y)
     self.image.gl_load()
     bgl.glEnable(bgl.GL_BLEND)
     bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.image.bindcode[0])
     bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)
     bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST)
     bgl.glEnable(bgl.GL_TEXTURE_2D)
     bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
     # bgl.glColor4f(1, 1, 1, 1)
     bgl.glBegin(bgl.GL_QUADS)
     bgl.glTexCoord2d(0, 0)
     bgl.glVertex2d(p0.x, p0.y)
     bgl.glTexCoord2d(0, 1)
     bgl.glVertex2d(p0.x, p1.y)
     bgl.glTexCoord2d(1, 1)
     bgl.glVertex2d(p1.x, p1.y)
     bgl.glTexCoord2d(1, 0)
     bgl.glVertex2d(p1.x, p0.y)
     bgl.glEnd()
     self.image.gl_free()
     bgl.glDisable(bgl.GL_TEXTURE_2D)
예제 #12
0
    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)
예제 #13
0
def screen_v3dBGL(context, args):
    region = context.region
    region3d = context.space_data.region_3d
    
    points = args[0]
    colors = args[1]
    size= 5.0
    
    bgl.glEnable(bgl.GL_POINT_SMOOTH) # for round vertex
    bgl.glPointSize(size)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    
    if colors:
        bgl.glBegin(bgl.GL_POINTS)
        for coord, color in zip(points, colors):
            bgl.glColor4f(*color)    
            bgl.glVertex3f(*coord)
        bgl.glEnd()

    else:
        gl_col = (0.9, 0.9, 0.8, 1.0)
        bgl.glColor4f(*gl_col)    
        bgl.glBegin(bgl.GL_POINTS)
        for coord in points:
            bgl.glVertex3f(*coord)
        bgl.glEnd()        
    
    bgl.glDisable(bgl.GL_POINT_SMOOTH)
    bgl.glDisable(bgl.GL_POINTS)
예제 #14
0
def draw_multicircles_fill_2d(positions,
                              color,
                              radius,
                              segments=8,
                              alpha=False):
    import gpu
    import bgl

    if alpha:
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

    coords = []
    triangles = []
    # create vertices
    for center in positions:
        actCoords = GenerateCircleVerts(center, radius, segments)
        coords.extend(actCoords)
    # create triangles
    for tris in range(len(positions)):
        actTris = GenerateCircleTris(segments, tris * (segments + 1))
        triangles.extend(actTris)
    # set shader and draw
    shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
    batch = batch_for_shader(shader,
                             'TRIS', {"pos": coords},
                             indices=triangles)
    shader.bind()
    shader.uniform_float("color", color)
    batch.draw(shader)
예제 #15
0
    def view_draw(self, context, depsgraph):
        # print('view_draw')
        region = context.region
        region3d = context.region_data
        scene = depsgraph.scene
        perspective = region3d.perspective_matrix.to_4x4()

        # Get viewport dimensions
        dimensions = region.width, region.height

        # Bind shader that converts from scene linear to display space,
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA)
        self.bind_display_space_shader(scene)

        if not self.draw_data or self.updated \
            or self.draw_data.dimensions != dimensions \
            or self.draw_data.perspective != perspective:
            # print('CustomDrawData')
            self.draw_data = CustomDrawData(dimensions, perspective, region3d)
            self.updated = False

        self.draw_data.draw()

        self.unbind_display_space_shader()
        bgl.glDisable(bgl.GL_BLEND)
예제 #16
0
    def draw(self):
        width = render.getWindowWidth()
        height = render.getWindowHeight()

        # 2D Projection
        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadIdentity()
        bgl.glOrtho(0, width, height, 0, -1, 1)
        bgl.glMatrixMode(bgl.GL_MODELVIEW)
        bgl.glLoadIdentity()

        # 2D Shading
        bgl.glDisable(bgl.GL_CULL_FACE)
        bgl.glDisable(bgl.GL_LIGHTING)
        bgl.glDisable(bgl.GL_DEPTH_TEST)
        bgl.glShadeModel(bgl.GL_SMOOTH)

        # Line antialias
        bgl.glEnable(bgl.GL_LINE_SMOOTH)
        bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)

        # 2D Blending (Alpha)
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

        if len(self.controls.values()) <= 0: return

        ctrls = sorted(self.controls.values(), key=lambda x: x.zorder)
        for c in ctrls:
            c.draw()
예제 #17
0
def vray_draw_light_shape():
    if not bpy.context:
        return
    ob = bpy.context.active_object
    if not ob:
        return
    if ob.type not in {'LAMP'}:
        return

    la = ob.data

    VRayLight = LibUtils.GetLightPropGroup(la)

    bgl.glEnable(bgl.GL_POINT_SMOOTH)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

    col = lamp_color if VRayLight.enabled else lamp_color_off
    bgl.glColor4f(*col)

    if la.type == 'POINT':
        if la.vray.omni_type == 'SPHERE':
            vray_draw_light_sphere_shape(ob)
    elif la.type == 'SUN':
        if la.vray.direct_type == 'DIRECT':
            vray_draw_light_direct_shape(ob)

    # Reset draw
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
예제 #18
0
    def draw_button_icon(self):
        if self.icon != "":
            bgl.glEnable(bgl.GL_BLEND)
            texture = bpy.data.images.load(filepath=self.icon)
            err = texture.gl_load(filter=bgl.GL_NEAREST, mag=bgl.GL_NEAREST)
            assert not err, 'OpenGL error: %i' % err

            bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.bindcode[0])
            bgl.glEnable(bgl.GL_TEXTURE_2D)
            bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

            bgl.glColor4f(1, 1, 1, 1)
            bgl.glBegin(bgl.GL_QUADS)
            bgl.glTexCoord2d(0, 0)
            bgl.glVertex2d(self.x_location + ICON_MARGIN_X, self.y_location)
            bgl.glTexCoord2d(0, 1)
            bgl.glVertex2d(self.x_location + ICON_MARGIN_X,
                           self.y_location + ICON_HEIGHT)
            bgl.glTexCoord2d(1, 1)
            bgl.glVertex2d(self.x_location + ICON_MARGIN_X + ICON_WIDTH,
                           self.y_location + ICON_HEIGHT)
            bgl.glTexCoord2d(1, 0)
            bgl.glVertex2d(self.x_location + ICON_MARGIN_X + ICON_WIDTH,
                           self.y_location)
            bgl.glEnd()
            bgl.glDisable(bgl.GL_TEXTURE_2D)
            bgl.glDisable(bgl.GL_BLEND)

            texture.gl_free()
def screen_v3dBGL_overlay(context, args):

    if not args[2]:
        return

    alpha = args[3]

    region = context.region
    region3d = context.space_data.region_3d

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

    for matrix, color in args[0]:
        r, g, b = color
        bgl.glColor4f(r, g, b, alpha)
        bgl.glBegin(bgl.GL_QUADS)
        M = Matrix(matrix)
        for x, y in [(-.5, .5), (.5, .5), (.5, -.5), (-.5, -.5)]:
            vector3d = M * Vector((x, y, 0))
            vector2d = loc3d2d(region, region3d, vector3d)
            bgl.glVertex2f(*vector2d)

        bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
예제 #20
0
def vray_draw_light_shape():
    if not bpy.context:
        return
    ob = bpy.context.active_object
    if not ob:
        return
    if ob.type not in {'LAMP'}:
        return

    la = ob.data

    VRayLight = LibUtils.GetLightPropGroup(la)

    bgl.glEnable(bgl.GL_POINT_SMOOTH)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

    col = lamp_color if VRayLight.enabled else lamp_color_off
    bgl.glColor4f(*col)

    if la.type == 'POINT':
        if la.vray.omni_type == 'SPHERE':
            vray_draw_light_sphere_shape(ob)
    elif la.type == 'SUN':
        if la.vray.direct_type == 'DIRECT':
            vray_draw_light_direct_shape(ob)

    # Reset draw
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
예제 #21
0
    def draw(self):
        if self.depth_test:
            bgl.glEnable(bgl.GL_DEPTH_TEST)
        bgl.glEnable(bgl.GL_BLEND)

        if self.blend_mode == BLEND:
            bgl.glEnable(bgl.GL_BLEND)

        elif self.blend_mode == MULTIPLY_BLEND:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glBlendFunc(bgl.GL_DST_COLOR, bgl.GL_ZERO)

        elif self.blend_mode == ADDITIVE_BLEND:
            bgl.glEnable(bgl.GL_BLEND)
            bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE)

        if self.line_batch:
            self.line_shader.bind()
            self.line_shader.uniform_float('z_offset', self.z_offset)
            bgl.glLineWidth(self.line_width)
            self.line_batch.draw(self.line_shader)

        if self.point_batch:
            self.point_shader.bind()
            self.point_shader.uniform_float('z_offset', self.z_offset)
            bgl.glPointSize(self.point_size)
            self.point_batch.draw(self.point_shader)

        bgl.glDisable(bgl.GL_BLEND)
        if self.depth_test:
            bgl.glDisable(bgl.GL_DEPTH_TEST)
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
예제 #23
0
def draw_udim_tiles(M, color):

    if len(UDM_TILES) == 0:
        return

    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()

    bgl.glColor4f(*color)

    # label placement
    for tile in UDM_TILES:
        y, x = udim_to_xy(tile)
        #print("label:",y,x)

        font_id = 0
        font_size = maprange((64, 512), (8, 12), M[0])
        if (M[0] > 64):
            blf.size(font_id, int(font_size), 72)
            offset = M[0] * (1 / 32.0)
            blf.position(font_id, x * M[0] + M[12] + offset, y * M[0] + M[13] + offset, 0)
            blf.draw(font_id, str(tile))

    bgl.glPopMatrix()

    bgl.glLineWidth(1.0)
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    draw_vertex_array("udims", bgl.GL_LINES, 2, color)
예제 #24
0
def h_draw_texture(id, w, h, bounds, coords):    
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, id)
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    
    bgl.glColor4f(*(1,1,1,1))
    
    B = bounds
    C = coords
    
    D = [
        (C[0][0]/w, C[0][1]/h),
        (C[1][0]/w, C[1][1]/h),
        (C[2][0]/w, C[2][1]/h),
        (C[3][0]/w, C[3][1]/h),
    ]
    #print(D)
    
    bgl.glBegin(bgl.GL_QUADS)
    bgl.glTexCoord2f(D[0][0], D[0][1])    
    bgl.glVertex2f(B[0], B[1])
    
    bgl.glTexCoord2f(D[1][0], D[1][1])    
    bgl.glVertex2f(B[0]+B[2], B[1])
    
    bgl.glTexCoord2f(D[2][0], D[2][1])    
    bgl.glVertex2f(B[0]+B[2], B[1]+B[3])
    
    bgl.glTexCoord2f(D[3][0], D[3][1])    
    bgl.glVertex2f(B[0], B[1]+B[3])
    bgl.glEnd()

    bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
    bgl.glDisable(bgl.GL_TEXTURE_2D)
예제 #25
0
def screen_v3dBGL_overlay(context, args):

    if not args[2]:
        return

    alpha = args[3]
    

    region = context.region
    region3d = context.space_data.region_3d

    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

    for matrix, color in args[0]:
        r, g, b = color
        bgl.glColor4f(r, g, b, alpha)
        bgl.glBegin(bgl.GL_QUADS)
        M = Matrix(matrix)
        for x, y in [(-.5, .5), (.5 ,.5), (.5 ,-.5), (-.5 ,-.5)]:
            vector3d = M * Vector((x, y, 0))
            vector2d = loc3d2d(region, region3d, vector3d)
            bgl.glVertex2f(*vector2d)

        bgl.glEnd()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
예제 #26
0
    def _draw_pixels(self, context, depsgraph):

        scene = depsgraph.scene
        w = context.region.width
        h = context.region.height

        # Draw text area that RenderMan is running.
        if get_pref('draw_ipr_text',
                    False) and not self.rman_render.rman_is_viewport_rendering:

            pos_x = w / 2 - 100
            pos_y = 20
            blf.enable(0, blf.SHADOW)
            blf.shadow_offset(0, 1, -1)
            blf.shadow(0, 5, 0.0, 0.0, 0.0, 0.8)
            blf.size(0, 32, 36)
            blf.position(0, pos_x, pos_y, 0)
            blf.color(0, 1.0, 0.0, 0.0, 1.0)
            blf.draw(0, "%s" % ('RenderMan Interactive Mode Running'))
            blf.disable(0, blf.SHADOW)

        if not self.rman_render.rman_is_viewport_rendering:
            return

        # Bind shader that converts from scene linear to display space,
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA)
        self.bind_display_space_shader(scene)

        self.rman_render.draw_pixels(w, h)

        self.unbind_display_space_shader()
        bgl.glDisable(bgl.GL_BLEND)
예제 #27
0
def restore_opengl_defaults():
    bgl.glPointSize(1)
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ZERO)
    bgl.glDisable(bgl.GL_CULL_FACE)
    bgl.glDisable(bgl.GL_LINE_SMOOTH);
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
예제 #28
0
def DrawGLLines(self, context, paths, temppaths, reflector_paths, reflected_temppaths, color, thickness, LINE_TYPE= "GL_LINE"):
    bgl.glEnable(bgl.GL_BLEND);
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA);
    bgl.glEnable(bgl.GL_LINE_SMOOTH);
    bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST);
#    bgl.glDisable(bgl.GL_DEPTH_TEST);

    bgl.glLineWidth(thickness);
    
    carr = context.scene.path_color;
    color = (carr[0],carr[1],carr[2],1.0);
    
    for path in paths:        
        bgl.glBegin(bgl.GL_LINE_STRIP);
        bgl.glColor4f(*color);
        
        for coord in path:
            bgl.glVertex3f(*coord);    
        
        bgl.glEnd();
    
    for path in reflector_paths:        
        bgl.glBegin(bgl.GL_LINE_STRIP);
        bgl.glColor4f(*color);
        
        for coord in path:
            bgl.glVertex3f(*coord);    
        
        bgl.glEnd();
    
    
    
    carr = context.scene.temp_path_color;
    color = (carr[0],carr[1],carr[2],1.0);
    for path in temppaths:
        bgl.glBegin(bgl.GL_LINE_STRIP);
        bgl.glColor4f(*color);
        
        for co in path:
            bgl.glVertex3f(*co);
            
        bgl.glEnd();
    
    
    for path in reflected_temppaths:
        bgl.glBegin(bgl.GL_LINE_STRIP);
        bgl.glColor4f(*color);
        
        for co in path:
            bgl.glVertex3f(*co);
            
        bgl.glEnd();

    # restore opengl defaults
    bgl.glLineWidth(1);
    bgl.glDisable(bgl.GL_BLEND);
#    bgl.glEnable(bgl.GL_DEPTH_TEST);
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
예제 #29
0
def crosshairs():
    """
    Show crosshais in Manipulation Mode
    Use the OpenGL-Wrapper to draw the image
    """
    
    imageHeight = windowHeight * 0.05
    imageWidth = imageHeight

    x = windowWidth * 0.5 - imageWidth/2
    y = windowHeight * 0.5 - imageHeight/2
    
    gl_position = [[x, y],[x+imageWidth, y],[x+imageWidth, y+imageHeight],[x, y+imageHeight]]
    
    view_buf = bgl.Buffer(bgl.GL_INT, 4)
    bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf)
    view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list
    # Save the state
    bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS)      
    # Disable depth test so we always draw over things
    bgl.glDisable(bgl.GL_DEPTH_TEST)   
    # Disable lighting so everything is shadless
    bgl.glDisable(bgl.GL_LIGHTING)   
    # Unbinding the texture prevents BGUI frames from somehow picking up on
    # color of the last used texture
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)      
    # Make sure we're using smooth shading instead of flat
    bgl.glShadeModel(bgl.GL_SMOOTH)
    # Setup the matrices
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()
    bgl.gluOrtho2D(0, view[2], 0, view[3])
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()
    
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    # Enable alpha blending
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    # Bind the texture
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, crosshairs_id)
    # Draw the textured quad
    bgl.glColor4f(1, 1, 1, 1)
    bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL)
    bgl.glPolygonOffset(1.0, 1.0)
    bgl.glBegin(bgl.GL_QUADS)
    for i in range(4):
       bgl.glTexCoord2f(texco[i][0], texco[i][1])
       bgl.glVertex2f(gl_position[i][0], gl_position[i][1])
    bgl.glEnd()
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

    bgl.glPopMatrix()
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPopMatrix()
    bgl.glPopAttrib()
예제 #30
0
def Draw_Image(image,
               _pos,
               _width,
               _height,
               _use_transparency=False,
               _flipX=False,
               _flipY=False):
    off_x, off_y = _pos
    if image.gl_load():
        raise Exception()
    # bottom left, top left, top right, bottom right
    if _flipX:
        vertices = ((off_x + _width, _height + off_y), (off_x + _width, off_y),
                    (off_x, off_y), (off_x, _height + off_y))
    elif _flipY:
        vertices = ((off_x, off_y), (off_x, _height + off_y),
                    (off_x + _width, _height + off_y), (off_x + _width, off_y))
    else:
        vertices = ((off_x, _height + off_y), (off_x, off_y),
                    (off_x + _width, off_y), (off_x + _width, _height + off_y))
    batch_img = batch_for_shader(
        shader_img,
        'TRI_FAN',
        {
            "pos": vertices,
            "texCoord": ((0, 1), (0, 0), (1, 0), (1, 1))
        },
    )
    #if _image is not None:
    try:
        if _use_transparency:
            bgl.glEnable(bgl.GL_BLEND)
            # TRANSPARENCIA
            bgl.glBlendFunc(bgl.GL_SRC_COLOR, bgl.GL_ONE)
            #bgl.glBlendFunc(bgl.GL_SRC_COLOR, bgl.GL_ONE_MINUS_SRC1_ALPHA) # GL_DST_ALPHA # GL_ONE_MINUS_CONSTANT_ALPHA # GL_SRC_ALPHA # GL_ONE
            # AUMENTA CONTRASTE Y SATURACION
            #bgl.glBlendFunc(bgl.GL_ONE_MINUS_CONSTANT_ALPHA, bgl.GL_SRC_COLOR)
            # bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_SRC_COLOR)
            # bgl.glBlendFunc(bgl.GL_SRC_COLOR, bgl.GL_CONSTANT_ALPHA)
        if image.ref.in_front:
            bgl.glDisable(bgl.GL_DEPTH_TEST)  # DELANTE
        else:
            bgl.glEnable(bgl.GL_DEPTH_TEST)  # DETRÁS
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode)
        #bgl.glColor4ub(0,0,0, 120)
        #bgl.glTexEnvf(bgl.GL_TEXTURE_2D, bgl.GL_SOURCE0_ALPHA, bgl.GL_TEXTURE0)
        #bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
        shader_img.bind()
        shader_img.uniform_int("image", 0)
        batch_img.draw(shader_img)
        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_DEPTH_TEST)
        return True
    except:
        return False
예제 #31
0
    def draw(self):
        if self.visible == False: return

        module.post_draw_step += 1

        height = render.getWindowHeight()
        width = render.getWindowWidth()

        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glLoadIdentity()
        bgl.gluOrtho2D(0, width, 0, height)
        bgl.glMatrixMode(bgl.GL_MODELVIEW)
        bgl.glLoadIdentity()

        # Enable textures
        bgl.glEnable(bgl.GL_TEXTURE_2D)

        # Enable alpha blending
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
        bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1)

        # Bind the texture
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._tex_id)

        # Fix position
        w, h = self._size
        bgl.glTranslatef(0, -h, 1)

        #MipLevel
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_BASE_LEVEL, 0)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAX_LEVEL, 0)
        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)

        # Draw the textured quad
        bgl.glColor4f(*self.color)

        bgl.glBegin(bgl.GL_QUADS)
        self.calculate_glposition()
        for i in range(4):
            bgl.glTexCoord2f(self.texco[i][0], self.texco[i][1])
            bgl.glVertex2f(self.gl_position[i][0], self.gl_position[i][1])
        bgl.glEnd()

        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        bgl.glDisable(bgl.GL_BLEND)
        bgl.glDisable(bgl.GL_TEXTURE_2D)

        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glPopMatrix()
        bgl.glMatrixMode(bgl.GL_MODELVIEW)
예제 #32
0
def drawMenuItem(item, x, y, width, height):
    global iconWidth
    global iconHeight

    iconMarginX = 4
    iconMarginY = 4
    textMarginX = 6

    textHeight = 16
    textWidth = 72

    bgl.glEnable(bgl.GL_BLEND)
    if item['highlighted']:
        bgl.glColor4f(0.555, 0.555, 0.555, 0.8)
    else:
        bgl.glColor4f(0.447, 0.447, 0.447, 0.8)

    bgl.glRectf(x, y, x + width, y + height)

    texture = item['icon']
    texture.gl_load()
    bgl.glColor4f(0.0, 0.0, 1.0, 0.5)
    #bgl.glLineWidth(1.5)

    #------ TEXTURE ---------#
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.bindcode[0])
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER,
                        bgl.GL_NEAREST)
    bgl.glTexParameteri(
        bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST
    )  #GL_LINEAR seems to be used in Blender for background images
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

    bgl.glColor4f(1, 1, 1, 1)
    bgl.glBegin(bgl.GL_QUADS)
    bgl.glTexCoord2d(0, 0)
    bgl.glVertex2d(x + iconMarginX, y)
    bgl.glTexCoord2d(0, 1)
    bgl.glVertex2d(x + iconMarginX, y + iconHeight)
    bgl.glTexCoord2d(1, 1)
    bgl.glVertex2d(x + iconMarginX + iconWidth, y + iconHeight)
    bgl.glTexCoord2d(1, 0)
    bgl.glVertex2d(x + iconMarginX + iconWidth, y)
    bgl.glEnd()

    texture.gl_free()

    # draw some text
    font_id = 0
    blf.position(font_id, x + iconMarginX + iconWidth + textMarginX,
                 y + iconHeight * 0.5 - 0.25 * textHeight, 0)
    blf.size(font_id, textHeight, textWidth)
    blf.draw(font_id, item['text'])
예제 #33
0
    def draw(self, context):
        log("Draw")

        if not self.is_synced:
            return

        stage = self.stage
        if not stage:
            return

        view_settings = ViewSettings(context)
        if view_settings.width * view_settings.height == 0:
            return

        gf_camera = view_settings.export_camera()
        self.renderer.SetCameraState(
            gf_camera.frustum.ComputeViewMatrix(),
            gf_camera.frustum.ComputeProjectionMatrix())
        if self.is_gl_delegate:
            self.renderer.SetRenderViewport(
                (*view_settings.border[0], *view_settings.border[1]))
        else:
            self.renderer.SetRenderViewport((0, 0, *view_settings.border[1]))
        self.renderer.SetRendererAov('color')
        self.render_params.renderResolution = (view_settings.width,
                                               view_settings.height)

        if self.is_gl_delegate:
            bgl.glEnable(bgl.GL_DEPTH_TEST)
        else:
            bgl.glDisable(bgl.GL_DEPTH_TEST)

        bgl.glViewport(*view_settings.border[0], *view_settings.border[1])
        bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
        bgl.glClearDepth(1.0)
        bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT)

        # Bind shader that converts from scene linear to display space,
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA)
        self.render_engine.bind_display_space_shader(context.scene)

        try:
            self.renderer.Render(stage.GetPseudoRoot(), self.render_params)
        except Exception as e:
            log.error(e)

        self.render_engine.unbind_display_space_shader()
        bgl.glDisable(bgl.GL_BLEND)

        if not self.renderer.IsConverged():
            self.notify_status("Rendering...", "")
        else:
            self.notify_status("Rendering Done", "", False)
예제 #34
0
def drawMenuItem(item, x, y, width, height):
    global iconWidth
    global iconHeight

    iconMarginX = 4
    iconMarginY = 4
    textMarginX = 6

    textHeight = 16
    textWidth = 72

    bgl.glEnable(bgl.GL_BLEND)
    if item["highlighted"]:
        bgl.glColor4f(0.555, 0.555, 0.555, 0.8)
    else:
        bgl.glColor4f(0.447, 0.447, 0.447, 0.8)

    bgl.glRectf(x, y, x + width, y + height)

    texture = item["icon"]
    texture.gl_load()
    bgl.glColor4f(0.0, 0.0, 1.0, 0.5)
    # bgl.glLineWidth(1.5)

    # ------ TEXTURE ---------#
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.bindcode)
    bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)
    bgl.glTexParameteri(
        bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST
    )  # GL_LINEAR seems to be used in Blender for background images
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

    bgl.glColor4f(1, 1, 1, 1)
    bgl.glBegin(bgl.GL_QUADS)
    bgl.glTexCoord2d(0, 0)
    bgl.glVertex2d(x + iconMarginX, y)
    bgl.glTexCoord2d(0, 1)
    bgl.glVertex2d(x + iconMarginX, y + iconHeight)
    bgl.glTexCoord2d(1, 1)
    bgl.glVertex2d(x + iconMarginX + iconWidth, y + iconHeight)
    bgl.glTexCoord2d(1, 0)
    bgl.glVertex2d(x + iconMarginX + iconWidth, y)
    bgl.glEnd()

    texture.gl_free()

    # draw some text
    font_id = 0
    blf.position(font_id, x + iconMarginX + iconWidth + textMarginX, y + iconHeight * 0.5 - 0.25 * textHeight, 0)
    blf.size(font_id, textHeight, textWidth)
    blf.draw(font_id, item["text"])
예제 #35
0
	def draw(self):
		if self.visible == False: return
		
		module.post_draw_step += 1
		
		height = render.getWindowHeight()
		width = render.getWindowWidth()
	
		bgl.glMatrixMode(bgl.GL_PROJECTION)
		bgl.glLoadIdentity()
		bgl.gluOrtho2D(0, width, 0, height)
		bgl.glMatrixMode(bgl.GL_MODELVIEW)
		bgl.glLoadIdentity()
	
		# Enable textures
		bgl.glEnable(bgl.GL_TEXTURE_2D)

		# Enable alpha blending
		bgl.glEnable(bgl.GL_BLEND)
		bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
		bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1)

		# Bind the texture
		bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._tex_id)

		# Fix position
		w, h = self._size
		bgl.glTranslatef(0, -h, 1)

		#MipLevel
		bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_BASE_LEVEL, 0);
		bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAX_LEVEL, 0);
		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);
		
		# Draw the textured quad
		bgl.glColor4f(*self.color)

		bgl.glBegin(bgl.GL_QUADS)
		self.calculate_glposition()
		for i in range(4):
			bgl.glTexCoord2f(self.texco[i][0], self.texco[i][1])
			bgl.glVertex2f(self.gl_position[i][0], self.gl_position[i][1])
		bgl.glEnd()

		bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)
		
		bgl.glDisable(bgl.GL_BLEND)
		bgl.glDisable(bgl.GL_TEXTURE_2D)

		bgl.glMatrixMode(bgl.GL_PROJECTION)
		bgl.glPopMatrix()
		bgl.glMatrixMode(bgl.GL_MODELVIEW)
예제 #36
0
def render_debug_cross(context, props: MasterProperties) -> (bgl.Buffer, int):
    """
    Render debug cross
    :returns buffer with image and draw call count
    """
    shaders = Shaders()

    offscreen = gpu.types.GPUOffScreen(props.resolution.resolution_x,
                                       props.resolution.resolution_y)
    draw_count = 0

    quad_batch = batch_quad(shaders.debug)

    with offscreen.bind():
        # black background
        bgl.glClearColor(0.0, 0.0, 0.0, 1.0)
        bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE)

        shaders.debug.bind()

        for position in props.positions:
            pos = Vector((position.manual_x, position.manual_y))

            # set position from object
            if position.variant == 'auto' and position.auto_object is not None:
                world_pos = position.auto_object.matrix_world.to_translation()
                pos = bpy_extras.object_utils.world_to_camera_view(
                    context.scene, context.scene.camera, world_pos)

            uniforms = {
                "flare_position":
                pos.xy,
                "aspect_ratio":
                props.resolution.resolution_x / props.resolution.resolution_y,
            }

            set_float_uniforms(shaders.debug, uniforms)

            quad_batch.draw(shaders.debug)
            draw_count += 1

        # copy rendered image to RAM
        buffer = bgl.Buffer(
            bgl.GL_FLOAT,
            props.resolution.resolution_x * props.resolution.resolution_y * 4)
        bgl.glReadBuffer(bgl.GL_BACK)
        bgl.glReadPixels(0, 0, props.resolution.resolution_x,
                         props.resolution.resolution_y, bgl.GL_RGBA,
                         bgl.GL_FLOAT, buffer)

    return buffer, draw_count
예제 #37
0
def color_placing():
    """
    Draw the green rectangle via OpenGL-Wrapper
    """
    imageHeight = windowHeight * 0.05
    imageWidth = imageHeight

    x = windowWidth * 0.5 - imageWidth/2
    y = windowHeight * 0.5 - imageHeight/2
    
    gl_position = [[x, y],[x+imageWidth, y],[x+imageWidth, y+imageHeight],[x, y+imageHeight]]
    
    view_buf = bgl.Buffer(bgl.GL_INT, 4)
    bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf)
    view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list
    # Save the state
    bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS)      
    # Disable depth test so we always draw over things
    bgl.glDisable(bgl.GL_DEPTH_TEST)   
    # Disable lighting so everything is shadless
    bgl.glDisable(bgl.GL_LIGHTING)   
    # Make sure we're using smooth shading instead of flat
    bgl.glShadeModel(bgl.GL_SMOOTH)
    # Setup the matrices
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()
    bgl.gluOrtho2D(0, view[2], 0, view[3])
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()
    
    
    # Enable alpha blending
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    # Draw the colored quad
    bgl.glColor4f(0, 1, 0, 0.25)
    bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL)
    bgl.glPolygonOffset(1.0, 1.0)
    bgl.glBegin(bgl.GL_QUADS)
    for i in range(4):
       bgl.glTexCoord2f(texco[i][0], texco[i][1])
       bgl.glVertex2f(gl_position[i][0], gl_position[i][1])
    bgl.glEnd()

    bgl.glPopMatrix()
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPopMatrix()
    bgl.glPopAttrib()
예제 #38
0
    def view_draw(self, context, depsgraph):
        region = context.region
        scene = depsgraph.scene

        # Get viewport dimensions
        dimensions = region.width, region.height

        # Bind shader that converts from scene linear to display space,
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_ONE, bgl.GL_ONE_MINUS_SRC_ALPHA);
        self.bind_display_space_shader(scene)

        if not self.draw_data or self.draw_data.dimensions != dimensions:
            self.draw_data = CustomDrawData(dimensions)

        self.draw_data.draw()

        self.unbind_display_space_shader()
        bgl.glDisable(bgl.GL_BLEND)
예제 #39
0
def draw_points(context, points, size, gl_col):
    region = context.region
    rv3d = context.space_data.region_3d
    
    # needed for adjusting the size of gl_points    
    bgl.glEnable(bgl.GL_POINT_SMOOTH)
    bgl.glPointSize(size)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    
    bgl.glBegin(bgl.GL_POINTS)
    bgl.glColor4f(*gl_col)    
    for coord in points:
        vector3d = (coord.x, coord.y, coord.z)
        vector2d = loc3d2d(region, rv3d, vector3d)
        bgl.glVertex2f(*vector2d)
    bgl.glEnd()
    
    bgl.glDisable(bgl.GL_POINT_SMOOTH)
    bgl.glDisable(bgl.GL_POINTS)
    return
예제 #40
0
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.glBegin(bgl.GL_POINTS)  
    # draw red  
    bgl.glColor4f(*color)      
    for coord in points:  
        vector3d = (coord.x, coord.y, coord.z)  
        vector2d = view3d_utils.location_3d_to_region_2d(region, rv3d, vector3d)  
        bgl.glVertex2f(*vector2d)  
    bgl.glEnd()  
      
    bgl.glDisable(bgl.GL_POINT_SMOOTH)  
    bgl.glDisable(bgl.GL_POINTS)  
    return
def draw_callback_px(self, context):  
    
   
    region = context.region  
    rv3d = context.space_data.region_3d  
    sel_obs = bpy.context.selected_objects
    
    #get the firs object information
    ob = bpy.context.object   
    mx = ob.matrix_world
    imx = mx.inverted()
    ob_loc_3d = ob.location + Vector((0,0,1))    
    ob_loc2D = location_3d_to_region_2d(region, rv3d, ob_loc_3d)
    
    #simple attempt to keep the results out of the way
    mouse_quad = [-1,-1]    
    if self.mouse[0] > ob_loc2D[0]:
        mouse_quad[0] = 1
    if self.mouse[1] > ob_loc2D[1]:
        mouse_quad[1] = 1
    
    #this is from this thread.
    #http://blenderartists.org/forum/showthread.php?247286-mouse-coordinate-to-view-3d-coordinate&p=2067020&viewfull=1#post2067020
    vec = region_2d_to_vector_3d(region, rv3d, self.mouse)
    loc = region_2d_to_location_3d(region, rv3d, self.mouse, vec)    
    
    #raycast onto active object
    #raycast needs to be in ob coordinates.
    a = loc + 3000*vec
    b = loc - 3000*vec       
    hit = ob.ray_cast(imx*a, imx*b)
    
    #get 2nd obj
    if hit[2] != -1:
        close_pt_gl = None
        thick = None
        
        if len(sel_obs) > 1:
            ob2 = [obj for obj in bpy.context.selected_objects if obj != ob][0]
            mx2 = ob2.matrix_world
            imx2 = mx2.inverted()
        
            close_pt = mx2*ob2.closest_point_on_mesh(imx2*(mx*hit[0]))[0]
            close_pt_gl = location_3d_to_region_2d(region, rv3d, close_pt)
            thick =  close_pt- mx*hit[0]
            
        else:
            a = hit[0]  #no need to go to world yet
            b1 = mx*hit[0] + 3000*vec  #the first hit going forward...
            b2 = mx*hit[0] - 3000*vec  #the first hit going backward
        
            hit1 = hit = ob.ray_cast(a, imx*b1)
            hit2 = hit = ob.ray_cast(a, imx*b2)
        
            if hit1[2] != -1 and hit2[2] != -1:
                thick = mx*hit1[0] - mx*hit2[0]
        
        if thick:
            #print out the thickness here...
            bgl.glColor4f(1.0, 1.0, 1.0, 1.0)         
            blf.position(0,self.mouse[0]+15,self.mouse[1]+30*mouse_quad[1],0)
            blf.size(0,20,72)
            blf.draw(0,str(thick.length)[0:5])
        if close_pt_gl:
            #draw a point on ob2 showing the closest point        
            bgl.glEnable(bgl.GL_POINT_SMOOTH)  
            bgl.glPointSize(3)  
            bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)  
      
            bgl.glBegin(bgl.GL_POINTS)  
            # draw red  
            bgl.glColor4f(1.0, 0.2, 0.2, 1.0)       
            bgl.glVertex2f(*close_pt_gl)
            
        bgl.glEnd()
        bgl.glDisable(bgl.GL_POINT_SMOOTH) 
              
    bgl.glLineWidth(1)  
    bgl.glDisable(bgl.GL_BLEND)  
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0) 
    return
예제 #42
0
	def draw(self):
		if self.visible == False: return
		module.post_draw_step += 1
		
		cam = self.scene.active_camera
		orth = cam.ortho_scale
		
		height = render.getWindowHeight()
		width = render.getWindowWidth()
		near = cam.near
		far = cam.far
		h = cam.worldPosition.z
		font_id = Label._fontname_id[self._font]
		unit = width/orth
		self._glunit = unit
		rpos = self._position - cam.worldPosition
		
		bgl.glMatrixMode(bgl.GL_PROJECTION)
		bgl.glLoadIdentity()
		bgl.gluOrtho2D(0, width, 0, height)
		bgl.glMatrixMode(bgl.GL_MODELVIEW)
		bgl.glLoadIdentity()
		
		bgl.glEnable(bgl.GL_BLEND)
		bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
		bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1)
		
		#Z AXIS
		oh = (far-near)/2
		ortho_unit = 1/oh
		dh = oh

		pos = list([width/2+rpos[0]*unit, height/2+rpos[1]*unit, dh*ortho_unit + rpos[2]*ortho_unit])
		if self._lastscale != self.scale or True:
			blf.size(font_id, int(self.scale.x*unit), 72)
		else:
			if self._lastorth != orth:
				sc = (float(self._lastorth) / float(orth)) * self.scale.x
				bgl.glScalef(sc,sc,1)
				print(str(self._lastorth) + " " + str(orth))
				pos[0] /= sc
				pos[1] /= sc
				
			else:
				self._lastorth = orth
		
		x, y = blf.dimensions(font_id, self._text) #NOTE: Always after blf.size()
		
		if self.align == ALIGN_CENTER:
			pos[0] -= (x)/2 * math.cos(self._rotation.z)
			pos[1] -= x/2 * math.sin(self._rotation.z)
		if self.align == ALIGN_RIGHT:
			pos[0] -= x * math.cos(self._rotation.z)
			pos[1] -= x * math.sin(self._rotation.z)
			
		if self.middle_height == True:
			pos[0] -= y/4 * math.sin(self._rotation.z)
			pos[1] -= y/4 * math.cos(self._rotation.z)
		
		blf.position(font_id, pos[0], pos[1], pos[2])
		blf.enable(font_id, blf.ROTATION)
		if self.rotation.z > 0.01 or self.rotation.z < -0.01:
			blf.rotation(font_id, self._rotation.z)
		else:
			blf.rotation(font_id, 0)
		
		if self.shadow == True:
			blf.position(font_id, pos[0]+self.shadow_offset[0], pos[1]+self.shadow_offset[1], pos[2])
			bgl.glColor4f(*self.shadow_color)
			blf.blur(font_id, self.shadow_blur)
			blf.draw(font_id, self._text)
			blf.position(font_id, pos[0], pos[1], pos[2])
			
		bgl.glColor4f(*self._color)
		blf.blur(font_id, self.blur)
		blf.draw(font_id, self._text)
		
		blf.disable(font_id, blf.ROTATION)
		
		self._lastscale = self.scale
예제 #43
0
def status_image():
    """
    Show the corrensponding Image for the status
    """
    imageHeight = windowHeight * 0.075
    imageWidth = imageHeight

    x = windowWidth * 0.35 - imageWidth/2
    y = windowHeight * 0.45 - imageHeight/2
    
    gl_position = [[x, y],[x+imageWidth, y],[x+imageWidth, y+imageHeight],[x, y+imageHeight]]

    cam = logic.getCurrentScene().active_camera

    # get the suffix of the human to reference the right objects
    suffix = cam.name[-4:] if cam.name[-4] == "." else ""

    hand = objects['Hand_Grab.R' + suffix]

    # select the right Image
    if hand["selected"]:
        tex_id = closed_id
    else:
        tex_id = open_id

    
    view_buf = bgl.Buffer(bgl.GL_INT, 4)
    bgl.glGetIntegerv(bgl.GL_VIEWPORT, view_buf)
    view = view_buf.to_list() if hasattr(view_buf, "to_list") else view_buf.list
    # Save the state
    bgl.glPushAttrib(bgl.GL_ALL_ATTRIB_BITS)      
    # Disable depth test so we always draw over things
    bgl.glDisable(bgl.GL_DEPTH_TEST)   
    # Disable lighting so everything is shadless
    bgl.glDisable(bgl.GL_LIGHTING)   
    # Unbinding the texture prevents BGUI frames from somehow picking up on
    # color of the last used texture
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)      
    # Make sure we're using smooth shading instead of flat
    bgl.glShadeModel(bgl.GL_SMOOTH)
    # Setup the matrices
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()
    bgl.gluOrtho2D(0, view[2], 0, view[3])
    bgl.glMatrixMode(bgl.GL_MODELVIEW)
    bgl.glPushMatrix()
    bgl.glLoadIdentity()
    
    
    bgl.glEnable(bgl.GL_TEXTURE_2D)
    # Enable alpha blending
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    # Bind the texture
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, tex_id)
    # Draw the textured quad
    bgl.glColor4f(1, 1, 1, 1)
    bgl.glEnable(bgl.GL_POLYGON_OFFSET_FILL)
    bgl.glPolygonOffset(1.0, 1.0)
    bgl.glBegin(bgl.GL_QUADS)
    for i in range(4):
       bgl.glTexCoord2f(texco[i][0], texco[i][1])
       bgl.glVertex2f(gl_position[i][0], gl_position[i][1])
    bgl.glEnd()
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

    bgl.glPopMatrix()
    bgl.glMatrixMode(bgl.GL_PROJECTION)
    bgl.glPopMatrix()
    bgl.glPopAttrib()
예제 #44
0
def draw_tris(region, rv3d, context):
    divs = 24   # verts per fan.
    n = 3       # ratio of shortest edge.

    def get_tri_coords(object_list):
        # replace for empties code.
        coordlist = [obj.location for obj in object_list]
        return coordlist
    
    def get_angle_rad(set_of_coords):
        # angle1_rad = (coord3-coord1).angle(coord2-coord1)
        # angle2_rad = (coord1-coord2).angle(coord3-coord2)
        # angle3_rad = (coord2-coord3).angle(coord1-coord3)
        # angle1_deg = degrees(angle1_rad)
        # angle2_deg = degrees(angle2_rad)
        # angle3_deg = degrees(angle3_rad)
        coord1, coord2, coord3 = set_of_coords
        angle_rad = (coord1-coord2).angle(coord3-coord2)
        angle_deg = degrees(angle_rad)
        return angle_rad, angle_deg
    
    
    # if 3 empties selected
    coord1, coord2, coord3 = get_tri_coords(context.selected_objects)
    
    # measure angle between 
    angle1 = [coord3, coord1, coord2]
    angle2 = [coord1, coord2, coord3]
    angle3 = [coord2, coord3, coord1]
        
    edge1 = (coord1-coord2).length
    edge2 = (coord2-coord3).length
    edge3 = (coord3-coord1).length
    shortest_edge = min(edge1, edge2, edge3)
    radial_d = shortest_edge / n
    
    
    
    def make_fan_poly_from_edges(angle_object, radius):
        coordinate_1, shared_co, coordinate_2 = angle_object
    
        # get length of edge, lerp it , place a point at radial distance (pointN)
        len1 = (coordinate_1-shared_co).length
        len2 = (coordinate_2-shared_co).length
        tlerp1 = 1/(len1/radius)
        tlerp2 = 1/(len2/radius)
        point1 = shared_co.lerp(coordinate_1, tlerp1)
        point2 = shared_co.lerp(coordinate_2, tlerp2)
                
        # place imaginary line between (point1, point2)
        radial_collection = []
        radial_collection.append(shared_co)
      
        # start from point1, place temp point (1/24)*i   
        # collect points. check all points for distance to angle_point.
        rate = 1/divs
        for notch in range(divs+1):
            new_vec = point1.lerp(point2, rate*notch)
            # move new vec away from shared point until the distance is radius.
            new_vec_len = (new_vec-shared_co).length
            lerp_distance = radius/new_vec_len
            radial_point = shared_co.lerp(new_vec, lerp_distance)
            radial_collection.append(radial_point)
        
        radial_collection.append(shared_co)
              
        # make polygon 
        return radial_collection
       
    
    bgl.glEnable(bgl.GL_BLEND) # enable blending
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    
    angle_list = [angle1, angle2, angle3]
    for item in angle_list:
        polyline = make_fan_poly_from_edges(item, radial_d)
    
        #can be modified per polyline    
        bgl.glColor4f(0.103, 0.3, 0.6, 0.4)    
    
        bgl.glBegin(bgl.GL_POLYGON)    
        for segment in polyline:
            scr_pixel = loc3d2d(region, rv3d, segment)
            bgl.glVertex2f(*scr_pixel)  
    
        bgl.glEnd()
        

    # get text.        
    for item in angle_list:    

        bgl.glColor4f(0.83, 0.8, 0.9, 0.7)    
        angrad, angdeg = get_angle_rad(item)
        polyline = make_fan_poly_from_edges(item, radial_d)

        # find coordinate to place the text
        cpoint1 = polyline[0]
        midpoint = floor(len(polyline)/2)
        cpoint2 = polyline[midpoint]
        cmidway = cpoint1.lerp(cpoint2, 0.5)
        scr_coord = loc3d2d(region, rv3d, cmidway)
        
        # round both text
        angrad_round = round(angrad, ANG_ROUND)
        angdeg_round = round(angdeg, DEG_ROUND)
        str_angrad = str(angrad_round)
        str_angdeg = str(angdeg_round)
        combined_string = str_angrad + " , " + str_angdeg
        
        
        # get length of text, place text .5 of length to the left of the coord.
        font_id = 0
        blf.size(font_id, 12, 72)  
        
        text_width, text_height = blf.dimensions(font_id, combined_string)
        x_pos = text_width/2

        blf.position(font_id, scr_coord[0]-x_pos, scr_coord[1], 0)
        blf.draw(font_id, combined_string)
        
    return
예제 #45
0
def generate_icon(name, verts=None, faces=None):
    pcoll = preview_collections["shape_types"]
    if name in pcoll:
        thumb = pcoll.get(name)
    else:
        thumb = pcoll.new(name)
    thumb.image_size = (200, 200)

    if verts is not None:
        import bgl

        polygon_color = bpy.context.user_preferences.themes[0].view_3d.edge_facesel
        edge_color = bpy.context.user_preferences.themes[0].view_3d.edge_select
        vertex_color = bpy.context.user_preferences.themes[0].view_3d.vertex_select
        clear_color = bpy.context.user_preferences.themes[0].user_interface.wcol_menu.inner

        viewport_info = bgl.Buffer(bgl.GL_INT, 4)
        bgl.glGetIntegerv(bgl.GL_VIEWPORT, viewport_info)
        buffer = bgl.Buffer(bgl.GL_FLOAT, 200 * 200 * 4)

        bgl.glDisable(bgl.GL_SCISSOR_TEST)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
        bgl.glEnable(bgl.GL_LINE_SMOOTH)
        bgl.glEnable(bgl.GL_POINT_SMOOTH)
        bgl.glViewport(0, 0, 200, 200)

        bgl.glMatrixMode(bgl.GL_MODELVIEW)
        bgl.glPushMatrix()
        bgl.glLoadIdentity()

        bgl.glMatrixMode(bgl.GL_PROJECTION)
        bgl.glPushMatrix()
        bgl.glLoadIdentity()
        bgl.gluOrtho2D(0, 0, 0, 0)

        bgl.glLineWidth(4.0)
        bgl.glPointSize(10.0)

        bgl.glClearColor(*clear_color)
        bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT)

        if faces is None:
            bgl.glBegin(bgl.GL_POLYGON)
            bgl.glColor3f(*polygon_color)
            for vert in verts:
                bgl.glVertex2f(*vert)
            bgl.glEnd()
        else:
            bgl.glBegin(bgl.GL_TRIANGLES)
            bgl.glColor3f(*polygon_color)
            for face in faces:
                bgl.glVertex2f(*verts[face[0]])
                bgl.glVertex2f(*verts[face[1]])
                bgl.glVertex2f(*verts[face[2]])
            bgl.glEnd()

        bgl.glBegin(bgl.GL_LINE_LOOP)
        bgl.glColor3f(*edge_color)
        for vert in verts:
            bgl.glVertex2f(*vert)
        bgl.glEnd()

        bgl.glBegin(bgl.GL_POINTS)
        bgl.glColor3f(*vertex_color)
        for vert in verts:
            bgl.glVertex2f(*vert)
        bgl.glEnd()

        bgl.glReadPixels(0, 0, 200, 200, bgl.GL_RGBA, bgl.GL_FLOAT, buffer)
        bgl.glEnable(bgl.GL_SCISSOR_TEST)
        bgl.glLineWidth(1.0)
        bgl.glPointSize(1.0)

        buffer = buffer[:]
        for idx in range(0, 200 * 200 * 4, 4):
            if (
                buffer[idx] == clear_color[0]
                and buffer[idx + 1] == clear_color[1]
                and buffer[idx + 2] == clear_color[2]
            ):
                buffer[idx + 3] = 0.0

        thumb.image_pixels_float = buffer