|
| 1 | +package bezierpatch |
| 2 | + |
| 3 | +import org.openrndr.application |
| 4 | +import org.openrndr.color.ColorRGBa |
| 5 | +import org.openrndr.draw.loadImage |
| 6 | +import org.openrndr.draw.shadeStyle |
| 7 | +import org.openrndr.extra.shapes.bezierpatches.bezierPatch |
| 8 | +import org.openrndr.math.Polar |
| 9 | +import org.openrndr.shape.LineSegment |
| 10 | +import org.openrndr.shape.Segment2D |
| 11 | + |
| 12 | +/** |
| 13 | + * Shows how to |
| 14 | + * - create a [bezierPatch] out of 4 curved Segment2D instances |
| 15 | + * - apply an image texture to the patch using a shadeStyle |
| 16 | + * |
| 17 | + */ |
| 18 | +fun main() = application { |
| 19 | + configure { |
| 20 | + width = 800 |
| 21 | + height = 800 |
| 22 | + } |
| 23 | + program { |
| 24 | + fun offset(n: Int) = Polar(n * 107.0, 100.0).cartesian |
| 25 | + |
| 26 | + // Take the window bounds, shift it inwards, then take 4 horizontal |
| 27 | + // LineSegment instances from that Rectangle |
| 28 | + val lineSegments = List(4) { |
| 29 | + drawer.bounds.offsetEdges(-100.0).horizontal(1.0 - it * 0.333) |
| 30 | + } |
| 31 | + // Map the 4 LineSegment instances to curved Segment2D instances by |
| 32 | + // offsetting 4 points in each. |
| 33 | + val bentSegments = lineSegments.mapIndexed { i, seg -> |
| 34 | + Segment2D( |
| 35 | + seg.position(0.0) + offset(i * 4 + 1), |
| 36 | + seg.position(0.333) + offset(i * 4 + 2), |
| 37 | + seg.position(0.666) + offset(i * 4 + 3), |
| 38 | + seg.position(1.0) + offset(i * 4 + 4) |
| 39 | + ) |
| 40 | + } |
| 41 | + |
| 42 | + val bp = bezierPatch(bentSegments[0], bentSegments[1], bentSegments[2], bentSegments[3]) |
| 43 | + |
| 44 | + val tex = loadImage("demo-data/images/peopleCity01.jpg") |
| 45 | + val style = shadeStyle { |
| 46 | + fragmentTransform = "x_fill = texture(p_tex, va_texCoord0.xy);" |
| 47 | + parameter("tex", tex) |
| 48 | + } |
| 49 | + extend { |
| 50 | + drawer.shadeStyle = style |
| 51 | + drawer.clear(ColorRGBa.PINK) |
| 52 | + drawer.bezierPatch(bp) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments