Пример #1
0
def draw_thick_cubic_bezier(points, width, color):

    t = 0.0
    curve_points = []
    while t <= 1.0:
        x = points[0][0] * t * t * t + 3 * points[1][0] * t * t * (
            1 - t) + 3 * points[2][0] * t * (1 - t) * (
                1 - t) + points[3][0] * (1 - t) * (1 - t) * (1 - t)
        y = points[0][1] * t * t * t + 3 * points[1][1] * t * t * (
            1 - t) + 3 * points[2][1] * t * (1 - t) * (
                1 - t) + points[3][1] * (1 - t) * (1 - t) * (1 - t)
        curve_points.append((x, y))
        t += 0.01

    outer_points = []
    outer_tex_coords = []
    inner_points = []
    inner_tex_coords = []
    ortho_vector = vec2.norm(
        vec2.sub(curve_points[1], curve_points[0])
    )  #todo SOLVED: What happens when two points are in the same spot? Two points never will be in the same spot, thanks to the direction always being added onto the endpoint.
    ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[0]
    ortho_vector = vec2.mul(ortho_vector, width)
    inner_points.append(vec2.sub(curve_points[0], ortho_vector))
    outer_points.append(vec2.add(curve_points[0], ortho_vector))

    diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1]))
    projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector)
    inner_tex_coords.extend([0.0, 1.0 - projected_diff / width / 2, 0.0])

    diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1]))
    projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector)
    outer_tex_coords.extend([0.0, (1 - projected_diff) / width / 2, 0.0])

    r = 1.0
    for i in range(1, len(curve_points) - 1):
        ortho_vector = vec2.norm(
            vec2.sub(curve_points[i + 1], curve_points[i - 1]))
        ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[
            0]
        ortho_vector = vec2.mul(ortho_vector, width)
        inner_points.append(vec2.sub(curve_points[i], ortho_vector))
        outer_points.append(vec2.add(curve_points[i], ortho_vector))

        diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1]))
        projected_diff = -vec2.inner(ortho_vector,
                                     diff) / vec2.abs(ortho_vector)
        inner_tex_coords.extend([r, 1.0 - projected_diff / width / 2, 0.0])

        diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1]))
        projected_diff = -vec2.inner(ortho_vector,
                                     diff) / vec2.abs(ortho_vector)
        outer_tex_coords.extend([r, (1 - projected_diff) / width / 2, 0.0])

        r = -r + 1  #make r alternate between 0 and 1

    ortho_vector = vec2.norm(vec2.sub(curve_points[-1], curve_points[-2]))
    ortho_vector[0], ortho_vector[1] = -1 * ortho_vector[1], ortho_vector[0]
    ortho_vector = vec2.mul(ortho_vector, width)
    inner_points.append(vec2.sub(curve_points[-1], ortho_vector))
    outer_points.append(vec2.add(curve_points[-1], ortho_vector))

    diff = vec2.sub(inner_points[-1], vec2.vecint(inner_points[-1]))
    projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector)
    inner_tex_coords.extend([r, 1.0 - projected_diff / width / 2, 0.0])

    diff = vec2.sub(outer_points[-1], vec2.vecint(outer_points[-1]))
    projected_diff = -vec2.inner(ortho_vector, diff) / vec2.abs(ortho_vector)
    outer_tex_coords.extend([r, (1 - projected_diff) / width / 2, 0.0])

    all_points = []
    tex_coords = []
    tex_coords.extend(inner_tex_coords)
    tex_coords.extend(outer_tex_coords)
    for point in inner_points:
        x = int(point[0])
        y = int(point[1])
        #diff = vec2.abs((x-point[0], y-point[1]))
        #if bit == True:
        #    tex_coords.extend([0.0, 1.0-diff/a, 0.0])
        #else:
        #    tex_coords.extend([1.0, 1.0-diff/a, 0.0])
        #bit ^= True
        all_points.extend([x, y])
    for point in outer_points:
        x = int(point[0])
        y = int(point[1])
        #if bit == True:
        #    tex_coords.extend([0.0, (1-diff)/a, 0.0])
        #else:
        #    tex_coords.extend([1.0, (1-diff)/a, 0.0])
        #bit ^= True
        all_points.extend(point)
    all_points = [int(p) for p in all_points]
    all_points = tuple(all_points)

    indices = []
    l = len(inner_points)
    for i in range(l - 1):
        indices.append(i)
        indices.append(i + 1)
        indices.append(i + l)

        indices.append(i + 1)
        indices.append(i + 1 + l)
        indices.append(i + l)

    glEnable(gradtex.target)
    glBindTexture(gradtex.target, gradtex.id)
    pyglet.graphics.draw_indexed(
        len(all_points) / 2, pyglet.gl.GL_TRIANGLES, indices,
        ('v2i', all_points), ('t3f', tuple(tex_coords))
        #('c4f', color*(len(all_points)/2))
    )