def init_opengl(cls):
        # OpenGL was already initialized, nothing to do here.
        if cls.shader is not None:
            return

        cls.shader = Shader(
            load_shader('3D_vert.glsl'),
            None,
            load_shader('primitive_id_frag.glsl'),
        )

        cls.unif_use_clip_planes = bgl.glGetUniformLocation(
            cls.shader.program, 'use_clip_planes')
        cls.unif_clip_plane = bgl.glGetUniformLocation(cls.shader.program,
                                                       'clip_plane')

        cls.unif_MVP = bgl.glGetUniformLocation(cls.shader.program, 'MVP')
        cls.unif_MV = bgl.glGetUniformLocation(cls.shader.program, 'MV')
        cls.unif_offset = bgl.glGetUniformLocation(cls.shader.program,
                                                   'offset')

        cls.attr_pos = bgl.glGetAttribLocation(cls.shader.program, 'pos')
        cls.attr_primitive_id = bgl.glGetAttribLocation(
            cls.shader.program, 'primitive_id')

        cls.P = bgl.Buffer(bgl.GL_FLOAT, (4, 4))
        cls.MV = bgl.Buffer(bgl.GL_FLOAT, (4, 4))

        # returns of public API #
        cls.vert_index = bgl.Buffer(bgl.GL_INT, 1)

        cls.tri_co = bgl.Buffer(bgl.GL_FLOAT, (3, 3))
        cls.edge_co = bgl.Buffer(bgl.GL_FLOAT, (2, 3))
        cls.vert_co = bgl.Buffer(bgl.GL_FLOAT, 3)
    def __init__(self, dimensions):
        # Generate dummy float image buffer
        self.dimensions = dimensions
        width, height = dimensions

        pixels = [1, 0.2, 0.1, 1.0] * width * height
        pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, pixels)

        # Generate texture
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height,
                         0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels)
        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)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        # Bind shader that converts from scene linear to display space,
        # use the scene's color management settings.
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        # Generate geometry buffers for drawing textured quad
        position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)
    def _draw_texture(texture_id, x, y, width, height):
        # INITIALIZATION

        # Getting shader program
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        # Generate vertex array
        vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, vertex_array)

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        # Generate geometry buffers for drawing textured quad
        position = [x, y, x + width, y, x + width, y + height, x, y + height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)
        bgl.glGenBuffers(2, vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)

        # DRAWING
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture_id)

        bgl.glBindVertexArray(vertex_array[0])
        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vertex_buffer[0])
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, vertex_buffer[1])
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)

        bgl.glDrawArrays(bgl.GL_TRIANGLE_FAN, 0, 4)

        bgl.glBindVertexArray(0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        # DELETING
        bgl.glDeleteBuffers(2, vertex_buffer)
        bgl.glDeleteVertexArrays(1, vertex_array)
    def __init__(self, dimensions):
        # Generate dummy float image buffer
        self.dimensions = dimensions
        width, height = dimensions

        pixels = [0.1, 0.2, 0.1, 1.0] * width * height
        pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, pixels)

        # Generate texture
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels)
        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)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        # Bind shader that converts from scene linear to display space,
        # use the scene's color management settings.
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program);

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(shader_program[0], "texCoord");
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos");

        bgl.glEnableVertexAttribArray(texturecoord_location);
        bgl.glEnableVertexAttribArray(position_location);

        # Generate geometry buffers for drawing textured quad
        position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)
Exemple #5
0
    def _init_opengl(self, engine, scene):
        # Create texture
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)
        self.texture_id = self.texture[0]

        # Bind shader that converts from scene linear to display space,
        # use the scene's color management settings.
        engine.bind_display_space_shader(scene)
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        # Generate geometry buffers for drawing textured quad
        width = self._width * self._pixel_size
        height = self._height * self._pixel_size
        position = [
            self._offset_x, self._offset_y, self._offset_x + width,
            self._offset_y, self._offset_x + width, self._offset_y + height,
            self._offset_x, self._offset_y + height
        ]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, NULL)
        bgl.glBindVertexArray(NULL)
        engine.unbind_display_space_shader()
Exemple #6
0
    def __init__(self, dimensions):
        self.dimensions = dimensions
        width, height = dimensions

        pixels = [0.1, 0.2, 0.1, 1.0] * width * height
        pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, pixels)

        self.texture = bgl.Buffer(bgl.GL_INT, 1)

        bgl.glGenTextures(1, self.texture)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height,
                         0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER,
                            bgl.GL_LINEAR)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)
Exemple #7
0
    def init_opengl(self, engine, scene):
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)

        self.generate_texture()

        engine.bind_display_space_shader(scene)

        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        position = [
            0.0, 0.0, self.width, 0.0, self.width, self.height, 0.0,
            self.height
        ]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)

        engine.unbind_display_space_shader()
Exemple #8
0
Fichier : zu.py Projet : vktec/zu
    def prepare_viewport(self, ctx, dg):
        width, height = ctx.region.width, ctx.region.height
        self.dim = (width, height)

        buf = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_DRAW_FRAMEBUFFER_BINDING, buf)
        fb = buf[0]

        self.genfb(width, height)

        bgl.glGenVertexArrays(1, buf)
        self.vao = buf[0]
        bgl.glBindVertexArray(self.vao)

        quad = bgl.Buffer(bgl.GL_FLOAT, 2 * 4,
                          [0, 0, width, 0, width, height, 0, height])
        uv = bgl.Buffer(bgl.GL_FLOAT, 2 * 4, [0, 0, 1, 0, 1, 1, 0, 1])

        self.bind_display_space_shader(dg.scene)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, buf)
        self.unbind_display_space_shader()
        self.quad_in = bgl.glGetAttribLocation(buf[0], "pos")
        self.uv_in = bgl.glGetAttribLocation(buf[0], "texCoord")

        bgl.glEnableVertexAttribArray(self.quad_in)
        bgl.glEnableVertexAttribArray(self.uv_in)

        self.vtx_buf = bgl.Buffer(bgl.GL_INT, 2)
        bgl.glGenBuffers(2, self.vtx_buf)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vtx_buf[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 2 * 4 * 4, quad,
                         bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(self.quad_in, 2, bgl.GL_FLOAT, bgl.GL_FALSE,
                                  0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vtx_buf[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 2 * 4 * 4, uv,
                         bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(self.uv_in, 2, bgl.GL_FLOAT, bgl.GL_FALSE, 0,
                                  None)

        bgl.glDisableVertexAttribArray(self.quad_in)
        bgl.glDisableVertexAttribArray(self.uv_in)

        bgl.glBindFramebuffer(bgl.GL_FRAMEBUFFER, fb)
Exemple #9
0
    def init_opengl(cls):
        # OpenGL was already initialized, nothing to do here.
        if cls.shader is not None:
            return

        import atexit

        # Make sure we only registered the callback once.
        atexit.unregister(cls.end_opengl)
        atexit.register(cls.end_opengl)

        cls.shader = Shader(
            load_shader('3D_vert.glsl'),
            None,
            load_shader('primitive_id_frag.glsl'),
        )

        cls.unif_use_clip_planes = bgl.glGetUniformLocation(
            cls.shader.program, 'use_clip_planes')
        cls.unif_clip_plane = bgl.glGetUniformLocation(cls.shader.program,
                                                       'clip_plane')

        cls._NULL = gl_buffer_void_as_long(0)

        cls.unif_MVP = bgl.glGetUniformLocation(cls.shader.program, 'MVP')
        cls.unif_MV = bgl.glGetUniformLocation(cls.shader.program, 'MV')
        cls.unif_offset = bgl.glGetUniformLocation(cls.shader.program,
                                                   'offset')

        cls.attr_pos = bgl.glGetAttribLocation(cls.shader.program, 'pos')
        cls.attr_primitive_id = bgl.glGetAttribLocation(
            cls.shader.program, 'primitive_id')

        cls.P = bgl.Buffer(bgl.GL_FLOAT, (4, 4))
        cls.MV = bgl.Buffer(bgl.GL_FLOAT, (4, 4))
        cls.MVP = bgl.Buffer(bgl.GL_FLOAT, (4, 4))

        # returns of public API #
        cls.vert_index = bgl.Buffer(bgl.GL_INT, 1)

        cls.tri_co = bgl.Buffer(bgl.GL_FLOAT, (3, 3))
        cls.edge_co = bgl.Buffer(bgl.GL_FLOAT, (2, 3))
        cls.vert_co = bgl.Buffer(bgl.GL_FLOAT, 3)
Exemple #10
0
    def init_opengl(cls):
        # OpenGL was already initialized, nothing to do here.
        if cls.shader is not None:
            return

        import atexit

        # Make sure we only registered the callback once.
        atexit.unregister(cls.end_opengl)
        atexit.register(cls.end_opengl)

        cls.shader = Shader(
            load_shader('3D_vert.glsl'),
            None,
            load_shader('primitive_id_frag.glsl'),
        )

        cls.unif_use_clip_planes = bgl.glGetUniformLocation(cls.shader.program, 'use_clip_planes')
        cls.unif_clip_plane = bgl.glGetUniformLocation(cls.shader.program, 'clip_plane')

        cls._NULL = gl_buffer_void_as_long(0)

        cls.unif_MVP = bgl.glGetUniformLocation(cls.shader.program, 'MVP')
        cls.unif_MV = bgl.glGetUniformLocation(cls.shader.program, 'MV')
        cls.unif_offset = bgl.glGetUniformLocation(cls.shader.program, 'offset')

        cls.attr_pos = bgl.glGetAttribLocation(cls.shader.program, 'pos')
        cls.attr_primitive_id = bgl.glGetAttribLocation(cls.shader.program, 'primitive_id')

        cls.P = bgl.Buffer(bgl.GL_FLOAT, (4, 4))
        cls.MV = bgl.Buffer(bgl.GL_FLOAT, (4, 4))
        cls.MVP = bgl.Buffer(bgl.GL_FLOAT, (4, 4))

        # returns of public API #
        cls.vert_index = bgl.Buffer(bgl.GL_INT, 1)

        cls.tri_co = bgl.Buffer(bgl.GL_FLOAT, (3, 3))
        cls.edge_co = bgl.Buffer(bgl.GL_FLOAT, (2, 3))
        cls.vert_co = bgl.Buffer(bgl.GL_FLOAT, 3)
class GPU_Indices:

    #
    if bpy.app.version >= (2, 91, 0):
        shader = gpu.types.GPUShader(vert_3d, primitive_id_frag)
        unif_MVP = shader.uniform_from_name("MVP")
        unif_offset = shader.uniform_from_name("offset")
        attr_pos = shader.attr_from_name('pos')
        attr_primitive_id = shader.attr_from_name('primitive_id')
    else:
        from .utils_shader import Shader
        shader = Shader(gl_version + vert_3d, None, gl_version + primitive_id_frag)
        unif_MVP = bgl.glGetUniformLocation(shader.program, 'MVP')
        unif_offset = bgl.glGetUniformLocation(shader.program, 'offset')
        attr_pos = bgl.glGetAttribLocation(shader.program, 'pos')
        attr_primitive_id = bgl.glGetAttribLocation(shader.program, 'primitive_id')

    # returns of public API #

    # knot_co = bgl.Buffer(bgl.GL_FLOAT, 3)
    seg_co = bgl.Buffer(bgl.GL_FLOAT, (2, 3))
    origin_co = bgl.Buffer(bgl.GL_FLOAT, 3)
    bounds_co = bgl.Buffer(bgl.GL_FLOAT, 3)

    def __init__(self, obj, typ):

        self._NULL = VoidBufValue(0)

        self.MVP = bgl.Buffer(bgl.GL_FLOAT, (4, 4))

        # self.obj = obj
        self.first_index = 0

        self.vbo_segs = None
        self.vbo_segs_co = None

        self.vbo_origin = None
        self.vbo_origin_co = None

        self.vbo_bounds = None
        self.vbo_bounds_co = None

        self.num_segs = 0
        self.num_origins = 0
        self.num_bounds = 0

        self._draw_segs = False
        self._draw_origins = False
        self._draw_bounds = False
        self.is_mesh = False

        self.snap_mode = 0

        self.vao = bgl.Buffer(bgl.GL_INT, 1)

        bgl.glGenVertexArrays(1, self.vao)
        check_error("glGenVertexArrays(1, self.vao)")
        bgl.glBindVertexArray(self.vao[0])
        check_error("glBindVertexArray(self.vao[0])")

        _arrays = _Object_Arrays(obj, typ)

        if _arrays.segs_co:

            self.vbo_segs_co = bgl.Buffer(bgl.GL_INT, 1)
            self.num_segs = len(_arrays.segs_co)
            bgl.glGenBuffers(1, self.vbo_segs_co)
            check_error("glGenBuffers")
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_segs_co[0])
            check_error("glBindBuffer")
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_segs * 24, _arrays.segs_co, bgl.GL_STATIC_DRAW)
            check_error("glBufferData")

            segs_indices = np.repeat(np.arange(self.num_segs, dtype='f4'), 2)
            self.vbo_segs = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_segs)
            check_error("glGenBuffers")
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_segs[0])
            check_error("glBindBuffer")
            bgl.glBufferData(
                bgl.GL_ARRAY_BUFFER, self.num_segs * 8, np_array_as_bgl_Buffer(segs_indices), bgl.GL_STATIC_DRAW
            )
            check_error("glBufferData")
            del segs_indices
            self._draw_segs = True

        # objects origin
        if _arrays.origin_co:

            self.vbo_origin_co = bgl.Buffer(bgl.GL_INT, 1)
            self.num_origins = len(_arrays.origin_co)
            bgl.glGenBuffers(1, self.vbo_origin_co)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_origin_co[0])
            check_error("glBindBuffer")
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_origins * 12, _arrays.origin_co, bgl.GL_STATIC_DRAW)
            check_error("glBufferData")

            orig_indices = np.arange(self.num_origins, dtype='f4')
            self.vbo_origin = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_origin)
            check_error("glGenBuffers")

            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_origin[0])
            check_error("glBindBuffer")
            bgl.glBufferData(
                bgl.GL_ARRAY_BUFFER, self.num_origins * 4, np_array_as_bgl_Buffer(orig_indices), bgl.GL_STATIC_DRAW
            )
            check_error("glBufferData")
            del orig_indices
            self.is_mesh = _arrays.is_mesh
            self._draw_origins = True

        # objects bound box
        if _arrays.bounds_co:
            self.vbo_bounds_co = bgl.Buffer(bgl.GL_INT, 1)

            self.num_bounds = len(_arrays.bounds_co)
            bgl.glGenBuffers(1, self.vbo_bounds_co)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_bounds_co[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_bounds * 12, _arrays.bounds_co, bgl.GL_STATIC_DRAW)

            bounds_indices = np.arange(self.num_bounds, dtype='f4')
            self.vbo_bounds = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_bounds)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_bounds[0])
            bgl.glBufferData(
                bgl.GL_ARRAY_BUFFER, self.num_bounds * 4, np_array_as_bgl_Buffer(bounds_indices), bgl.GL_STATIC_DRAW
            )
            del bounds_indices
            self._draw_bounds = True

        del _arrays

        bgl.glBindVertexArray(0)
        check_error("glBindVertexArray(0)")

    def get_tot_elems(self):
        tot = 0
        if self.draw_segs:
            tot += self.num_segs
        if self.draw_origin:
            tot += self.num_origins
        if self.draw_bounds:
            tot += self.num_bounds
        return tot
    
    @property
    def draw_segs(self):
        return self._draw_segs and self.vbo_segs is not None

    @property
    def draw_bounds(self):
        return self._draw_bounds and self.vbo_bounds is not None

    @property
    def draw_origin(self):
        # origins and isolated vertices
        return (self._draw_origins or (self._draw_segs and self.is_mesh)) and self.vbo_origin is not None

    def set_snap_mode(self, snap_mode):
        self.snap_mode = snap_mode

    def set_draw_mode(self, draw_segs, draw_origin, draw_bounds):
        self._draw_segs = draw_segs
        self._draw_origins = draw_origin
        self._draw_bounds = draw_bounds

    @classmethod
    def set_ProjectionMatrix(cls, P):
        cls.P = P

    def set_ModelViewMatrix(self, MV):
        self.MVP[:] = self.P @ MV

    def bind(self, co, buf_id, offset):
        bgl.glUniform1f(self.unif_offset, float(offset))
        check_error("glUniform1f")
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, co[0])
        check_error("glBindBuffer")
        bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf)
        check_error("glVertexAttribPointer")
        bgl.glEnableVertexAttribArray(self.attr_pos)
        check_error("glEnableVertexAttribArray")
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, buf_id[0])
        check_error("glBindBuffer")
        bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf)
        check_error("glVertexAttribPointer")
        bgl.glEnableVertexAttribArray(self.attr_primitive_id)
        check_error("glEnableVertexAttribArray")

    def draw(self, index_offset):

        self.first_index = index_offset

        bgl.glUseProgram(self.shader.program)
        check_error("glUseProgram")

        bgl.glBindVertexArray(self.vao[0])
        check_error("glBindVertexArray(self.vao[0])")
        bgl.glUniformMatrix4fv(self.unif_MVP, 1, bgl.GL_TRUE, self.MVP)
        check_error("glUniformMatrix4fv")
        # 2.91 - require sequence of floats
        # self.shader.uniform_float("MVP", np.reshape(self.MVP, 16))

        if self.draw_segs:
            self.bind(self.vbo_segs_co, self.vbo_segs, index_offset)
            check_error("draw_segs bind")
            # 2.91
            # self.shader.uniform_float("offset", float(index_offset))
            bgl.glDrawArrays(bgl.GL_LINES, 0, self.num_segs * 2)
            check_error("draw_segs glDrawArrays")
            index_offset += self.num_segs

        if self.draw_origin:
            self.bind(self.vbo_origin_co, self.vbo_origin, index_offset)
            check_error("draw_origin bind")
            # 2.91
            # self.shader.uniform_float("offset", float(index_offset))
            bgl.glDrawArrays(bgl.GL_POINTS, 0, self.num_origins)
            check_error("draw_origin glDrawArrays")
            index_offset += self.num_origins

        if self.draw_bounds:
            self.bind(self.vbo_bounds_co, self.vbo_bounds, index_offset)
            check_error("draw_bounds bind")
            # 2.91
            # self.shader.uniform_float("offset", float(index_offset))
            bgl.glDrawArrays(bgl.GL_POINTS, 0, self.num_bounds)
            check_error("draw_bounds glDrawArrays")
            index_offset += self.num_bounds

        bgl.glBindVertexArray(0)
        check_error("glBindVertexArray(0)")

            
    def get_seg_co(self, index):
        bgl.glBindVertexArray(self.vao[0])
        check_error("glBindVertexArray(self.vao[0])")
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_segs_co[0])
        check_error("glBindBuffer")
        bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 24, 24, self.seg_co)
        check_error("glGetBufferSubData")
        bgl.glBindVertexArray(0)
        check_error("glBindVertexArray(0)")
        return self.seg_co

    def get_origin_co(self, index):
        bgl.glBindVertexArray(self.vao[0])
        check_error("glBindVertexArray(self.vao[0])")
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_origin_co[0])
        check_error("glBindBuffer")
        bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 12, 12, self.origin_co)
        check_error("glGetBufferSubData")
        bgl.glBindVertexArray(0)
        check_error("glBindVertexArray(0)")
        return self.origin_co

    def get_bounds_co(self, index):
        bgl.glBindVertexArray(self.vao[0])
        check_error("glBindVertexArray(self.vao[0])")
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_bounds_co[0])
        check_error("glBindBuffer")
        bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 12, 12, self.bounds_co)
        check_error("glGetBufferSubData")
        bgl.glBindVertexArray(0)
        check_error("glBindVertexArray(0)")
        return self.bounds_co

    def __del__(self):
        del self._NULL

        if self.vbo_segs_co:
            bgl.glDeleteBuffers(1, self.vbo_segs_co)
            bgl.glDeleteBuffers(1, self.vbo_segs)

        if self.vbo_origin_co:
            bgl.glDeleteBuffers(1, self.vbo_origin_co)
            bgl.glDeleteBuffers(1, self.vbo_origin)

        if self.vbo_bounds_co:
            bgl.glDeleteBuffers(1, self.vbo_bounds_co)
            bgl.glDeleteBuffers(1, self.vbo_bounds)

        bgl.glDeleteVertexArrays(1, self.vao)
Exemple #12
0
    def __init__(self, img):
        print('CustomDrawData.__init__()')

        self.img = img

        width, height = self.dimensions = self.img.size

        imgbytes = img.transpose(Image.FLIP_TOP_BOTTOM).tobytes()
        nimg = numpy.frombuffer(imgbytes, dtype=numpy.uint8)
        nimg = nimg.astype(numpy.float32) / 255.0

        pixels = bgl.Buffer(bgl.GL_FLOAT, width * height * 4, nimg)

        # Generate texture
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height,
                         0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels)
        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.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        # Bind shader that converts from scene linear to display space,
        # use the scene's color management settings.
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        # Generate geometry buffers for drawing textured quad
        position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)
Exemple #13
0
class GPU_Indices_Mesh():
    shader = Shader(
        load_shader('3D_vert.glsl'),
        None,
        load_shader('primitive_id_frag.glsl'),
    )

    unif_use_clip_planes = bgl.glGetUniformLocation(shader.program,
                                                    'use_clip_planes')
    unif_clip_plane = bgl.glGetUniformLocation(shader.program, 'clip_plane')

    unif_MVP = bgl.glGetUniformLocation(shader.program, 'MVP')
    unif_MV = bgl.glGetUniformLocation(shader.program, 'MV')
    unif_offset = bgl.glGetUniformLocation(shader.program, 'offset')

    attr_pos = bgl.glGetAttribLocation(shader.program, 'pos')
    attr_primitive_id = bgl.glGetAttribLocation(shader.program, 'primitive_id')

    P = bgl.Buffer(bgl.GL_FLOAT, (4, 4))
    MV = bgl.Buffer(bgl.GL_FLOAT, (4, 4))

    # returns of public API #
    vert_index = bgl.Buffer(bgl.GL_INT, 1)

    tri_co = bgl.Buffer(bgl.GL_FLOAT, (3, 3))
    edge_co = bgl.Buffer(bgl.GL_FLOAT, (2, 3))
    vert_co = bgl.Buffer(bgl.GL_FLOAT, 3)

    def __init__(self, obj, draw_tris, draw_edges, draw_verts):
        self._NULL = VoidBufValue(0)

        self.MVP = bgl.Buffer(bgl.GL_FLOAT, (4, 4))

        self.obj = obj
        self.draw_tris = draw_tris
        self.draw_edges = draw_edges
        self.draw_verts = draw_verts

        self.vbo = None
        self.vbo_tris = None
        self.vbo_edges = None
        self.vbo_verts = None

        ## Create VAO ##
        self.vao = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vao)
        bgl.glBindVertexArray(self.vao[0])

        ## Init Array ##
        mesh_arrays = _Mesh_Arrays(obj, draw_tris, draw_edges, draw_verts)

        ## Create VBO for vertices ##
        if mesh_arrays.verts_co is None:
            self.draw_tris = False
            self.draw_edges = False
            self.draw_verts = False
            return

        if False:  # Blender 2.8
            self.vbo_len = len(mesh_arrays.verts_co)

            self.vbo = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.vbo_len * 12,
                             np_array_as_bgl_Buffer(mesh_arrays.verts_co),
                             bgl.GL_STATIC_DRAW)

        ## Create VBO for Tris ##
        if mesh_arrays.tri_verts is not None:
            self.tri_verts = mesh_arrays.tri_verts
            self.num_tris = len(self.tri_verts)

            np_tris_co = mesh_arrays.verts_co[mesh_arrays.tri_verts]
            self.vbo_tris = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_tris)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 36,
                             np_array_as_bgl_Buffer(np_tris_co),
                             bgl.GL_STATIC_DRAW)
            del np_tris_co

            tri_indices = np.repeat(np.arange(self.num_tris, dtype='f4'), 3)
            self.vbo_tri_indices = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_tri_indices)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tri_indices[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 12,
                             np_array_as_bgl_Buffer(tri_indices),
                             bgl.GL_STATIC_DRAW)
            del tri_indices

        else:
            self.num_tris = 0
            self.draw_tris = False

        ## Create VBO for Edges ##
        if mesh_arrays.edge_verts is not None:
            self.edge_verts = mesh_arrays.edge_verts
            self.num_edges = len(self.edge_verts)

            np_edges_co = mesh_arrays.verts_co[mesh_arrays.edge_verts]
            self.vbo_edges = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_edges)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 24,
                             np_array_as_bgl_Buffer(np_edges_co),
                             bgl.GL_STATIC_DRAW)
            del np_edges_co

            edge_indices = np.repeat(np.arange(self.num_edges, dtype='f4'), 2)
            self.vbo_edge_indices = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_edge_indices)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edge_indices[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 8,
                             np_array_as_bgl_Buffer(edge_indices),
                             bgl.GL_STATIC_DRAW)
            del edge_indices
        else:
            self.num_edges = 0
            self.draw_edges = False

        ## Create EBO for Loose Verts ##
        if mesh_arrays.looseverts is not None:
            self.looseverts = mesh_arrays.looseverts
            self.num_verts = len(mesh_arrays.looseverts)

            np_lverts_co = mesh_arrays.verts_co[mesh_arrays.looseverts]
            self.vbo_verts = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_verts)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 12,
                             np_array_as_bgl_Buffer(np_lverts_co),
                             bgl.GL_STATIC_DRAW)
            del np_lverts_co

            looseverts_indices = np.arange(self.num_verts, dtype='f4')
            self.vbo_looseverts_indices = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_looseverts_indices)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER,
                             self.vbo_looseverts_indices[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 4,
                             np_array_as_bgl_Buffer(looseverts_indices),
                             bgl.GL_STATIC_DRAW)
            del looseverts_indices
        else:
            self.num_verts = 0
            self.draw_verts = False

        del mesh_arrays

        bgl.glBindVertexArray(0)

    def get_tot_elems(self):
        tot = 0

        if self.draw_tris:
            tot += self.num_tris

        if self.draw_edges:
            tot += self.num_edges

        if self.draw_verts:
            tot += self.num_verts

        return tot

    def set_draw_mode(self, draw_tris, draw_edges, draw_verts):
        self.draw_tris = draw_tris and self.vbo_tris
        self.draw_edges = draw_edges and self.vbo_edges
        self.draw_verts = draw_verts and self.vbo_verts

    def set_ModelViewMatrix(self, MV):
        self.MV[:] = MV[:]
        self.MVP[:] = Matrix(self.P) * MV

    def Draw(self, index_offset):
        self.first_index = index_offset
        bgl.glUseProgram(self.shader.program)
        bgl.glBindVertexArray(self.vao[0])

        bgl.glUniformMatrix4fv(self.unif_MV, 1, bgl.GL_TRUE, self.MV)
        bgl.glUniformMatrix4fv(self.unif_MVP, 1, bgl.GL_TRUE, self.MVP)

        if self.draw_tris:
            bgl.glUniform1f(self.unif_offset,
                            float(index_offset))  # bgl has no glUniform1ui :\

            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0])
            bgl.glEnableVertexAttribArray(self.attr_pos)
            bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT,
                                      bgl.GL_FALSE, 0, self._NULL.buf)

            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tri_indices[0])
            bgl.glEnableVertexAttribArray(self.attr_primitive_id)
            bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT,
                                      bgl.GL_FALSE, 0, self._NULL.buf)

            bgl.glDrawArrays(bgl.GL_TRIANGLES, 0, self.num_tris * 3)

            index_offset += self.num_tris
            bgl.glDepthRange(-0.00005, 0.99995)

        if self.draw_edges:
            bgl.glUniform1f(self.unif_offset,
                            float(index_offset))  #TODO: use glUniform1ui

            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0])
            bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT,
                                      bgl.GL_FALSE, 0, self._NULL.buf)
            bgl.glEnableVertexAttribArray(self.attr_pos)

            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edge_indices[0])
            bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT,
                                      bgl.GL_FALSE, 0, self._NULL.buf)
            bgl.glEnableVertexAttribArray(self.attr_primitive_id)

            bgl.glDrawArrays(bgl.GL_LINES, 0, self.num_edges * 2)

            index_offset += self.num_edges

        if self.draw_verts:
            bgl.glUniform1f(self.unif_offset,
                            float(index_offset))  #TODO: use glUniform1ui

            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0])
            bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT,
                                      bgl.GL_FALSE, 0, self._NULL.buf)
            bgl.glEnableVertexAttribArray(self.attr_pos)

            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER,
                             self.vbo_looseverts_indices[0])
            bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT,
                                      bgl.GL_FALSE, 0, self._NULL.buf)
            bgl.glEnableVertexAttribArray(self.attr_primitive_id)

            bgl.glDrawArrays(bgl.GL_POINTS, 0, self.num_verts)

        bgl.glDepthRange(0.0, 1.0)

    def get_tri_co(self, index):
        bgl.glBindVertexArray(self.vao[0])
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0])
        bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 36, 36,
                               self.tri_co)
        bgl.glBindVertexArray(0)
        return self.tri_co

    def get_edge_co(self, index):
        bgl.glBindVertexArray(self.vao[0])
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0])
        bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 24, 24,
                               self.edge_co)
        bgl.glBindVertexArray(0)
        return self.edge_co

    def get_loosevert_co(self, index):
        bgl.glBindVertexArray(self.vao[0])
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0])
        bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 12, 12,
                               self.vert_co)
        bgl.glBindVertexArray(0)
        return self.vert_co

    def get_tri_verts(self, index):
        return self.tri_verts[index]

    def get_edge_verts(self, index):
        return self.edge_verts[index]

    def get_loosevert_index(self, index):
        return self.looseverts[index]

    def __del__(self):
        del self._NULL

        if self.vbo_tris:
            bgl.glDeleteBuffers(1, self.vbo_tris)
            bgl.glDeleteBuffers(1, self.vbo_tri_indices)
            del self.tri_verts

        if self.vbo_edges:
            bgl.glDeleteBuffers(1, self.vbo_edges)
            bgl.glDeleteBuffers(1, self.vbo_edge_indices)
            del self.edge_verts

        if self.vbo_verts:
            bgl.glDeleteBuffers(1, self.vbo_verts)
            bgl.glDeleteBuffers(1, self.vbo_looseverts_indices)
            del self.looseverts

        bgl.glDeleteVertexArrays(1, self.vao)
Exemple #14
0
class GPU_Indices:

    shader = gpu.types.GPUShader(vert_3d, primitive_id_frag)
    unif_MVP = bgl.glGetUniformLocation(shader.program, 'MVP')
    unif_offset = bgl.glGetUniformLocation(shader.program, 'offset')
    attr_pos = bgl.glGetAttribLocation(shader.program, 'pos')
    attr_primitive_id = bgl.glGetAttribLocation(shader.program, 'primitive_id')
    # returns of public API #
    knot_co = bgl.Buffer(bgl.GL_FLOAT, 3)
    seg_co = bgl.Buffer(bgl.GL_FLOAT, (2, 3))
    origin_co = bgl.Buffer(bgl.GL_FLOAT, 3)

    def __init__(self, obj):

        self._NULL = VoidBufValue(0)

        self.MVP = bgl.Buffer(bgl.GL_FLOAT, (4, 4))

        # self.obj = obj
        self.first_index = 0

        self.vbo_segs = None
        self.vbo_segs_co = None

        self.vbo_origin = None
        self.vbo_origin_co = None

        self.num_segs = 0
        self.num_origins = 0
        self._draw_segs = False
        self._draw_origins = False
        self.is_mesh = False

        self.snap_mode = 0

        self.vao = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vao)
        bgl.glBindVertexArray(self.vao[0])

        _arrays = _Object_Arrays(obj)

        if _arrays.segs_co:

            self.vbo_segs_co = bgl.Buffer(bgl.GL_INT, 1)
            self.num_segs = len(_arrays.segs_co)
            bgl.glGenBuffers(1, self.vbo_segs_co)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_segs_co[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_segs * 24, _arrays.segs_co, bgl.GL_STATIC_DRAW)

            segs_indices = np.repeat(np.arange(self.num_segs, dtype='f4'), 2)
            self.vbo_segs = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_segs)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_segs[0])
            bgl.glBufferData(
                bgl.GL_ARRAY_BUFFER, self.num_segs * 8, np_array_as_bgl_Buffer(segs_indices), bgl.GL_STATIC_DRAW
            )
            del segs_indices
            self._draw_segs = True

        # objects origin
        if _arrays.origin_co:

            self.vbo_origin_co = bgl.Buffer(bgl.GL_INT, 1)

            self.num_origins = len(_arrays.origin_co)
            bgl.glGenBuffers(1, self.vbo_origin_co)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_origin_co[0])
            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_origins * 12, _arrays.origin_co, bgl.GL_STATIC_DRAW)

            orig_indices = np.arange(self.num_origins, dtype='f4')
            self.vbo_origin = bgl.Buffer(bgl.GL_INT, 1)
            bgl.glGenBuffers(1, self.vbo_origin)
            bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_origin[0])
            bgl.glBufferData(
                bgl.GL_ARRAY_BUFFER, self.num_origins * 4, np_array_as_bgl_Buffer(orig_indices), bgl.GL_STATIC_DRAW
            )
            del orig_indices
            self.is_mesh = _arrays.is_mesh
            self._draw_origins = True
        del _arrays

        bgl.glBindVertexArray(0)

    def get_tot_elems(self):
        tot = 0
        if self.draw_segs:
            tot += self.num_segs
        if self.draw_origin:
            tot += self.num_origins
        return tot
    
    @property
    def draw_segs(self):
        return self._draw_segs and self.vbo_segs_co is not None

    def set_snap_mode(self, snap_mode):
        self.snap_mode = snap_mode

    @property
    def draw_origin(self):
        return (self._draw_origins or (self._draw_segs and self.is_mesh)) and self.vbo_origin_co is not None

    def set_draw_mode(self, draw_segs, draw_origin):
        self._draw_segs = draw_segs
        self._draw_origins = draw_origin

    @classmethod
    def set_ProjectionMatrix(cls, P):
        cls.P = P

    def set_ModelViewMatrix(self, MV):
        self.MVP[:] = self.P @ MV

    def bind(self, co, buf_id, offset):
        bgl.glUniform1f(self.unif_offset, float(offset))
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, co[0])
        bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf)
        bgl.glEnableVertexAttribArray(self.attr_pos)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, buf_id[0])
        bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf)
        bgl.glEnableVertexAttribArray(self.attr_primitive_id)

    def draw(self, index_offset):
        self.first_index = index_offset
        bgl.glUseProgram(self.shader.program)
        bgl.glBindVertexArray(self.vao[0])
        bgl.glUniformMatrix4fv(self.unif_MVP, 1, bgl.GL_TRUE, self.MVP)
        # bgl.glUniform1f(self.unif_size, float(2.0))

        if self.draw_segs:
            self.bind(self.vbo_segs_co, self.vbo_segs, index_offset)
            bgl.glDrawArrays(bgl.GL_LINES, 0, self.num_segs * 2)
            index_offset += self.num_segs

        if self.draw_origin:
            self.bind(self.vbo_origin_co, self.vbo_origin, index_offset)
            bgl.glDrawArrays(bgl.GL_POINTS, 0, self.num_origins)
            index_offset += self.num_origins

        bgl.glBindVertexArray(0)

    def get_seg_co(self, index):
        bgl.glBindVertexArray(self.vao[0])
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_segs_co[0])
        bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 24, 24, self.seg_co)
        bgl.glBindVertexArray(0)
        return self.seg_co

    def get_origin_co(self, index):
        bgl.glBindVertexArray(self.vao[0])
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_origin_co[0])
        bgl.glGetBufferSubData(bgl.GL_ARRAY_BUFFER, index * 12, 12, self.origin_co)
        bgl.glBindVertexArray(0)
        return self.origin_co

    def __del__(self):
        del self._NULL

        if self.vbo_segs_co:
            bgl.glDeleteBuffers(1, self.vbo_segs_co)
            bgl.glDeleteBuffers(1, self.vbo_segs)

        if self.vbo_origin_co:
            bgl.glDeleteBuffers(1, self.vbo_origin_co)
            bgl.glDeleteBuffers(1, self.vbo_origin)

        bgl.glDeleteVertexArrays(1, self.vao)
Exemple #15
0
    def __init__(self, dimensions):
        global urhoImage
        # Generate dummy float image buffer
        self.dimensions = dimensions
        width, height = dimensions

        print("NEW CUSTOMDRAWDATA with resolution %s:%s" % (width, height))

        blank = Image.new('RGBA', (width, height), (100, 100, 100, 255))
        blank.alpha_composite(urhoImage,
                              dest=(int(width / 2 - urhoImage.width / 2),
                                    int(height / 2 - urhoImage.height / 2)))

        #self.pixels = [255,0,0,255] * width * height
        self.pixels = list(blank.tobytes())
        self.pixels = bgl.Buffer(bgl.GL_BYTE, width * height * 4, self.pixels)

        # Generate texture
        self.updateTextureOnDraw = False
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA, width, height, 0,
                         bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, self.pixels)
        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)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        # Bind shader that converts from scene linear to display space,
        # use the scene's color management settings.
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        # Generate geometry buffers for drawing textured quad
        position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)
Exemple #16
0
    def try_initialize(self):
        if self.initialized:
            return

        self.initialized = True

        resx, resy = self.resx, self.resy
        width, height = self.dimensions
        self.pixels = bgl.Buffer(bgl.GL_FLOAT, resx * resy * 3, self.pixels_np)

        # Generate texture
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGB16F, resx, resy, 0,
                         bgl.GL_RGB, bgl.GL_FLOAT, self.pixels)
        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.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S,
                            bgl.GL_CLAMP_TO_EDGE)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T,
                            bgl.GL_CLAMP_TO_EDGE)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        # Bind shader that converts from scene linear to display space,
        # use the scene's color management settings.
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        # Generate geometry buffers for drawing textured quad
        position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)
