*/}}
Browse Source

Smudging basic logic

Yiming Wu 1 year ago
parent
commit
ef941c3004
2 changed files with 188 additions and 74 deletions
  1. 178 71
      ouroperations.c
  2. 10 3
      ourpaint.h

+ 178 - 71
ouroperations.c

@@ -6,31 +6,45 @@ extern tnsMain* T;
 
 const char OUR_CANVAS_SHADER[]="#version 430\n\
 layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;\n\
-layout(rgba8, binding = 0) coherent uniform image2D img;\n\
-layout(std430, binding = 1) buffer CCOLOR { float ccolor[4]; };\n\
+layout(rgba8, binding = 0) uniform image2D img;\n\
+layout(rgba8, binding = 1) coherent uniform image2D smudge_buckets;\n\
 uniform ivec2 uBrushCorner;\n\
 uniform vec2 uBrushCenter;\n\
 uniform float uBrushSize;\n\
 uniform float uBrushHardness;\n\
 uniform float uBrushSmudge;\n\
 uniform vec4 uBrushColor;\n\
-\n\
+uniform vec4 uBackgroundColor;\n\
 int dab(float d, vec4 color, float size, float hardness, float smudge, vec4 smudge_color, vec4 last_color, out vec4 final){\n\
-    color.rgb=mix(color,smudge_color,smudge).rgb;\n\
+    color.rgb=mix(color,smudge_color,smudge*smudge_color.a).rgb;\n\
+    color.a=color.a*(1-smudge);\n\
     float a=clamp(color.a*1-pow(d/size,1+1/(1-hardness)),0,1);\n\
     final=vec4(mix(last_color.rgb,color.rgb,a), 1-(1-last_color.a)*(1-a));\n\
     return 1;\n\
 }\n\
-void main() {\n\
+subroutine void BrushRoutines();\n\
+subroutine(BrushRoutines) void DoDabs(){\n\
     ivec2 px = ivec2(gl_GlobalInvocationID.xy)+uBrushCorner;\n\
     float dd=distance(vec2(px),uBrushCenter); if(dd>uBrushSize) return;\n\
     vec4 final;\n\
     vec4 dabc=imageLoad(img, px);\n\
-    //vec4 smugec=imageLoad(img,uBrushCenter);\n\
-    dab(dd,uBrushColor,uBrushSize,uBrushHardness,0,vec4(0,0,0,0),dabc,final);\n\
+    vec4 smudgec=imageLoad(smudge_buckets,ivec2(0,0));\n\
+    dab(dd,uBrushColor,uBrushSize,uBrushHardness,uBrushSmudge,smudgec,dabc,final);\n\
     dabc=final;\n\
     imageStore(img, px, dabc);\n\
 }\n\
+subroutine(BrushRoutines) void DoSample(){\n\
+    ivec2 p=ivec2(gl_GlobalInvocationID.xy);\n\
+    if(p!=ivec2(0,0)) return;\n\
+    ivec2 px=p+uBrushCorner;\n\
+    vec4 color=imageLoad(img, px);\n\
+    imageStore(smudge_buckets,ivec2(0,0),color);\n\
+}\n\
+subroutine uniform BrushRoutines uBrushRoutineSelection;\n\
+\n\
+void main() {\n\
+    uBrushRoutineSelection();\n\
+}\n\
 ";
 
 
@@ -66,6 +80,11 @@ void ourui_LayersPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProp
     }laEndCondition(uil,b);
 
     laShowItemFull(uil,c,0,"our.canvas.layers",0,0,0,0);
+
+    b=laBeginRow(uil,c,0,0);
+    laShowLabel(uil,c,"Background",0,0)->Expand=1;
+    laShowItemFull(uil,c,0,"our.background_color",LA_WIDGET_FLOAT_COLOR,0,0,0);
+    laEndRow(uil,b);
 }
 void ourui_Brush(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
     laColumn* c=laFirstColumn(uil); laColumn* cl,*cr; laSplitColumn(uil,c,0.7); cl=laLeftColumn(c,0);cr=laRightColumn(c,1);
@@ -84,13 +103,18 @@ void ourui_Brush(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laC
 }
 void ourui_BrushesPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
     laColumn* c=laFirstColumn(uil);
+    laUiItem* b1;
+#define OUR_BR b1=laBeginRow(uil,c,0,0);
+#define OUR_ER laEndRow(uil,b1);
 
     laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.current_brush"));{
         laShowItem(uil,c,0,"our.current_brush.name");
-        laShowItem(uil,c,0,"our.current_brush.size");
-        laShowItem(uil,c,0,"our.current_brush.transparency");
-        laShowItem(uil,c,0,"our.current_brush.hardness");
+        OUR_BR laShowItem(uil,c,0,"our.current_brush.size")->Expand=1; laShowItemFull(uil,c,0,"our.current_brush.pressure_size",0,"text=P",0,0); OUR_ER
+        OUR_BR laShowItem(uil,c,0,"our.current_brush.transparency")->Expand=1;  laShowItemFull(uil,c,0,"our.current_brush.pressure_transparency",0,"text=P",0,0); OUR_ER
+        OUR_BR laShowItem(uil,c,0,"our.current_brush.hardness")->Expand=1;  laShowItemFull(uil,c,0,"our.current_brush.pressure_hardness",0,"text=P",0,0); OUR_ER
+        OUR_BR laShowItem(uil,c,0,"our.current_brush.smudge")->Expand=1; laShowItemFull(uil,c,0,"our.current_brush.pressure_smudge",0,"text=P",0,0); OUR_ER
         laShowItem(uil,c,0,"our.current_brush.dabs_per_size");
+        laShowItem(uil,c,0,"our.current_brush.smudge_resample_length");
     }laEndCondition(uil,b);
 
     laShowLabel(uil,c,"Select a brush:",0,0);
@@ -105,44 +129,6 @@ void ourui_ColorPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps
 }
 
 
