*/}}

ouroperations.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. #include "ourpaint.h"
  2. OurPaint *Our;
  3. extern LA MAIN;
  4. extern tnsMain* T;
  5. const char OUR_CANVAS_SHADER[]="#version 430\n\
  6. layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in;\n\
  7. layout(rgba16, binding = 0) uniform image2D img;\n\
  8. layout(rgba16, binding = 1) coherent uniform image2D smudge_buckets;\n\
  9. uniform ivec2 uBrushCorner;\n\
  10. uniform vec2 uBrushCenter;\n\
  11. uniform float uBrushSize;\n\
  12. uniform float uBrushHardness;\n\
  13. uniform float uBrushSmudge;\n\
  14. uniform vec4 uBrushColor;\n\
  15. uniform vec4 uBackgroundColor;\n\
  16. vec4 mix_over(vec4 colora, vec4 colorb){\n\
  17. vec4 c; c.a=colora.a+colorb.a*(1-colora.a);\n\
  18. c.rgb=(colora.rgb+colorb.rgb*(1-colora.a));\n\
  19. return c;\n\
  20. }\n\
  21. int dab(float d, vec4 color, float size, float hardness, float smudge, vec4 smudge_color, vec4 last_color, out vec4 final){\n\
  22. vec4 cc=color;\n\
  23. float fac=1-pow(d/size,1+1/(1-hardness+1e-4));\n\
  24. cc.a=color.a*fac*(1-smudge); cc.rgb=cc.rgb*cc.a;\n\
  25. vec4 c1=mix_over(cc,last_color);\n\
  26. vec4 c2=mix(c1,smudge_color,smudge*fac*color.a);\n\
  27. final=c2;\n\
  28. return 1;\n\
  29. }\n\
  30. subroutine void BrushRoutines();\n\
  31. subroutine(BrushRoutines) void DoDabs(){\n\
  32. ivec2 px = ivec2(gl_GlobalInvocationID.xy)+uBrushCorner;\n\
  33. if(px.x<0||px.y<0||px.x>1024||px.y>1024) return;\n\
  34. float dd=distance(vec2(px),uBrushCenter); if(dd>uBrushSize) return;\n\
  35. vec4 final;\n\
  36. vec4 dabc=imageLoad(img, px);\n\
  37. vec4 smudgec=imageLoad(smudge_buckets,ivec2(0,0));\n\
  38. dab(dd,uBrushColor,uBrushSize,uBrushHardness,uBrushSmudge,smudgec,dabc,final);\n\
  39. dabc=final;\n\
  40. imageStore(img, px, dabc);\n\
  41. }\n\
  42. subroutine(BrushRoutines) void DoSample(){\n\
  43. ivec2 p=ivec2(gl_GlobalInvocationID.xy);\n\
  44. if(p!=ivec2(0,0)) return;\n\
  45. ivec2 px=p+uBrushCorner;\n\
  46. vec4 color=imageLoad(img, px);\n\
  47. imageStore(smudge_buckets,ivec2(0,0),color);\n\
  48. }\n\
  49. subroutine uniform BrushRoutines uBrushRoutineSelection;\n\
  50. \n\
  51. void main() {\n\
  52. uBrushRoutineSelection();\n\
  53. }\n\
  54. ";
  55. void ourui_CanvasPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  56. laColumn* c=laFirstColumn(uil);
  57. laUiItem* ui=laShowCanvas(uil,c,0,"our.canvas",0,-1);
  58. }
  59. void ourui_Layer(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  60. laColumn* c=laFirstColumn(uil); laColumn* cl,*cr; laSplitColumn(uil,c,0.7); cl=laLeftColumn(c,0);cr=laRightColumn(c,1);
  61. laUiItem* b=laBeginRow(uil,cl,0,0);
  62. laShowHeightAdjuster(uil,cl,This,"__move",0);
  63. laShowItemFull(uil,cl,This,"name",LA_WIDGET_STRING_PLAIN,0,0,0)->Expand=1;
  64. laEndRow(uil,b);
  65. laUiItem* b1=laOnConditionToggle(uil,cr,0,0,0,0,0);{ strSafeSet(&b1->ExtraInstructions,"text=☰");
  66. b=laBeginRow(uil,c,0,0);
  67. laShowItem(uil,c,This,"remove")->Flags|=LA_UI_FLAGS_ICON;
  68. laShowSeparator(uil,c)->Expand=1;
  69. laShowItemFull(uil,c,This,"move",0,"direction=up;icon=🡱;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  70. laShowItemFull(uil,c,This,"move",0,"direction=down;icon=🡳;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  71. laEndRow(uil,b);
  72. }laEndCondition(uil,b1);
  73. }
  74. void ourui_LayersPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  75. laColumn* c=laFirstColumn(uil);
  76. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.canvas.current_layer"));{
  77. laUiItem* b1=laBeginRow(uil,c,0,0);
  78. laShowItem(uil,c,0,"our.canvas.current_layer.name")->Expand=1;
  79. laShowItem(uil,c,0,"OUR_new_layer")->Flags|=LA_UI_FLAGS_ICON;
  80. laEndRow(uil,b1);
  81. }laElse(uil,b);{
  82. laShowItem(uil,c,0,"OUR_new_layer");
  83. }laEndCondition(uil,b);
  84. laShowItemFull(uil,c,0,"our.canvas.layers",0,0,0,0);
  85. b=laBeginRow(uil,c,0,0);
  86. laShowLabel(uil,c,"Background",0,0)->Expand=1;
  87. laShowItemFull(uil,c,0,"our.background_color",LA_WIDGET_FLOAT_COLOR,0,0,0);
  88. laEndRow(uil,b);
  89. }
  90. void ourui_Brush(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  91. laColumn* c=laFirstColumn(uil); laColumn* cl,*cr; laSplitColumn(uil,c,0.7); cl=laLeftColumn(c,0);cr=laRightColumn(c,1);
  92. laUiItem* b=laBeginRow(uil,cl,0,0);
  93. laShowHeightAdjuster(uil,cl,This,"__move",0);
  94. laShowItemFull(uil,cl,This,"name",LA_WIDGET_STRING_PLAIN,0,0,0)->Expand=1;
  95. laEndRow(uil,b);
  96. laUiItem* b1=laOnConditionToggle(uil,cr,0,0,0,0,0);{ strSafeSet(&b1->ExtraInstructions,"text=☰");
  97. b=laBeginRow(uil,c,0,0);
  98. laShowItem(uil,c,This,"remove")->Flags|=LA_UI_FLAGS_ICON;
  99. laShowSeparator(uil,c)->Expand=1;
  100. laShowItemFull(uil,c,This,"move",0,"direction=up;icon=🡱;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  101. laShowItemFull(uil,c,This,"move",0,"direction=down;icon=🡳;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  102. laEndRow(uil,b);
  103. }laEndCondition(uil,b1);
  104. }
  105. void ourui_ToolsPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  106. laColumn* c=laFirstColumn(uil); laColumn* cl,*cr; laSplitColumn(uil,c,0.5); cl=laLeftColumn(c,0);cr=laRightColumn(c,0);
  107. laUiItem* b1;
  108. #define OUR_BR b1=laBeginRow(uil,c,0,0);
  109. #define OUR_ER laEndRow(uil,b1);
  110. laShowItem(uil,c,0,"our.tool")->Flags|=LA_UI_FLAGS_EXPAND;
  111. laUiItem* bt=laOnConditionThat(uil,c,laEqual(laPropExpression(0,"our.tool"),laIntExpression(OUR_TOOL_PAINT)));{
  112. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.current_brush"));{
  113. laShowItem(uil,c,0,"our.current_brush.name");
  114. 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
  115. 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
  116. 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
  117. 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
  118. laShowItem(uil,c,0,"our.current_brush.dabs_per_size");
  119. laShowItem(uil,c,0,"our.current_brush.smudge_resample_length");
  120. }laEndCondition(uil,b);
  121. laShowLabel(uil,c,"Select a brush:",0,0);
  122. laShowItemFull(uil,c,0,"our.brushes",0,0,0,0);
  123. laShowItem(uil,c,0,"OUR_new_brush");
  124. }laEndCondition(uil,bt);
  125. bt=laOnConditionThat(uil,c,laEqual(laPropExpression(0,"our.tool"),laIntExpression(OUR_TOOL_CROP)));{
  126. laShowItemFull(uil,c,0,"our.show_border",LA_WIDGET_ENUM_HIGHLIGHT,0,0,0);
  127. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.show_border"));{
  128. laShowLabel(uil,cl,"Position:",0,0); laShowItem(uil,cr,0,"our.canvas.position")->Flags|=LA_UI_FLAGS_TRANSPOSE;
  129. laShowSeparator(uil,c);
  130. laShowLabel(uil,cl,"Size:",0,0); laShowItem(uil,cr,0,"our.canvas.size")->Flags|=LA_UI_FLAGS_TRANSPOSE;
  131. laShowSeparator(uil,c);
  132. laShowItem(uil,c,0,"our.border_alpha");
  133. }laEndCondition(uil,b);
  134. }laEndCondition(uil,bt);
  135. }
  136. void ourui_ColorPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  137. laColumn* c=laFirstColumn(uil);
  138. laShowItemFull(uil,c,0,"our.current_color",LA_WIDGET_FLOAT_COLOR_HCY,0,0,0);
  139. }
  140. void our_CanvasDrawTextures(){
  141. tnsUseImmShader; tnsEnableShaderv(T->immShader);
  142. for(OurLayer* l=Our->Layers.pLast;l;l=l->Item.pPrev){
  143. int any=0;
  144. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){
  145. if(!l->TexTiles[row]) continue;
  146. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){
  147. if(!l->TexTiles[row][col]) continue;
  148. 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;
  149. real pad=(real)OUR_TEX_TILE_SEAM/OUR_TEX_TILE_W; int seam=OUR_TEX_TILE_SEAM;
  150. tnsDraw2DTextureArg(l->TexTiles[row][col]->Texture,sx+seam,sy+OUR_TEX_TILE_W-seam,OUR_TEX_TILE_W-seam*2,-OUR_TEX_TILE_W+seam*2,0,pad,pad,pad,pad);
  151. any=1;
  152. }
  153. }
  154. if(any) tnsFlush();
  155. }
  156. }
  157. void our_CanvasDrawTiles(){
  158. OurLayer* l=Our->CurrentLayer; if(!l) return;
  159. tnsUseImmShader; tnsEnableShaderv(T->immShader); tnsUniformUseTexture(T->immShader,0,0); tnsUseNoTexture();
  160. int any=0;
  161. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){
  162. if(!l->TexTiles[row]) continue;
  163. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){
  164. if(!l->TexTiles[row][col]) continue;
  165. 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;
  166. //tnsVertex2d(sx, sy); tnsVertex2d(sx+OUR_TEX_TILE_W,sy);
  167. //tnsVertex2d(sx+OUR_TEX_TILE_W, sy+OUR_TEX_TILE_W); tnsVertex2d(sx,sy+OUR_TEX_TILE_W);
  168. //tnsColor4dv(laAccentColor(LA_BT_NORMAL));
  169. //tnsPackAs(GL_TRIANGLE_FAN);
  170. tnsVertex2d(sx, sy); tnsVertex2d(sx+OUR_TEX_TILE_W,sy);
  171. tnsVertex2d(sx+OUR_TEX_TILE_W, sy+OUR_TEX_TILE_W); tnsVertex2d(sx,sy+OUR_TEX_TILE_W);
  172. tnsColor4dv(laAccentColor(LA_BT_TEXT));
  173. tnsPackAs(GL_LINE_LOOP);
  174. }
  175. }
  176. if(any) tnsFlush();
  177. }
  178. void our_CanvasDrawCropping(OurCanvasDraw* ocd){
  179. tnsUseImmShader; tnsEnableShaderv(T->immShader); tnsUniformUseTexture(T->immShader,0,0); tnsUseNoTexture();
  180. tnsColor4d(0,0,0,Our->BorderAlpha);
  181. tnsVertex2d(-1e6,Our->Y); tnsVertex2d(1e6,Our->Y); tnsVertex2d(-1e6,1e6); tnsVertex2d(1e6,1e6); tnsPackAs(GL_TRIANGLE_FAN);
  182. tnsVertex2d(-1e6,Our->Y); tnsVertex2d(Our->X,Our->Y); tnsVertex2d(Our->X,Our->Y-Our->H); tnsVertex2d(-1e6,Our->Y-Our->H); tnsPackAs(GL_TRIANGLE_FAN);
  183. tnsVertex2d(1e6,Our->Y); tnsVertex2d(Our->X+Our->W,Our->Y); tnsVertex2d(Our->X+Our->W,Our->Y-Our->H); tnsVertex2d(1e6,Our->Y-Our->H); tnsPackAs(GL_TRIANGLE_FAN);
  184. tnsVertex2d(-1e6,Our->Y-Our->H); tnsVertex2d(1e6,Our->Y-Our->H); tnsVertex2d(-1e6,-1e6); tnsVertex2d(1e6,-1e6); tnsPackAs(GL_TRIANGLE_FAN);
  185. if(Our->Tool==OUR_TOOL_CROP){
  186. tnsColor4dv(laAccentColor(LA_BT_TEXT));
  187. tnsVertex2d(Our->X,Our->Y); tnsVertex2d(Our->X+Our->W,Our->Y); tnsVertex2d(Our->X+Our->W,Our->Y-Our->H); tnsVertex2d(Our->X,Our->Y-Our->H);
  188. tnsPackAs(GL_LINE_LOOP);
  189. glLineWidth(3); tnsFlush(); glLineWidth(1);
  190. }
  191. }
  192. void our_CanvasDrawInit(laUiItem* ui){
  193. ui->Extra=memAcquireHyper(sizeof(OurCanvasDraw));
  194. laFirstColumn(laAddTabPage(ui, "New Group"));
  195. OurCanvasDraw* ocd=ui->Extra;
  196. ocd->Base.ParentUi=ui;
  197. ocd->Base.HeightCoeff = 10;
  198. ocd->Base.ZoomX = 1;
  199. ocd->Base.ZoomY = 1;
  200. ocd->Base.ImageDrawAlpha = 1;
  201. ocd->Base.ImageDrawBorder = 1;
  202. ocd->Base.AdaptiveLineWidth = 1;
  203. ocd->Base.ClearBackground = 1;
  204. ocd->ShowTiles=1;
  205. int work_grp_cnt[3];
  206. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 0, &work_grp_cnt[0]);
  207. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 1, &work_grp_cnt[1]);
  208. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 2, &work_grp_cnt[2]);
  209. printf("max global (total) work group counts x:%i y:%i z:%i\n", work_grp_cnt[0], work_grp_cnt[1], work_grp_cnt[2]);
  210. int work_grp_size[3];
  211. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &work_grp_size[0]);
  212. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1, &work_grp_size[1]);
  213. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2, &work_grp_size[2]);
  214. printf("max local (in one shader) work group sizes x:%i y:%i z:%i\n", work_grp_size[0], work_grp_size[1], work_grp_size[2]);
  215. int work_grp_inv;
  216. glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &work_grp_inv);
  217. printf("max local work group invocations %i\n", work_grp_inv);
  218. }
  219. void our_CanvasDrawCanvas(laBoxedTheme *bt, OurPaint *unused_c, laUiItem* ui){
  220. OurCanvasDraw* ocd=ui->Extra; OurPaint* oc=ui->PP.EndInstance; laCanvasExtra*e=&ocd->Base;
  221. int W, H; W = ui->R - ui->L; H = ui->B - ui->U;
  222. tnsFlush();
  223. if (!e->OffScr || e->OffScr->pColor[0]->Height != ui->B - ui->U || e->OffScr->pColor[0]->Width != ui->R - ui->L){
  224. if (e->OffScr) tnsDelete2DOffscreen(e->OffScr);
  225. e->OffScr = tnsCreate2DOffscreen(GL_RGBA, W, H, 0, 0);
  226. }
  227. //our_CANVAS_TEST(bt,ui);
  228. glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
  229. //glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,GL_ONE,GL_ONE);
  230. tnsDrawToOffscreen(e->OffScr,1,0);
  231. tnsViewportWithScissor(0, 0, W, H);
  232. tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  233. 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);
  234. tnsClearColor(LA_COLOR3(Our->BackgroundColor),1); tnsClearAll();
  235. if(ocd->ShowTiles){ our_CanvasDrawTiles(); }
  236. our_CanvasDrawTextures();
  237. if(Our->ShowBorder){ our_CanvasDrawCropping(ocd); }
  238. glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,GL_ONE,GL_ONE_MINUS_SRC_ALPHA);
  239. }
  240. OurLayer* our_NewLayer(char* name){
  241. OurLayer* l=memAcquire(sizeof(OurLayer)); strSafeSet(&l->Name,name); lstPushItem(&Our->Layers, l);
  242. memAssignRef(Our, &Our->CurrentLayer, l);
  243. return l;
  244. }
  245. void our_RemoveLayer(OurLayer* l){
  246. strSafeDestroy(&l->Name); lstRemoveItem(&Our->Layers, l);
  247. if(Our->CurrentLayer==l){ OurLayer* nl=l->Item.pPrev?l->Item.pPrev:l->Item.pNext; memAssignRef(Our, &Our->CurrentLayer, nl); }
  248. memLeave(l);
  249. }
  250. OurBrush* our_NewBrush(char* name, real Size, real Hardness, real DabsPerSize, real Transparency, real Smudge, real SmudgeResampleLength,
  251. int PressureSize, int PressureHardness, int PressureTransparency, int PressureSmudge){
  252. OurBrush* b=memAcquireHyper(sizeof(OurBrush)); strSafeSet(&b->Name,name); lstAppendItem(&Our->Brushes, b);
  253. b->Size=Size; b->Hardness=Hardness; b->DabsPerSize=DabsPerSize; b->Transparency=Transparency; b->Smudge=Smudge;
  254. b->PressureHardness=PressureHardness; b->PressureSize=PressureSize; b->PressureTransparency=PressureTransparency; b->PressureSmudge=PressureSmudge;
  255. b->SmudgeResampleLength = SmudgeResampleLength;
  256. memAssignRef(Our, &Our->CurrentBrush, b);
  257. return b;
  258. }
  259. void our_RemoveBrush(OurBrush* b){
  260. strSafeDestroy(&b->Name); lstRemoveItem(&Our->Brushes, b);
  261. if(Our->CurrentLayer==b){ OurLayer* nb=b->Item.pPrev?b->Item.pPrev:b->Item.pNext; memAssignRef(Our, &Our->CurrentBrush, nb); }
  262. memLeave(b);
  263. }
  264. void our_LayerEnsureTiles(OurLayer* ol, real xmin,real xmax, real ymin,real ymax, int *tl, int *tr, int* tu, int* tb){
  265. int l=(int)(floor(OUR_TEX_TILE_CTR+(xmin-OUR_TEX_TILE_SEAM)/OUR_TEX_TILE_W_USE+0.5)); TNS_CLAMP(l,0,OUR_TEX_TILES_PER_ROW-1);
  266. int r=(int)(floor(OUR_TEX_TILE_CTR+(xmax+OUR_TEX_TILE_SEAM)/OUR_TEX_TILE_W_USE+0.5)); TNS_CLAMP(r,0,OUR_TEX_TILES_PER_ROW-1);
  267. int u=(int)(floor(OUR_TEX_TILE_CTR+(ymax+OUR_TEX_TILE_SEAM)/OUR_TEX_TILE_W_USE+0.5)); TNS_CLAMP(u,0,OUR_TEX_TILES_PER_ROW-1);
  268. int b=(int)(floor(OUR_TEX_TILE_CTR+(ymin-OUR_TEX_TILE_SEAM)/OUR_TEX_TILE_W_USE+0.5)); TNS_CLAMP(b,0,OUR_TEX_TILES_PER_ROW-1);
  269. for(int row=b;row<=u;row++){
  270. if(!ol->TexTiles[row]){ol->TexTiles[row]=memAcquireSimple(sizeof(OurTexTile*)*OUR_TEX_TILES_PER_ROW);}
  271. for(int col=l;col<=r;col++){
  272. if(ol->TexTiles[row][col]) continue;
  273. ol->TexTiles[row][col]=memAcquireSimple(sizeof(OurTexTile));
  274. ol->TexTiles[row][col]->Texture=tnsCreate2DTexture(GL_RGBA16,OUR_TEX_TILE_W,OUR_TEX_TILE_W,0);
  275. uint16_t initColor[]={0,0,0,0};
  276. glClearTexImage(ol->TexTiles[row][col]->Texture->GLTexHandle, 0, GL_RGBA, GL_UNSIGNED_SHORT, 0);
  277. }
  278. }
  279. *tl=l; *tr=r; *tu=u; *tb=b;
  280. }
  281. void our_UiToCanvas(laCanvasExtra* ex, laEvent*e, real* x, real *y){
  282. *x = (real)((real)e->x - (real)(ex->ParentUi->R - ex->ParentUi->L) / 2 - ex->ParentUi->L) * ex->ZoomX + ex->PanX;
  283. *y = (real)((real)(ex->ParentUi->B - ex->ParentUi->U) / 2 - (real)e->y + ex->ParentUi->U) * ex->ZoomY + ex->PanY;
  284. }
  285. void our_PaintResetBrushState(OurBrush* b){
  286. b->BrushRemainingDist = 0; b->SmudgeAccum=0; b->SmudgeRestart=1;
  287. }
  288. real our_PaintGetDabStepDistance(OurBrush* b, real pressure){
  289. if(!b->PressureSize) return b->Size/b->DabsPerSize;
  290. real d=b->Size/b->DabsPerSize*pressure; if(d<1e-2) d=1e-2; return d;
  291. }
  292. 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){
  293. Our->NextDab=0;
  294. real size=b->Size; real dd=our_PaintGetDabStepDistance(b,last_pressure); real len=tnsDistIdv2(x,y,xto,yto); real rem=b->BrushRemainingDist;
  295. real alllen=len+rem; real uselen=dd,step=0; if(!len)return 0; if(dd>alllen){ b->BrushRemainingDist+=len; return 0; }
  296. real xmin=FLT_MAX,xmax=-FLT_MAX,ymin=FLT_MAX,ymax=-FLT_MAX;
  297. while(1){
  298. arrEnsureLength(&Our->Dabs,Our->NextDab,&Our->MaxDab,sizeof(OurDab)); OurDab* od=&Our->Dabs[Our->NextDab];
  299. real r=tnsGetRatiod(0,len,uselen-rem); od->X=tnsInterpolate(x,xto,r); od->Y=tnsInterpolate(y,yto,r); TNS_CLAMP(r,0,1);
  300. #define pfac(psw) (psw?tnsInterpolate(last_pressure,pressure,r):1);
  301. real sizepfac=pfac(b->PressureSize)
  302. od->Size = b->Size*sizepfac; od->Hardness = b->Hardness*pfac(b->PressureHardness);
  303. od->Smudge = b->Smudge*pfac(b->PressureSmudge); od->Color[3]=b->Transparency*pfac(b->PressureTransparency);
  304. tnsVectorSet3v(od->Color,Our->CurrentColor);
  305. #undef pfac;
  306. xmin=TNS_MIN2(xmin, od->X-od->Size); xmax=TNS_MAX2(xmax, od->X+od->Size);
  307. ymin=TNS_MIN2(ymin, od->Y-od->Size); ymax=TNS_MAX2(ymax, od->Y+od->Size);
  308. if(od->Size>1e-1) Our->NextDab++;
  309. step=our_PaintGetDabStepDistance(b,sizepfac);
  310. od->ResampleSmudge=0;
  311. if(b->Smudge>1e-3){ b->SmudgeAccum+=step; if(b->SmudgeAccum>(b->SmudgeResampleLength*od->Size)){ b->SmudgeAccum-=(b->SmudgeResampleLength*od->Size); od->ResampleSmudge=1; } }
  312. if(step+uselen<alllen)uselen+=step; else break;
  313. }
  314. b->BrushRemainingDist=alllen-uselen;
  315. if(Our->NextDab) { our_LayerEnsureTiles(l,xmin,xmax,ymin,ymax,tl,tr,tu,tb); return 1; }
  316. return 0;
  317. }
  318. void our_PaintDoSample(int x, int y, int sx, int sy){
  319. glUniformSubroutinesuiv(GL_COMPUTE_SHADER,1,&Our->RoutineDoSample);
  320. glUniform2i(Our->uBrushCorner,x-sx,y-sy);
  321. glUniform2f(Our->uBrushCenter,x-sx,y-sy);
  322. glDispatchCompute(1,1,1);
  323. glUniformSubroutinesuiv(GL_COMPUTE_SHADER,1,&Our->RoutineDoDabs);
  324. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
  325. }
  326. void our_PaintDoDab(OurDab* d, int tl, int tr, int tu, int tb){
  327. int corner[2]; corner[0]=floorf(d->X-d->Size); corner[1]=floorf(d->Y-d->Size);
  328. real MaxX,MaxY; MaxX=ceil(d->X+d->Size); MaxY=ceil(d->Y+d->Size);
  329. if(corner[0]>tr||MaxX<tl||corner[1]>tb||MaxY<tu) return;
  330. corner[0]=corner[0]-tl; corner[1]=corner[1]-tu;
  331. float center[2]; center[0]=d->X-tl; center[1]=d->Y-tu;
  332. glUniform2iv(Our->uBrushCorner,1,corner);
  333. glUniform2fv(Our->uBrushCenter,1,center);
  334. glUniform1f(Our->uBrushSize,d->Size);
  335. glUniform1f(Our->uBrushHardness,d->Hardness);
  336. glUniform1f(Our->uBrushSmudge,d->Smudge);
  337. glUniform4fv(Our->uBrushColor,1,d->Color);
  338. glDispatchCompute(ceil(d->Size/16), ceil(d->Size/16), 1);
  339. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
  340. }
  341. void our_PaintDoDabs(OurLayer* l,int tl, int tr, int tu, int tb, int Start, int End){
  342. for(int row=tb;row<=tu;row++){
  343. for(int col=tl;col<=tr;col++){
  344. OurTexTile* ott=l->TexTiles[row][col];
  345. glBindImageTexture(0, ott->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA16);
  346. 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;
  347. for(int i=Start;i<End;i++){
  348. our_PaintDoDab(&Our->Dabs[i],sx,sx+OUR_TEX_TILE_W,sy,sy+OUR_TEX_TILE_W);
  349. }
  350. }
  351. }
  352. }
  353. STRUCTURE(OurSmudgeSegement){
  354. laListItem Item;
  355. int Start,End,Resample;
  356. };
  357. void our_PaintDoDabsWithSmudgeSegments(OurLayer* l,int tl, int tr, int tu, int tb){
  358. laListHandle Segments={0}; int from=0,to=Our->NextDab; if(!Our->NextDab) return;
  359. OurSmudgeSegement* oss;
  360. oss=lstAppendPointerSized(&Segments, 0,sizeof(OurSmudgeSegement));
  361. for(int i=1;i<to;i++){
  362. if(Our->Dabs[i].ResampleSmudge){ oss->Resample=1;
  363. oss->Start=from; oss->End=i; from=i; oss=lstAppendPointerSized(&Segments, 0,sizeof(OurSmudgeSegement)); }
  364. }
  365. oss->Start=from; oss->End=to;
  366. if(Our->Dabs[0].ResampleSmudge){ ((OurSmudgeSegement*)Segments.pFirst)->Resample=1; }
  367. glUseProgram(Our->CanvasProgram);
  368. while(oss=lstPopItem(&Segments)){
  369. if(oss->Resample || Our->CurrentBrush->SmudgeRestart){
  370. float x=Our->Dabs[oss->Start].X, y=Our->Dabs[oss->Start].Y;
  371. 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);
  372. 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);
  373. glBindImageTexture(0, l->TexTiles[row][col]->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA16);
  374. 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;
  375. glBindImageTexture(1, Our->SmudgeTexture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA16);
  376. our_PaintDoSample(x,y,sx,sy);
  377. Our->CurrentBrush->SmudgeRestart=0;
  378. }
  379. //printf("from to %d %d %d\n", oss->Start,oss->End,Our->Dabs[oss->Start].ResampleSmudge);
  380. our_PaintDoDabs(l,tl,tr,tu,tb,oss->Start,oss->End);
  381. }
  382. }
  383. void our_ReadWidgetColor(laCanvasExtra*e,int x,int y){
  384. uint8_t color[4];
  385. glBindFramebuffer(GL_READ_FRAMEBUFFER, e->OffScr->FboHandle);
  386. glReadBuffer(GL_COLOR_ATTACHMENT0);
  387. glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  388. glReadPixels(x,y,1,1, GL_RGBA, GL_UNSIGNED_BYTE, color);
  389. real a=(real)color[3]/255;
  390. Our->CurrentColor[0]=(real)color[0]/255*a;
  391. Our->CurrentColor[1]=(real)color[1]/255*a;
  392. Our->CurrentColor[2]=(real)color[2]/255*a;
  393. }
  394. void our_StartCropping(OurCanvasDraw* cd){
  395. if(cd->CanvasDownX<Our->X){
  396. if(cd->CanvasDownY<Our->Y-Our->H){ cd->AtCrop=OUR_AT_CROP_BL; }
  397. elif(cd->CanvasDownY>=Our->Y-Our->H&&cd->CanvasDownY<=Our->Y){ cd->AtCrop=OUR_AT_CROP_L; }
  398. elif(cd->CanvasDownY>Our->Y){ cd->AtCrop=OUR_AT_CROP_UL; }
  399. }elif(cd->CanvasDownX>=Our->X&&cd->CanvasDownX<=Our->X+Our->W){
  400. if(cd->CanvasDownY<Our->Y-Our->H){ cd->AtCrop=OUR_AT_CROP_B; }
  401. elif(cd->CanvasDownY>=Our->Y-Our->H&&cd->CanvasDownY<=Our->Y){ cd->AtCrop=OUR_AT_CROP_CENTER; }
  402. elif(cd->CanvasDownY>Our->Y){ cd->AtCrop=OUR_AT_CROP_U; }
  403. }elif(cd->CanvasDownX>Our->X+Our->W){
  404. if(cd->CanvasDownY<Our->Y-Our->H){ cd->AtCrop=OUR_AT_CROP_BR; }
  405. elif(cd->CanvasDownY>=Our->Y-Our->H&&cd->CanvasDownY<=Our->Y){ cd->AtCrop=OUR_AT_CROP_R; }
  406. elif(cd->CanvasDownY>Our->Y){ cd->AtCrop=OUR_AT_CROP_UR; }
  407. }
  408. }
  409. void our_DoCropping(OurCanvasDraw* cd, real x, real y){
  410. int dx=x-cd->CanvasLastX, dy=y-cd->CanvasLastY;
  411. if(cd->AtCrop==OUR_AT_CROP_B||cd->AtCrop==OUR_AT_CROP_BL||cd->AtCrop==OUR_AT_CROP_BR){ Our->H-=dy; }
  412. if(cd->AtCrop==OUR_AT_CROP_U||cd->AtCrop==OUR_AT_CROP_UL||cd->AtCrop==OUR_AT_CROP_UR){ Our->Y+=dy; Our->H+=dy; }
  413. if(cd->AtCrop==OUR_AT_CROP_L||cd->AtCrop==OUR_AT_CROP_BL||cd->AtCrop==OUR_AT_CROP_UL){ Our->X+=dx; Our->W-=dx; }
  414. if(cd->AtCrop==OUR_AT_CROP_R||cd->AtCrop==OUR_AT_CROP_BR||cd->AtCrop==OUR_AT_CROP_UR){ Our->W+=dx; }
  415. if(cd->AtCrop==OUR_AT_CROP_CENTER){ Our->Y+=dy; Our->X+=dx; }
  416. if(Our->W<32) Our->W=32; if(Our->H<32) Our->H=32;
  417. cd->CanvasLastX+=dx; cd->CanvasLastY+=dy;
  418. }
  419. int ourinv_NewLayer(laOperator* a, laEvent* e){
  420. our_NewLayer("Our Layer"); laNotifyUsers("our.canvas.layers");
  421. return LA_FINISHED;
  422. }
  423. int ourinv_RemoveLayer(laOperator* a, laEvent* e){
  424. OurLayer* l=a->This?a->This->EndInstance:0; if(!l) return LA_CANCELED;
  425. our_RemoveLayer(l); laNotifyUsers("our.canvas.layers");
  426. return LA_FINISHED;
  427. }
  428. int ourinv_MoveLayer(laOperator* a, laEvent* e){
  429. OurLayer* l=a->This?a->This->EndInstance:0; if(!l) return LA_CANCELED;
  430. char* direction=strGetArgumentString(a->ExtraInstructionsP,"direction");
  431. if(strSame(direction,"up")&&l->Item.pPrev){ lstMoveUp(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  432. elif(l->Item.pNext){ lstMoveDown(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  433. return LA_FINISHED;
  434. }
  435. int ourinv_NewBrush(laOperator* a, laEvent* e){
  436. our_NewBrush("Our Brush",15,0.95,9,0.5,0.5,5,0,0,0,0); laNotifyUsers("our.brushes");
  437. return LA_FINISHED;
  438. }
  439. int ourinv_RemoveBrush(laOperator* a, laEvent* e){
  440. OurBrush* b=a->This?a->This->EndInstance:0; if(!b) return LA_CANCELED;
  441. our_RemoveLayer(b); laNotifyUsers("our.brushes");
  442. return LA_FINISHED;
  443. }
  444. int ourinv_MoveBrush(laOperator* a, laEvent* e){
  445. OurBrush* b=a->This?a->This->EndInstance:0; if(!b) return LA_CANCELED;
  446. char* direction=strGetArgumentString(a->ExtraInstructionsP,"direction");
  447. if(strSame(direction,"up")&&b->Item.pPrev){ lstMoveUp(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
  448. elif(b->Item.pNext){ lstMoveDown(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
  449. return LA_FINISHED;
  450. }
  451. int ourinv_Action(laOperator* a, laEvent* e){
  452. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  453. our_PaintResetBrushState(ob);
  454. real x,y; our_UiToCanvas(&ex->Base,e,&x,&y); ex->CanvasLastX=x;ex->CanvasLastY=y;ex->LastPressure=e->Pressure;
  455. ex->CanvasDownX=x; ex->CanvasDownY=y;
  456. Our->ActiveTool=Our->Tool;
  457. if(Our->ActiveTool==OUR_TOOL_CROP){ if(!Our->ShowBorder) return LA_FINISHED; our_StartCropping(ex); }
  458. return LA_RUNNING;
  459. }
  460. int ourmod_Paint(laOperator* a, laEvent* e){
  461. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  462. if(e->Type==LA_L_MOUSE_UP || e->Type==LA_R_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ return LA_FINISHED; }
  463. if(e->Type==LA_MOUSEMOVE||e->Type==LA_L_MOUSE_DOWN){
  464. real x,y; our_UiToCanvas(&ex->Base,e,&x,&y);
  465. int tl,tr,tu,tb;
  466. if(our_PaintGetDabs(ob,l,ex->CanvasLastX,ex->CanvasLastY,x,y,ex->LastPressure,e->Pressure,&tl,&tr,&tu,&tb)){
  467. our_PaintDoDabsWithSmudgeSegments(l,tl,tr,tu,tb);
  468. }
  469. ex->CanvasLastX=x;ex->CanvasLastY=y;ex->LastPressure=e->Pressure;
  470. laNotifyUsers("our.canvas");
  471. }
  472. return LA_RUNNING;
  473. }
  474. int ourmod_Crop(laOperator* a, laEvent* e){
  475. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  476. if(e->Type==LA_L_MOUSE_UP || e->Type==LA_R_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ return LA_FINISHED; }
  477. if(e->Type==LA_MOUSEMOVE||e->Type==LA_L_MOUSE_DOWN){
  478. real x,y; our_UiToCanvas(&ex->Base,e,&x,&y);
  479. our_DoCropping(ex,x,y);
  480. laNotifyUsers("our.canvas");
  481. }
  482. return LA_RUNNING;
  483. }
  484. int ourmod_Action(laOperator* a, laEvent* e){
  485. OurCanvasDraw *ex = a->This?a->This->EndInstance:0; if(!ex) return LA_CANCELED;
  486. switch(Our->ActiveTool){
  487. case OUR_TOOL_PAINT: OurLayer* l=Our->CurrentLayer; OurBrush* ob=Our->CurrentBrush; if(!l||!ob) return LA_CANCELED;
  488. return ourmod_Paint(a,e);
  489. case OUR_TOOL_CROP:
  490. return ourmod_Crop(a,e);
  491. default: return LA_FINISHED;
  492. }
  493. return LA_RUNNING;
  494. }
  495. int ourinv_PickColor(laOperator* a, laEvent* e){
  496. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  497. laUiItem* ui=ex->Base.ParentUi;
  498. our_ReadWidgetColor(ex, e->x-ui->L, ui->B-e->y); laNotifyUsers("our.current_color");
  499. return LA_RUNNING;
  500. }
  501. int ourmod_PickColor(laOperator* a, laEvent* e){
  502. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  503. laUiItem* ui=ex->Base.ParentUi;
  504. if(e->Type==LA_R_MOUSE_UP || e->Type==LA_L_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ return LA_FINISHED; }
  505. if(e->Type==LA_MOUSEMOVE||e->Type==LA_R_MOUSE_DOWN){
  506. our_ReadWidgetColor(ex, e->x-ui->L, ui->B-e->y); laNotifyUsers("our.current_color");
  507. }
  508. return LA_RUNNING;
  509. }
  510. void* ourget_FirstLayer(void* unused, void* unused1){
  511. return Our->Layers.pFirst;
  512. }
  513. void* ourget_FirstBrush(void* unused, void* unused1){
  514. return Our->Brushes.pFirst;
  515. }
  516. void* ourget_our(void* unused, void* unused1){
  517. return Our;
  518. }
  519. void ourset_LayerMove(OurLayer* l, int move){
  520. if(move<0 && l->Item.pPrev){ lstMoveUp(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  521. elif(move>0 && l->Item.pNext){ lstMoveDown(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  522. }
  523. void ourset_BrushMove(OurBrush* b, int move){
  524. if(move<0 && b->Item.pPrev){ lstMoveUp(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
  525. elif(move>0 && b->Item.pNext){ lstMoveDown(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
  526. }
  527. void ourset_BackgroundColor(void* unused, real* arr){
  528. memcpy(Our->BackgroundColor, arr, sizeof(real)*3); laNotifyUsers("our.canvas");
  529. }
  530. void ourset_BorderAlpha(void* unused, real a){
  531. Our->BorderAlpha=a; laNotifyUsers("our.canvas");
  532. }
  533. void ourset_Tool(void* unused, int a){
  534. Our->Tool=a; laNotifyUsers("our.canvas");
  535. }
  536. void ourset_ShowBorder(void* unused, int a){
  537. Our->ShowBorder=a; laNotifyUsers("our.canvas");
  538. }
  539. void ourset_CanvasSize(void* unused, int* wh){
  540. Our->W=wh[0]; Our->H=wh[1]; if(Our->W<32) Our->W=32; if(Our->H<32) Our->H=32; laNotifyUsers("our.canvas");
  541. }
  542. void ourset_CanvasPosition(void* unused, int* xy){
  543. Our->X=xy[0]; Our->Y=xy[1]; laNotifyUsers("our.canvas");
  544. }
  545. #define OUR_ADD_PRESSURE_SWITCH(p)\
  546. laAddEnumItemAs(p,"NONE","None","Not using pressure",0,0);\
  547. laAddEnumItemAs(p,"ENABLED","Enabled","Using pressure",1,0);
  548. void ourRegisterEverything(){
  549. laPropContainer* pc; laKeyMapper* km; laProp* p;
  550. laCreateOperatorType("OUR_new_layer","New Layer","Create a new layer",0,0,0,ourinv_NewLayer,0,'+',0);
  551. laCreateOperatorType("OUR_remove_layer","Remove Layer","Remove this layer",0,0,0,ourinv_RemoveLayer,0,L'🗴',0);
  552. laCreateOperatorType("OUR_move_layer","Move Layer","Remove this layer",0,0,0,ourinv_MoveLayer,0,0,0);
  553. laCreateOperatorType("OUR_new_brush","New Brush","Create a new brush",0,0,0,ourinv_NewBrush,0,'+',0);
  554. laCreateOperatorType("OUR_remove_brush","Remove Brush","Remove this brush",0,0,0,ourinv_RemoveBrush,0,L'🗴',0);
  555. laCreateOperatorType("OUR_move_brush","Move Brush","Remove this brush",0,0,0,ourinv_MoveBrush,0,0,0);
  556. laCreateOperatorType("OUR_action","Action","Doing action on a layer",0,0,0,ourinv_Action,ourmod_Action,0,LA_EXTRA_TO_PANEL);
  557. laCreateOperatorType("OUR_pick","Pick color","Pick color on the widget",0,0,0,ourinv_PickColor,ourmod_PickColor,0,LA_EXTRA_TO_PANEL);
  558. laRegisterUiTemplate("panel_canvas", "Canvas", ourui_CanvasPanel, 0, 0,"Our Paint");
  559. laRegisterUiTemplate("panel_layers", "Layers", ourui_LayersPanel, 0, 0,0);
  560. laRegisterUiTemplate("panel_tools", "Tools", ourui_ToolsPanel, 0, 0,0);
  561. laRegisterUiTemplate("panel_color", "Color", ourui_ColorPanel, 0, 0,0);
  562. pc=laDefineRoot();
  563. laAddSubGroup(pc,"our","Our","OurPaint main","our_paint",0,0,0,-1,ourget_our,0,0,0,0,0,0,0);
  564. pc=laAddPropertyContainer("our_paint","Our Paint","OurPaint main",0,0,sizeof(OurPaint),0,0,1);
  565. laAddSubGroup(pc,"canvas","Canvas","OurPaint canvas","our_canvas",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  566. laAddSubGroup(pc,"brushes","Brushes","Brushes","our_brush",0,0,ourui_Brush,offsetof(OurPaint,CurrentBrush),0,0,0,0,0,0,offsetof(OurPaint,Brushes),0);
  567. 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);
  568. 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);
  569. 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);
  570. laAddFloatProperty(pc,"border_alpha","Border Alpha","Alpha of the border region around the canvas",0,0,0,1,0,0.05,0.5,0,offsetof(OurPaint,BorderAlpha),0,0,0,0,0,0,0,ourset_BorderAlpha,0,0,0);
  571. p=laAddEnumProperty(pc,"tool","Tool","Tool to use on the canvas",0,0,0,0,0,offsetof(OurPaint,Tool),0,ourset_Tool,0,0,0,0,0,0,0,0);
  572. laAddEnumItemAs(p,"PAINT","Paint","Paint stuff on the canvas",OUR_TOOL_PAINT,L'🖌');
  573. laAddEnumItemAs(p,"CROP","Cropping","Crop the focused region",OUR_TOOL_CROP,L'🖼');
  574. p=laAddEnumProperty(pc,"show_border","Show Border","Whether to show border on the canvas",0,0,0,0,0,offsetof(OurPaint,ShowBorder),0,ourset_ShowBorder,0,0,0,0,0,0,0,0);
  575. laAddEnumItemAs(p,"FALSE","No","Dont' show border on the canvas",OUR_TOOL_PAINT,L'🖌');
  576. laAddEnumItemAs(p,"TRUE","Yes","Show border on the canvas",OUR_TOOL_CROP,L'🖼');
  577. pc=laAddPropertyContainer("our_brush","Our Brush","OurPaint brush",0,0,sizeof(OurBrush),0,0,2);
  578. laAddStringProperty(pc,"name","Name","Name of the layer",0,0,0,0,1,offsetof(OurBrush,Name),0,0,0,0,LA_AS_IDENTIFIER);
  579. laAddIntProperty(pc,"__move","Move Slider","Move Slider",LA_WIDGET_HEIGHT_ADJUSTER,0,0,0,0,0,0,0,0,0,ourset_LayerMove,0,0,0,0,0,0,0,0,0);
  580. 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);
  581. 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);
  582. 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);
  583. 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);
  584. 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);
  585. 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);
  586. 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);
  587. OUR_ADD_PRESSURE_SWITCH(p);
  588. 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);
  589. OUR_ADD_PRESSURE_SWITCH(p);
  590. 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);
  591. OUR_ADD_PRESSURE_SWITCH(p);
  592. 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);
  593. OUR_ADD_PRESSURE_SWITCH(p);
  594. laAddOperatorProperty(pc,"move","Move","Move brush","OUR_move_brush",0,0);
  595. laAddOperatorProperty(pc,"remove","Remove","Remove brush","OUR_remove_brush",L'🗴',0);
  596. pc=laAddPropertyContainer("our_canvas","Our Canvas","OurPaint canvas",0,0,sizeof(OurPaint),0,0,1);
  597. laAddSubGroup(pc,"layers","Layers","Layers","our_layer",0,0,ourui_Layer,offsetof(OurPaint,CurrentLayer),0,0,0,0,0,0,offsetof(OurPaint,Layers),0);
  598. laAddSubGroup(pc,"current_layer","Current Layer","Current layer","our_layer",0,0,0,offsetof(OurPaint,CurrentLayer),ourget_FirstLayer,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  599. laAddIntProperty(pc,"size","Size","Size of the cropping area",0,"X,Y","px",0,0,0,2400,0,offsetof(OurPaint,W),0,0,2,0,0,0,0,ourset_CanvasSize,0,0,0);
  600. laAddIntProperty(pc,"position","Position","Position of the cropping area",0,"X,Y","px",0,0,0,2400,0,offsetof(OurPaint,X),0,0,2,0,0,0,0,ourset_CanvasPosition,0,0,0);
  601. pc=laAddPropertyContainer("our_layer","Our Layer","OurPaint layer",0,0,sizeof(OurLayer),0,0,1);
  602. laAddStringProperty(pc,"name","Name","Name of the layer",0,0,0,0,1,offsetof(OurLayer,Name),0,0,0,0,LA_AS_IDENTIFIER);
  603. laAddIntProperty(pc,"__move","Move Slider","Move Slider",LA_WIDGET_HEIGHT_ADJUSTER,0,0,0,0,0,0,0,0,0,ourset_LayerMove,0,0,0,0,0,0,0,0,0);
  604. //laAddSubGroup(pc,"Rows","Rows","Rows of tiles","our_rows",0,0,0,0,0,0,0,0,0,0,offsetof(OurLayer,Rows),0);
  605. laAddOperatorProperty(pc,"move","Move","Move Layer","OUR_move_layer",0,0);
  606. laAddOperatorProperty(pc,"remove","Remove","Remove layer","OUR_remove_layer",L'🗴',0);
  607. laCanvasTemplate* ct=laRegisterCanvasTemplate("our_CanvasDraw", "our_canvas", 0, our_CanvasDrawCanvas, la_CanvasDrawOverlay, our_CanvasDrawInit, la_CanvasDestroy);
  608. pc = laCanvasHasExtraProps(ct,sizeof(OurCanvasDraw),2);
  609. km = &ct->KeyMapper;
  610. laAssignNewKey(km, 0, "LA_2d_view_zoom", LA_KM_SEL_UI_EXTRA, 0, LA_MOUSE_WHEEL_DOWN, 0, "direction=out");
  611. laAssignNewKey(km, 0, "LA_2d_view_zoom", LA_KM_SEL_UI_EXTRA, 0, LA_MOUSE_WHEEL_UP, 0, "direction=in");
  612. laAssignNewKey(km, 0, "LA_2d_view_move", LA_KM_SEL_UI_EXTRA, LA_KEY_ALT, LA_L_MOUSE_DOWN, 0, 0);
  613. laAssignNewKey(km, 0, "LA_2d_view_move", LA_KM_SEL_UI_EXTRA, 0, LA_M_MOUSE_DOWN, 0, 0);
  614. laAssignNewKey(km, 0, "OUR_action", LA_KM_SEL_UI_EXTRA, 0, LA_L_MOUSE_DOWN, 0, 0);
  615. laAssignNewKey(km, 0, "OUR_pick", LA_KM_SEL_UI_EXTRA, 0, LA_R_MOUSE_DOWN, 0, 0);
  616. }
  617. void ourInit(){
  618. Our=memAcquire(sizeof(OurPaint));
  619. ourRegisterEverything();
  620. char error[1024]; int status;
  621. Our->SmudgeTexture=tnsCreate2DTexture(GL_RGBA16,256,1,0);
  622. Our->CanvasShader = glCreateShader(GL_COMPUTE_SHADER);
  623. const GLchar* source = OUR_CANVAS_SHADER;
  624. glShaderSource(Our->CanvasShader, 1, &source, NULL);
  625. glCompileShader(Our->CanvasShader);
  626. glGetShaderiv(Our->CanvasShader, GL_COMPILE_STATUS, &status);
  627. if (status == GL_FALSE){
  628. glGetShaderInfoLog(Our->CanvasShader, sizeof(error), 0, error);
  629. printf("Compute shader error:\n%s", error);
  630. glDeleteShader(Our->CanvasShader);
  631. return -1;
  632. }
  633. Our->CanvasProgram = glCreateProgram();
  634. glAttachShader(Our->CanvasProgram, Our->CanvasShader);
  635. glLinkProgram(Our->CanvasProgram);
  636. glGetProgramiv(Our->CanvasProgram, GL_LINK_STATUS, &status);
  637. if (status == GL_FALSE){
  638. glGetProgramInfoLog(Our->CanvasProgram, sizeof(error), 0, error);
  639. printf("Shader Linking error:\n%s", error);
  640. return 0;
  641. }
  642. Our->uBrushCorner=glGetUniformLocation(Our->CanvasProgram,"uBrushCorner");
  643. Our->uBrushCenter=glGetUniformLocation(Our->CanvasProgram,"uBrushCenter");
  644. Our->uBrushSize=glGetUniformLocation(Our->CanvasProgram,"uBrushSize");
  645. Our->uBrushHardness=glGetUniformLocation(Our->CanvasProgram,"uBrushHardness");
  646. Our->uBrushSmudge=glGetUniformLocation(Our->CanvasProgram,"uBrushSmudge");
  647. Our->uBrushColor=glGetUniformLocation(Our->CanvasProgram,"uBrushColor");
  648. Our->uBrushRoutineSelection=glGetSubroutineUniformLocation(Our->CanvasProgram, GL_COMPUTE_SHADER, "uBrushRoutineSelection");
  649. Our->RoutineDoDabs=glGetSubroutineIndex(Our->CanvasProgram, GL_COMPUTE_SHADER, "DoDabs");
  650. Our->RoutineDoSample=glGetSubroutineIndex(Our->CanvasProgram, GL_COMPUTE_SHADER, "DoSample");
  651. Our->X=-2800/2; Our->W=2800;
  652. Our->Y=2400/2; Our->H=2400;
  653. Our->BorderAlpha=0.6;
  654. tnsEnableShaderv(T->immShader);
  655. }