Need To Combine Canvas Instructions, FBO Not Helping

9 views
Skip to first unread message

deives....@gmail.com

unread,
Feb 16, 2017, 12:21:44 PM2/16/17
to Kivy users support
Kivy 1.9.1 and Python 3.5.2

In my application, I visualize data by constructing a line between two squares on a grid, circling the contents of the squares, and adding arrow-tips to the lines to indicate a direction of flow. I construct these by using canvas instructions:

Color(*self.ls_colors[piece.color])
piece.get_targets()
for target in piece.targeting:
    squares = (piece.position,
               Board.tup_from_an(target))
    centers = []
    for idx in range(2):
        cent_x = squares[idx][0]*self.square_size+\
                 self.square_size/2
        cent_y = squares[idx][1]*self.square_size+\
                 self.square_size/2
        centers.append((cent_x,cent_y))
        if idx == 1:
            Line(circle=(cent_x,cent_y,circ_size),
                 close=True,
                 width=width)
    angle = atan2(squares[1][1]-squares[0][1],
                  squares[1][0]-squares[0][0])
    points = []
    for center in centers:
        x = center[0] + circ_size * cos(angle)
        y = center[1] + circ_size * sin(angle)
        points += [x,y]
        angle += pi

    arrow_size = self.square_size*0.2
    point = tuple(points[2:])
    angle += pi
    normal = angle+pi/2
    base = (point[0]+arrow_size*cos(angle),
            point[1]+arrow_size*sin(angle))
    tr_points = [
        *point,
        base[0]+arrow_size/2*cos(normal),
        base[1]+arrow_size/2*sin(normal),
        base[0]+arrow_size/2*cos(normal+pi),
        base[1]+arrow_size/2*sin(normal+pi)]
    Triangle(points=tr_points)

    Line(points=points,
         width=width)

It's important to the visualization that the final output have an alpha value less than one so that differently colored lines and circles can overlap and create a composite color. If I directly draw to the canvas with these instructions, the entire shape does not have a uniform alpha value because the primitives overlap in certain areas, and it is both ugly and distracting. My attempted solution to this has been to draw to an FBO at alpha 1 and then draw the contents of the FBO to the canvas with alpha 0.5. This almost works, but it means that overlapping lines totally eclipse each other rather than blending their colors.

My next thought was to draw each line-circle-arrow-thing to an FBO one-at-a-time with alpha 1, draw that onto a second FBO with alpha at 0.5, clear the first FBO, and repeat, then output the finished texture from FBO 2 into a Rectangle on the canvas. This has not worked, for reasons I don't understand. Whenever I try to get the two FBO's to interact, I get a blank texture out of the second. It seems like maybe changing the texture of the first FBO updates the contents of the second? My next thought, which I haven't tried yet, is to attempt to duplicate each texture out of FBO 1 so that this doesn't happen, but that doesn't feel like a good approach. I've been fighting with FBO's for hours in an effort to get this to work, and I'm feeling like there might be a better way to go about it.

Any guidance is appreciated.

Reply all
Reply to author
Forward
0 new messages