Update C sources
This commit is contained in:
parent
07d1374c41
commit
d0612c27b2
17 changed files with 2216 additions and 1053 deletions
852
raylib/rshapes.c
852
raylib/rshapes.c
|
@ -65,10 +65,10 @@
|
|||
// Error rate to calculate how many segments we need to draw a smooth circle,
|
||||
// taken from https://stackoverflow.com/a/2244088
|
||||
#ifndef SMOOTH_CIRCLE_ERROR_RATE
|
||||
#define SMOOTH_CIRCLE_ERROR_RATE 0.5f // Circle error rate
|
||||
#define SMOOTH_CIRCLE_ERROR_RATE 0.5f // Circle error rate
|
||||
#endif
|
||||
#ifndef SPLINE_LINE_DIVISIONS
|
||||
#define SPLINE_LINE_DIVISIONS 24 // Spline lines segment divisions
|
||||
#ifndef SPLINE_SEGMENT_DIVISIONS
|
||||
#define SPLINE_SEGMENT_DIVISIONS 24 // Spline segment divisions
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -161,7 +161,7 @@ void DrawPixelV(Vector2 position, Color color)
|
|||
#endif
|
||||
}
|
||||
|
||||
// Draw a line
|
||||
// Draw a line (using gl lines)
|
||||
void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color color)
|
||||
{
|
||||
rlBegin(RL_LINES);
|
||||
|
@ -171,7 +171,7 @@ void DrawLine(int startPosX, int startPosY, int endPosX, int endPosY, Color colo
|
|||
rlEnd();
|
||||
}
|
||||
|
||||
// Draw a line (Vector version)
|
||||
// Draw a line (using gl lines)
|
||||
void DrawLineV(Vector2 startPos, Vector2 endPos, Color color)
|
||||
{
|
||||
rlBegin(RL_LINES);
|
||||
|
@ -181,6 +181,61 @@ void DrawLineV(Vector2 startPos, Vector2 endPos, Color color)
|
|||
rlEnd();
|
||||
}
|
||||
|
||||
// Draw lines sequuence (using gl lines)
|
||||
void DrawLineStrip(Vector2 *points, int pointCount, Color color)
|
||||
{
|
||||
if (pointCount >= 2)
|
||||
{
|
||||
rlBegin(RL_LINES);
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
||||
for (int i = 0; i < pointCount - 1; i++)
|
||||
{
|
||||
rlVertex2f(points[i].x, points[i].y);
|
||||
rlVertex2f(points[i + 1].x, points[i + 1].y);
|
||||
}
|
||||
rlEnd();
|
||||
}
|
||||
}
|
||||
|
||||
// Draw line using cubic-bezier spline, in-out interpolation, no control points
|
||||
void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color)
|
||||
{
|
||||
Vector2 previous = startPos;
|
||||
Vector2 current = { 0 };
|
||||
|
||||
Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 1; i <= SPLINE_SEGMENT_DIVISIONS; i++)
|
||||
{
|
||||
// Cubic easing in-out
|
||||
// NOTE: Easing is calculated only for y position value
|
||||
current.y = EaseCubicInOut((float)i, startPos.y, endPos.y - startPos.y, (float)SPLINE_SEGMENT_DIVISIONS);
|
||||
current.x = previous.x + (endPos.x - startPos.x)/(float)SPLINE_SEGMENT_DIVISIONS;
|
||||
|
||||
float dy = current.y - previous.y;
|
||||
float dx = current.x - previous.x;
|
||||
float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
points[0].x = previous.x + dy*size;
|
||||
points[0].y = previous.y - dx*size;
|
||||
points[1].x = previous.x - dy*size;
|
||||
points[1].y = previous.y + dx*size;
|
||||
}
|
||||
|
||||
points[2*i + 1].x = current.x - dy*size;
|
||||
points[2*i + 1].y = current.y + dx*size;
|
||||
points[2*i].x = current.x + dy*size;
|
||||
points[2*i].y = current.y - dx*size;
|
||||
|
||||
previous = current;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
// Draw a line defining thickness
|
||||
void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
|
||||
{
|
||||
|
@ -203,295 +258,6 @@ void DrawLineEx(Vector2 startPos, Vector2 endPos, float thick, Color color)
|
|||
}
|
||||
}
|
||||
|
||||
// Draw line using cubic-bezier curves in-out
|
||||
void DrawLineBezier(Vector2 startPos, Vector2 endPos, float thick, Color color)
|
||||
{
|
||||
Vector2 previous = startPos;
|
||||
Vector2 current = { 0 };
|
||||
|
||||
Vector2 points[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 1; i <= SPLINE_LINE_DIVISIONS; i++)
|
||||
{
|
||||
// Cubic easing in-out
|
||||
// NOTE: Easing is calculated only for y position value
|
||||
current.y = EaseCubicInOut((float)i, startPos.y, endPos.y - startPos.y, (float)SPLINE_LINE_DIVISIONS);
|
||||
current.x = previous.x + (endPos.x - startPos.x)/(float)SPLINE_LINE_DIVISIONS;
|
||||
|
||||
float dy = current.y - previous.y;
|
||||
float dx = current.x - previous.x;
|
||||
float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
points[0].x = previous.x + dy*size;
|
||||
points[0].y = previous.y - dx*size;
|
||||
points[1].x = previous.x - dy*size;
|
||||
points[1].y = previous.y + dx*size;
|
||||
}
|
||||
|
||||
points[2*i + 1].x = current.x - dy*size;
|
||||
points[2*i + 1].y = current.y + dx*size;
|
||||
points[2*i].x = current.x + dy*size;
|
||||
points[2*i].y = current.y - dx*size;
|
||||
|
||||
previous = current;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(points, 2*SPLINE_LINE_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
// Draw line using quadratic bezier curves with a control point
|
||||
void DrawLineBezierQuad(Vector2 startPos, Vector2 endPos, Vector2 controlPos, float thick, Color color)
|
||||
{
|
||||
const float step = 1.0f/SPLINE_LINE_DIVISIONS;
|
||||
|
||||
Vector2 previous = startPos;
|
||||
Vector2 current = { 0 };
|
||||
float t = 0.0f;
|
||||
|
||||
Vector2 points[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 1; i <= SPLINE_LINE_DIVISIONS; i++)
|
||||
{
|
||||
t = step*i;
|
||||
|
||||
float a = powf(1.0f - t, 2);
|
||||
float b = 2.0f*(1.0f - t)*t;
|
||||
float c = powf(t, 2);
|
||||
|
||||
// NOTE: The easing functions aren't suitable here because they don't take a control point
|
||||
current.y = a*startPos.y + b*controlPos.y + c*endPos.y;
|
||||
current.x = a*startPos.x + b*controlPos.x + c*endPos.x;
|
||||
|
||||
float dy = current.y - previous.y;
|
||||
float dx = current.x - previous.x;
|
||||
float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
points[0].x = previous.x + dy*size;
|
||||
points[0].y = previous.y - dx*size;
|
||||
points[1].x = previous.x - dy*size;
|
||||
points[1].y = previous.y + dx*size;
|
||||
}
|
||||
|
||||
points[2*i + 1].x = current.x - dy*size;
|
||||
points[2*i + 1].y = current.y + dx*size;
|
||||
points[2*i].x = current.x + dy*size;
|
||||
points[2*i].y = current.y - dx*size;
|
||||
|
||||
previous = current;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(points, 2*SPLINE_LINE_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
// Draw line using cubic bezier curves with 2 control points
|
||||
void DrawLineBezierCubic(Vector2 startPos, Vector2 endPos, Vector2 startControlPos, Vector2 endControlPos, float thick, Color color)
|
||||
{
|
||||
const float step = 1.0f/SPLINE_LINE_DIVISIONS;
|
||||
|
||||
Vector2 previous = startPos;
|
||||
Vector2 current = { 0 };
|
||||
float t = 0.0f;
|
||||
|
||||
Vector2 points[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 1; i <= SPLINE_LINE_DIVISIONS; i++)
|
||||
{
|
||||
t = step*i;
|
||||
|
||||
float a = powf(1.0f - t, 3);
|
||||
float b = 3.0f*powf(1.0f - t, 2)*t;
|
||||
float c = 3.0f*(1.0f - t)*powf(t, 2);
|
||||
float d = powf(t, 3);
|
||||
|
||||
current.y = a*startPos.y + b*startControlPos.y + c*endControlPos.y + d*endPos.y;
|
||||
current.x = a*startPos.x + b*startControlPos.x + c*endControlPos.x + d*endPos.x;
|
||||
|
||||
float dy = current.y - previous.y;
|
||||
float dx = current.x - previous.x;
|
||||
float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
points[0].x = previous.x + dy*size;
|
||||
points[0].y = previous.y - dx*size;
|
||||
points[1].x = previous.x - dy*size;
|
||||
points[1].y = previous.y + dx*size;
|
||||
}
|
||||
|
||||
points[2*i + 1].x = current.x - dy*size;
|
||||
points[2*i + 1].y = current.y + dx*size;
|
||||
points[2*i].x = current.x + dy*size;
|
||||
points[2*i].y = current.y - dx*size;
|
||||
|
||||
previous = current;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(points, 2*SPLINE_LINE_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
// Draw a B-Spline line, minimum 4 points
|
||||
void DrawLineBSpline(Vector2 *points, int pointCount, float thick, Color color)
|
||||
{
|
||||
if (pointCount < 4) return;
|
||||
|
||||
float a[4] = { 0 };
|
||||
float b[4] = { 0 };
|
||||
float dy = 0.0f;
|
||||
float dx = 0.0f;
|
||||
float size = 0.0f;
|
||||
|
||||
Vector2 currentPoint = { 0 };
|
||||
Vector2 nextPoint = { 0 };
|
||||
Vector2 vertices[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 0; i < (pointCount - 3); i++)
|
||||
{
|
||||
float t = 0.0f;
|
||||
Vector2 p1 = points[i], p2 = points[i + 1], p3 = points[i + 2], p4 = points[i + 3];
|
||||
|
||||
a[0] = (-p1.x + 3.0f*p2.x - 3.0f*p3.x + p4.x)/6.0f;
|
||||
a[1] = (3.0f*p1.x - 6.0f*p2.x + 3.0f*p3.x)/6.0f;
|
||||
a[2] = (-3.0f*p1.x + 3.0f*p3.x)/6.0f;
|
||||
a[3] = (p1.x + 4.0f*p2.x + p3.x)/6.0f;
|
||||
|
||||
b[0] = (-p1.y + 3.0f*p2.y - 3.0f*p3.y + p4.y)/6.0f;
|
||||
b[1] = (3.0f*p1.y - 6.0f*p2.y + 3.0f*p3.y)/6.0f;
|
||||
b[2] = (-3.0f*p1.y + 3.0f*p3.y)/6.0f;
|
||||
b[3] = (p1.y + 4.0f*p2.y + p3.y)/6.0f;
|
||||
|
||||
currentPoint.x = a[3];
|
||||
currentPoint.y = b[3];
|
||||
|
||||
if (i == 0) DrawCircleV(currentPoint, thick/2.0f, color); // Draw init line circle-cap
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
vertices[0].x = currentPoint.x + dy*size;
|
||||
vertices[0].y = currentPoint.y - dx*size;
|
||||
vertices[1].x = currentPoint.x - dy*size;
|
||||
vertices[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
for (int j = 1; j <= SPLINE_LINE_DIVISIONS; j++)
|
||||
{
|
||||
t = ((float)j)/((float)SPLINE_LINE_DIVISIONS);
|
||||
|
||||
nextPoint.x = a[3] + t*(a[2] + t*(a[1] + t*a[0]));
|
||||
nextPoint.y = b[3] + t*(b[2] + t*(b[1] + t*b[0]));
|
||||
|
||||
dy = nextPoint.y - currentPoint.y;
|
||||
dx = nextPoint.x - currentPoint.x;
|
||||
size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
|
||||
|
||||
if ((i == 0) && (j == 1))
|
||||
{
|
||||
vertices[0].x = currentPoint.x + dy*size;
|
||||
vertices[0].y = currentPoint.y - dx*size;
|
||||
vertices[1].x = currentPoint.x - dy*size;
|
||||
vertices[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
vertices[2*j + 1].x = nextPoint.x - dy*size;
|
||||
vertices[2*j + 1].y = nextPoint.y + dx*size;
|
||||
vertices[2*j].x = nextPoint.x + dy*size;
|
||||
vertices[2*j].y = nextPoint.y - dx*size;
|
||||
|
||||
currentPoint = nextPoint;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(vertices, 2*SPLINE_LINE_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
DrawCircleV(currentPoint, thick/2.0f, color); // Draw end line circle-cap
|
||||
}
|
||||
|
||||
// Draw a Catmull Rom spline line, minimum 4 points
|
||||
void DrawLineCatmullRom(Vector2 *points, int pointCount, float thick, Color color)
|
||||
{
|
||||
if (pointCount < 4) return;
|
||||
|
||||
float dy = 0.0f;
|
||||
float dx = 0.0f;
|
||||
float size = 0.0f;
|
||||
|
||||
Vector2 currentPoint = points[1];
|
||||
Vector2 nextPoint = { 0 };
|
||||
Vector2 vertices[2*SPLINE_LINE_DIVISIONS + 2] = { 0 };
|
||||
|
||||
DrawCircleV(currentPoint, thick/2.0f, color); // Draw init line circle-cap
|
||||
|
||||
for (int i = 0; i < (pointCount - 3); i++)
|
||||
{
|
||||
float t = 0.0f;
|
||||
Vector2 p1 = points[i], p2 = points[i + 1], p3 = points[i + 2], p4 = points[i + 3];
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
vertices[0].x = currentPoint.x + dy*size;
|
||||
vertices[0].y = currentPoint.y - dx*size;
|
||||
vertices[1].x = currentPoint.x - dy*size;
|
||||
vertices[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
for (int j = 1; j <= SPLINE_LINE_DIVISIONS; j++)
|
||||
{
|
||||
t = ((float)j)/((float)SPLINE_LINE_DIVISIONS);
|
||||
|
||||
float q0 = (-1.0f*t*t*t) + (2.0f*t*t) + (-1.0f*t);
|
||||
float q1 = (3.0f*t*t*t) + (-5.0f*t*t) + 2.0f;
|
||||
float q2 = (-3.0f*t*t*t) + (4.0f*t*t) + t;
|
||||
float q3 = t*t*t - t*t;
|
||||
|
||||
nextPoint.x = 0.5f*((p1.x*q0) + (p2.x*q1) + (p3.x*q2) + (p4.x*q3));
|
||||
nextPoint.y = 0.5f*((p1.y*q0) + (p2.y*q1) + (p3.y*q2) + (p4.y*q3));
|
||||
|
||||
dy = nextPoint.y - currentPoint.y;
|
||||
dx = nextPoint.x - currentPoint.x;
|
||||
size = (0.5f*thick)/sqrtf(dx*dx + dy*dy);
|
||||
|
||||
if ((i == 0) && (j == 1))
|
||||
{
|
||||
vertices[0].x = currentPoint.x + dy*size;
|
||||
vertices[0].y = currentPoint.y - dx*size;
|
||||
vertices[1].x = currentPoint.x - dy*size;
|
||||
vertices[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
vertices[2*j + 1].x = nextPoint.x - dy*size;
|
||||
vertices[2*j + 1].y = nextPoint.y + dx*size;
|
||||
vertices[2*j].x = nextPoint.x + dy*size;
|
||||
vertices[2*j].y = nextPoint.y - dx*size;
|
||||
|
||||
currentPoint = nextPoint;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(vertices, 2*SPLINE_LINE_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
DrawCircleV(currentPoint, thick/2.0f, color); // Draw end line circle-cap
|
||||
}
|
||||
|
||||
// Draw lines sequence
|
||||
void DrawLineStrip(Vector2 *points, int pointCount, Color color)
|
||||
{
|
||||
if (pointCount >= 2)
|
||||
{
|
||||
rlBegin(RL_LINES);
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
||||
for (int i = 0; i < pointCount - 1; i++)
|
||||
{
|
||||
rlVertex2f(points[i].x, points[i].y);
|
||||
rlVertex2f(points[i + 1].x, points[i + 1].y);
|
||||
}
|
||||
rlEnd();
|
||||
}
|
||||
}
|
||||
|
||||
// Draw a color-filled circle
|
||||
void DrawCircle(int centerX, int centerY, float radius, Color color)
|
||||
{
|
||||
|
@ -1773,6 +1539,504 @@ void DrawPolyLinesEx(Vector2 center, int sides, float radius, float rotation, fl
|
|||
#endif
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition - Splines functions
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw spline: linear, minimum 2 points
|
||||
void DrawSplineLinear(Vector2 *points, int pointCount, float thick, Color color)
|
||||
{
|
||||
Vector2 delta = { 0 };
|
||||
float length = 0.0f;
|
||||
float scale = 0.0f;
|
||||
|
||||
for (int i = 0; i < pointCount - 1; i++)
|
||||
{
|
||||
delta = (Vector2){ points[i + 1].x - points[i].x, points[i + 1].y - points[i].y };
|
||||
length = sqrtf(delta.x*delta.x + delta.y*delta.y);
|
||||
|
||||
if (length > 0) scale = thick/(2*length);
|
||||
|
||||
Vector2 radius = { -scale*delta.y, scale*delta.x };
|
||||
Vector2 strip[4] = {
|
||||
{ points[i].x - radius.x, points[i].y - radius.y },
|
||||
{ points[i].x + radius.x, points[i].y + radius.y },
|
||||
{ points[i + 1].x - radius.x, points[i + 1].y - radius.y },
|
||||
{ points[i + 1].x + radius.x, points[i + 1].y + radius.y }
|
||||
};
|
||||
|
||||
DrawTriangleStrip(strip, 4, color);
|
||||
}
|
||||
#if defined(SUPPORT_SPLINE_SEGMENT_CAPS)
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
// Draw spline: B-Spline, minimum 4 points
|
||||
void DrawSplineBasis(Vector2 *points, int pointCount, float thick, Color color)
|
||||
{
|
||||
if (pointCount < 4) return;
|
||||
|
||||
float a[4] = { 0 };
|
||||
float b[4] = { 0 };
|
||||
float dy = 0.0f;
|
||||
float dx = 0.0f;
|
||||
float size = 0.0f;
|
||||
|
||||
Vector2 currentPoint = { 0 };
|
||||
Vector2 nextPoint = { 0 };
|
||||
Vector2 vertices[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 0; i < (pointCount - 3); i++)
|
||||
{
|
||||
float t = 0.0f;
|
||||
Vector2 p1 = points[i], p2 = points[i + 1], p3 = points[i + 2], p4 = points[i + 3];
|
||||
|
||||
a[0] = (-p1.x + 3.0f*p2.x - 3.0f*p3.x + p4.x)/6.0f;
|
||||
a[1] = (3.0f*p1.x - 6.0f*p2.x + 3.0f*p3.x)/6.0f;
|
||||
a[2] = (-3.0f*p1.x + 3.0f*p3.x)/6.0f;
|
||||
a[3] = (p1.x + 4.0f*p2.x + p3.x)/6.0f;
|
||||
|
||||
b[0] = (-p1.y + 3.0f*p2.y - 3.0f*p3.y + p4.y)/6.0f;
|
||||
b[1] = (3.0f*p1.y - 6.0f*p2.y + 3.0f*p3.y)/6.0f;
|
||||
b[2] = (-3.0f*p1.y + 3.0f*p3.y)/6.0f;
|
||||
b[3] = (p1.y + 4.0f*p2.y + p3.y)/6.0f;
|
||||
|
||||
currentPoint.x = a[3];
|
||||
currentPoint.y = b[3];
|
||||
|
||||
if (i == 0) DrawCircleV(currentPoint, thick/2.0f, color); // Draw init line circle-cap
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
vertices[0].x = currentPoint.x + dy*size;
|
||||
vertices[0].y = currentPoint.y - dx*size;
|
||||
vertices[1].x = currentPoint.x - dy*size;
|
||||
vertices[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
for (int j = 1; j <= SPLINE_SEGMENT_DIVISIONS; j++)
|
||||
{
|
||||
t = ((float)j)/((float)SPLINE_SEGMENT_DIVISIONS);
|
||||
|
||||
nextPoint.x = a[3] + t*(a[2] + t*(a[1] + t*a[0]));
|
||||
nextPoint.y = b[3] + t*(b[2] + t*(b[1] + t*b[0]));
|
||||
|
||||
dy = nextPoint.y - currentPoint.y;
|
||||
dx = nextPoint.x - currentPoint.x;
|
||||
size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
|
||||
|
||||
if ((i == 0) && (j == 1))
|
||||
{
|
||||
vertices[0].x = currentPoint.x + dy*size;
|
||||
vertices[0].y = currentPoint.y - dx*size;
|
||||
vertices[1].x = currentPoint.x - dy*size;
|
||||
vertices[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
vertices[2*j + 1].x = nextPoint.x - dy*size;
|
||||
vertices[2*j + 1].y = nextPoint.y + dx*size;
|
||||
vertices[2*j].x = nextPoint.x + dy*size;
|
||||
vertices[2*j].y = nextPoint.y - dx*size;
|
||||
|
||||
currentPoint = nextPoint;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(vertices, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
DrawCircleV(currentPoint, thick/2.0f, color); // Draw end line circle-cap
|
||||
}
|
||||
|
||||
// Draw spline: Catmull-Rom, minimum 4 points
|
||||
void DrawSplineCatmullRom(Vector2 *points, int pointCount, float thick, Color color)
|
||||
{
|
||||
if (pointCount < 4) return;
|
||||
|
||||
float dy = 0.0f;
|
||||
float dx = 0.0f;
|
||||
float size = 0.0f;
|
||||
|
||||
Vector2 currentPoint = points[1];
|
||||
Vector2 nextPoint = { 0 };
|
||||
Vector2 vertices[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
|
||||
|
||||
DrawCircleV(currentPoint, thick/2.0f, color); // Draw init line circle-cap
|
||||
|
||||
for (int i = 0; i < (pointCount - 3); i++)
|
||||
{
|
||||
float t = 0.0f;
|
||||
Vector2 p1 = points[i], p2 = points[i + 1], p3 = points[i + 2], p4 = points[i + 3];
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
vertices[0].x = currentPoint.x + dy*size;
|
||||
vertices[0].y = currentPoint.y - dx*size;
|
||||
vertices[1].x = currentPoint.x - dy*size;
|
||||
vertices[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
for (int j = 1; j <= SPLINE_SEGMENT_DIVISIONS; j++)
|
||||
{
|
||||
t = ((float)j)/((float)SPLINE_SEGMENT_DIVISIONS);
|
||||
|
||||
float q0 = (-1.0f*t*t*t) + (2.0f*t*t) + (-1.0f*t);
|
||||
float q1 = (3.0f*t*t*t) + (-5.0f*t*t) + 2.0f;
|
||||
float q2 = (-3.0f*t*t*t) + (4.0f*t*t) + t;
|
||||
float q3 = t*t*t - t*t;
|
||||
|
||||
nextPoint.x = 0.5f*((p1.x*q0) + (p2.x*q1) + (p3.x*q2) + (p4.x*q3));
|
||||
nextPoint.y = 0.5f*((p1.y*q0) + (p2.y*q1) + (p3.y*q2) + (p4.y*q3));
|
||||
|
||||
dy = nextPoint.y - currentPoint.y;
|
||||
dx = nextPoint.x - currentPoint.x;
|
||||
size = (0.5f*thick)/sqrtf(dx*dx + dy*dy);
|
||||
|
||||
if ((i == 0) && (j == 1))
|
||||
{
|
||||
vertices[0].x = currentPoint.x + dy*size;
|
||||
vertices[0].y = currentPoint.y - dx*size;
|
||||
vertices[1].x = currentPoint.x - dy*size;
|
||||
vertices[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
vertices[2*j + 1].x = nextPoint.x - dy*size;
|
||||
vertices[2*j + 1].y = nextPoint.y + dx*size;
|
||||
vertices[2*j].x = nextPoint.x + dy*size;
|
||||
vertices[2*j].y = nextPoint.y - dx*size;
|
||||
|
||||
currentPoint = nextPoint;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(vertices, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
DrawCircleV(currentPoint, thick/2.0f, color); // Draw end line circle-cap
|
||||
}
|
||||
|
||||
// Draw spline: Quadratic Bezier, minimum 3 points (1 control point): [p1, c2, p3, c4...]
|
||||
void DrawSplineBezierQuadratic(Vector2 *points, int pointCount, float thick, Color color)
|
||||
{
|
||||
if (pointCount < 3) return;
|
||||
|
||||
for (int i = 0; i < pointCount - 2; i++)
|
||||
{
|
||||
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...]
|
||||
void DrawSplineBezierCubic(Vector2 *points, int pointCount, float thick, Color color)
|
||||
{
|
||||
if (pointCount < 4) return;
|
||||
|
||||
for (int i = 0; i < pointCount - 3; i++)
|
||||
{
|
||||
DrawSplineSegmentBezierCubic(points[i], points[i + 1], points[i + 2], points[i + 3], thick, color);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw spline segment: Linear, 2 points
|
||||
void DrawSplineSegmentLinear(Vector2 p1, Vector2 p2, float thick, Color color)
|
||||
{
|
||||
// NOTE: For the linear spline we don't use subdivisions, just a single quad
|
||||
|
||||
Vector2 delta = { p2.x - p1.x, p2.y - p1.y };
|
||||
float length = sqrtf(delta.x*delta.x + delta.y*delta.y);
|
||||
|
||||
if ((length > 0) && (thick > 0))
|
||||
{
|
||||
float scale = thick/(2*length);
|
||||
|
||||
Vector2 radius = { -scale*delta.y, scale*delta.x };
|
||||
Vector2 strip[4] = {
|
||||
{ p1.x - radius.x, p1.y - radius.y },
|
||||
{ p1.x + radius.x, p1.y + radius.y },
|
||||
{ p2.x - radius.x, p2.y - radius.y },
|
||||
{ p2.x + radius.x, p2.y + radius.y }
|
||||
};
|
||||
|
||||
DrawTriangleStrip(strip, 4, color);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw spline segment: B-Spline, 4 points
|
||||
void DrawSplineSegmentBasis(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float thick, Color color)
|
||||
{
|
||||
const float step = 1.0f/SPLINE_SEGMENT_DIVISIONS;
|
||||
|
||||
Vector2 currentPoint = { 0 };
|
||||
Vector2 nextPoint = { 0 };
|
||||
float t = 0.0f;
|
||||
|
||||
Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
|
||||
|
||||
float a[4] = { 0 };
|
||||
float b[4] = { 0 };
|
||||
|
||||
a[0] = (-p1.x + 3*p2.x - 3*p3.x + p4.x)/6.0f;
|
||||
a[1] = (3*p1.x - 6*p2.x + 3*p3.x)/6.0f;
|
||||
a[2] = (-3*p1.x + 3*p3.x)/6.0f;
|
||||
a[3] = (p1.x + 4*p2.x + p3.x)/6.0f;
|
||||
|
||||
b[0] = (-p1.y + 3*p2.y - 3*p3.y + p4.y)/6.0f;
|
||||
b[1] = (3*p1.y - 6*p2.y + 3*p3.y)/6.0f;
|
||||
b[2] = (-3*p1.y + 3*p3.y)/6.0f;
|
||||
b[3] = (p1.y + 4*p2.y + p3.y)/6.0f;
|
||||
|
||||
currentPoint.x = a[3];
|
||||
currentPoint.y = b[3];
|
||||
|
||||
for (int i = 0; i <= SPLINE_SEGMENT_DIVISIONS; i++)
|
||||
{
|
||||
t = step*(float)i;
|
||||
|
||||
nextPoint.x = a[3] + t*(a[2] + t*(a[1] + t*a[0]));
|
||||
nextPoint.y = b[3] + t*(b[2] + t*(b[1] + t*b[0]));
|
||||
|
||||
float dy = nextPoint.y - currentPoint.y;
|
||||
float dx = nextPoint.x - currentPoint.x;
|
||||
float size = (0.5f*thick)/sqrtf(dx*dx + dy*dy);
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
points[0].x = currentPoint.x + dy*size;
|
||||
points[0].y = currentPoint.y - dx*size;
|
||||
points[1].x = currentPoint.x - dy*size;
|
||||
points[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
points[2*i + 1].x = nextPoint.x - dy*size;
|
||||
points[2*i + 1].y = nextPoint.y + dx*size;
|
||||
points[2*i].x = nextPoint.x + dy*size;
|
||||
points[2*i].y = nextPoint.y - dx*size;
|
||||
|
||||
currentPoint = nextPoint;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS+2, color);
|
||||
}
|
||||
|
||||
// Draw spline segment: Catmull-Rom, 4 points
|
||||
void DrawSplineSegmentCatmullRom(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float thick, Color color)
|
||||
{
|
||||
const float step = 1.0f/SPLINE_SEGMENT_DIVISIONS;
|
||||
|
||||
Vector2 currentPoint = p1;
|
||||
Vector2 nextPoint = { 0 };
|
||||
float t = 0.0f;
|
||||
|
||||
Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 0; i <= SPLINE_SEGMENT_DIVISIONS; i++)
|
||||
{
|
||||
t = step*(float)i;
|
||||
|
||||
float q0 = (-1*t*t*t) + (2*t*t) + (-1*t);
|
||||
float q1 = (3*t*t*t) + (-5*t*t) + 2;
|
||||
float q2 = (-3*t*t*t) + (4*t*t) + t;
|
||||
float q3 = t*t*t - t*t;
|
||||
|
||||
nextPoint.x = 0.5f*((p1.x*q0) + (p2.x*q1) + (p3.x*q2) + (p4.x*q3));
|
||||
nextPoint.y = 0.5f*((p1.y*q0) + (p2.y*q1) + (p3.y*q2) + (p4.y*q3));
|
||||
|
||||
float dy = nextPoint.y - currentPoint.y;
|
||||
float dx = nextPoint.x - currentPoint.x;
|
||||
float size = (0.5f*thick)/sqrtf(dx*dx + dy*dy);
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
points[0].x = currentPoint.x + dy*size;
|
||||
points[0].y = currentPoint.y - dx*size;
|
||||
points[1].x = currentPoint.x - dy*size;
|
||||
points[1].y = currentPoint.y + dx*size;
|
||||
}
|
||||
|
||||
points[2*i + 1].x = nextPoint.x - dy*size;
|
||||
points[2*i + 1].y = nextPoint.y + dx*size;
|
||||
points[2*i].x = nextPoint.x + dy*size;
|
||||
points[2*i].y = nextPoint.y - dx*size;
|
||||
|
||||
currentPoint = nextPoint;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
// Draw spline segment: Quadratic Bezier, 2 points, 1 control point
|
||||
void DrawSplineSegmentBezierQuadratic(Vector2 p1, Vector2 c2, Vector2 p3, float thick, Color color)
|
||||
{
|
||||
const float step = 1.0f/SPLINE_SEGMENT_DIVISIONS;
|
||||
|
||||
Vector2 previous = p1;
|
||||
Vector2 current = { 0 };
|
||||
float t = 0.0f;
|
||||
|
||||
Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 1; i <= SPLINE_SEGMENT_DIVISIONS; i++)
|
||||
{
|
||||
t = step*(float)i;
|
||||
|
||||
float a = powf(1.0f - t, 2);
|
||||
float b = 2.0f*(1.0f - t)*t;
|
||||
float c = powf(t, 2);
|
||||
|
||||
// NOTE: The easing functions aren't suitable here because they don't take a control point
|
||||
current.y = a*p1.y + b*c2.y + c*p3.y;
|
||||
current.x = a*p1.x + b*c2.x + c*p3.x;
|
||||
|
||||
float dy = current.y - previous.y;
|
||||
float dx = current.x - previous.x;
|
||||
float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
points[0].x = previous.x + dy*size;
|
||||
points[0].y = previous.y - dx*size;
|
||||
points[1].x = previous.x - dy*size;
|
||||
points[1].y = previous.y + dx*size;
|
||||
}
|
||||
|
||||
points[2*i + 1].x = current.x - dy*size;
|
||||
points[2*i + 1].y = current.y + dx*size;
|
||||
points[2*i].x = current.x + dy*size;
|
||||
points[2*i].y = current.y - dx*size;
|
||||
|
||||
previous = current;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
// Draw spline segment: Cubic Bezier, 2 points, 2 control points
|
||||
void DrawSplineSegmentBezierCubic(Vector2 p1, Vector2 c2, Vector2 c3, Vector2 p4, float thick, Color color)
|
||||
{
|
||||
const float step = 1.0f/SPLINE_SEGMENT_DIVISIONS;
|
||||
|
||||
Vector2 previous = p1;
|
||||
Vector2 current = { 0 };
|
||||
float t = 0.0f;
|
||||
|
||||
Vector2 points[2*SPLINE_SEGMENT_DIVISIONS + 2] = { 0 };
|
||||
|
||||
for (int i = 1; i <= SPLINE_SEGMENT_DIVISIONS; i++)
|
||||
{
|
||||
t = step*(float)i;
|
||||
|
||||
float a = powf(1.0f - t, 3);
|
||||
float b = 3.0f*powf(1.0f - t, 2)*t;
|
||||
float c = 3.0f*(1.0f - t)*powf(t, 2);
|
||||
float d = powf(t, 3);
|
||||
|
||||
current.y = a*p1.y + b*c2.y + c*c3.y + d*p4.y;
|
||||
current.x = a*p1.x + b*c2.x + c*c3.x + d*p4.x;
|
||||
|
||||
float dy = current.y - previous.y;
|
||||
float dx = current.x - previous.x;
|
||||
float size = 0.5f*thick/sqrtf(dx*dx+dy*dy);
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
points[0].x = previous.x + dy*size;
|
||||
points[0].y = previous.y - dx*size;
|
||||
points[1].x = previous.x - dy*size;
|
||||
points[1].y = previous.y + dx*size;
|
||||
}
|
||||
|
||||
points[2*i + 1].x = current.x - dy*size;
|
||||
points[2*i + 1].y = current.y + dx*size;
|
||||
points[2*i].x = current.x + dy*size;
|
||||
points[2*i].y = current.y - dx*size;
|
||||
|
||||
previous = current;
|
||||
}
|
||||
|
||||
DrawTriangleStrip(points, 2*SPLINE_SEGMENT_DIVISIONS + 2, color);
|
||||
}
|
||||
|
||||
// Get spline point for a given t [0.0f .. 1.0f], Linear
|
||||
Vector2 GetSplinePointLinear(Vector2 startPos, Vector2 endPos, float t)
|
||||
{
|
||||
Vector2 point = { 0 };
|
||||
|
||||
point.x = startPos.x*(1.0f - t) + endPos.x*t;
|
||||
point.y = startPos.y*(1.0f - t) + endPos.y*t;
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
// Get spline point for a given t [0.0f .. 1.0f], B-Spline
|
||||
Vector2 GetSplinePointBasis(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t)
|
||||
{
|
||||
Vector2 point = { 0 };
|
||||
|
||||
float a[4] = { 0 };
|
||||
float b[4] = { 0 };
|
||||
|
||||
a[0] = (-p1.x + 3*p2.x - 3*p3.x + p4.x)/6.0f;
|
||||
a[1] = (3*p1.x - 6*p2.x + 3*p3.x)/6.0f;
|
||||
a[2] = (-3*p1.x + 3*p3.x)/6.0f;
|
||||
a[3] = (p1.x + 4*p2.x + p3.x)/6.0f;
|
||||
|
||||
b[0] = (-p1.y + 3*p2.y - 3*p3.y + p4.y)/6.0f;
|
||||
b[1] = (3*p1.y - 6*p2.y + 3*p3.y)/6.0f;
|
||||
b[2] = (-3*p1.y + 3*p3.y)/6.0f;
|
||||
b[3] = (p1.y + 4*p2.y + p3.y)/6.0f;
|
||||
|
||||
point.x = a[3] + t*(a[2] + t*(a[1] + t*a[0]));
|
||||
point.y = b[3] + t*(b[2] + t*(b[1] + t*b[0]));
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
// Get spline point for a given t [0.0f .. 1.0f], Catmull-Rom
|
||||
Vector2 GetSplinePointCatmullRom(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, float t)
|
||||
{
|
||||
Vector2 point = { 0 };
|
||||
|
||||
float q0 = (-1*t*t*t) + (2*t*t) + (-1*t);
|
||||
float q1 = (3*t*t*t) + (-5*t*t) + 2;
|
||||
float q2 = (-3*t*t*t) + (4*t*t) + t;
|
||||
float q3 = t*t*t - t*t;
|
||||
|
||||
point.x = 0.5f*((p1.x*q0) + (p2.x*q1) + (p3.x*q2) + (p4.x*q3));
|
||||
point.y = 0.5f*((p1.y*q0) + (p2.y*q1) + (p3.y*q2) + (p4.y*q3));
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
// Get spline point for a given t [0.0f .. 1.0f], Quadratic Bezier
|
||||
Vector2 GetSplinePointBezierQuad(Vector2 startPos, Vector2 controlPos, Vector2 endPos, float t)
|
||||
{
|
||||
Vector2 point = { 0 };
|
||||
|
||||
float a = powf(1.0f - t, 2);
|
||||
float b = 2.0f*(1.0f - t)*t;
|
||||
float c = powf(t, 2);
|
||||
|
||||
point.y = a*startPos.y + b*controlPos.y + c*endPos.y;
|
||||
point.x = a*startPos.x + b*controlPos.x + c*endPos.x;
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
// Get spline point for a given t [0.0f .. 1.0f], Cubic Bezier
|
||||
Vector2 GetSplinePointBezierCubic(Vector2 startPos, Vector2 startControlPos, Vector2 endControlPos, Vector2 endPos, float t)
|
||||
{
|
||||
Vector2 point = { 0 };
|
||||
|
||||
float a = powf(1.0f - t, 3);
|
||||
float b = 3.0f*powf(1.0f - t, 2)*t;
|
||||
float c = 3.0f*(1.0f - t)*powf(t, 2);
|
||||
float d = powf(t, 3);
|
||||
|
||||
point.y = a*startPos.y + b*startControlPos.y + c*endControlPos.y + d*endPos.y;
|
||||
point.x = a*startPos.x + b*startControlPos.x + c*endControlPos.x + d*endPos.x;
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Module Functions Definition - Collision Detection functions
|
||||
//----------------------------------------------------------------------------------
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue