REVIEWED: Spline cubic bezier example
This commit is contained in:
parent
4119b6dd66
commit
715633e0de
2 changed files with 39 additions and 28 deletions
|
@ -56,6 +56,10 @@ int main(void)
|
||||||
{ 710.0f, 260.0f },
|
{ 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 pointCount = 5;
|
||||||
int selectedPoint = -1;
|
int selectedPoint = -1;
|
||||||
int focusedPoint = -1;
|
int focusedPoint = -1;
|
||||||
|
@ -188,23 +192,32 @@ int main(void)
|
||||||
}
|
}
|
||||||
else if (splineTypeActive == SPLINE_BEZIER)
|
else if (splineTypeActive == SPLINE_BEZIER)
|
||||||
{
|
{
|
||||||
// Draw spline: cubic-bezier (with control points)
|
// 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
|
||||||
Vector2 fullPoints[3*(MAX_SPLINE_POINTS-1)+1] = {0};
|
for (int i = 0; i < (pointCount - 1); i++)
|
||||||
for (int i = 0; i < pointCount-1; i++) {
|
{
|
||||||
fullPoints[3*i] = points[i];
|
pointsInterleaved[3*i] = points[i];
|
||||||
fullPoints[3*i+1] = control[i].start;
|
pointsInterleaved[3*i + 1] = control[i].start;
|
||||||
fullPoints[3*i+2] = control[i].end;
|
pointsInterleaved[3*i + 2] = control[i].end;
|
||||||
}
|
}
|
||||||
fullPoints[3*(pointCount-1)] = points[pointCount-1],
|
|
||||||
DrawSplineBezierCubic(fullPoints, 3*(pointCount-1)+1, splineThickness, RED);
|
|
||||||
|
|
||||||
for (int i = 0; i < pointCount - 1; i++)
|
pointsInterleaved[3*(pointCount - 1)] = points[pointCount - 1];
|
||||||
|
|
||||||
|
// 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
|
// 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].start, 6, GOLD);
|
||||||
DrawCircleV(control[i].end, 6, GOLD);
|
DrawCircleV(control[i].end, 6, GOLD);
|
||||||
if (focusedControlPoint == &control[i].start) DrawCircleV(control[i].start, 8, GREEN);
|
if (focusedControlPoint == &control[i].start) DrawCircleV(control[i].start, 8, GREEN);
|
||||||
|
|
|
@ -1759,7 +1759,8 @@ void DrawSplineBasis(const Vector2 *points, int pointCount, float thick, Color c
|
||||||
DrawTriangleStrip(vertices, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
|
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
|
// 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);
|
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...]
|
// 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)
|
void DrawSplineBezierQuadratic(const Vector2 *points, int pointCount, float thick, Color color)
|
||||||
{
|
{
|
||||||
if (pointCount < 3) return;
|
if (pointCount >= 3)
|
||||||
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) DrawSplineSegmentBezierQuadratic(points[i], points[i + 1], points[i + 2], thick, color);
|
||||||
}
|
|
||||||
for (int i = 0; i < pointCount - 2; i += 2)
|
// 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);
|
||||||
DrawSplineSegmentBezierQuadratic(points[i], points[i + 1], points[i + 2], thick, color);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw spline: Cubic Bezier, minimum 4 points (2 control points): [p1, c2, c3, p4, c5, c6...]
|
// 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)
|
void DrawSplineBezierCubic(const Vector2 *points, int pointCount, float thick, Color color)
|
||||||
{
|
{
|
||||||
if (pointCount < 4) return;
|
if (pointCount >= 4)
|
||||||
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) DrawSplineSegmentBezierCubic(points[i], points[i + 1], points[i + 2], points[i + 3], thick, color);
|
||||||
}
|
|
||||||
for (int i = 0; i < pointCount - 3; i += 3)
|
// 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);
|
||||||
DrawSplineSegmentBezierCubic(points[i], points[i + 1], points[i + 2], points[i + 3], thick, color);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue