Python bgl.glClear() Examples

The following are 3 code examples of bgl.glClear(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bgl , or try the search function .
Example #1
Source File: offscreen.py    From jewelcraft with GNU General Public License v3.0 6 votes vote down vote up
def offscreen_refresh(self, context):
        if self.offscreen is not None:
            self.offscreen.free()

        width = self.region.width
        height = self.region.height
        self.offscreen = gpu.types.GPUOffScreen(width, height)

        mat_offscreen = Matrix()
        mat_offscreen[0][0] = 2 / width
        mat_offscreen[0][3] = -1
        mat_offscreen[1][1] = 2 / height
        mat_offscreen[1][3] = -1

        with self.offscreen.bind():
            bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)

            with gpu.matrix.push_pop():
                gpu.matrix.load_matrix(mat_offscreen)
                gpu.matrix.load_projection_matrix(Matrix())

                self.draw_gems(context) 
Example #2
Source File: opengl_utils.py    From Blender-Addon-Photogrammetry-Importer with MIT License 4 votes vote down vote up
def render_opengl_image(image_name, cam, point_size):
    draw_manager = DrawManager.get_singleton()
    coords, colors = draw_manager.get_coords_and_colors()

    scene = bpy.context.scene
    render = bpy.context.scene.render

    width = render.resolution_x
    height = render.resolution_y
    # TODO Provide an option to render from the 3D view perspective
    # width = bpy.context.region.width
    # height = bpy.context.region.height

    offscreen = gpu.types.GPUOffScreen(width, height)
    with offscreen.bind():

        bgl.glPointSize(point_size)
        #bgl.glClear(bgl.GL_COLOR_BUFFER_BIT)
        #bgl.glClear(bgl.GL_DEPTH_BUFFER_BIT)

        view_matrix = cam.matrix_world.inverted()
        projection_matrix = cam.calc_matrix_camera(
            bpy.context.evaluated_depsgraph_get(), 
            x=width,
            y=height)
        perspective_matrix = projection_matrix @ view_matrix

        gpu.matrix.load_matrix(perspective_matrix)
        gpu.matrix.load_projection_matrix(Matrix.Identity(4))
        
        shader = gpu.shader.from_builtin('3D_FLAT_COLOR')
        shader.bind()
        batch = batch_for_shader(shader, "POINTS", {"pos": coords, "color": colors})
        batch.draw(shader)
        
        buffer = bgl.Buffer(bgl.GL_BYTE, width * height * 4)
        bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer)

    offscreen.free()

    image = create_image_lazy(image_name, width, height)
    copy_buffer_to_pixel(buffer, image) 
Example #3
Source File: __init__.py    From cellblender with GNU General Public License v2.0 4 votes vote down vote up
def draw_callback_px(context):
    # Note that the "context" passed in here is a regular dictionary and not the Blender context
    global screen_display_lines
    pid = None
    if 'mcell' in bpy.context.scene:
      mcell = bpy.context.scene.mcell
      if 'run_simulation' in mcell:
        rs = mcell.run_simulation
        if len(rs.processes_list) > 0:
          pid_str = rs.processes_list[rs.active_process_index].name
          pid = pid_str.split(',')[0].split()[1]

    bgl.glPushAttrib(bgl.GL_ENABLE_BIT)

    if parameter_dictionary['Clear']['val']:
      bgl.glClearColor ( 0.0, 0.0, 0.0, 1.0 )
      bgl.glClear ( bgl.GL_COLOR_BUFFER_BIT )

    font_id = 0  # XXX, need to find out how best to get this.

    y_pos = 15 * (scroll_offset + 1)
    if pid and (pid in screen_display_lines):
      for l in screen_display_lines[pid]:
          blf.position(font_id, 15, y_pos, 0)
          y_pos += 15
          blf.size(font_id, 14, 72) # fontid, size, DPI
          bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
          blf.draw(font_id, l)
    else:
      keys = screen_display_lines.keys()
      for k in keys:
          for l in screen_display_lines[k]:
              blf.position(font_id, 15, y_pos, 0)
              y_pos += 15
              blf.size(font_id, 14, 72) # fontid, size, DPI
              bgl.glColor4f(1.0, 1.0, 1.0, 0.5)
              blf.draw(font_id, l)

    # 100% alpha, 2 pixel width line
    bgl.glEnable(bgl.GL_BLEND)

    bgl.glPopAttrib()

    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)