示例#1
0
def draw_image(  # noqa: WPS210, WPS211
        x: float,  # noqa: WPS111
        y: float,  # noqa: WPS111
        width: float,
        height: float,
        image: bpy.types.Image,
        transparency: float,
        crop: Color = (0, 0, 1, 1),
) -> None:
    """Draw a image on the screen.

    Parameters:
        x: Bottom-left x-coordinate
        y: Bottom-left y-coordinate
        width: Width of the image
        height: Height of the image
        image: Image to be drawn
        transparency: Image transparency
        crop: Tuple describing how the image should be cropped

    Raises:
        Exception: Failed to load into an OpenGL texture
    """
    coords = [
        (x, y),
        (x + width, y),
        (x, y + height),
        (x + width, y + height),
    ]

    uvs = [
        (crop[0], crop[1]),
        (crop[2], crop[1]),
        (crop[0], crop[3]),
        (crop[2], crop[3]),
    ]

    indices = [(0, 1, 2), (2, 1, 3)]

    shader = gpu.shader.from_builtin('2D_IMAGE')
    batch = batch_for_shader(shader,
                             'TRIS', {
                                 'pos': coords,
                                 'texCoord': uvs
                             },
                             indices=indices)

    # send image to gpu if it isn't there already
    if image.gl_load():
        raise Exception()

    # texture identifier on gpu
    texture_id = image.bindcode

    # in case someone disabled it before
    bgl.glEnable(bgl.GL_BLEND)

    # bind texture to image unit 0
    bgl.glActiveTexture(bgl.GL_TEXTURE0)
    bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture_id)

    shader.bind()
    # tell shader to use the image that is bound to image unit 0
    shader.uniform_int('image', 0)
    batch.draw(shader)

    bgl.glDisable(bgl.GL_TEXTURE_2D)
示例#2
0
def load_texture(texture: bpy.types.Image) -> int:
    """Load the texture, return OpenGL error code."""
    return texture.gl_load()
示例#3
0
def load_texture(texture: bpy.types.Image) -> int:
    """Load the texture, return OpenGL error code."""
    return texture.gl_load(filter=bgl.GL_NEAREST, mag=bgl.GL_NEAREST)