summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRay <[email protected]>2024-06-16 10:41:47 +0200
committerRay <[email protected]>2024-06-16 10:41:47 +0200
commit715633e0de101cb1a2b7b322455e4eab047f5f15 (patch)
tree8f1f25c80d54d64583bc77b08e4d013900ef7ab2
parent4119b6dd660544bff9f6431fa04f162eacb612a3 (diff)
downloadraylib-715633e0de101cb1a2b7b322455e4eab047f5f15.tar.gz
raylib-715633e0de101cb1a2b7b322455e4eab047f5f15.zip
REVIEWED: Spline cubic bezier example
-rw-r--r--examples/shapes/shapes_splines_drawing.c37
-rw-r--r--src/rshapes.c30
2 files changed, 39 insertions, 28 deletions
diff --git a/examples/shapes/shapes_splines_drawing.c b/examples/shapes/shapes_splines_drawing.c
index 0d5693eb..7c099e7e 100644
--- a/examples/shapes/shapes_splines_drawing.c
+++ b/examples/shapes/shapes_splines_drawing.c
@@ -56,6 +56,10 @@ int main(void)
{ 710.0f, 260.0f },
};
+ // Array required for spline bezier-cubic,
+ // including control points interleaved with start-end segment points
+ Vector2 pointsInterleaved[3*(MAX_SPLINE_POINTS - 1) + 1] = { 0 };
+
int pointCount = 5;
int selectedPoint = -1;
int focusedPoint = -1;
@@ -188,23 +192,32 @@ int main(void)
}
else if (splineTypeActive == SPLINE_BEZIER)
{
- // Draw spline: cubic-bezier (with control points)
-
- Vector2 fullPoints[3*(MAX_SPLINE_POINTS-1)+1] = {0};
- for (int i = 0; i < pointCount-1; i++) {
- fullPoints[3*i] = points[i];
- fullPoints[3*i+1] = control[i].start;
- fullPoints[3*i+2] = control[i].end;
+ // NOTE: Cubic-bezier spline requires the 2 control points of each segnment to be
+ // provided interleaved with the start and end point of every segment
+ for (int i = 0; i < (pointCount - 1); i++)
+ {
+ pointsInterleaved[3*i] = points[i];
+ pointsInterleaved[3*i + 1] = control[i].start;
+ pointsInterleaved[3*i + 2] = control[i].end;
}
- fullPoints[3*(pointCount-1)] = points[pointCount-1],
- DrawSplineBezierCubic(fullPoints, 3*(pointCount-1)+1, splineThickness, RED);
+
+ pointsInterleaved[3*(pointCount - 1)] = points[pointCount - 1];
- for (int i = 0; i < pointCount - 1; i++)
+ // Draw spline: cubic-bezier (with control points)
+ DrawSplineBezierCubic(pointsInterleaved, 3*(pointCount - 1) + 1, splineThickness, RED);
+
+ /*
+ for (int i = 0; i < 3*(pointCount - 1); i += 3)
{
// Drawing individual segments, not considering thickness connection compensation
- // DrawSplineSegmentBezierCubic(points[i], control[i].start, control[i].end, points[i + 1], splineThickness, RED);
+ DrawSplineSegmentBezierCubic(pointsInterleaved[i], pointsInterleaved[i + 1], pointsInterleaved[i + 2], pointsInterleaved[i + 3], splineThickness, MAROON);
+ }
+ */
- // Every cubic bezier point should have two control points
+ // Draw spline control points
+ for (int i = 0; i < pointCount - 1; i++)
+ {
+ // Every cubic bezier point have two control points
DrawCircleV(control[i].start, 6, GOLD);
DrawCircleV(control[i].end, 6, GOLD);
if (focusedControlPoint == &control[i].start) DrawCircleV(control[i].start, 8, GREEN);
diff --git a/src/rshapes.c b/src/rshapes.c
index 2c4a57d3..95c54770 100644
--- a/src/rshapes.c
+++ b/src/rshapes.c
@@ -1759,7 +1759,8 @@ void DrawSplineBasis(const Vector2 *points, int pointCount, float thick, Color c
DrawTriangleStrip(vertices, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
}
- DrawCircleV(currentPoint, thick/2.0f, color); // Draw end line circle-cap
+ // Cap circle drawing at the end of every segment
+ DrawCircleV(currentPoint, thick/2.0f, color);
}
// Draw spline: Catmull-Rom, minimum 4 points
@@ -1825,34 +1826,31 @@ void DrawSplineCatmullRom(const Vector2 *points, int pointCount, float thick, Co
DrawTriangleStrip(vertices, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
}
- DrawCircleV(currentPoint, thick/2.0f, color); // Draw end line circle-cap
+ // Cap circle drawing at the end of every segment
+ DrawCircleV(currentPoint, thick/2.0f, color);
}
// Draw spline: Quadratic Bezier, minimum 3 points (1 control point): [p1, c2, p3, c4...]
void DrawSplineBezierQuadratic(const Vector2 *points, int pointCount, float thick, Color color)
{
- if (pointCount < 3) return;
- for (int i = 2; i < pointCount - 2; i += 2)
- {
- DrawCircleV(points[i], thick/2.0f, color);
- }
- for (int i = 0; i < pointCount - 2; i += 2)
+ if (pointCount >= 3)
{
- DrawSplineSegmentBezierQuadratic(points[i], points[i + 1], points[i + 2], thick, color);
+ for (int i = 0; i < pointCount - 2; i += 2) DrawSplineSegmentBezierQuadratic(points[i], points[i + 1], points[i + 2], thick, color);
+
+ // Cap circle drawing at the end of every segment
+ //for (int i = 2; i < pointCount - 2; i += 2) DrawCircleV(points[i], thick/2.0f, color);
}
}
// Draw spline: Cubic Bezier, minimum 4 points (2 control points): [p1, c2, c3, p4, c5, c6...]
void DrawSplineBezierCubic(const Vector2 *points, int pointCount, float thick, Color color)
{
- if (pointCount < 4) return;
- for (int i = 3; i < pointCount - 3; i += 3)
- {
- DrawCircleV(points[i], thick/2.0f, color);
- }
- for (int i = 0; i < pointCount - 3; i += 3)
+ if (pointCount >= 4)
{
- DrawSplineSegmentBezierCubic(points[i], points[i + 1], points[i + 2], points[i + 3], thick, color);
+ for (int i = 0; i < pointCount - 3; i += 3) DrawSplineSegmentBezierCubic(points[i], points[i + 1], points[i + 2], points[i + 3], thick, color);
+
+ // Cap circle drawing at the end of every segment
+ //for (int i = 3; i < pointCount - 3; i += 3) DrawCircleV(points[i], thick/2.0f, color);
}
}