[wip, win32, canvas] encode strokes
This commit is contained in:
parent
d00024b515
commit
e24a872fad
656
src/gl_canvas.c
656
src/gl_canvas.c
|
@ -209,6 +209,660 @@ void mg_gl_canvas_encode_element(mg_gl_encoding_context* context, mg_path_elt_ty
|
|||
}
|
||||
}
|
||||
|
||||
bool mg_intersect_hull_legs(vec2 p0, vec2 p1, vec2 p2, vec2 p3, vec2* intersection)
|
||||
{
|
||||
/*NOTE: check intersection of lines (p0-p1) and (p2-p3)
|
||||
|
||||
P = p0 + u(p1-p0)
|
||||
P = p2 + w(p3-p2)
|
||||
*/
|
||||
bool found = false;
|
||||
|
||||
f32 den = (p0.x - p1.x)*(p2.y - p3.y) - (p0.y - p1.y)*(p2.x - p3.x);
|
||||
if(fabs(den) > 0.0001)
|
||||
{
|
||||
f32 u = ((p0.x - p2.x)*(p2.y - p3.y) - (p0.y - p2.y)*(p2.x - p3.x))/den;
|
||||
f32 w = ((p0.x - p2.x)*(p0.y - p1.y) - (p0.y - p2.y)*(p0.x - p1.x))/den;
|
||||
|
||||
intersection->x = p0.x + u*(p1.x - p0.x);
|
||||
intersection->y = p0.y + u*(p1.y - p0.y);
|
||||
found = true;
|
||||
}
|
||||
return(found);
|
||||
}
|
||||
|
||||
bool mg_offset_hull(int count, vec2* p, vec2* result, f32 offset)
|
||||
{
|
||||
//NOTE: we should have no more than two coincident points here. This means the leg between
|
||||
// those two points can't be offset, but we can set a double point at the start of first leg,
|
||||
// end of first leg, or we can join the first and last leg to create a missing middle one
|
||||
|
||||
vec2 legs[3][2] = {0};
|
||||
bool valid[3] = {0};
|
||||
|
||||
for(int i=0; i<count-1; i++)
|
||||
{
|
||||
vec2 n = {p[i].y - p[i+1].y,
|
||||
p[i+1].x - p[i].x};
|
||||
|
||||
f32 norm = sqrt(n.x*n.x + n.y*n.y);
|
||||
if(norm >= 1e-6)
|
||||
{
|
||||
n = vec2_mul(offset/norm, n);
|
||||
legs[i][0] = vec2_add(p[i], n);
|
||||
legs[i][1] = vec2_add(p[i+1], n);
|
||||
valid[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
//NOTE: now we find intersections
|
||||
|
||||
// first point is either the start of the first or second leg
|
||||
if(valid[0])
|
||||
{
|
||||
result[0] = legs[0][0];
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT(valid[1]);
|
||||
result[0] = legs[1][0];
|
||||
}
|
||||
|
||||
for(int i=1; i<count-1; i++)
|
||||
{
|
||||
//NOTE: we're computing the control point i, at the end of leg (i-1)
|
||||
|
||||
if(!valid[i-1])
|
||||
{
|
||||
ASSERT(valid[i]);
|
||||
result[i] = legs[i][0];
|
||||
}
|
||||
else if(!valid[i])
|
||||
{
|
||||
ASSERT(valid[i-1]);
|
||||
result[i] = legs[i-1][0];
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!mg_intersect_hull_legs(legs[i-1][0], legs[i-1][1], legs[i][0], legs[i][1], &result[i]))
|
||||
{
|
||||
// legs don't intersect.
|
||||
return(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(valid[count-2])
|
||||
{
|
||||
result[count-1] = legs[count-2][1];
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT(valid[count-3]);
|
||||
result[count-1] = legs[count-3][1];
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
vec2 mg_quadratic_get_point(vec2 p[3], f32 t)
|
||||
{
|
||||
vec2 r;
|
||||
|
||||
f32 oneMt = 1-t;
|
||||
f32 oneMt2 = Square(oneMt);
|
||||
f32 t2 = Square(t);
|
||||
|
||||
r.x = oneMt2*p[0].x + 2*oneMt*t*p[1].x + t2*p[2].x;
|
||||
r.y = oneMt2*p[0].y + 2*oneMt*t*p[1].y + t2*p[2].y;
|
||||
|
||||
return(r);
|
||||
}
|
||||
|
||||
void mg_quadratic_split(vec2 p[3], f32 t, vec2 outLeft[3], vec2 outRight[3])
|
||||
{
|
||||
//NOTE(martin): split bezier curve p at parameter t, using De Casteljau's algorithm
|
||||
// the q_n are the points along the hull's segments at parameter t
|
||||
// s is the split point.
|
||||
|
||||
f32 oneMt = 1-t;
|
||||
|
||||
vec2 q0 = {oneMt*p[0].x + t*p[1].x,
|
||||
oneMt*p[0].y + t*p[1].y};
|
||||
|
||||
vec2 q1 = {oneMt*p[1].x + t*p[2].x,
|
||||
oneMt*p[1].y + t*p[2].y};
|
||||
|
||||
vec2 s = {oneMt*q0.x + t*q1.x,
|
||||
oneMt*q0.y + t*q1.y};
|
||||
|
||||
outLeft[0] = p[0];
|
||||
outLeft[1] = q0;
|
||||
outLeft[2] = s;
|
||||
|
||||
outRight[0] = s;
|
||||
outRight[1] = q1;
|
||||
outRight[2] = p[2];
|
||||
}
|
||||
|
||||
vec2 mg_cubic_get_point(vec2 p[4], f32 t)
|
||||
{
|
||||
vec2 r;
|
||||
|
||||
f32 oneMt = 1-t;
|
||||
f32 oneMt2 = Square(oneMt);
|
||||
f32 oneMt3 = oneMt2*oneMt;
|
||||
f32 t2 = Square(t);
|
||||
f32 t3 = t2*t;
|
||||
|
||||
r.x = oneMt3*p[0].x + 3*oneMt2*t*p[1].x + 3*oneMt*t2*p[2].x + t3*p[3].x;
|
||||
r.y = oneMt3*p[0].y + 3*oneMt2*t*p[1].y + 3*oneMt*t2*p[2].y + t3*p[3].y;
|
||||
|
||||
return(r);
|
||||
}
|
||||
|
||||
void mg_cubic_split(vec2 p[4], f32 t, vec2 outLeft[4], vec2 outRight[4])
|
||||
{
|
||||
//NOTE(martin): split bezier curve p at parameter t, using De Casteljau's algorithm
|
||||
// the q_n are the points along the hull's segments at parameter t
|
||||
// the r_n are the points along the (q_n, q_n+1) segments at parameter t
|
||||
// s is the split point.
|
||||
|
||||
vec2 q0 = {(1-t)*p[0].x + t*p[1].x,
|
||||
(1-t)*p[0].y + t*p[1].y};
|
||||
|
||||
vec2 q1 = {(1-t)*p[1].x + t*p[2].x,
|
||||
(1-t)*p[1].y + t*p[2].y};
|
||||
|
||||
vec2 q2 = {(1-t)*p[2].x + t*p[3].x,
|
||||
(1-t)*p[2].y + t*p[3].y};
|
||||
|
||||
vec2 r0 = {(1-t)*q0.x + t*q1.x,
|
||||
(1-t)*q0.y + t*q1.y};
|
||||
|
||||
vec2 r1 = {(1-t)*q1.x + t*q2.x,
|
||||
(1-t)*q1.y + t*q2.y};
|
||||
|
||||
vec2 s = {(1-t)*r0.x + t*r1.x,
|
||||
(1-t)*r0.y + t*r1.y};;
|
||||
|
||||
outLeft[0] = p[0];
|
||||
outLeft[1] = q0;
|
||||
outLeft[2] = r0;
|
||||
outLeft[3] = s;
|
||||
|
||||
outRight[0] = s;
|
||||
outRight[1] = r1;
|
||||
outRight[2] = q2;
|
||||
outRight[3] = p[3];
|
||||
}
|
||||
|
||||
void mg_gl_encode_stroke_line(mg_gl_encoding_context* context, vec2* p)
|
||||
{
|
||||
f32 width = context->primitive->attributes.width;
|
||||
|
||||
vec2 v = {p[1].x-p[0].x, p[1].y-p[0].y};
|
||||
vec2 n = {v.y, -v.x};
|
||||
f32 norm = sqrt(n.x*n.x + n.y*n.y);
|
||||
vec2 offset = vec2_mul(0.5*width/norm, n);
|
||||
|
||||
vec2 left[2] = {vec2_add(p[0], offset), vec2_add(p[1], offset)};
|
||||
vec2 right[2] = {vec2_add(p[1], vec2_mul(-1, offset)), vec2_add(p[0], vec2_mul(-1, offset))};
|
||||
vec2 joint0[2] = {vec2_add(p[0], vec2_mul(-1, offset)), vec2_add(p[0], offset)};
|
||||
vec2 joint1[2] = {vec2_add(p[1], offset), vec2_add(p[1], vec2_mul(-1, offset))};
|
||||
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, right);
|
||||
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, left);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, joint0);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, joint1);
|
||||
}
|
||||
|
||||
enum { MG_HULL_CHECK_SAMPLE_COUNT = 5 };
|
||||
|
||||
void mg_gl_encode_stroke_quadratic(mg_gl_encoding_context* context, vec2* p)
|
||||
{
|
||||
f32 width = context->primitive->attributes.width;
|
||||
f32 tolerance = minimum(context->primitive->attributes.tolerance, 0.5 * width);
|
||||
|
||||
//NOTE: check for degenerate line case
|
||||
const f32 equalEps = 1e-3;
|
||||
if(vec2_close(p[0], p[1], equalEps))
|
||||
{
|
||||
mg_gl_encode_stroke_line(context, p+1);
|
||||
return;
|
||||
}
|
||||
else if(vec2_close(p[1], p[2], equalEps))
|
||||
{
|
||||
mg_gl_encode_stroke_line(context, p);
|
||||
return;
|
||||
}
|
||||
|
||||
vec2 leftHull[3];
|
||||
vec2 rightHull[3];
|
||||
|
||||
if( !mg_offset_hull(3, p, leftHull, width/2)
|
||||
|| !mg_offset_hull(3, p, rightHull, -width/2))
|
||||
{
|
||||
//TODO split and recurse
|
||||
//NOTE: offsetting the hull failed, split the curve
|
||||
vec2 splitLeft[3];
|
||||
vec2 splitRight[3];
|
||||
mg_quadratic_split(p, 0.5, splitLeft, splitRight);
|
||||
mg_gl_encode_stroke_quadratic(context, splitLeft);
|
||||
mg_gl_encode_stroke_quadratic(context, splitRight);
|
||||
}
|
||||
else
|
||||
{
|
||||
f32 checkSamples[MG_HULL_CHECK_SAMPLE_COUNT] = {1./6, 2./6, 3./6, 4./6, 5./6};
|
||||
|
||||
f32 d2LowBound = Square(0.5 * width - tolerance);
|
||||
f32 d2HighBound = Square(0.5 * width + tolerance);
|
||||
|
||||
f32 maxOvershoot = 0;
|
||||
f32 maxOvershootParameter = 0;
|
||||
|
||||
for(int i=0; i<MG_HULL_CHECK_SAMPLE_COUNT; i++)
|
||||
{
|
||||
f32 t = checkSamples[i];
|
||||
|
||||
vec2 c = mg_quadratic_get_point(p, t);
|
||||
vec2 cp = mg_quadratic_get_point(leftHull, t);
|
||||
vec2 cn = mg_quadratic_get_point(rightHull, t);
|
||||
|
||||
f32 positiveDistSquare = Square(c.x - cp.x) + Square(c.y - cp.y);
|
||||
f32 negativeDistSquare = Square(c.x - cn.x) + Square(c.y - cn.y);
|
||||
|
||||
f32 positiveOvershoot = maximum(positiveDistSquare - d2HighBound, d2LowBound - positiveDistSquare);
|
||||
f32 negativeOvershoot = maximum(negativeDistSquare - d2HighBound, d2LowBound - negativeDistSquare);
|
||||
|
||||
f32 overshoot = maximum(positiveOvershoot, negativeOvershoot);
|
||||
|
||||
if(overshoot > maxOvershoot)
|
||||
{
|
||||
maxOvershoot = overshoot;
|
||||
maxOvershootParameter = t;
|
||||
}
|
||||
}
|
||||
|
||||
if(maxOvershoot > 0)
|
||||
{
|
||||
vec2 splitLeft[3];
|
||||
vec2 splitRight[3];
|
||||
mg_quadratic_split(p, maxOvershootParameter, splitLeft, splitRight);
|
||||
mg_gl_encode_stroke_quadratic(context, splitLeft);
|
||||
mg_gl_encode_stroke_quadratic(context, splitRight);
|
||||
}
|
||||
else
|
||||
{
|
||||
vec2 tmp = leftHull[0];
|
||||
leftHull[0] = leftHull[2];
|
||||
leftHull[2] = tmp;
|
||||
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_QUADRATIC, rightHull);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_QUADRATIC, leftHull);
|
||||
|
||||
vec2 joint0[2] = {rightHull[2], leftHull[0]};
|
||||
vec2 joint1[2] = {leftHull[2], rightHull[0]};
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, joint0);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, joint1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mg_gl_encode_stroke_cubic(mg_gl_encoding_context* context, vec2* p)
|
||||
{
|
||||
f32 width = context->primitive->attributes.width;
|
||||
f32 tolerance = minimum(context->primitive->attributes.tolerance, 0.5 * width);
|
||||
|
||||
//NOTE: check degenerate line cases
|
||||
f32 equalEps = 1e-3;
|
||||
|
||||
if( (vec2_close(p[0], p[1], equalEps) && vec2_close(p[2], p[3], equalEps))
|
||||
||(vec2_close(p[0], p[1], equalEps) && vec2_close(p[1], p[2], equalEps))
|
||||
||(vec2_close(p[1], p[2], equalEps) && vec2_close(p[2], p[3], equalEps)))
|
||||
{
|
||||
vec2 line[2] = {p[0], p[3]};
|
||||
mg_gl_encode_stroke_line(context, line);
|
||||
return;
|
||||
}
|
||||
else if(vec2_close(p[0], p[1], equalEps) && vec2_close(p[1], p[3], equalEps))
|
||||
{
|
||||
vec2 line[2] = {p[0], vec2_add(vec2_mul(5./9, p[0]), vec2_mul(4./9, p[2]))};
|
||||
mg_gl_encode_stroke_line(context, line);
|
||||
return;
|
||||
}
|
||||
else if(vec2_close(p[0], p[2], equalEps) && vec2_close(p[2], p[3], equalEps))
|
||||
{
|
||||
vec2 line[2] = {p[0], vec2_add(vec2_mul(5./9, p[0]), vec2_mul(4./9, p[1]))};
|
||||
mg_gl_encode_stroke_line(context, line);
|
||||
return;
|
||||
}
|
||||
|
||||
vec2 leftHull[4];
|
||||
vec2 rightHull[4];
|
||||
|
||||
if( !mg_offset_hull(4, p, leftHull, width/2)
|
||||
|| !mg_offset_hull(4, p, rightHull, -width/2))
|
||||
{
|
||||
//TODO split and recurse
|
||||
//NOTE: offsetting the hull failed, split the curve
|
||||
vec2 splitLeft[4];
|
||||
vec2 splitRight[4];
|
||||
mg_cubic_split(p, 0.5, splitLeft, splitRight);
|
||||
mg_gl_encode_stroke_cubic(context, splitLeft);
|
||||
mg_gl_encode_stroke_cubic(context, splitRight);
|
||||
}
|
||||
else
|
||||
{
|
||||
f32 checkSamples[MG_HULL_CHECK_SAMPLE_COUNT] = {1./6, 2./6, 3./6, 4./6, 5./6};
|
||||
|
||||
f32 d2LowBound = Square(0.5 * width - tolerance);
|
||||
f32 d2HighBound = Square(0.5 * width + tolerance);
|
||||
|
||||
f32 maxOvershoot = 0;
|
||||
f32 maxOvershootParameter = 0;
|
||||
|
||||
for(int i=0; i<MG_HULL_CHECK_SAMPLE_COUNT; i++)
|
||||
{
|
||||
f32 t = checkSamples[i];
|
||||
|
||||
vec2 c = mg_cubic_get_point(p, t);
|
||||
vec2 cp = mg_cubic_get_point(leftHull, t);
|
||||
vec2 cn = mg_cubic_get_point(rightHull, t);
|
||||
|
||||
f32 positiveDistSquare = Square(c.x - cp.x) + Square(c.y - cp.y);
|
||||
f32 negativeDistSquare = Square(c.x - cn.x) + Square(c.y - cn.y);
|
||||
|
||||
f32 positiveOvershoot = maximum(positiveDistSquare - d2HighBound, d2LowBound - positiveDistSquare);
|
||||
f32 negativeOvershoot = maximum(negativeDistSquare - d2HighBound, d2LowBound - negativeDistSquare);
|
||||
|
||||
f32 overshoot = maximum(positiveOvershoot, negativeOvershoot);
|
||||
|
||||
if(overshoot > maxOvershoot)
|
||||
{
|
||||
maxOvershoot = overshoot;
|
||||
maxOvershootParameter = t;
|
||||
}
|
||||
}
|
||||
|
||||
if(maxOvershoot > 0)
|
||||
{
|
||||
vec2 splitLeft[4];
|
||||
vec2 splitRight[4];
|
||||
mg_cubic_split(p, maxOvershootParameter, splitLeft, splitRight);
|
||||
mg_gl_encode_stroke_cubic(context, splitLeft);
|
||||
mg_gl_encode_stroke_cubic(context, splitRight);
|
||||
}
|
||||
else
|
||||
{
|
||||
vec2 tmp = leftHull[0];
|
||||
leftHull[0] = leftHull[3];
|
||||
leftHull[3] = tmp;
|
||||
tmp = leftHull[1];
|
||||
leftHull[1] = leftHull[2];
|
||||
leftHull[2] = tmp;
|
||||
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_CUBIC, rightHull);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_CUBIC, leftHull);
|
||||
|
||||
vec2 joint0[2] = {rightHull[3], leftHull[0]};
|
||||
vec2 joint1[2] = {leftHull[3], rightHull[0]};
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, joint0);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, joint1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mg_gl_encode_stroke_element(mg_gl_encoding_context* context,
|
||||
mg_path_elt* element,
|
||||
vec2 currentPoint,
|
||||
vec2* startTangent,
|
||||
vec2* endTangent,
|
||||
vec2* endPoint)
|
||||
{
|
||||
vec2 controlPoints[4] = {currentPoint, element->p[0], element->p[1], element->p[2]};
|
||||
int endPointIndex = 0;
|
||||
|
||||
switch(element->type)
|
||||
{
|
||||
case MG_PATH_LINE:
|
||||
mg_gl_encode_stroke_line(context, controlPoints);
|
||||
endPointIndex = 1;
|
||||
break;
|
||||
|
||||
case MG_PATH_QUADRATIC:
|
||||
mg_gl_encode_stroke_quadratic(context, controlPoints);
|
||||
endPointIndex = 2;
|
||||
break;
|
||||
|
||||
case MG_PATH_CUBIC:
|
||||
mg_gl_encode_stroke_cubic(context, controlPoints);
|
||||
endPointIndex = 3;
|
||||
break;
|
||||
|
||||
case MG_PATH_MOVE:
|
||||
ASSERT(0, "should be unreachable");
|
||||
break;
|
||||
}
|
||||
|
||||
//NOTE: ensure tangents are properly computed even in presence of coincident points
|
||||
//TODO: see if we can do this in a less hacky way
|
||||
|
||||
for(int i=1; i<4; i++)
|
||||
{
|
||||
if( controlPoints[i].x != controlPoints[0].x
|
||||
|| controlPoints[i].y != controlPoints[0].y)
|
||||
{
|
||||
*startTangent = (vec2){.x = controlPoints[i].x - controlPoints[0].x,
|
||||
.y = controlPoints[i].y - controlPoints[0].y};
|
||||
break;
|
||||
}
|
||||
}
|
||||
*endPoint = controlPoints[endPointIndex];
|
||||
|
||||
for(int i=endPointIndex-1; i>=0; i++)
|
||||
{
|
||||
if( controlPoints[i].x != endPoint->x
|
||||
|| controlPoints[i].y != endPoint->y)
|
||||
{
|
||||
*endTangent = (vec2){.x = endPoint->x - controlPoints[i].x,
|
||||
.y = endPoint->y - controlPoints[i].y};
|
||||
break;
|
||||
}
|
||||
}
|
||||
DEBUG_ASSERT(startTangent->x != 0 || startTangent->y != 0);
|
||||
}
|
||||
|
||||
void mg_gl_stroke_cap(mg_gl_encoding_context* context,
|
||||
vec2 p0,
|
||||
vec2 direction)
|
||||
{
|
||||
mg_attributes* attributes = &context->primitive->attributes;
|
||||
|
||||
//NOTE(martin): compute the tangent and normal vectors (multiplied by half width) at the cap point
|
||||
f32 dn = sqrt(Square(direction.x) + Square(direction.y));
|
||||
f32 alpha = 0.5 * attributes->width/dn;
|
||||
|
||||
vec2 n0 = {-alpha*direction.y,
|
||||
alpha*direction.x};
|
||||
|
||||
vec2 m0 = {alpha*direction.x,
|
||||
alpha*direction.y};
|
||||
|
||||
vec2 points[] = {{p0.x + n0.x, p0.y + n0.y},
|
||||
{p0.x + n0.x + m0.x, p0.y + n0.y + m0.y},
|
||||
{p0.x - n0.x + m0.x, p0.y - n0.y + m0.y},
|
||||
{p0.x - n0.x, p0.y - n0.y},
|
||||
{p0.x + n0.x, p0.y + n0.y}};
|
||||
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points+1);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points+2);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points+3);
|
||||
}
|
||||
|
||||
void mg_gl_stroke_joint(mg_gl_encoding_context* context,
|
||||
vec2 p0,
|
||||
vec2 t0,
|
||||
vec2 t1)
|
||||
{
|
||||
mg_attributes* attributes = &context->primitive->attributes;
|
||||
|
||||
//NOTE(martin): compute the normals at the joint point
|
||||
f32 norm_t0 = sqrt(Square(t0.x) + Square(t0.y));
|
||||
f32 norm_t1 = sqrt(Square(t1.x) + Square(t1.y));
|
||||
|
||||
vec2 n0 = {-t0.y, t0.x};
|
||||
n0.x /= norm_t0;
|
||||
n0.y /= norm_t0;
|
||||
|
||||
vec2 n1 = {-t1.y, t1.x};
|
||||
n1.x /= norm_t1;
|
||||
n1.y /= norm_t1;
|
||||
|
||||
//NOTE(martin): the sign of the cross product determines if the normals are facing outwards or inwards the angle.
|
||||
// we flip them to face outwards if needed
|
||||
f32 crossZ = n0.x*n1.y - n0.y*n1.x;
|
||||
if(crossZ > 0)
|
||||
{
|
||||
n0.x *= -1;
|
||||
n0.y *= -1;
|
||||
n1.x *= -1;
|
||||
n1.y *= -1;
|
||||
}
|
||||
|
||||
//NOTE(martin): use the same code as hull offset to find mitter point...
|
||||
/*NOTE(martin): let vector u = (n0+n1) and vector v = pIntersect - p1
|
||||
then v = u * (2*offset / norm(u)^2)
|
||||
(this can be derived from writing the pythagoras theorems in the triangles of the joint)
|
||||
*/
|
||||
f32 halfW = 0.5 * attributes->width;
|
||||
vec2 u = {n0.x + n1.x, n0.y + n1.y};
|
||||
f32 uNormSquare = u.x*u.x + u.y*u.y;
|
||||
f32 alpha = attributes->width / uNormSquare;
|
||||
vec2 v = {u.x * alpha, u.y * alpha};
|
||||
|
||||
f32 excursionSquare = uNormSquare * Square(alpha - attributes->width/4);
|
||||
|
||||
if( attributes->joint == MG_JOINT_MITER
|
||||
&& excursionSquare <= Square(attributes->maxJointExcursion))
|
||||
{
|
||||
//NOTE(martin): add a mitter joint
|
||||
vec2 points[] = {p0,
|
||||
{p0.x + n0.x*halfW, p0.y + n0.y*halfW},
|
||||
{p0.x + v.x, p0.y + v.y},
|
||||
{p0.x + n1.x*halfW, p0.y + n1.y*halfW},
|
||||
p0};
|
||||
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points+1);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points+2);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points+3);
|
||||
}
|
||||
else
|
||||
{
|
||||
//NOTE(martin): add a bevel joint
|
||||
vec2 points[] = {p0,
|
||||
{p0.x + n0.x*halfW, p0.y + n0.y*halfW},
|
||||
{p0.x + n1.x*halfW, p0.y + n1.y*halfW},
|
||||
p0};
|
||||
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points+1);
|
||||
mg_gl_canvas_encode_element(context, MG_PATH_LINE, points+2);
|
||||
}
|
||||
}
|
||||
|
||||
u32 mg_gl_encode_stroke_subpath(mg_gl_encoding_context* context,
|
||||
mg_path_elt* elements,
|
||||
mg_path_descriptor* path,
|
||||
u32 startIndex,
|
||||
vec2 startPoint)
|
||||
{
|
||||
u32 eltCount = path->count;
|
||||
DEBUG_ASSERT(startIndex < eltCount);
|
||||
|
||||
vec2 currentPoint = startPoint;
|
||||
vec2 endPoint = {0, 0};
|
||||
vec2 previousEndTangent = {0, 0};
|
||||
vec2 firstTangent = {0, 0};
|
||||
vec2 startTangent = {0, 0};
|
||||
vec2 endTangent = {0, 0};
|
||||
|
||||
//NOTE(martin): encode first element and compute first tangent
|
||||
mg_gl_encode_stroke_element(context, elements + startIndex, currentPoint, &startTangent, &endTangent, &endPoint);
|
||||
|
||||
firstTangent = startTangent;
|
||||
previousEndTangent = endTangent;
|
||||
currentPoint = endPoint;
|
||||
|
||||
//NOTE(martin): encode subsequent elements along with their joints
|
||||
|
||||
mg_attributes* attributes = &context->primitive->attributes;
|
||||
|
||||
u32 eltIndex = startIndex + 1;
|
||||
for(;
|
||||
eltIndex<eltCount && elements[eltIndex].type != MG_PATH_MOVE;
|
||||
eltIndex++)
|
||||
{
|
||||
mg_gl_encode_stroke_element(context, elements + eltIndex, currentPoint, &startTangent, &endTangent, &endPoint);
|
||||
|
||||
if(attributes->joint != MG_JOINT_NONE)
|
||||
{
|
||||
mg_gl_stroke_joint(context, currentPoint, previousEndTangent, startTangent);
|
||||
}
|
||||
previousEndTangent = endTangent;
|
||||
currentPoint = endPoint;
|
||||
}
|
||||
u32 subPathEltCount = eltIndex - startIndex;
|
||||
|
||||
//NOTE(martin): draw end cap / joint. We ensure there's at least two segments to draw a closing joint
|
||||
if( subPathEltCount > 1
|
||||
&& startPoint.x == endPoint.x
|
||||
&& startPoint.y == endPoint.y)
|
||||
{
|
||||
if(attributes->joint != MG_JOINT_NONE)
|
||||
{
|
||||
//NOTE(martin): add a closing joint if the path is closed
|
||||
mg_gl_stroke_joint(context, endPoint, endTangent, firstTangent);
|
||||
}
|
||||
}
|
||||
else if(attributes->cap == MG_CAP_SQUARE)
|
||||
{
|
||||
//NOTE(martin): add start and end cap
|
||||
mg_gl_stroke_cap(context, startPoint, (vec2){-startTangent.x, -startTangent.y});
|
||||
mg_gl_stroke_cap(context, endPoint, endTangent);
|
||||
}
|
||||
return(eltIndex);
|
||||
}
|
||||
|
||||
void mg_gl_encode_stroke(mg_gl_encoding_context* context,
|
||||
mg_path_elt* elements,
|
||||
mg_path_descriptor* path)
|
||||
{
|
||||
u32 eltCount = path->count;
|
||||
DEBUG_ASSERT(eltCount);
|
||||
|
||||
vec2 startPoint = path->startPoint;
|
||||
u32 startIndex = 0;
|
||||
|
||||
while(startIndex < eltCount)
|
||||
{
|
||||
//NOTE(martin): eliminate leading moves
|
||||
while(startIndex < eltCount && elements[startIndex].type == MG_PATH_MOVE)
|
||||
{
|
||||
startPoint = elements[startIndex].p[0];
|
||||
startIndex++;
|
||||
}
|
||||
if(startIndex < eltCount)
|
||||
{
|
||||
startIndex = mg_gl_encode_stroke_subpath(context, elements, path, startIndex, startPoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mg_gl_render_batch(mg_gl_canvas_backend* backend,
|
||||
mg_wgl_surface* surface,
|
||||
int pathCount,
|
||||
|
@ -434,7 +1088,7 @@ void mg_gl_canvas_render(mg_canvas_backend* interface,
|
|||
|
||||
if(primitive->cmd == MG_CMD_STROKE)
|
||||
{
|
||||
//TODO mg_gl_render_stroke(&context, pathElements + primitive->path.startIndex, &primitive->path);
|
||||
mg_gl_encode_stroke(&context, pathElements + primitive->path.startIndex, &primitive->path);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue