*/}}

ouroperations.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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(rgba8, binding = 0) coherent uniform image2D img;\n\
  8. layout(std430, binding = 1) buffer CCOLOR { float ccolor[4]; };\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. \n\
  16. int dab(float d, vec4 color, float size, float hardness, float smudge, vec4 smudge_color, vec4 last_color, out vec4 final){\n\
  17. color.rgb=mix(color,smudge_color,smudge).rgb;\n\
  18. float a=clamp(color.a*1-pow(d/size,1+1/(1-hardness)),0,1);\n\
  19. final=vec4(mix(last_color.rgb,color.rgb,a), 1-(1-last_color.a)*(1-a));\n\
  20. return 1;\n\
  21. }\n\
  22. void main() {\n\
  23. ivec2 px = ivec2(gl_GlobalInvocationID.xy)+uBrushCorner;\n\
  24. float dd=distance(vec2(px),uBrushCenter); if(dd>uBrushSize) return;\n\
  25. vec4 final;\n\
  26. vec4 dabc=imageLoad(img, px);\n\
  27. //vec4 smugec=imageLoad(img,uBrushCenter);\n\
  28. dab(dd,uBrushColor,uBrushSize,uBrushHardness,0,vec4(0,0,0,0),dabc,final);\n\
  29. dabc=final;\n\
  30. imageStore(img, px, dabc);\n\
  31. }\n\
  32. ";
  33. void ourui_CanvasPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  34. laColumn* c=laFirstColumn(uil);
  35. laUiItem* ui=laShowCanvas(uil,c,0,"our.canvas",0,-1);
  36. }
  37. void ourui_Layer(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  38. laColumn* c=laFirstColumn(uil); laColumn* cl,*cr; laSplitColumn(uil,c,0.7); cl=laLeftColumn(c,0);cr=laRightColumn(c,1);
  39. laUiItem* b=laBeginRow(uil,cl,0,0);
  40. laShowHeightAdjuster(uil,cl,This,"__move",0);
  41. laShowItemFull(uil,cl,This,"name",LA_WIDGET_STRING_PLAIN,0,0,0)->Expand=1;
  42. laEndRow(uil,b);
  43. laUiItem* b1=laOnConditionToggle(uil,cr,0,0,0,0,0);{ strSafeSet(&b1->ExtraInstructions,"text=☰");
  44. b=laBeginRow(uil,c,0,0);
  45. laShowItem(uil,c,This,"remove")->Flags|=LA_UI_FLAGS_ICON;
  46. laShowSeparator(uil,c)->Expand=1;
  47. laShowItemFull(uil,c,This,"move",0,"direction=up;icon=🡱;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  48. laShowItemFull(uil,c,This,"move",0,"direction=down;icon=🡳;",0,0)->Flags|=LA_UI_FLAGS_ICON;
  49. laEndRow(uil,b);
  50. }laEndCondition(uil,b1);
  51. }
  52. void ourui_LayersPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  53. laColumn* c=laFirstColumn(uil);
  54. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.canvas.current_layer"));{
  55. laUiItem* b1=laBeginRow(uil,c,0,0);
  56. laShowItem(uil,c,0,"our.canvas.current_layer.name")->Expand=1;
  57. laShowItem(uil,c,0,"OUR_new_layer")->Flags|=LA_UI_FLAGS_ICON;
  58. laEndRow(uil,b1);
  59. }laElse(uil,b);{
  60. laShowItem(uil,c,0,"OUR_new_layer");
  61. }laEndCondition(uil,b);
  62. laShowItemFull(uil,c,0,"our.canvas.layers",0,0,0,0);
  63. }
  64. void ourui_Brush(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_BrushesPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  80. laColumn* c=laFirstColumn(uil);
  81. laUiItem* b=laOnConditionThat(uil,c,laPropExpression(0,"our.current_brush"));{
  82. laShowItem(uil,c,0,"our.current_brush.name");
  83. laShowItem(uil,c,0,"our.current_brush.size");
  84. laShowItem(uil,c,0,"our.current_brush.transparency");
  85. laShowItem(uil,c,0,"our.current_brush.hardness");
  86. laShowItem(uil,c,0,"our.current_brush.dabs_per_size");
  87. }laEndCondition(uil,b);
  88. laShowLabel(uil,c,"Select a brush:",0,0);
  89. laShowItemFull(uil,c,0,"our.brushes",0,0,0,0);
  90. laShowItem(uil,c,0,"OUR_new_brush");
  91. }
  92. void ourui_ColorPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  93. laColumn* c=laFirstColumn(uil);
  94. laShowItemFull(uil,c,0,"our.current_color",LA_WIDGET_FLOAT_COLOR_HCY,0,0,0);
  95. }
  96. void our_CANVAS_TEST(laBoxedTheme *bt, laUiItem* ui){
  97. OurCanvasDraw* ocd=ui->Extra; OurPaint* oc=ui->PP.EndInstance; laCanvasExtra*e=&ocd->Base;
  98. int W, H; W = ui->R - ui->L; H = ui->B - ui->U;
  99. if(!oc->Content){
  100. oc->Content=tnsCreate2DTexture(GL_RGBA8,1024,1024,0);
  101. }
  102. tnsBindTexture(oc->Content);
  103. glBindImageTexture(0, oc->Content->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
  104. glUseProgram(Our->CanvasProgram);
  105. for(int i=0;i<100;i++){
  106. //glUniform2i(Our->CanvasTaskUniform,i,0);
  107. //glDispatchCompute(32, 32, 1);
  108. //glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
  109. }
  110. //for(int i=0;i<32;i++){
  111. // for(int j=0;j<32;j++){
  112. // glUniform2i(Our->CanvasTaskUniform,i,j);
  113. // glDispatchCompute(1, 1, 1);
  114. // glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
  115. // }
  116. //}
  117. tnsEnableShaderv(T->immShader); tnsUseImmShader();
  118. tnsDrawToOffscreen(e->OffScr, 1, 0);
  119. tnsViewportWithScissor(0, 0, W, H);
  120. tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  121. 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);
  122. glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT);
  123. real w2=oc->Content->Width/2, h2=oc->Content->Height/2;
  124. tnsDraw2DTextureDirectly(oc->Content, -w2, h2, oc->Content->Width, -oc->Content->Height);
  125. tnsFlush();
  126. }
  127. void our_CanvasDrawTextures(){
  128. tnsUseImmShader; tnsEnableShaderv(T->immShader); tnsUniformUseTexture(T->immShader,0,0); tnsUseNoTexture();
  129. for(OurLayer* l=Our->Layers.pLast;l;l=l->Item.pPrev){
  130. int any=0;
  131. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){
  132. if(!l->TexTiles[row]) continue;
  133. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){
  134. if(!l->TexTiles[row][col]) continue;
  135. 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;
  136. tnsDraw2DTextureDirectly(l->TexTiles[row][col]->Texture,sx,sy+OUR_TEX_TILE_W,OUR_TEX_TILE_W,-OUR_TEX_TILE_W);
  137. any=1;
  138. }
  139. }
  140. if(any) tnsFlush();
  141. }
  142. }
  143. void our_CanvasDrawTiles(){
  144. OurLayer* l=Our->CurrentLayer; if(!l) return;
  145. tnsUseImmShader; tnsEnableShaderv(T->immShader); tnsUniformUseTexture(T->immShader,0,0); tnsUseNoTexture();
  146. int any=0;
  147. for(int row=0;row<OUR_TEX_TILES_PER_ROW;row++){
  148. if(!l->TexTiles[row]) continue;
  149. for(int col=0;col<OUR_TEX_TILES_PER_ROW;col++){
  150. if(!l->TexTiles[row][col]) continue;
  151. 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;
  152. tnsVertex2d(sx, sy); tnsVertex2d(sx+OUR_TEX_TILE_W,sy);
  153. tnsVertex2d(sx+OUR_TEX_TILE_W, sy+OUR_TEX_TILE_W); tnsVertex2d(sx,sy+OUR_TEX_TILE_W);
  154. tnsColor4dv(laAccentColor(LA_BT_NORMAL));
  155. tnsPackAs(GL_TRIANGLE_FAN);
  156. tnsVertex2d(sx, sy); tnsVertex2d(sx+OUR_TEX_TILE_W,sy);
  157. tnsVertex2d(sx+OUR_TEX_TILE_W, sy+OUR_TEX_TILE_W); tnsVertex2d(sx,sy+OUR_TEX_TILE_W);
  158. tnsColor4dv(laAccentColor(LA_BT_TEXT));
  159. tnsPackAs(GL_LINE_LOOP);
  160. }
  161. }
  162. if(any) tnsFlush();
  163. }
  164. void our_CanvasDrawInit(laUiItem* ui){
  165. ui->Extra=memAcquireHyper(sizeof(OurCanvasDraw));
  166. laFirstColumn(laAddTabPage(ui, "New Group"));
  167. OurCanvasDraw* ocd=ui->Extra;
  168. ocd->Base.ParentUi=ui;
  169. ocd->Base.HeightCoeff = 10;
  170. ocd->Base.ZoomX = 1;
  171. ocd->Base.ZoomY = 1;
  172. ocd->Base.ImageDrawAlpha = 1;
  173. ocd->Base.ImageDrawBorder = 1;
  174. ocd->Base.AdaptiveLineWidth = 1;
  175. ocd->Base.ClearBackground = 1;
  176. ocd->ShowTiles=1;
  177. int work_grp_cnt[3];
  178. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 0, &work_grp_cnt[0]);
  179. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 1, &work_grp_cnt[1]);
  180. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 2, &work_grp_cnt[2]);
  181. 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]);
  182. int work_grp_size[3];
  183. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &work_grp_size[0]);
  184. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1, &work_grp_size[1]);
  185. glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2, &work_grp_size[2]);
  186. 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]);
  187. int work_grp_inv;
  188. glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &work_grp_inv);
  189. printf("max local work group invocations %i\n", work_grp_inv);
  190. }
  191. void our_CanvasDrawCanvas(laBoxedTheme *bt, OurPaint *unused_c, laUiItem* ui){
  192. OurCanvasDraw* ocd=ui->Extra; OurPaint* oc=ui->PP.EndInstance; laCanvasExtra*e=&ocd->Base;
  193. int W, H; W = ui->R - ui->L; H = ui->B - ui->U;
  194. tnsFlush();
  195. if (!e->OffScr || e->OffScr->pColor[0]->Height != ui->B - ui->U || e->OffScr->pColor[0]->Width != ui->R - ui->L){
  196. if (e->OffScr) tnsDelete2DOffscreen(e->OffScr);
  197. e->OffScr = tnsCreate2DOffscreen(GL_RGBA, W, H, 0, 0);
  198. }
  199. //our_CANVAS_TEST(bt,ui);
  200. tnsDrawToOffscreen(e->OffScr,1,0);
  201. tnsViewportWithScissor(0, 0, W, H);
  202. tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  203. 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);
  204. tnsClearColor(0,0,0,0); tnsClearAll();
  205. if(ocd->ShowTiles){ our_CanvasDrawTiles(); }
  206. our_CanvasDrawTextures();
  207. }
  208. OurLayer* our_NewLayer(char* name){
  209. OurLayer* l=memAcquire(sizeof(OurLayer)); strSafeSet(&l->Name,name); lstPushItem(&Our->Layers, l);
  210. memAssignRef(Our, &Our->CurrentLayer, l);
  211. return l;
  212. }
  213. void our_RemoveLayer(OurLayer* l){
  214. strSafeDestroy(&l->Name); lstRemoveItem(&Our->Layers, l);
  215. if(Our->CurrentLayer==l){ OurLayer* nl=l->Item.pPrev?l->Item.pPrev:l->Item.pNext; memAssignRef(Our, &Our->CurrentLayer, nl); }
  216. memLeave(l);
  217. }
  218. OurBrush* our_NewBrush(char* name, real Size, real Hardness, real DabsPerSize, real Transparency){
  219. OurBrush* b=memAcquireHyper(sizeof(OurBrush)); strSafeSet(&b->Name,name); lstAppendItem(&Our->Brushes, b);
  220. b->Size=Size; b->Hardness=Hardness; b->DabsPerSize=DabsPerSize; b->Transparency=Transparency;
  221. memAssignRef(Our, &Our->CurrentBrush, b);
  222. return b;
  223. }
  224. void our_RemoveBrush(OurBrush* b){
  225. strSafeDestroy(&b->Name); lstRemoveItem(&Our->Brushes, b);
  226. if(Our->CurrentLayer==b){ OurLayer* nb=b->Item.pPrev?b->Item.pPrev:b->Item.pNext; memAssignRef(Our, &Our->CurrentBrush, nb); }
  227. memLeave(b);
  228. }
  229. void our_LayerEnsureTiles(OurLayer* ol, real xmin,real xmax, real ymin,real ymax, int *tl, int *tr, int* tu, int* tb){
  230. 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);
  231. 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);
  232. 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);
  233. 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);
  234. //printf("%lf\n",OUR_TEX_TILE_CTR+(real)xmin/OUR_TEX_TILE_W_USE+0.5);
  235. for(int row=b;row<=u;row++){
  236. if(!ol->TexTiles[row]){ol->TexTiles[row]=memAcquireSimple(sizeof(OurTexTile*)*OUR_TEX_TILES_PER_ROW);}
  237. for(int col=l;col<=r;col++){
  238. if(ol->TexTiles[row][col]) continue;
  239. ol->TexTiles[row][col]=memAcquireSimple(sizeof(OurTexTile));
  240. ol->TexTiles[row][col]->Texture=tnsCreate2DTexture(GL_RGBA8,OUR_TEX_TILE_W,OUR_TEX_TILE_W,0);
  241. float initColor[]={0,0,0,0};
  242. glClearTexImage(ol->TexTiles[row][col]->Texture->GLTexHandle, 0, GL_BGRA, GL_UNSIGNED_BYTE, &initColor);
  243. }
  244. }
  245. *tl=l; *tr=r; *tu=u; *tb=b;
  246. }
  247. void our_UiToCanvas(laCanvasExtra* ex, laEvent*e, real* x, real *y){
  248. *x = (real)((real)e->x - (real)(ex->ParentUi->R - ex->ParentUi->L) / 2 - ex->ParentUi->L) * ex->ZoomX + ex->PanX;
  249. *y = (real)((real)(ex->ParentUi->B - ex->ParentUi->U) / 2 - (real)e->y + ex->ParentUi->U) * ex->ZoomY + ex->PanY;
  250. }
  251. 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){
  252. Our->NextDab=0;
  253. real size=b->Size; real dd=b->Size/b->DabsPerSize; real len=tnsDistIdv2(x,y,xto,yto); real alllen=len+Our->BrushRemainingDist;
  254. int count=alllen/dd; real uselen=count*dd; if(!count){ Our->BrushRemainingDist=alllen; return 0; }
  255. real rem=Our->BrushRemainingDist;
  256. real xmin=FLT_MAX,xmax=FLT_MIN,ymin=FLT_MAX,ymax=FLT_MIN; int starti=0;//Our->BrushRemainingDist<1e-9?0:1;
  257. for(int i=starti;i<count;i++){
  258. arrEnsureLength(&Our->Dabs,Our->NextDab,&Our->MaxDab,sizeof(OurDab)); OurDab* od=&Our->Dabs[Our->NextDab];
  259. real r=tnsGetRatiod(0,len,i*dd-rem); od->X=tnsInterpolate(x,xto,r); od->Y=tnsInterpolate(y,yto,r);
  260. TNS_CLAMP(r,0,1); od->Size = b->Size*tnsInterpolate(last_pressure,pressure,r);
  261. od->Hardness = b->Hardness; od->Smudge = b->Smudge;
  262. tnsVectorSet3v(od->Color,Our->CurrentColor); od->Color[3]=b->Transparency;
  263. xmin=TNS_MIN2(xmin, od->X-od->Size); xmax=TNS_MAX2(xmax, od->X+od->Size);
  264. ymin=TNS_MIN2(ymin, od->Y-od->Size); ymax=TNS_MAX2(ymax, od->Y+od->Size);
  265. Our->NextDab++;
  266. }
  267. Our->BrushRemainingDist=alllen-uselen;
  268. if(Our->NextDab) { our_LayerEnsureTiles(l,xmin,xmax,ymin,ymax,tl,tr,tu,tb); return 1; }
  269. return 0;
  270. }
  271. void our_PaintDoDab(OurDab* d, int tl, int tr, int tu, int tb){
  272. int corner[2]; corner[0]=floorf(d->X-d->Size); corner[1]=floorf(d->Y-d->Size);
  273. real MaxX,MaxY; MaxX=ceil(d->X+d->Size); MaxY=ceil(d->Y+d->Size);
  274. if(corner[0]>tr||MaxX<tl||corner[1]>tb||MaxY<tu) return;
  275. corner[0]=corner[0]-tl; corner[1]=corner[1]-tu;
  276. float center[2]; center[0]=d->X-tl; center[1]=d->Y-tu;
  277. glUniform2iv(Our->uBrushCorner,1,corner);
  278. glUniform2fv(Our->uBrushCenter,1,center);
  279. glUniform1f(Our->uBrushSize,d->Size);
  280. glUniform1f(Our->uBrushHardness,d->Hardness);
  281. glUniform4fv(Our->uBrushColor,1,d->Color);
  282. glDispatchCompute(ceil(d->Size/16), ceil(d->Size/16), 1);
  283. glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
  284. }
  285. void our_PaintDoDabs(OurLayer* l,int tl, int tr, int tu, int tb){
  286. glUseProgram(Our->CanvasProgram);
  287. for(int row=tb;row<=tu;row++){
  288. for(int col=tl;col<=tr;col++){
  289. OurTexTile* ott=l->TexTiles[row][col];
  290. tnsBindTexture(ott->Texture); glBindImageTexture(0, ott->Texture->GLTexHandle, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
  291. 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;
  292. for(int i=0;i<Our->NextDab;i++){
  293. our_PaintDoDab(&Our->Dabs[i],sx,sx+OUR_TEX_TILE_W,sy,sy+OUR_TEX_TILE_W);
  294. }
  295. }
  296. }
  297. }
  298. int ourinv_NewLayer(laOperator* a, laEvent* e){
  299. our_NewLayer("Our Layer"); laNotifyUsers("our.canvas.layers");
  300. return LA_FINISHED;
  301. }
  302. int ourinv_RemoveLayer(laOperator* a, laEvent* e){
  303. OurLayer* l=a->This?a->This->EndInstance:0; if(!l) return LA_CANCELED;
  304. our_RemoveLayer(l); laNotifyUsers("our.canvas.layers");
  305. return LA_FINISHED;
  306. }
  307. int ourinv_MoveLayer(laOperator* a, laEvent* e){
  308. OurLayer* l=a->This?a->This->EndInstance:0; if(!l) return LA_CANCELED;
  309. char* direction=strGetArgumentString(a->ExtraInstructionsP,"direction");
  310. if(strSame(direction,"up")&&l->Item.pPrev){ lstMoveUp(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  311. elif(l->Item.pNext){ lstMoveDown(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  312. return LA_FINISHED;
  313. }
  314. int ourinv_NewBrush(laOperator* a, laEvent* e){
  315. our_NewBrush("Our Brush",15,0.95,9,0.5); laNotifyUsers("our.brushes");
  316. return LA_FINISHED;
  317. }
  318. int ourinv_RemoveBrush(laOperator* a, laEvent* e){
  319. OurBrush* b=a->This?a->This->EndInstance:0; if(!b) return LA_CANCELED;
  320. our_RemoveLayer(b); laNotifyUsers("our.brushes");
  321. return LA_FINISHED;
  322. }
  323. int ourinv_MoveBrush(laOperator* a, laEvent* e){
  324. OurBrush* b=a->This?a->This->EndInstance:0; if(!b) return LA_CANCELED;
  325. char* direction=strGetArgumentString(a->ExtraInstructionsP,"direction");
  326. if(strSame(direction,"up")&&b->Item.pPrev){ lstMoveUp(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
  327. elif(b->Item.pNext){ lstMoveDown(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
  328. return LA_FINISHED;
  329. }
  330. int ourinv_Paint(laOperator* a, laEvent* e){
  331. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  332. real x,y; our_UiToCanvas(&ex->Base,e,&x,&y); ex->CanvasLastX=x;ex->CanvasLastY=y;ex->LastPressure=e->Pressure;
  333. return LA_RUNNING;
  334. }
  335. int ourmod_Paint(laOperator* a, laEvent* e){
  336. OurLayer* l=Our->CurrentLayer; OurCanvasDraw *ex = a->This?a->This->EndInstance:0; OurBrush* ob=Our->CurrentBrush; if(!l||!ex||!ob) return LA_CANCELED;
  337. if(e->Type==LA_L_MOUSE_UP || e->Type==LA_R_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ return LA_FINISHED; }
  338. if(e->Type==LA_MOUSEMOVE||e->Type==LA_L_MOUSE_DOWN){
  339. real x,y; our_UiToCanvas(&ex->Base,e,&x,&y);
  340. int tl,tr,tu,tb;
  341. if(our_PaintGetDabs(ob,l,ex->CanvasLastX,ex->CanvasLastY,x,y,ex->LastPressure,e->Pressure,&tl,&tr,&tu,&tb)){
  342. our_PaintDoDabs(l,tl,tr,tu,tb);
  343. }
  344. ex->CanvasLastX=x;ex->CanvasLastY=y;ex->LastPressure=e->Pressure;
  345. laNotifyUsers("our.canvas");
  346. }
  347. return LA_RUNNING;
  348. }
  349. void* ourget_FirstLayer(void* unused, void* unused1){
  350. return Our->Layers.pFirst;
  351. }
  352. void* ourget_FirstBrush(void* unused, void* unused1){
  353. return Our->Brushes.pFirst;
  354. }
  355. void* ourget_our(void* unused, void* unused1){
  356. return Our;
  357. }
  358. void ourset_LayerMove(OurLayer* l, int move){
  359. if(move<0 && l->Item.pPrev){ lstMoveUp(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  360. elif(move>0 && l->Item.pNext){ lstMoveDown(&Our->Layers, l); laNotifyUsers("our.canvas.layers"); }
  361. }
  362. void ourset_BrushMove(OurBrush* b, int move){
  363. if(move<0 && b->Item.pPrev){ lstMoveUp(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
  364. elif(move>0 && b->Item.pNext){ lstMoveDown(&Our->Brushes, b); laNotifyUsers("our.brushes"); }
  365. }
  366. void ourRegisterEverything(){
  367. laPropContainer* pc; laKeyMapper* km;
  368. laCreateOperatorType("OUR_new_layer","New Layer","Create a new layer",0,0,0,ourinv_NewLayer,0,'+',0);
  369. laCreateOperatorType("OUR_remove_layer","Remove Layer","Remove this layer",0,0,0,ourinv_RemoveLayer,0,L'🗴',0);
  370. laCreateOperatorType("OUR_move_layer","Move Layer","Remove this layer",0,0,0,ourinv_MoveLayer,0,0,0);
  371. laCreateOperatorType("OUR_new_brush","New Brush","Create a new brush",0,0,0,ourinv_NewBrush,0,'+',0);
  372. laCreateOperatorType("OUR_remove_brush","Remove Brush","Remove this brush",0,0,0,ourinv_RemoveBrush,0,L'🗴',0);
  373. laCreateOperatorType("OUR_move_brush","Move Brush","Remove this brush",0,0,0,ourinv_MoveBrush,0,0,0);
  374. laCreateOperatorType("OUR_paint","Paint","Paint on a layer",0,0,0,ourinv_Paint,ourmod_Paint,0,LA_EXTRA_TO_PANEL);
  375. laRegisterUiTemplate("panel_canvas", "Canvas", ourui_CanvasPanel, 0, 0,0);
  376. laRegisterUiTemplate("panel_layers", "Layers", ourui_LayersPanel, 0, 0,0);
  377. laRegisterUiTemplate("panel_brushes", "Brushes", ourui_BrushesPanel, 0, 0,0);
  378. laRegisterUiTemplate("panel_color", "Color", ourui_ColorPanel, 0, 0,0);
  379. pc=laDefineRoot();
  380. laAddSubGroup(pc,"our","Our","OurPaint main","our_paint",0,0,0,-1,ourget_our,0,0,0,0,0,0,0);
  381. pc=laAddPropertyContainer("our_paint","Our Paint","OurPaint main",0,0,sizeof(OurPaint),0,0,1);
  382. laAddSubGroup(pc,"canvas","Canvas","OurPaint canvas","our_canvas",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  383. laAddSubGroup(pc,"brushes","Brushes","Brushes","our_brush",0,0,ourui_Brush,offsetof(OurPaint,CurrentBrush),0,0,0,0,0,0,offsetof(OurPaint,Brushes),0);
  384. 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);
  385. 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);
  386. pc=laAddPropertyContainer("our_brush","Our Brush","OurPaint brush",0,0,sizeof(OurBrush),0,0,2);
  387. laAddStringProperty(pc,"name","Name","Name of the layer",0,0,0,0,1,offsetof(OurBrush,Name),0,0,0,0,LA_AS_IDENTIFIER);
  388. 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);
  389. 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);
  390. 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);
  391. 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);
  392. 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);
  393. laAddOperatorProperty(pc,"move","Move","Move brush","OUR_move_brush",0,0);
  394. laAddOperatorProperty(pc,"remove","Remove","Remove brush","OUR_remove_brush",L'🗴',0);
  395. pc=laAddPropertyContainer("our_canvas","Our Canvas","OurPaint canvas",0,0,sizeof(OurPaint),0,0,1);
  396. laAddSubGroup(pc,"layers","Layers","Layers","our_layer",0,0,ourui_Layer,offsetof(OurPaint,CurrentLayer),0,0,0,0,0,0,offsetof(OurPaint,Layers),0);
  397. 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);
  398. pc=laAddPropertyContainer("our_layer","Our Layer","OurPaint layer",0,0,sizeof(OurLayer),0,0,1);
  399. laAddStringProperty(pc,"name","Name","Name of the layer",0,0,0,0,1,offsetof(OurLayer,Name),0,0,0,0,LA_AS_IDENTIFIER);
  400. 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);
  401. //laAddSubGroup(pc,"Rows","Rows","Rows of tiles","our_rows",0,0,0,0,0,0,0,0,0,0,offsetof(OurLayer,Rows),0);
  402. laAddOperatorProperty(pc,"move","Move","Move Layer","OUR_move_layer",0,0);
  403. laAddOperatorProperty(pc,"remove","Remove","Remove layer","OUR_remove_layer",L'🗴',0);
  404. laCanvasTemplate* ct=laRegisterCanvasTemplate("our_CanvasDraw", "our_canvas", 0, our_CanvasDrawCanvas, la_CanvasDrawOverlay, our_CanvasDrawInit, la_CanvasDestroy);
  405. pc = laCanvasHasExtraProps(ct,sizeof(OurCanvasDraw),2);
  406. km = &ct->KeyMapper;
  407. laAssignNewKey(km, 0, "LA_2d_view_zoom", LA_KM_SEL_UI_EXTRA, 0, LA_MOUSE_WHEEL_DOWN, 0, "direction=out");
  408. laAssignNewKey(km, 0, "LA_2d_view_zoom", LA_KM_SEL_UI_EXTRA, 0, LA_MOUSE_WHEEL_UP, 0, "direction=in");
  409. laAssignNewKey(km, 0, "LA_2d_view_move", LA_KM_SEL_UI_EXTRA, LA_KEY_ALT, LA_L_MOUSE_DOWN, 0, 0);
  410. laAssignNewKey(km, 0, "LA_2d_view_move", LA_KM_SEL_UI_EXTRA, 0, LA_M_MOUSE_DOWN, 0, 0);
  411. laAssignNewKey(km, 0, "OUR_paint", LA_KM_SEL_UI_EXTRA, 0, LA_L_MOUSE_DOWN, 0, 0);
  412. }
  413. void ourInit(){
  414. Our=memAcquire(sizeof(OurPaint));
  415. ourRegisterEverything();
  416. char error[1024]; int status;
  417. Our->CanvasShader = glCreateShader(GL_COMPUTE_SHADER);
  418. const GLchar* source = OUR_CANVAS_SHADER;
  419. glShaderSource(Our->CanvasShader, 1, &source, NULL);
  420. glCompileShader(Our->CanvasShader);
  421. glGetShaderiv(Our->CanvasShader, GL_COMPILE_STATUS, &status);
  422. if (status == GL_FALSE){
  423. glGetShaderInfoLog(Our->CanvasShader, sizeof(error), 0, error);
  424. printf("Compute shader error:\n%s", error);
  425. glDeleteShader(Our->CanvasShader);
  426. return -1;
  427. }
  428. Our->CanvasProgram = glCreateProgram();
  429. glAttachShader(Our->CanvasProgram, Our->CanvasShader);
  430. glLinkProgram(Our->CanvasProgram);
  431. glGetProgramiv(Our->CanvasProgram, GL_LINK_STATUS, &status);
  432. if (status == GL_FALSE){
  433. glGetProgramInfoLog(Our->CanvasProgram, sizeof(error), 0, error);
  434. printf("Shader Linking error:\n%s", error);
  435. return 0;
  436. }
  437. Our->uBrushCorner=glGetUniformLocation(Our->CanvasProgram,"uBrushCorner");
  438. Our->uBrushCenter=glGetUniformLocation(Our->CanvasProgram,"uBrushCenter");
  439. Our->uBrushSize=glGetUniformLocation(Our->CanvasProgram,"uBrushSize");
  440. Our->uBrushHardness=glGetUniformLocation(Our->CanvasProgram,"uBrushHardness");
  441. Our->uBrushSmudge=glGetUniformLocation(Our->CanvasProgram,"uBrushSmudge");
  442. Our->uBrushColor=glGetUniformLocation(Our->CanvasProgram,"uBrushColor");
  443. tnsEnableShaderv(T->immShader);
  444. }