Beispiel #1
0
    def draw(self):
        # For debug purposes
        # gpu_Indices_enable_state()
        _offset_cur = 0
        proj_mat = self.rv3d.perspective_matrix.copy()

        multisample_enabled = bgl.glIsEnabled(bgl.GL_MULTISAMPLE)

        if multisample_enabled:
            bgl.glDisable(bgl.GL_MULTISAMPLE)

        dither_enabled = bgl.glIsEnabled(bgl.GL_DITHER)

        if dither_enabled:
            bgl.glDisable(bgl.GL_DITHER)

        # if self.proj_mat != proj_mat:
        GPU_Indices.set_ProjectionMatrix(proj_mat)
        # clear buffer
        # bgl.glClearColor(0.0, 0.0, 0.0, 0.0)
        # bgl.glClear(bgl.GL_COLOR_BUFFER_BIT | bgl.GL_DEPTH_BUFFER_BIT)

        for i, snap_obj in enumerate(self.snap_objects):

            obj = snap_obj.data[0]

            # create shader and data for detail analysis
            if len(snap_obj.data) == 1:
                # tim = time.time()
                snap_obj.data.append(GPU_Indices(obj, snap_obj.typ))
                # print("create data %.4f" % (time.time() - tim))

            snap_obj.data[1].set_draw_mode(
                (self._snap_mode & (KNOT | SEGS | SEGS_CENTER
                                    | SEGS_PERPENDICULAR | SEGS_PARALLEL)) > 0,
                (self._snap_mode & ORIGIN) > 0, (self._snap_mode & BOUNDS) > 0)

            snap_obj.data[1].set_ModelViewMatrix(snap_obj.mat)
            snap_obj.data[1].draw(_offset_cur)

            _offset_cur += snap_obj.data[1].get_tot_elems()

        if dither_enabled:
            bgl.glEnable(bgl.GL_DITHER
                         )  # dithering and AA break color coding, so disable #
        if multisample_enabled:
            bgl.glEnable(bgl.GL_MULTISAMPLE)
Beispiel #2
0
    def _gl_state_push(self):
        self._is_point_size_enabled = bgl.glIsEnabled(
            bgl.GL_PROGRAM_POINT_SIZE)
        if self._is_point_size_enabled:
            bgl.glDisable(bgl.GL_PROGRAM_POINT_SIZE)

        # draw 3d point OpenGL in the 3D View
        bgl.glEnable(bgl.GL_BLEND)
Beispiel #3
0
    def start(context):
        assert not ScissorStack.started

        rgn = context.region
        ScissorStack.context = context
        ScissorStack.box = (rgn.x, rgn.y, rgn.width, rgn.height)

        bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, ScissorStack.buf)
        ScissorStack.scissor_enabled = (bgl.glIsEnabled(bgl.GL_SCISSOR_TEST) == bgl.GL_TRUE)
        ScissorStack.stack = [tuple(ScissorStack.buf)]

        ScissorStack.started = True

        if not ScissorStack.scissor_enabled:
            bgl.glEnable(bgl.GL_SCISSOR_TEST)
Beispiel #4
0
    def start(context):
        assert not ScissorStack.started

        rgn = context.region
        ScissorStack.context = context
        ScissorStack.box = (rgn.x, rgn.y, rgn.width, rgn.height)

        bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, ScissorStack.buf)
        ScissorStack.scissor_enabled = (bgl.glIsEnabled(bgl.GL_SCISSOR_TEST) == bgl.GL_TRUE)
        ScissorStack.stack = [tuple(ScissorStack.buf)]

        ScissorStack.started = True

        if not ScissorStack.scissor_enabled:
            bgl.glEnable(bgl.GL_SCISSOR_TEST)
