Ver código fonte

REVIEWED: Spline cubic bezier example

pull/4074/head
Ray 9 meses atrás
pai
commit
715633e0de
2 arquivos alterados com 39 adições e 28 exclusões
  1. +25
    -12
      examples/shapes/shapes_splines_drawing.c
  2. +14
    -16
      src/rshapes.c

+ 25
- 12
examples/shapes/shapes_splines_drawing.c Ver arquivo

@ -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)
n">Vector2 fullPoints[3*(MAX_SPLINE_POINTS-1)+1] = {0};
k">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;
// l">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
k">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)o">+1, splineThickness, RED);
pointsInterleaved[3*(pointCount - 1)p">] = 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);

+ 14
- 16
src/rshapes.c Ver arquivo

@ -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);
}
}

Carregando…
Cancelar
Salvar