]> O.S.I.I.S - jp/vkvg.git/commitdiff
save commit
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sat, 11 Dec 2021 20:45:20 +0000 (21:45 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Sat, 11 Dec 2021 20:45:20 +0000 (21:45 +0100)
src/vkvg_context_internal.c
src/vkvg_context_internal.h

index bd1593bfe8a7df833a1c2ad0a9e2076e3526a702..49b716a27e5e0ed5ac9c8e8a73caa8569fc90f07 100644 (file)
@@ -736,6 +736,15 @@ void _init_descriptor_sets (VkvgContext ctx){
        descriptorSetAllocateInfo.pSetLayouts = &dev->dslGrad;
        VK_CHECK_RESULT(vkAllocateDescriptorSets(dev->vkDev, &descriptorSetAllocateInfo, &ctx->dsGrad));
 }
+vec2 intersect (float x1, float y1,float x2, float y2,float x3, float y3,float x4, float y4) {
+       float t = ((x1-x3)*(y3-y4)-(y1-y3)*(x3-x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
+       //float u = ((x1-x3)*(y1-y2)-(y1-y3)*(x1-x2))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
+       vec2 a = {
+               x1 + t * (x2 - x1),
+               y1 + t * (y2 - y1)
+       };
+       return a;
+}
 //populate vertice buff for stroke
 float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool isCurve){
        Vertex v = {{0},ctx->curColor, {0,0,-1}};
@@ -755,44 +764,101 @@ float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool i
        vec2 bisec_n = vec2_norm(vec2_add(v0n,v1n));
 
        float dot = vec2_dot (v0n, v1n);
+
        float alpha = acosf(dot);
        float det = v0n.x * v1n.y - v0n.y * v1n.x;
 
+       if (EQUF(dot,1.0f))
+               return det;
+
        if (det<0)
                alpha = -alpha;
 
        float lh = hw / cosf(alpha/2);
        bisec_n = vec2_perp(bisec_n);
 
-       //limit bisectrice length, may be improved but ok for perf
-       //lh=fminf (lh, fminf (sqrtf(length_v0*length_v0+hw*hw), sqrtf(length_v1*length_v1+hw*hw)));
+       vkvg_line_join_t join = ctx->lineJoin;
+       if (dot < -0.9 && join == VKVG_LINE_JOIN_MITER)
+               join =  VKVG_LINE_JOIN_BEVEL;
 
+       //limit bisectrice length, may be improved but ok for perf
+//     if (lh > fminf (lh, fminf (sqrtf(length_v0*length_v0+hw*hw), sqrtf(length_v1*length_v1+hw*hw))))
+       //bool reducedLH = (lh > fminf (lh, fminf (sqrtf(length_v0*length_v0+hw*hw), sqrtf(length_v1*length_v1+hw*hw))));
+       //bool reducedLH = (lh > sqrtf(length_v0*length_v0+hw*hw));
+       bool reducedLH = EQUF(dot,-1) || (lh > fminf (lh, fminf (length_v0, length_v1)));
+       //lh = fmax(hw, fminf (lh, fminf (length_v0, length_v1)));
 
        vec2 bisec = vec2_mult(bisec_n,lh);
 
+
        VKVG_IBO_INDEX_TYPE idx = (VKVG_IBO_INDEX_TYPE)(ctx->vertCount - ctx->curVertOffset);
 
-       if (ctx->lineJoin == VKVG_LINE_JOIN_MITER || isCurve){
-               v.pos = vec2_add(p0, bisec);
-               _add_vertex(ctx, v);
-               v.pos = vec2_sub(p0, bisec);
+       if (join == VKVG_LINE_JOIN_MITER || isCurve){
+               if (dot < 0 && reducedLH && det < 0) {
+                       if (length_v0 < length_v1)
+                               v.pos = vec2_add (p0, vec2_mult (vec2_perp(v1n), hw));
+                       else
+                               v.pos = vec2_sub (p0, vec2_mult (vec2_perp(v1n), hw));
+                       /*else
+                               v.pos = vec2_add (p0, vec2_mult (vec2_perp(v1n), hw));*/
+               } else
+                       v.pos = vec2_add(p0, bisec);
+
+               /*vec2 vA = vec2_sub(v.pos, ctx->vertexCache[ctx->vertCount-2].pos);
+               float determ = vec2_det(vA, v1);
+               if (str->has_prev_v0n) {
+                       if ((det > 0 && determ < 0)||(det < 0 && determ > 0)) {
+                               vec2 pA = vec2_add (vec2_mult (vec2_perp (str->prev_v0n), hw), pL);
+                               vec2 pA2 = vec2_add (pA, str->prev_v0n);
+                               vec2 pB = vec2_add (vec2_mult (vec2_perp (v1n), hw), p0);
+                               vec2 pB2 = vec2_add (pB, v1n);
+                               v.pos = intersect(pA.x, pA.y,pA2.x, pA2.y, pB.x, pB.y,pB2.x, pB2.y);
+                               ctx->vertexCache[ctx->vertCount-2].pos = v.pos;
+                       }
+                       _add_vertex(ctx, v);
+               } else*/
+                       _add_vertex(ctx, v);
+
+               /*if (reducedLH)
+                       v.pos = ctx->vertexCache[ctx->vertCount-3].pos;
+               else*/
+               if (dot < 0 && reducedLH && det > 0) {
+                       if (length_v0 < length_v1)
+                               v.pos = vec2_sub (p0, vec2_mult (vec2_perp(v1n), hw));
+                       else
+                               v.pos = vec2_add (p0, vec2_mult (vec2_perp(v1n), hw));
+               } else
+                       v.pos = vec2_sub(p0, bisec);
+
                _add_vertex(ctx, v);
                _add_tri_indices_for_rect(ctx, idx);
 
        }else{
                vec2 vp = vec2_perp(v0n);
                if (det<0){
-                       v.pos = vec2_add (p0, bisec);
+                       if (reducedLH) {
+                               if (length_v0 < length_v1)
+                                       v.pos = vec2_add (p0, vec2_mult (vec2_perp(v1n), hw));
+                               else
+                                       v.pos = vec2_sub (p0, vec2_mult (vec2_perp(v1n), hw));
+                       } else
+                               v.pos = vec2_add (p0, bisec);
                        _add_vertex(ctx, v);
                        v.pos = vec2_sub (p0, vec2_mult (vp, hw));
                }else{
                        v.pos = vec2_add (p0, vec2_mult (vp, hw));
                        _add_vertex(ctx, v);
-                       v.pos = vec2_sub (p0, bisec);
+                       if (reducedLH) {
+                               if (length_v0 < length_v1)
+                                       v.pos = vec2_sub (p0, vec2_mult (vec2_perp(v1n), hw));
+                               else
+                                       v.pos = vec2_add (p0, vec2_mult (vec2_perp(v1n), hw));
+                       } else
+                               v.pos = vec2_sub (p0, bisec);
                }
                _add_vertex(ctx, v);
 
-               if (ctx->lineJoin == VKVG_LINE_JOIN_BEVEL){
+               if (join == VKVG_LINE_JOIN_BEVEL){
                        if (det<0){
                                _add_triangle_indices(ctx, idx, idx+2, idx+1);
                                _add_triangle_indices(ctx, idx+2, idx+4, idx+0);
@@ -802,7 +868,7 @@ float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool i
                                _add_triangle_indices(ctx, idx+2, idx+3, idx+1);
                                _add_triangle_indices(ctx, idx+1, idx+3, idx+4);
                        }
-               }else if (ctx->lineJoin == VKVG_LINE_JOIN_ROUND){
+               }else if (join == VKVG_LINE_JOIN_ROUND){
                        float step = M_PIF / hw;
                        float a = acosf(vp.x);
                        if (vp.y < 0)
@@ -846,8 +912,6 @@ float _build_vb_step (vkvg_context* ctx, float hw, stroke_context_t* str, bool i
                else
                        v.pos = vec2_add (p0, vp);
                _add_vertex(ctx, v);
-               str->has_prev_v0n = true;
-               str->prev_v0n = v0n;
        }
 
 /*
index 1fa7830b38cb9ec7ce4877d063d068365f5bf52b..734e44af9ad614617b545a3842d8fbad3bff4f51 100644 (file)
@@ -202,10 +202,7 @@ typedef struct {
 typedef struct {
        uint32_t iL;
        uint32_t iR;
-       uint32_t iR2;
        uint32_t cp;//current point
-       bool has_prev_v0n;//true if set
-       vec2 prev_v0n;//store previous left normal for path correction
 }stroke_context_t;
 
 void _check_vertex_cache_size(VkvgContext ctx);