Beispiel #5
0
def draw_callback_common(self, context, ots):
    if Pref.use_alpha_clip:
        bgl.glAlphaFunc(bgl.GL_GREATER, 0.1)
        bgl.glEnable(bgl.GL_ALPHA_TEST)

    orig_is_enabled_depthtest = bgl.glIsEnabled(bgl.GL_DEPTH_TEST)
    bgl.glEnable(bgl.GL_DEPTH_TEST)

    bgl.glColor4f(1.0, 1.0, 1.0, 1.0)
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_TEXTURE_2D)

    texcos = [(0.,0.), (1.,0.), (1.,1.), (0.,1.)]


    fr = context.scene.frame_current
    for l,obj,tex in ots:
        ps = [obj.matrix_world*v.co for v in obj.data.vertices[0:4]]
        if tex.blendmode == BlendMode.ALPHAOVER:
            bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
            bgl.glBlendEquation(bgl.GL_FUNC_ADD)
        elif tex.blendmode == BlendMode.ADDITIVE:
            bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE)
            bgl.glBlendEquation(bgl.GL_FUNC_ADD)
        tex.bind()
        bgl.glBegin(bgl.GL_QUADS)
        for p,t in zip(ps, texcos):
            bgl.glTexCoord2f(*t)
            bgl.glVertex3f(*p)
        bgl.glEnd()

    #bgl.glDepthMask(bgl.GL_TRUE)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
    bgl.glBlendEquation(bgl.GL_FUNC_ADD)
    bgl.glDisable(bgl.GL_TEXTURE_2D)
    bgl.glDisable(bgl.GL_BLEND)
    if Pref.use_alpha_clip:
        bgl.glDisable(bgl.GL_ALPHA_TEST)
    if not orig_is_enabled_depthtest:
        bgl.glDisable(bgl.GL_DEPTH_TEST)
    def render(self, draw_data):
        io = self.io
        shader = self._bl_shader

        display_width, display_height = io.display_size
        fb_width = int(display_width * io.display_fb_scale[0])
        fb_height = int(display_height * io.display_fb_scale[1])

        if fb_width == 0 or fb_height == 0:
            return

        draw_data.scale_clip_rects(*io.display_fb_scale)

        # backup GL state
        (
            last_program,
            last_texture,
            last_active_texture,
            last_array_buffer,
            last_element_array_buffer,
            last_vertex_array,
            last_blend_src,
            last_blend_dst,
            last_blend_equation_rgb,
            last_blend_equation_alpha,
            last_viewport,
            last_scissor_box,
        ) = self._backup_integers(
            gl.GL_CURRENT_PROGRAM,
            1,
            gl.GL_TEXTURE_BINDING_2D,
            1,
            gl.GL_ACTIVE_TEXTURE,
            1,
            gl.GL_ARRAY_BUFFER_BINDING,
            1,
            gl.GL_ELEMENT_ARRAY_BUFFER_BINDING,
            1,
            gl.GL_VERTEX_ARRAY_BINDING,
            1,
            gl.GL_BLEND_SRC,
            1,
            gl.GL_BLEND_DST,
            1,
            gl.GL_BLEND_EQUATION_RGB,
            1,
            gl.GL_BLEND_EQUATION_ALPHA,
            1,
            gl.GL_VIEWPORT,
            4,
            gl.GL_SCISSOR_BOX,
            4,
        )

        last_enable_blend = gl.glIsEnabled(gl.GL_BLEND)
        last_enable_cull_face = gl.glIsEnabled(gl.GL_CULL_FACE)
        last_enable_depth_test = gl.glIsEnabled(gl.GL_DEPTH_TEST)
        last_enable_scissor_test = gl.glIsEnabled(gl.GL_SCISSOR_TEST)

        gl.glEnable(gl.GL_BLEND)
        gl.glBlendEquation(gl.GL_FUNC_ADD)
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)
        gl.glDisable(gl.GL_CULL_FACE)
        gl.glDisable(gl.GL_DEPTH_TEST)
        gl.glEnable(gl.GL_SCISSOR_TEST)
        gl.glActiveTexture(gl.GL_TEXTURE0)

        gl.glViewport(0, 0, int(fb_width), int(fb_height))

        ortho_projection = (2.0 / display_width, 0.0, 0.0, 0.0, 0.0,
                            2.0 / -display_height, 0.0, 0.0, 0.0, 0.0, -1.0,
                            0.0, -1.0, 1.0, 0.0, 1.0)
        shader.bind()
        shader.uniform_float("ProjMtx", ortho_projection)
        shader.uniform_int("Texture", 0)

        for commands in draw_data.commands_lists:
            size = commands.idx_buffer_size * imgui.INDEX_SIZE // 4
            address = commands.idx_buffer_data
            ptr = C.cast(address, C.POINTER(C.c_int))
            idx_buffer_np = np.ctypeslib.as_array(ptr, shape=(size, ))

            size = commands.vtx_buffer_size * imgui.VERTEX_SIZE // 4
            address = commands.vtx_buffer_data
            ptr = C.cast(address, C.POINTER(C.c_float))
            vtx_buffer_np = np.ctypeslib.as_array(ptr, shape=(size, ))
            vtx_buffer_shaped = vtx_buffer_np.reshape(-1,
                                                      imgui.VERTEX_SIZE // 4)

            idx_buffer_offset = 0
            for command in commands.commands:
                x, y, z, w = command.clip_rect
                gl.glScissor(int(x), int(fb_height - w), int(z - x),
                             int(w - y))

                vertices = vtx_buffer_shaped[:, :2]
                uvs = vtx_buffer_shaped[:, 2:4]
                colors = vtx_buffer_shaped.view(np.uint8)[:, 4 * 4:]
                colors = colors.astype('f') / 255.0

                indices = idx_buffer_np[idx_buffer_offset:idx_buffer_offset +
                                        command.elem_count]

                gl.glBindTexture(gl.GL_TEXTURE_2D, command.texture_id)

                batch = batch_for_shader(shader,
                                         'TRIS', {
                                             "Position": vertices,
                                             "UV": uvs,
                                             "Color": colors,
                                         },
                                         indices=indices)
                batch.draw(shader)

                idx_buffer_offset += command.elem_count

        # restore modified GL state
        gl.glUseProgram(last_program)
        gl.glActiveTexture(last_active_texture)
        gl.glBindTexture(gl.GL_TEXTURE_2D, last_texture)
        gl.glBindVertexArray(last_vertex_array)
        gl.glBindBuffer(gl.GL_ARRAY_BUFFER, last_array_buffer)
        gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, last_element_array_buffer)
        gl.glBlendEquationSeparate(last_blend_equation_rgb,
                                   last_blend_equation_alpha)
        gl.glBlendFunc(last_blend_src, last_blend_dst)

        if last_enable_blend:
            gl.glEnable(gl.GL_BLEND)
        else:
            gl.glDisable(gl.GL_BLEND)

        if last_enable_cull_face:
            gl.glEnable(gl.GL_CULL_FACE)
        else:
            gl.glDisable(gl.GL_CULL_FACE)

        if last_enable_depth_test:
            gl.glEnable(gl.GL_DEPTH_TEST)
        else:
            gl.glDisable(gl.GL_DEPTH_TEST)

        if last_enable_scissor_test:
            gl.glEnable(gl.GL_SCISSOR_TEST)
        else:
            gl.glDisable(gl.GL_SCISSOR_TEST)

        gl.glViewport(last_viewport[0], last_viewport[1], last_viewport[2],
                      last_viewport[3])
        gl.glScissor(last_scissor_box[0], last_scissor_box[1],
                     last_scissor_box[2], last_scissor_box[3])
 def __get__(self, instance, owner):
     return glIsEnabled(state_id)
    def snap_get(self, mval, main_snap_obj=None):
        ret = None, None, None
        self.mval[:] = mval
        snap_vert = self._snap_mode & VERT != 0
        snap_edge = self._snap_mode & EDGE != 0
        snap_face = self._snap_mode & FACE != 0

        _Internal.gpu_Indices_enable_state()
        self._offscreen.bind()

        #bgl.glDisable(bgl.GL_DITHER) # dithering and AA break color coding, so disable #
        #multisample_enabled = bgl.glIsEnabled(bgl.GL_MULTISAMPLE)
        #bgl.glDisable(bgl.GL_MULTISAMPLE)
        bgl.glEnable(bgl.GL_DEPTH_TEST)

        is_point_size_enabled = bgl.glIsEnabled(bgl.GL_PROGRAM_POINT_SIZE)
        if is_point_size_enabled:
            bgl.glDisable(bgl.GL_PROGRAM_POINT_SIZE)

        bgl.glPointSize(4.0)

        proj_mat = self.rv3d.perspective_matrix.copy()
        if self.proj_mat != proj_mat:
            self.proj_mat = proj_mat
            _Internal.gpu_Indices_set_ProjectionMatrix(self.proj_mat)
            self.update_drawing()

        ray_dir, ray_orig = self.get_ray(mval)
        for i, snap_obj in enumerate(self.snap_objects[self.drawn_count:],
                                     self.drawn_count):
            obj = snap_obj.data[0]
            try:
                bbmin = Vector(obj.bound_box[0])
                bbmax = Vector(obj.bound_box[6])
            except ReferenceError:
                self.snap_objects.remove(snap_obj)
                continue

            if bbmin != bbmax:
                MVP = proj_mat @ snap_obj.mat
                mat_inv = snap_obj.mat.inverted_safe()
                ray_orig_local = mat_inv @ ray_orig
                ray_dir_local = mat_inv.to_3x3() @ ray_dir
                in_threshold = _Internal.intersect_boundbox_threshold(
                    self, MVP, ray_orig_local, ray_dir_local, bbmin, bbmax)
            else:
                proj_co = _Internal.project_co_v3(self,
                                                  snap_obj.mat.translation)
                dist = self.mval - proj_co
                in_threshold = abs(dist.x) < self._dist_px and abs(
                    dist.y) < self._dist_px
                #snap_obj.data[1] = primitive_point

            if in_threshold:
                if len(snap_obj.data) == 1:
                    from .mesh_drawing import GPU_Indices_Mesh
                    is_bound = obj.display_type == 'BOUNDS'
                    draw_face = snap_face and not is_bound and obj.display_type != 'WIRE'
                    draw_edge = snap_edge and not is_bound
                    draw_vert = snap_vert and not is_bound
                    snap_obj.data.append(
                        GPU_Indices_Mesh(self.depsgraph, obj, draw_face,
                                         draw_edge, draw_vert))

                snap_obj.data[1].set_draw_mode(snap_face, snap_edge, snap_vert)
                snap_obj.data[1].set_ModelViewMatrix(snap_obj.mat)

                if snap_obj == main_snap_obj:
                    snap_obj.data[1].Draw(self._offset_cur, -0.0001)
                else:
                    snap_obj.data[1].Draw(self._offset_cur)
                self._offset_cur += snap_obj.data[1].get_tot_elems()

                tmp = self.snap_objects[self.drawn_count]
                self.snap_objects[self.drawn_count] = self.snap_objects[i]
                self.snap_objects[i] = tmp

                self.drawn_count += 1

        self._read_buffer(mval)
        #import numpy as np
        #a = np.array(self._snap_buffer)
        #print(a)

        snap_obj, index = self._get_nearest_index()
        #print("index:", index)
        if snap_obj:
            ret = self._get_loc(snap_obj, index)

        if is_point_size_enabled:
            bgl.glEnable(bgl.GL_PROGRAM_POINT_SIZE)

        bgl.glDisable(bgl.GL_DEPTH_TEST)
        _Internal.gpu_Indices_restore_state()
        self._offscreen.unbind()

        return (snap_obj, *ret)
Beispiel #9
0
    def snap(self, mval):
        t = time.time()

        ret = None
        self.mval[:] = mval

        with self._offscreen.bind():

            multisample_enabled = bgl.glIsEnabled(bgl.GL_MULTISAMPLE)
            check_error("glIsEnabled")

            if multisample_enabled:
                bgl.glDisable(bgl.GL_MULTISAMPLE)
                check_error("glDisable")

            dither_enabled = bgl.glIsEnabled(bgl.GL_DITHER)
            check_error("glIsEnabled")

            if dither_enabled:
                bgl.glDisable(bgl.GL_DITHER)
                check_error("glDisable")

            proj_mat = self.rv3d.perspective_matrix.copy()

            if self.proj_mat != proj_mat:
                self.proj_mat = proj_mat
                GPU_Indices.set_ProjectionMatrix(self.proj_mat)
                self.update_all()

            ray_dir, ray_orig = self.get_ray(mval)
            for i, snap_obj in enumerate(self.snap_objects[self.drawn_count:],
                                         self.drawn_count):

                obj = snap_obj.data[0]

                # origins
                if snap_obj.typ == TYP_ORIGIN:  #obj.__class__.__name__ == 'list':
                    # filter by visibility
                    in_threshold = (self._snap_mode & ORIGIN)

                elif snap_obj.typ == TYP_BOUNDS:
                    in_threshold = (self._snap_mode & BOUNDS)

                else:
                    # allow to hide some objects from snap, eg active object when moving
                    if obj is None or obj.name in self._exclude or not obj.visible_get(
                    ):
                        # print("exclude %s" % obj.name)
                        continue

                    bbmin = Vector(obj.bound_box[0])
                    bbmax = Vector(obj.bound_box[6])

                    # check objects under ray using bound box
                    if bbmin != bbmax:
                        MVP = proj_mat @ snap_obj.mat
                        mat_inv = snap_obj.mat.inverted()
                        ray_orig_local = mat_inv @ ray_orig
                        ray_dir_local = ray_dir @ snap_obj.mat
                        in_threshold = intersect_boundbox_threshold(
                            self, MVP, ray_orig_local, ray_dir_local, bbmin,
                            bbmax)

                    else:
                        dist = self._max_pixel_dist(snap_obj.mat.translation)
                        in_threshold = dist < self._dist_px

                # print("ray_orig %s  ray_dir %s  %s in_threshold %s" % (ray_orig, ray_dir, obj.name, in_threshold))

                if in_threshold:

                    # create shader and data for detail analysis
                    if len(snap_obj.data) == 1:
                        # tim = time.time()

                        snap_obj.data.append(GPU_Indices(obj, snap_obj.typ))
                        # print("create data %.4f" % (time.time() - tim))

                    snap_obj.data[1].set_draw_mode(
                        (self._snap_mode &
                         (KNOT | SEGS | SEGS_CENTER | SEGS_PERPENDICULAR
                          | SEGS_PARALLEL)) > 0,
                        (self._snap_mode & ORIGIN) > 0,
                        (self._snap_mode & BOUNDS) > 0)
                    snap_obj.data[1].set_ModelViewMatrix(snap_obj.mat)
                    snap_obj.data[1].draw(self._offset_cur)

                    self._offset_cur += snap_obj.data[1].get_tot_elems()

                    self.snap_objects[self.drawn_count], self.snap_objects[i] = \
                        self.snap_objects[i], self.snap_objects[self.drawn_count]
                    self.drawn_count += 1

            bgl.glGetIntegerv(bgl.GL_READ_BUFFER, self.buffer_type)
            check_error("glGetIntegerv(bgl.GL_READ_BUFFER)")

            bgl.glReadBuffer(bgl.GL_COLOR_ATTACHMENT0)
            check_error("glReadBuffer(bgl.GL_COLOR_ATTACHMENT0)")

            bgl.glReadPixels(
                int(self.mval[0]) - self._dist_px,
                int(self.mval[1]) - self._dist_px, self.threshold,
                self.threshold, bgl.GL_RED_INTEGER, bgl.GL_UNSIGNED_INT,
                self._snap_buffer)

            check_error("glReadPixels snap_buffer")

            snap_obj, index = self._get_nearest_index()

            if snap_obj:
                ret = self._get_loc(snap_obj, index)
                # print(ret, index)

            # self.as_image(0, 0, self._offscreen.width, self._offscreen.height, "full_screen")
            if DEBUG_SNAP_BUFFER:
                self.as_image(
                    int(self.mval[0]) - self._dist_px,
                    int(self.mval[1]) - self._dist_px, self.threshold,
                    self.threshold, "snap_buffer")

            # print(ret)
            if dither_enabled:
                bgl.glEnable(
                    bgl.GL_DITHER
                )  # dithering and AA break color coding, so disable #
                check_error("glEnable")

            if multisample_enabled:
                bgl.glEnable(bgl.GL_MULTISAMPLE)
                check_error("glEnable")

        # print("curve snap %s %.4f" % (len(self.snap_objects), time.time() - t))

        return snap_obj, ret