Exemple #17
0
    def __init__(self, viewport_dimensions, image_dimensions, pixels):
        self.log = logging.getLogger('blospray')

        self.log.info('CustomDrawData.__init__(viewport_dimensions=%s, image_dimensions=%s, fbpixels=%s) [%s]' % \
            (viewport_dimensions, image_dimensions, pixels.shape, self))

        viewport_width, viewport_height = self.viewport_dimensions = viewport_dimensions
        image_width, image_height = self.image_dimensions = image_dimensions

        assert pixels is not None
        pixels = bgl.Buffer(bgl.GL_FLOAT, image_width * image_height * 4,
                            pixels)

        # Generate texture
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)

        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, image_width,
                         image_height, 0, bgl.GL_RGBA, bgl.GL_FLOAT, pixels)
        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.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        # Bind shader that converts from scene linear to display space,
        # use the scene's color management settings.
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        # Generate geometry buffers for drawing textured quad
        position = [
            0.0, 0.0, viewport_width, 0.0, viewport_width, viewport_height,
            0.0, viewport_height
        ]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)
Exemple #18
0
    def __init__(self, dimensions, perspective, region3d):
        self.dimensions = dimensions
        self.perspective = perspective

        width = bpy.context.scene.tina_resolution_x
        height = bpy.context.scene.tina_resolution_y
        pixels = worker.render_main(width, height, region3d)

        # Generate dummy image buffer
        pixels = bgl.Buffer(bgl.GL_INT, width * height, pixels)

        # Generate texture
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGBA16F, width, height,
                         0, bgl.GL_RGBA, bgl.GL_UNSIGNED_INT_8_8_8_8_REV,
                         pixels)
        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)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        bgl.glUseProgram(self.program)

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            self.program, "texCoord")
        position_location = bgl.glGetAttribLocation(self.program, "pos")
        tex0_location = bgl.glGetAttribLocation(self.program, "tex0")

        bgl.glUniform1i(tex0_location, 0)

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        # Generate geometry buffers for drawing textured quad
        width, height = dimensions
        position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)
