*/}}

ouroperations.c 40 KB

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