-void our_CANVAS_TEST(laBoxedTheme *bt, laUiItem* ui){
-    OurCanvasDraw* ocd=ui->Extra; OurPaint* oc=ui->PP.EndInstance; laCanvasExtra*e=&ocd->Base;
-    int W, H; W = ui->R - ui->L; H = ui->B - ui->U;
-    if(!oc->Content){
-        oc->Content=tnsCreate2DTexture(GL_RGBA8,1024,1024,0);
-    }
-
-    tnsBindTexture(oc->Content);
-    glBindImageTexture(0, oc->Content->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
-
-    glUseProgram(Our->CanvasProgram);
-    
-    for(int i=0;i<100;i++){
-        //glUniform2i(Our->CanvasTaskUniform,i,0);
-        //glDispatchCompute(32, 32, 1);
-        //glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
-    }
-    //for(int i=0;i<32;i++){
-    //    for(int j=0;j<32;j++){
-    //        glUniform2i(Our->CanvasTaskUniform,i,j);
-    //        glDispatchCompute(1, 1, 1);
-    //        glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
-    //    }
-    //}
-
-    tnsEnableShaderv(T->immShader); tnsUseImmShader();
-    
-    tnsDrawToOffscreen(e->OffScr, 1, 0);
-    tnsViewportWithScissor(0, 0, W, H);
-    tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
-    tnsOrtho(e->PanX - W * e->ZoomX / 2, e->PanX + W * e->ZoomX / 2, e->PanY - e->ZoomY * H / 2, e->PanY + e->ZoomY * H / 2, 100, -100);
-
-    glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT);
-
-    real w2=oc->Content->Width/2, h2=oc->Content->Height/2;
-    tnsDraw2DTextureDirectly(oc->Content, -w2, h2, oc->Content->Width, -oc->Content->Height);
-    tnsFlush();
-}
 void our_CanvasDrawTextures(){
     tnsUseImmShader; tnsEnableShaderv(T->immShader); tnsUniformUseTexture(T->immShader,0,0); tnsUseNoTexture();
     for(OurLayer* l=Our->Layers.pLast;l;l=l->Item.pPrev){
@@ -226,13 +212,17 @@ void our_CanvasDrawCanvas(laBoxedTheme *bt, OurPaint *unused_c, laUiItem* ui){
 
     //our_CANVAS_TEST(bt,ui);
 
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+
     tnsDrawToOffscreen(e->OffScr,1,0);
     tnsViewportWithScissor(0, 0, W, H);
     tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
     tnsOrtho(e->PanX - W * e->ZoomX / 2, e->PanX + W * e->ZoomX / 2, e->PanY - e->ZoomY * H / 2, e->PanY + e->ZoomY * H / 2, 100, -100);
-    tnsClearColor(0,0,0,0); tnsClearAll();
-    if(ocd->ShowTiles){ our_CanvasDrawTiles(); }
+    tnsClearColor(LA_COLOR3(Our->BackgroundColor),1); tnsClearAll();
+    //if(ocd->ShowTiles){ our_CanvasDrawTiles(); }
     our_CanvasDrawTextures();
+
+    glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
 }
 
 OurLayer* our_NewLayer(char* name){
@@ -245,9 +235,12 @@ void our_RemoveLayer(OurLayer* l){
     if(Our->CurrentLayer==l){ OurLayer* nl=l->Item.pPrev?l->Item.pPrev:l->Item.pNext; memAssignRef(Our, &Our->CurrentLayer, nl); }
     memLeave(l);
 }
-OurBrush* our_NewBrush(char* name, real Size, real Hardness, real DabsPerSize, real Transparency){
+OurBrush* our_NewBrush(char* name, real Size, real Hardness, real DabsPerSize, real Transparency, real Smudge, real SmudgeResampleLength,
+    int PressureSize, int PressureHardness, int PressureTransparency, int PressureSmudge){
     OurBrush* b=memAcquireHyper(sizeof(OurBrush)); strSafeSet(&b->Name,name); lstAppendItem(&Our->Brushes, b);
-    b->Size=Size; b->Hardness=Hardness; b->DabsPerSize=DabsPerSize; b->Transparency=Transparency;
+    b->Size=Size; b->Hardness=Hardness; b->DabsPerSize=DabsPerSize; b->Transparency=Transparency; b->Smudge=Smudge;
+    b->PressureHardness=PressureHardness; b->PressureSize=PressureSize; b->PressureTransparency=PressureTransparency; b->PressureSmudge=PressureSmudge;
+    b->SmudgeResampleLength = SmudgeResampleLength;
     memAssignRef(Our, &Our->CurrentBrush, b);
     return b;
 }
@@ -279,26 +272,44 @@ void our_UiToCanvas(laCanvasExtra* ex, laEvent*e, real* x, real *y){
     *x = (real)((real)e->x - (real)(ex->ParentUi->R - ex->ParentUi->L) / 2 - ex->ParentUi->L) * ex->ZoomX + ex->PanX;
     *y = (real)((real)(ex->ParentUi->B - ex->ParentUi->U) / 2 - (real)e->y + ex->ParentUi->U) * ex->ZoomY + ex->PanY;
 }
+real our_PaintGetDabStepDistance(OurBrush* b, real pressure){
+    if(!b->PressureSize) return b->Size/b->DabsPerSize;
+    real d=b->Size/b->DabsPerSize*pressure; if(d<1e-2) d=1e-2; return d;
+}
 int our_PaintGetDabs(OurBrush* b, OurLayer* l, real x, real y, real xto, real yto, real last_pressure, real pressure, int *tl, int *tr, int* tu, int* tb){
     Our->NextDab=0;
-    real size=b->Size; real dd=b->Size/b->DabsPerSize; real len=tnsDistIdv2(x,y,xto,yto); real alllen=len+Our->BrushRemainingDist;
-    int count=alllen/dd; real uselen=count*dd; if(!count){ Our->BrushRemainingDist=alllen; return 0; }
-    real rem=Our->BrushRemainingDist;
-    real xmin=FLT_MAX,xmax=FLT_MIN,ymin=FLT_MAX,ymax=FLT_MIN; int starti=0;//Our->BrushRemainingDist<1e-9?0:1;
-    for(int i=starti;i<count;i++){
+    real size=b->Size; real dd=our_PaintGetDabStepDistance(b,last_pressure); real len=tnsDistIdv2(x,y,xto,yto); real rem=b->BrushRemainingDist;
+    real alllen=len+rem; real uselen=dd,step=0; if(!len)return 0; if(dd>alllen){ b->BrushRemainingDist+=len; return 0; }
+    real xmin=FLT_MAX,xmax=FLT_MIN,ymin=FLT_MAX,ymax=FLT_MIN;
+    while(1){
         arrEnsureLength(&Our->Dabs,Our->NextDab,&Our->MaxDab,sizeof(OurDab)); OurDab* od=&Our->Dabs[Our->NextDab];
-        real r=tnsGetRatiod(0,len,i*dd-rem); od->X=tnsInterpolate(x,xto,r); od->Y=tnsInterpolate(y,yto,r);
-        TNS_CLAMP(r,0,1); od->Size = b->Size*tnsInterpolate(last_pressure,pressure,r);
-        od->Hardness = b->Hardness; od->Smudge = b->Smudge;
-        tnsVectorSet3v(od->Color,Our->CurrentColor); od->Color[3]=b->Transparency;
+        real r=tnsGetRatiod(0,len,uselen-rem); od->X=tnsInterpolate(x,xto,r); od->Y=tnsInterpolate(y,yto,r); TNS_CLAMP(r,0,1);
+#define pfac(psw) (psw?tnsInterpolate(last_pressure,pressure,r):1);
+        real sizepfac=pfac(b->PressureSize)
+        od->Size = b->Size*sizepfac;       od->Hardness = b->Hardness*pfac(b->PressureHardness);
+        od->Smudge = b->Smudge*pfac(b->PressureSmudge); od->Color[3]=b->Transparency*pfac(b->PressureTransparency);
+        tnsVectorSet3v(od->Color,Our->CurrentColor);
+#undef pfac;
         xmin=TNS_MIN2(xmin, od->X-od->Size); xmax=TNS_MAX2(xmax, od->X+od->Size); 
         ymin=TNS_MIN2(ymin, od->Y-od->Size); ymax=TNS_MAX2(ymax, od->Y+od->Size);
-        Our->NextDab++;
+        if(od->Size>1e-1) Our->NextDab++;
+        step=our_PaintGetDabStepDistance(b,sizepfac);
+        od->ResampleSmudge=0;
+        if(b->Smudge>1e-3){ b->SmudgeAccum+=step; if(b->SmudgeAccum>(b->SmudgeResampleLength*od->Size)){ b->SmudgeAccum-=(b->SmudgeResampleLength*od->Size); od->ResampleSmudge=1; } }
+        if(step+uselen<alllen)uselen+=step; else break;
     }
-    Our->BrushRemainingDist=alllen-uselen;
+    b->BrushRemainingDist=alllen-uselen;
     if(Our->NextDab) { our_LayerEnsureTiles(l,xmin,xmax,ymin,ymax,tl,tr,tu,tb); return 1; }
     return 0;
 }
+void our_PaintDoSample(int x, int y, int sx, int sy){
+    glUniformSubroutinesuiv(GL_COMPUTE_SHADER,1,&Our->RoutineDoSample);
+    glUniform2i(Our->uBrushCorner,x-sx,y-sy);
+    glUniform2f(Our->uBrushCenter,x-sx,y-sy);
+    glDispatchCompute(1,1,1);
+    glUniformSubroutinesuiv(GL_COMPUTE_SHADER,1,&Our->RoutineDoDabs);
+    glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
+}
 void our_PaintDoDab(OurDab* d, int tl, int tr, int tu, int tb){
     int corner[2]; corner[0]=floorf(d->X-d->Size); corner[1]=floorf(d->Y-d->Size);
     real MaxX,MaxY; MaxX=ceil(d->X+d->Size); MaxY=ceil(d->Y+d->Size);
@@ -309,23 +320,75 @@ void our_PaintDoDab(OurDab* d, int tl, int tr, int tu, int tb){
     glUniform2fv(Our->uBrushCenter,1,center);
     glUniform1f(Our->uBrushSize,d->Size);
     glUniform1f(Our->uBrushHardness,d->Hardness);
+    glUniform1f(Our->uBrushSmudge,d->Smudge);
     glUniform4fv(Our->uBrushColor,1,d->Color);
     glDispatchCompute(ceil(d->Size/16), ceil(d->Size/16), 1);
     glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
 }
-void our_PaintDoDabs(OurLayer* l,int tl, int tr, int tu, int tb){
-    glUseProgram(Our->CanvasProgram);
+void our_PaintDoDabs(OurLayer* l,int tl, int tr, int tu, int tb, int Start, int End){
     for(int row=tb;row<=tu;row++){
         for(int col=tl;col<=tr;col++){
             OurTexTile* ott=l->TexTiles[row][col];
-            tnsBindTexture(ott->Texture); glBindImageTexture(0, ott->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
+            glBindImageTexture(0, ott->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
             int sx=((real)col-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE-OUR_TEX_TILE_SEAM,sy=((real)row-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE-OUR_TEX_TILE_SEAM;
-            for(int i=0;i<Our->NextDab;i++){
+            for(int i=Start;i<End;i++){
                 our_PaintDoDab(&Our->Dabs[i],sx,sx+OUR_TEX_TILE_W,sy,sy+OUR_TEX_TILE_W);
             }
         }
     }
 }
+STRUCTURE(OurSmudgeSegement){
+    laListItem Item;
+    int Start,End;
+};
+void our_PaintDoDabsWithSmudgeSegments(OurLayer* l,int tl, int tr, int tu, int tb){
+    laListHandle Segments={0}; int from=0,to=Our->NextDab; if(!Our->NextDab) return;
+    OurSmudgeSegement* oss;
+    oss=lstAppendPointerSized(&Segments, 0,sizeof(OurSmudgeSegement));
+    for(int i=1;i<to;i++){
+        if(Our->Dabs[i].ResampleSmudge){ oss->Start=from; oss->End=i; from=i; oss=lstAppendPointerSized(&Segments, 0,sizeof(OurSmudgeSegement)); }
+    }
+    oss->Start=from; oss->End=to;
+
+    glUseProgram(Our->CanvasProgram);
+
+    if(!Segments.pFirst){ our_PaintDoDabs(l,tl,tr,tu,tb,0,Our->NextDab); return; }
+
+    while(oss=lstPopItem(&Segments)){
+        float x=Our->Dabs[oss->Start].X, y=Our->Dabs[oss->Start].Y;
+        int col=(int)(floor(OUR_TEX_TILE_CTR+x/OUR_TEX_TILE_W_USE+0.5)); TNS_CLAMP(col,0,OUR_TEX_TILES_PER_ROW-1);
+        int row=(int)(floor(OUR_TEX_TILE_CTR+y/OUR_TEX_TILE_W_USE+0.5)); TNS_CLAMP(row,0,OUR_TEX_TILES_PER_ROW-1);
+        glBindImageTexture(0, l->TexTiles[row][col]->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
+        int sx=((real)col-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE-OUR_TEX_TILE_SEAM,sy=((real)row-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE-OUR_TEX_TILE_SEAM;
+        glBindImageTexture(1, Our->SmudgeTexture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
+        our_PaintDoSample(x,y,sx,sy);
+
+        //printf("from to %d %d %d\n", oss->Start,oss->End,Our->Dabs[oss->Start].ResampleSmudge);
+
+        for(int row=tb;row<=tu;row++){
+            for(int col=tl;col<=tr;col++){
+                OurTexTile* ott=l->TexTiles[row][col];
+                tnsBindTexture(ott->Texture); glBindImageTexture(0, ott->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
+                int sx=((real)col-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE-OUR_TEX_TILE_SEAM,sy=((real)row-OUR_TEX_TILE_CTR-0.5)*OUR_TEX_TILE_W_USE-OUR_TEX_TILE_SEAM;
+                for(int i=oss->Start;i<oss->End;i++){
+                    our_PaintDoDab(&Our->Dabs[i],sx,sx+OUR_TEX_TILE_W,sy,sy+OUR_TEX_TILE_W);
+                }
+            }
+        }
+    }
+}
+
+void our_ReadWidgetColor(laCanvasExtra*e,int x,int y){
+    uint8_t color[4];
+    glBindFramebuffer(GL_READ_FRAMEBUFFER, e->OffScr->FboHandle);
+    glReadBuffer(GL_COLOR_ATTACHMENT0);
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+    glReadPixels(x,y,1,1, GL_RGBA, GL_UNSIGNED_BYTE, color);
+    real a=(real)color[3]/255;
+    Our->CurrentColor[0]=(real)color[0]/255*a;
+    Our->CurrentColor[1]=(real)color[1]/255*a;
+    Our->CurrentColor[2]=(real)color[2]/255*a;
+}
 
 int ourinv_NewLayer(laOperator* a, laEvent* e){
     our_NewLayer("Our Layer"); laNotifyUsers("our.canvas.layers");
@@ -344,7 +407,7 @@ int ourinv_MoveLayer(laOperator* a, laEvent* e){
     return LA_FINISHED;
 }
 int ourinv_NewBrush(laOperator* a, laEvent* e){
-    our_NewBrush("Our Brush",15,0.95,9,0.5); laNotifyUsers("our.brushes");
+    our_NewBrush("Our Brush",15,0.95,9,0.5,0.5,5,0,0,0,0); laNotifyUsers("our.brushes");
     return LA_FINISHED;
 }
 int ourinv_RemoveBrush(laOperator* a, laEvent* e){
@@ -360,7 +423,6 @@ int ourinv_MoveBrush(laOperator* a, laEvent* e){
     return LA_FINISHED;
 }
 
-
 int ourinv_Paint(laOperator* a, laEvent* e){
     OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
     real x,y; our_UiToCanvas(&ex->Base,e,&x,&y); ex->CanvasLastX=x;ex->CanvasLastY=y;ex->LastPressure=e->Pressure;
@@ -375,7 +437,7 @@ int ourmod_Paint(laOperator* a, laEvent* e){
         real x,y; our_UiToCanvas(&ex->Base,e,&x,&y);
         int tl,tr,tu,tb;
         if(our_PaintGetDabs(ob,l,ex->CanvasLastX,ex->CanvasLastY,x,y,ex->LastPressure,e->Pressure,&tl,&tr,&tu,&tb)){
-            our_PaintDoDabs(l,tl,tr,tu,tb);
+            our_PaintDoDabsWithSmudgeSegments(l,tl,tr,tu,tb);
         }
         ex->CanvasLastX=x;ex->CanvasLastY=y;ex->LastPressure=e->Pressure;
         laNotifyUsers("our.canvas");
@@ -383,6 +445,24 @@ int ourmod_Paint(laOperator* a, laEvent* e){
 
     return LA_RUNNING;
 }
+int ourinv_PickColor(laOperator* a, laEvent* e){
+    OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
+    laUiItem* ui=ex->Base.ParentUi;
+    our_ReadWidgetColor(ex, e->x-ui->L, ui->B-e->y); laNotifyUsers("our.current_color");
+    return LA_RUNNING;
+}
+int ourmod_PickColor(laOperator* a, laEvent* e){
+    OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
+    laUiItem* ui=ex->Base.ParentUi;
+
+    if(e->Type==LA_R_MOUSE_UP || e->Type==LA_L_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ return LA_FINISHED; }
+
+    if(e->Type==LA_MOUSEMOVE||e->Type==LA_R_MOUSE_DOWN){
+        our_ReadWidgetColor(ex, e->x-ui->L, ui->B-e->y); laNotifyUsers("our.current_color");
+    }
+
+    return LA_RUNNING;
+}
 
 
 void* ourget_FirstLayer(void* unused, void* unused1){
@@ -402,9 +482,17 @@ void ourset_BrushMove(OurBrush* b, int move){
     if(move<0 && b->Item.pPrev){ lstMoveUp(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
     elif(move>0 && b->Item.pNext){ lstMoveDown(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
 }
+void ourset_BackgroundColor(void* unused, real* arr){
+    memcpy(Our->BackgroundColor, arr, sizeof(real)*3);
+    laNotifyUsers("our.canvas");
+}
+
+#define OUR_ADD_PRESSURE_SWITCH(p)\
+    laAddEnumItemAs(p,"NONE","None","Not using pressure",0,0);\
+    laAddEnumItemAs(p,"ENABLED","Enabled","Using pressure",1,0);
 
 void ourRegisterEverything(){
-    laPropContainer* pc; laKeyMapper* km;
+    laPropContainer* pc; laKeyMapper* km; laProp* p;
 
     laCreateOperatorType("OUR_new_layer","New Layer","Create a new layer",0,0,0,ourinv_NewLayer,0,'+',0);
     laCreateOperatorType("OUR_remove_layer","Remove Layer","Remove this layer",0,0,0,ourinv_RemoveLayer,0,L'🗴',0);
@@ -413,6 +501,7 @@ void ourRegisterEverything(){
     laCreateOperatorType("OUR_remove_brush","Remove Brush","Remove this brush",0,0,0,ourinv_RemoveBrush,0,L'🗴',0);
     laCreateOperatorType("OUR_move_brush","Move Brush","Remove this brush",0,0,0,ourinv_MoveBrush,0,0,0);
     laCreateOperatorType("OUR_paint","Paint","Paint on a layer",0,0,0,ourinv_Paint,ourmod_Paint,0,LA_EXTRA_TO_PANEL);
+    laCreateOperatorType("OUR_pick","Pick color","Pick color on the widget",0,0,0,ourinv_PickColor,ourmod_PickColor,0,LA_EXTRA_TO_PANEL);
 
     laRegisterUiTemplate("panel_canvas", "Canvas", ourui_CanvasPanel, 0, 0,"Our Paint");
     laRegisterUiTemplate("panel_layers", "Layers", ourui_LayersPanel, 0, 0,0);
@@ -427,6 +516,7 @@ void ourRegisterEverything(){
     laAddSubGroup(pc,"brushes","Brushes","Brushes","our_brush",0,0,ourui_Brush,offsetof(OurPaint,CurrentBrush),0,0,0,0,0,0,offsetof(OurPaint,Brushes),0);
     laAddSubGroup(pc,"current_brush","Current Brush","Current brush","our_brush",0,0,0,offsetof(OurPaint,CurrentBrush),ourget_FirstBrush,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
     laAddFloatProperty(pc,"current_color","Current Color","Current color used to paint",0,0,0,1,0,0.05,0.8,0,offsetof(OurPaint,CurrentColor),0,0,4,0,0,0,0,0,0,0,0);
+    laAddFloatProperty(pc,"background_color","Background Color","Background color of the canvas",0,0,0,1,0,0.05,0.8,0,offsetof(OurPaint,BackgroundColor),0,0,3,0,0,0,0,ourset_BackgroundColor,0,0,0);
     
     pc=laAddPropertyContainer("our_brush","Our Brush","OurPaint brush",0,0,sizeof(OurBrush),0,0,2);
     laAddStringProperty(pc,"name","Name","Name of the layer",0,0,0,0,1,offsetof(OurBrush,Name),0,0,0,0,LA_AS_IDENTIFIER);
@@ -434,7 +524,17 @@ void ourRegisterEverything(){
     laAddFloatProperty(pc,"size","Size","Base size(radius) of the brush",0,0,"px",1000,0,1,10,0,offsetof(OurBrush,Size),0,0,0,0,0,0,0,0,0,0,0);
     laAddFloatProperty(pc,"transparency","Transparency","Transparency of a dab",0,0,0,1,0,0.05,0.5,0,offsetof(OurBrush,Transparency),0,0,0,0,0,0,0,0,0,0,0);
     laAddFloatProperty(pc,"hardness","Hardness","Hardness of the brush",0,0,0,1,0,0.05,0.95,0,offsetof(OurBrush,Hardness),0,0,0,0,0,0,0,0,0,0,0);
+    laAddFloatProperty(pc,"smudge","Smudge","Smudge of the brush",0,0,0,1,0,0.05,0.95,0,offsetof(OurBrush,Smudge),0,0,0,0,0,0,0,0,0,0,0);
     laAddFloatProperty(pc,"dabs_per_size","Dabs Per Size","How many dabs per size of the brush",0,0,0,0,0,0,0,0,offsetof(OurBrush,DabsPerSize),0,0,0,0,0,0,0,0,0,0,0);
+    laAddFloatProperty(pc,"smudge_resample_length","Smudge Resample Length","How long of a distance (based on size) should elapse before resampling smudge",0,0,0,0,0,0,0,0,offsetof(OurBrush,SmudgeResampleLength),0,0,0,0,0,0,0,0,0,0,0);
+    p=laAddEnumProperty(pc,"pressure_size","Pressure Size","Use pen pressure to control size",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,PressureSize),0,0,0,0,0,0,0,0,0,0);
+    OUR_ADD_PRESSURE_SWITCH(p);
+    p=laAddEnumProperty(pc,"pressure_transparency","Pressure Transparency","Use pen pressure to control transparency",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,PressureTransparency),0,0,0,0,0,0,0,0,0,0);
+    OUR_ADD_PRESSURE_SWITCH(p);
+    p=laAddEnumProperty(pc,"pressure_hardness","Pressure Hardness","Use pen pressure to control hardness",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,PressureHardness),0,0,0,0,0,0,0,0,0,0);
+    OUR_ADD_PRESSURE_SWITCH(p);
+    p=laAddEnumProperty(pc,"pressure_smudge","Pressure Smudge","Use pen pressure to control smudging",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0,0,offsetof(OurBrush,PressureSmudge),0,0,0,0,0,0,0,0,0,0);
+    OUR_ADD_PRESSURE_SWITCH(p);
     laAddOperatorProperty(pc,"move","Move","Move brush","OUR_move_brush",0,0);
     laAddOperatorProperty(pc,"remove","Remove","Remove brush","OUR_remove_brush",L'🗴',0);
 
@@ -457,6 +557,7 @@ void ourRegisterEverything(){
     laAssignNewKey(km, 0, "LA_2d_view_move", LA_KM_SEL_UI_EXTRA, LA_KEY_ALT, LA_L_MOUSE_DOWN, 0, 0);
     laAssignNewKey(km, 0, "LA_2d_view_move", LA_KM_SEL_UI_EXTRA, 0, LA_M_MOUSE_DOWN, 0, 0);
     laAssignNewKey(km, 0, "OUR_paint", LA_KM_SEL_UI_EXTRA, 0, LA_L_MOUSE_DOWN, 0, 0);
+    laAssignNewKey(km, 0, "OUR_pick", LA_KM_SEL_UI_EXTRA, 0, LA_R_MOUSE_DOWN, 0, 0);
 }
 
 
@@ -467,6 +568,8 @@ void ourInit(){
 
     char error[1024]; int status;
 
+    Our->SmudgeTexture=tnsCreate2DTexture(GL_RGBA,256,1,0);
+
     Our->CanvasShader = glCreateShader(GL_COMPUTE_SHADER);
     const GLchar* source = OUR_CANVAS_SHADER;
     glShaderSource(Our->CanvasShader, 1, &source, NULL);
@@ -496,6 +599,10 @@ void ourInit(){
     Our->uBrushSmudge=glGetUniformLocation(Our->CanvasProgram,"uBrushSmudge");
     Our->uBrushColor=glGetUniformLocation(Our->CanvasProgram,"uBrushColor");
 
+    Our->uBrushRoutineSelection=glGetSubroutineUniformLocation(Our->CanvasProgram, GL_COMPUTE_SHADER, "uBrushRoutineSelection");
+    Our->RoutineDoDabs=glGetSubroutineIndex(Our->CanvasProgram, GL_COMPUTE_SHADER, "DoDabs");
+    Our->RoutineDoSample=glGetSubroutineIndex(Our->CanvasProgram, GL_COMPUTE_SHADER, "DoSample");
+
     tnsEnableShaderv(T->immShader);
 }
 

+ 10 - 3
ourpaint.h

@@ -38,12 +38,16 @@ STRUCTURE(OurBrush){
     real Hardness;
     real Transparency;
     real Smudge;
+    real SmudgeResampleLength; real SmudgeAccum;
+    real BrushRemainingDist;
+    int UseNodes; // the flexible way
+    int PressureSize,PressureHardness,PressureTransparency,PressureSmudge; // the simple way
 };
 STRUCTURE(OurDab){
     float X,Y;
     float Size;
     float Hardness;
-    float Smudge;
+    float Smudge; int ResampleSmudge;
     float Color[4];
 };
 
@@ -55,10 +59,9 @@ STRUCTURE(OurPaint){
 
     laListHandle Brushes;
     OurBrush*    CurrentBrush;
-    real BrushRemainingDist;
     OurDab* Dabs; int NextDab,MaxDab;
 
-    tnsTexture* Content;
+    tnsTexture* SmudgeTexture;
     GLuint CanvasShader;
     GLuint CanvasProgram;
     GLint uBrushCorner;
@@ -67,8 +70,12 @@ STRUCTURE(OurPaint){
     GLint uBrushHardness;
     GLint uBrushSmudge;
     GLint uBrushColor;
+    GLint uBrushRoutineSelection;
+    GLint RoutineDoDabs;
+    GLint RoutineDoSample;
 
     real CurrentColor[4];
+    real BackgroundColor[3];
 };
 
 void ourInit();