Exemple #19
0
    def __init__(self, scene, dimensions, perspective, blocksize):
        print('redraw!')
        # Generate dummy float image buffer
        self.dimensions = dimensions
        self.perspective = perspective
        width, height = dimensions

        resx, resy = scene.res
        if blocksize != 0:
            resx //= blocksize
            resy //= blocksize

        pixels = np.empty(resx * resy * 3, np.float32)
        scene._fast_export_image(pixels, blocksize)
        self.pixels = bgl.Buffer(bgl.GL_FLOAT, resx * resy * 3, pixels)

        # Generate texture
        self.texture = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenTextures(1, self.texture)
        bgl.glActiveTexture(bgl.GL_TEXTURE0)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.texture[0])
        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_RGB16F, resx, resy, 0,
                         bgl.GL_RGB, bgl.GL_FLOAT, self.pixels)
        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.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_S,
                            bgl.GL_CLAMP_TO_EDGE)
        bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_WRAP_T,
                            bgl.GL_CLAMP_TO_EDGE)
        bgl.glBindTexture(bgl.GL_TEXTURE_2D, 0)

        # Bind shader that converts from scene linear to display space,
        # use the scene's color management settings.
        shader_program = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGetIntegerv(bgl.GL_CURRENT_PROGRAM, shader_program)

        # Generate vertex array
        self.vertex_array = bgl.Buffer(bgl.GL_INT, 1)
        bgl.glGenVertexArrays(1, self.vertex_array)
        bgl.glBindVertexArray(self.vertex_array[0])

        texturecoord_location = bgl.glGetAttribLocation(
            shader_program[0], "texCoord")
        position_location = bgl.glGetAttribLocation(shader_program[0], "pos")

        bgl.glEnableVertexAttribArray(texturecoord_location)
        bgl.glEnableVertexAttribArray(position_location)

        # Generate geometry buffers for drawing textured quad
        position = [0.0, 0.0, width, 0.0, width, height, 0.0, height]
        position = bgl.Buffer(bgl.GL_FLOAT, len(position), position)
        texcoord = [0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
        texcoord = bgl.Buffer(bgl.GL_FLOAT, len(texcoord), texcoord)

        self.vertex_buffer = bgl.Buffer(bgl.GL_INT, 2)

        bgl.glGenBuffers(2, self.vertex_buffer)
        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[0])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, position, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(position_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vertex_buffer[1])
        bgl.glBufferData(bgl.GL_ARRAY_BUFFER, 32, texcoord, bgl.GL_STATIC_DRAW)
        bgl.glVertexAttribPointer(texturecoord_location, 2, bgl.GL_FLOAT,
                                  bgl.GL_FALSE, 0, None)

        bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, 0)
        bgl.glBindVertexArray(0)