c16ea84ec812375528150827f2660351612032d0
[blender.git] / source / blender / editors / gpencil / drawgpencil.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008, Blender Foundation
19  * This is a new part of Blender
20  *
21  * Contributor(s): Joshua Leung, Antonio Vazquez
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/gpencil/drawgpencil.c
27  *  \ingroup edgpencil
28  */
29
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stddef.h>
35 #include <math.h>
36 #include <float.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_sys_types.h"
41
42 #include "BLI_math.h"
43 #include "BLI_utildefines.h"
44 #include "BLI_polyfill_2d.h"
45
46 #include "BLF_api.h"
47 #include "BLT_translation.h"
48
49 #include "DNA_brush_types.h"
50 #include "DNA_gpencil_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_screen_types.h"
53 #include "DNA_space_types.h"
54 #include "DNA_view3d_types.h"
55 #include "DNA_userdef_types.h"
56 #include "DNA_object_types.h"
57
58 #include "BKE_context.h"
59 #include "BKE_brush.h"
60 #include "BKE_global.h"
61 #include "BKE_paint.h"
62 #include "BKE_gpencil.h"
63 #include "BKE_image.h"
64
65 #include "DEG_depsgraph.h"
66
67 #include "WM_api.h"
68
69 #include "BIF_glutil.h"
70
71 #include "GPU_immediate.h"
72 #include "GPU_draw.h"
73 #include "GPU_state.h"
74
75 #include "ED_gpencil.h"
76 #include "ED_screen.h"
77 #include "ED_view3d.h"
78 #include "ED_space_api.h"
79
80 #include "UI_interface_icons.h"
81 #include "UI_resources.h"
82
83 #include "IMB_imbuf_types.h"
84
85 #include "gpencil_intern.h"
86
87 /* ************************************************** */
88 /* GREASE PENCIL DRAWING */
89
90 /* ----- General Defines ------ */
91 /* flags for sflag */
92 typedef enum eDrawStrokeFlags {
93         GP_DRAWDATA_NOSTATUS    = (1 << 0),   /* don't draw status info */
94         GP_DRAWDATA_ONLY3D      = (1 << 1),   /* only draw 3d-strokes */
95         GP_DRAWDATA_ONLYV2D     = (1 << 2),   /* only draw 'canvas' strokes */
96         GP_DRAWDATA_ONLYI2D     = (1 << 3),   /* only draw 'image' strokes */
97         GP_DRAWDATA_IEDITHACK   = (1 << 4),   /* special hack for drawing strokes in Image Editor (weird coordinates) */
98         GP_DRAWDATA_NO_XRAY     = (1 << 5),   /* don't draw xray in 3D view (which is default) */
99         GP_DRAWDATA_NO_ONIONS   = (1 << 6),       /* no onionskins should be drawn (for animation playback) */
100         GP_DRAWDATA_VOLUMETRIC  = (1 << 7),   /* draw strokes as "volumetric" circular billboards */
101         GP_DRAWDATA_FILL        = (1 << 8)    /* fill insides/bounded-regions of strokes */
102 } eDrawStrokeFlags;
103
104
105
106 /* thickness above which we should use special drawing */
107 #if 0
108 #define GP_DRAWTHICKNESS_SPECIAL    3
109 #endif
110
111 /* conversion utility (float --> normalized unsigned byte) */
112 #define F2UB(x) (uchar)(255.0f * x)
113
114 /* ----- Tool Buffer Drawing ------ */
115 /* helper functions to set color of buffer point */
116
117 static void gp_set_tpoint_varying_color(const tGPspoint *pt, const float ink[4], uint attr_id)
118 {
119         float alpha = ink[3] * pt->strength;
120         CLAMP(alpha, GPENCIL_STRENGTH_MIN, 1.0f);
121         immAttr4ub(attr_id, F2UB(ink[0]), F2UB(ink[1]), F2UB(ink[2]), F2UB(alpha));
122 }
123
124 static void gp_set_point_uniform_color(const bGPDspoint *pt, const float ink[4])
125 {
126         float alpha = ink[3] * pt->strength;
127         CLAMP(alpha, GPENCIL_STRENGTH_MIN, 1.0f);
128         immUniformColor3fvAlpha(ink, alpha);
129 }
130
131 static void gp_set_point_varying_color(const bGPDspoint *pt, const float ink[4], uint attr_id)
132 {
133         float alpha = ink[3] * pt->strength;
134         CLAMP(alpha, GPENCIL_STRENGTH_MIN, 1.0f);
135         immAttr4ub(attr_id, F2UB(ink[0]), F2UB(ink[1]), F2UB(ink[2]), F2UB(alpha));
136 }
137
138 /* draw fills for buffer stroke */
139 static void gp_draw_stroke_buffer_fill(const tGPspoint *points, int totpoints, float ink[4])
140 {
141         if (totpoints < 3) {
142                 return;
143         }
144         int tot_triangles = totpoints - 2;
145         /* allocate memory for temporary areas */
146         uint(*tmp_triangles)[3] = MEM_mallocN(sizeof(*tmp_triangles) * tot_triangles, "GP Stroke buffer temp triangulation");
147         float(*points2d)[2] = MEM_mallocN(sizeof(*points2d) * totpoints, "GP Stroke buffer temp 2d points");
148
149         /* Convert points to array and triangulate
150          * Here a cache is not used because while drawing the information changes all the time, so the cache
151          * would be recalculated constantly, so it is better to do direct calculation for each function call
152          */
153         for (int i = 0; i < totpoints; i++) {
154                 const tGPspoint *pt = &points[i];
155                 points2d[i][0] = pt->x;
156                 points2d[i][1] = pt->y;
157         }
158         BLI_polyfill_calc((const float(*)[2])points2d, (uint)totpoints, 0, (uint(*)[3])tmp_triangles);
159
160         /* draw triangulation data */
161         if (tot_triangles > 0) {
162                 GPUVertFormat *format = immVertexFormat();
163                 uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
164                 uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
165
166                 immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR);
167
168                 /* Draw all triangles for filling the polygon */
169                 immBegin(GPU_PRIM_TRIS, tot_triangles * 3);
170                 /* TODO: use batch instead of immediate mode, to share vertices */
171
172                 const tGPspoint *pt;
173                 for (int i = 0; i < tot_triangles; i++) {
174                         /* vertex 1 */
175                         pt = &points[tmp_triangles[i][0]];
176                         gp_set_tpoint_varying_color(pt, ink, color);
177                         immVertex2fv(pos, &pt->x);
178                         /* vertex 2 */
179                         pt = &points[tmp_triangles[i][1]];
180                         gp_set_tpoint_varying_color(pt, ink, color);
181                         immVertex2fv(pos, &pt->x);
182                         /* vertex 3 */
183                         pt = &points[tmp_triangles[i][2]];
184                         gp_set_tpoint_varying_color(pt, ink, color);
185                         immVertex2fv(pos, &pt->x);
186                 }
187
188                 immEnd();
189                 immUnbindProgram();
190         }
191
192         /* clear memory */
193         if (tmp_triangles) {
194                 MEM_freeN(tmp_triangles);
195         }
196         if (points2d) {
197                 MEM_freeN(points2d);
198         }
199 }
200
201 /* draw stroke defined in buffer (simple ogl lines/points for now, as dotted lines) */
202 static void gp_draw_stroke_buffer(
203         const tGPspoint *points, int totpoints, short thickness,
204         short dflag, short sflag, float ink[4], float fill_ink[4])
205 {
206         int draw_points = 0;
207
208         /* error checking */
209         if ((points == NULL) || (totpoints <= 0))
210                 return;
211
212         /* check if buffer can be drawn */
213         if (dflag & (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_ONLYV2D))
214                 return;
215
216         if (sflag & GP_STROKE_ERASER) {
217                 /* don't draw stroke at all! */
218                 return;
219         }
220
221         GPUVertFormat *format = immVertexFormat();
222         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
223         uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
224
225         const tGPspoint *pt = points;
226
227         if (totpoints == 1) {
228                 /* if drawing a single point, draw it larger */
229                 GPU_point_size((float)(thickness + 2) * points->pressure);
230                 immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
231                 immBegin(GPU_PRIM_POINTS, 1);
232                 gp_set_tpoint_varying_color(pt, ink, color);
233                 immVertex2fv(pos, &pt->x);
234         }
235         else {
236                 float oldpressure = points[0].pressure;
237
238                 /* draw stroke curve */
239                 GPU_line_width(max_ff(oldpressure * thickness, 1.0));
240                 immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR);
241                 immBeginAtMost(GPU_PRIM_LINE_STRIP, totpoints);
242
243                 /* TODO: implement this with a geometry shader to draw one continuous tapered stroke */
244
245                 for (int i = 0; i < totpoints; i++, pt++) {
246                         /* if there was a significant pressure change, stop the curve, change the thickness of the stroke,
247                          * and continue drawing again (since line-width cannot change in middle of GL_LINE_STRIP)
248                          */
249                         if (fabsf(pt->pressure - oldpressure) > 0.2f) {
250                                 /* need to have 2 points to avoid immEnd assert error */
251                                 if (draw_points < 2) {
252                                         gp_set_tpoint_varying_color(pt - 1, ink, color);
253                                         immVertex2fv(pos, &(pt - 1)->x);
254                                 }
255
256                                 immEnd();
257                                 draw_points = 0;
258
259                                 GPU_line_width(max_ff(pt->pressure * thickness, 1.0f));
260                                 immBeginAtMost(GPU_PRIM_LINE_STRIP, totpoints - i + 1);
261
262                                 /* need to roll-back one point to ensure that there are no gaps in the stroke */
263                                 if (i != 0) {
264                                         gp_set_tpoint_varying_color(pt - 1, ink, color);
265                                         immVertex2fv(pos, &(pt - 1)->x);
266                                         draw_points++;
267                                 }
268
269                                 oldpressure = pt->pressure; /* reset our threshold */
270                         }
271
272                         /* now the point we want */
273                         gp_set_tpoint_varying_color(pt, ink, color);
274                         immVertex2fv(pos, &pt->x);
275                         draw_points++;
276                 }
277                 /* need to have 2 points to avoid immEnd assert error */
278                 if (draw_points < 2) {
279                         gp_set_tpoint_varying_color(pt - 1, ink, color);
280                         immVertex2fv(pos, &(pt - 1)->x);
281                 }
282         }
283
284         immEnd();
285         immUnbindProgram();
286
287         // draw fill
288         if (fill_ink[3] > GPENCIL_ALPHA_OPACITY_THRESH) {
289                 gp_draw_stroke_buffer_fill(points, totpoints, fill_ink);
290         }
291 }
292
293 /* --------- 2D Stroke Drawing Helpers --------- */
294 /* change in parameter list */
295 static void gp_calc_2d_stroke_fxy(const float pt[3], short sflag, int offsx, int offsy, int winx, int winy, float r_co[2])
296 {
297         if (sflag & GP_STROKE_2DSPACE) {
298                 r_co[0] = pt[0];
299                 r_co[1] = pt[1];
300         }
301         else if (sflag & GP_STROKE_2DIMAGE) {
302                 const float x = (float)((pt[0] * winx) + offsx);
303                 const float y = (float)((pt[1] * winy) + offsy);
304
305                 r_co[0] = x;
306                 r_co[1] = y;
307         }
308         else {
309                 const float x = (float)(pt[0] / 100 * winx) + offsx;
310                 const float y = (float)(pt[1] / 100 * winy) + offsy;
311
312                 r_co[0] = x;
313                 r_co[1] = y;
314         }
315 }
316 /* ----------- Volumetric Strokes --------------- */
317
318 /* draw a 2D buffer stroke in "volumetric" style
319  * NOTE: the stroke buffer doesn't have any coordinate offsets/transforms
320  */
321 static void gp_draw_stroke_volumetric_buffer(
322         const tGPspoint *points, int totpoints, short thickness,
323         short dflag, const float ink[4])
324 {
325         /* error checking */
326         if ((points == NULL) || (totpoints <= 0))
327                 return;
328
329         /* check if buffer can be drawn */
330         if (dflag & (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_ONLYV2D))
331                 return;
332
333         GPUVertFormat *format = immVertexFormat();
334         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
335         uint size = GPU_vertformat_attr_add(format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
336         uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
337
338         immBindBuiltinProgram(GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR);
339         GPU_enable_program_point_size();
340         immBegin(GPU_PRIM_POINTS, totpoints);
341
342         const tGPspoint *pt = points;
343         for (int i = 0; i < totpoints; i++, pt++) {
344                 gp_set_tpoint_varying_color(pt, ink, color);
345                 immAttr1f(size, pt->pressure * thickness); /* TODO: scale based on view transform (zoom level) */
346                 immVertex2f(pos, pt->x, pt->y);
347         }
348
349         immEnd();
350         immUnbindProgram();
351         GPU_disable_program_point_size();
352 }
353
354 /* draw a 2D strokes in "volumetric" style */
355 static void gp_draw_stroke_volumetric_2d(
356         const bGPDspoint *points, int totpoints, short thickness,
357         short UNUSED(dflag), short sflag,
358         int offsx, int offsy, int winx, int winy,
359         const float diff_mat[4][4], const float ink[4])
360 {
361         GPUVertFormat *format = immVertexFormat();
362         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
363         uint size = GPU_vertformat_attr_add(format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
364         uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
365
366         immBindBuiltinProgram(GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR);
367         GPU_enable_program_point_size();
368         immBegin(GPU_PRIM_POINTS, totpoints);
369
370         const bGPDspoint *pt = points;
371         for (int i = 0; i < totpoints; i++, pt++) {
372                 /* transform position to 2D */
373                 float co[2];
374                 float fpt[3];
375
376                 mul_v3_m4v3(fpt, diff_mat, &pt->x);
377                 gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, co);
378
379                 gp_set_point_varying_color(pt, ink, color);
380                 immAttr1f(size, pt->pressure * thickness); /* TODO: scale based on view transform */
381                 immVertex2f(pos, co[0], co[1]);
382         }
383
384         immEnd();
385         immUnbindProgram();
386         GPU_disable_program_point_size();
387 }
388
389 /* draw a 3D stroke in "volumetric" style */
390 static void gp_draw_stroke_volumetric_3d(
391         const bGPDspoint *points, int totpoints, short thickness,
392         const float ink[4])
393 {
394         GPUVertFormat *format = immVertexFormat();
395         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
396         uint size = GPU_vertformat_attr_add(format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
397         uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
398
399         immBindBuiltinProgram(GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR);
400         GPU_enable_program_point_size();
401         immBegin(GPU_PRIM_POINTS, totpoints);
402
403         const bGPDspoint *pt = points;
404         for (int i = 0; i < totpoints && pt; i++, pt++) {
405                 gp_set_point_varying_color(pt, ink, color);
406                 immAttr1f(size, pt->pressure * thickness); /* TODO: scale based on view transform */
407                 immVertex3fv(pos, &pt->x);                   /* we can adjust size in vertex shader based on view/projection! */
408         }
409
410         immEnd();
411         immUnbindProgram();
412         GPU_disable_program_point_size();
413 }
414
415
416 /* --------------- Stroke Fills ----------------- */
417 /* calc bounding box in 2d using flat projection data */
418 static void gp_calc_2d_bounding_box(const float(*points2d)[2], int totpoints, float minv[2], float maxv[2], bool expand)
419 {
420         copy_v2_v2(minv, points2d[0]);
421         copy_v2_v2(maxv, points2d[0]);
422
423         for (int i = 1; i < totpoints; i++) {
424                 /* min */
425                 if (points2d[i][0] < minv[0]) {
426                         minv[0] = points2d[i][0];
427                 }
428                 if (points2d[i][1] < minv[1]) {
429                         minv[1] = points2d[i][1];
430                 }
431                 /* max */
432                 if (points2d[i][0] > maxv[0]) {
433                         maxv[0] = points2d[i][0];
434                 }
435                 if (points2d[i][1] > maxv[1]) {
436                         maxv[1] = points2d[i][1];
437                 }
438         }
439         /* If not expanded, use a perfect square */
440         if (expand == false) {
441                 if (maxv[0] > maxv[1]) {
442                         maxv[1] = maxv[0];
443                 }
444                 else {
445                         maxv[0] = maxv[1];
446                 }
447         }
448 }
449
450 /* calc texture coordinates using flat projected points */
451 static void gp_calc_stroke_text_coordinates(const float(*points2d)[2], int totpoints, float minv[2], float maxv[2], float(*r_uv)[2])
452 {
453         float d[2];
454         d[0] = maxv[0] - minv[0];
455         d[1] = maxv[1] - minv[1];
456         for (int i = 0; i < totpoints; i++) {
457                 r_uv[i][0] = (points2d[i][0] - minv[0]) / d[0];
458                 r_uv[i][1] = (points2d[i][1] - minv[1]) / d[1];
459         }
460 }
461
462 /* Get points of stroke always flat to view not affected by camera view or view position */
463 static void gp_stroke_2d_flat(const bGPDspoint *points, int totpoints, float(*points2d)[2], int *r_direction)
464 {
465         const bGPDspoint *pt0 = &points[0];
466         const bGPDspoint *pt1 = &points[1];
467         const bGPDspoint *pt3 = &points[(int)(totpoints * 0.75)];
468
469         float locx[3];
470         float locy[3];
471         float loc3[3];
472         float normal[3];
473
474         /* local X axis (p0 -> p1) */
475         sub_v3_v3v3(locx, &pt1->x, &pt0->x);
476
477         /* point vector at 3/4 */
478         sub_v3_v3v3(loc3, &pt3->x, &pt0->x);
479
480         /* vector orthogonal to polygon plane */
481         cross_v3_v3v3(normal, locx, loc3);
482
483         /* local Y axis (cross to normal/x axis) */
484         cross_v3_v3v3(locy, normal, locx);
485
486         /* Normalize vectors */
487         normalize_v3(locx);
488         normalize_v3(locy);
489
490         /* Get all points in local space */
491         for (int i = 0; i < totpoints; i++) {
492                 const bGPDspoint *pt = &points[i];
493                 float loc[3];
494
495                 /* Get local space using first point as origin */
496                 sub_v3_v3v3(loc, &pt->x, &pt0->x);
497
498                 points2d[i][0] = dot_v3v3(loc, locx);
499                 points2d[i][1] = dot_v3v3(loc, locy);
500         }
501
502         /* Concave (-1), Convex (1), or Autodetect (0)? */
503         *r_direction = (int)locy[2];
504 }
505
506 /* Triangulate stroke for high quality fill (this is done only if cache is null or stroke was modified) */
507 static void gp_triangulate_stroke_fill(bGPDstroke *gps)
508 {
509         BLI_assert(gps->totpoints >= 3);
510
511         /* allocate memory for temporary areas */
512         gps->tot_triangles = gps->totpoints - 2;
513         uint (*tmp_triangles)[3] = MEM_mallocN(sizeof(*tmp_triangles) * gps->tot_triangles, "GP Stroke temp triangulation");
514         float (*points2d)[2] = MEM_mallocN(sizeof(*points2d) * gps->totpoints, "GP Stroke temp 2d points");
515         float(*uv)[2] = MEM_mallocN(sizeof(*uv) * gps->totpoints, "GP Stroke temp 2d uv data");
516
517         int direction = 0;
518
519         /* convert to 2d and triangulate */
520         gp_stroke_2d_flat(gps->points, gps->totpoints, points2d, &direction);
521         BLI_polyfill_calc(points2d, (uint)gps->totpoints, direction, tmp_triangles);
522
523         /* calc texture coordinates automatically */
524         float minv[2];
525         float maxv[2];
526         /* first needs bounding box data */
527         gp_calc_2d_bounding_box((const float(*)[2])points2d, gps->totpoints, minv, maxv, false);
528         /* calc uv data */
529         gp_calc_stroke_text_coordinates((const float(*)[2])points2d, gps->totpoints, minv, maxv, uv);
530
531         /* Number of triangles */
532         gps->tot_triangles = gps->totpoints - 2;
533         /* save triangulation data in stroke cache */
534         if (gps->tot_triangles > 0) {
535                 if (gps->triangles == NULL) {
536                         gps->triangles = MEM_callocN(sizeof(*gps->triangles) * gps->tot_triangles, "GP Stroke triangulation");
537                 }
538                 else {
539                         gps->triangles = MEM_recallocN(gps->triangles, sizeof(*gps->triangles) * gps->tot_triangles);
540                 }
541
542                 for (int i = 0; i < gps->tot_triangles; i++) {
543                         bGPDtriangle *stroke_triangle = &gps->triangles[i];
544                         memcpy(stroke_triangle->verts, tmp_triangles[i], sizeof(uint[3]));
545                         /* copy texture coordinates */
546                         copy_v2_v2(stroke_triangle->uv[0], uv[tmp_triangles[i][0]]);
547                         copy_v2_v2(stroke_triangle->uv[1], uv[tmp_triangles[i][1]]);
548                         copy_v2_v2(stroke_triangle->uv[2], uv[tmp_triangles[i][2]]);
549                 }
550         }
551         else {
552                 /* No triangles needed - Free anything allocated previously */
553                 if (gps->triangles)
554                         MEM_freeN(gps->triangles);
555
556                 gps->triangles = NULL;
557         }
558
559         /* disable recalculation flag */
560         if (gps->flag & GP_STROKE_RECALC_CACHES) {
561                 gps->flag &= ~GP_STROKE_RECALC_CACHES;
562         }
563
564         /* clear memory */
565         MEM_SAFE_FREE(tmp_triangles);
566         MEM_SAFE_FREE(points2d);
567         MEM_SAFE_FREE(uv);
568 }
569
570 /* add a new fill point and texture coordinates to vertex buffer */
571 static void gp_add_filldata_tobuffer(
572         const bGPDspoint *pt, const float uv[2], uint pos, uint texcoord, short flag,
573         int offsx, int offsy, int winx, int winy, const float diff_mat[4][4])
574 {
575         float fpt[3];
576         float co[2];
577
578         mul_v3_m4v3(fpt, diff_mat, &pt->x);
579         /* if 2d, need conversion */
580         if (!(flag & GP_STROKE_3DSPACE)) {
581                 gp_calc_2d_stroke_fxy(fpt, flag, offsx, offsy, winx, winy, co);
582                 copy_v2_v2(fpt, co);
583                 fpt[2] = 0.0f; /* 2d always is z=0.0f */
584         }
585
586         immAttr2f(texcoord, uv[0], uv[1]); /* texture coordinates */
587         immVertex3fv(pos, fpt); /* position */
588 }
589
590 #if 0 /* GPXX disabled, not used in annotations */
591 /* assign image texture for filling stroke */
592 static int gp_set_filling_texture(Image *image, short flag)
593 {
594         ImBuf *ibuf;
595         uint *bind = &image->bindcode[TEXTARGET_TEXTURE_2D];
596         int error = GL_NO_ERROR;
597         ImageUser iuser = { NULL };
598         void *lock;
599
600         iuser.ok = true;
601
602         ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
603
604         if (ibuf == NULL || ibuf->rect == NULL) {
605                 BKE_image_release_ibuf(image, ibuf, NULL);
606                 return (int)GL_INVALID_OPERATION;
607         }
608
609         GPU_create_gl_tex(bind, ibuf->rect, ibuf->rect_float, ibuf->x, ibuf->y, GL_TEXTURE_2D,
610                 false, false, image);
611
612         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
613         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
614         if (flag & GP_STYLE_COLOR_TEX_CLAMP) {
615                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
616                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
617         }
618         else {
619                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
620                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
621         }
622         BKE_image_release_ibuf(image, ibuf, NULL);
623
624         return error;
625 }
626 #endif
627
628 /* draw fills for shapes */
629 static void gp_draw_stroke_fill(
630         bGPdata *gpd, bGPDstroke *gps,
631         int offsx, int offsy, int winx, int winy, const float diff_mat[4][4], const float color[4])
632 {
633         BLI_assert(gps->totpoints >= 3);
634         Material *ma = gpd->mat[gps->mat_nr];
635         MaterialGPencilStyle *gp_style = ma->gp_style;
636
637         /* Calculate triangles cache for filling area (must be done only after changes) */
638         if ((gps->flag & GP_STROKE_RECALC_CACHES) || (gps->tot_triangles == 0) || (gps->triangles == NULL)) {
639                 gp_triangulate_stroke_fill(gps);
640         }
641         BLI_assert(gps->tot_triangles >= 1);
642
643         GPUVertFormat *format = immVertexFormat();
644         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
645         uint texcoord = GPU_vertformat_attr_add(format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
646         immBindBuiltinProgram(GPU_SHADER_GPENCIL_FILL);
647
648         immUniformColor4fv(color);
649         immUniform4fv("color2", gp_style->mix_rgba);
650         immUniform1i("fill_type", gp_style->fill_style);
651         immUniform1f("mix_factor", gp_style->mix_factor);
652
653         immUniform1f("gradient_angle", gp_style->gradient_angle);
654         immUniform1f("gradient_radius", gp_style->gradient_radius);
655         immUniform1f("pattern_gridsize", gp_style->pattern_gridsize);
656         immUniform2fv("gradient_scale", gp_style->gradient_scale);
657         immUniform2fv("gradient_shift", gp_style->gradient_shift);
658
659         immUniform1f("texture_angle", gp_style->texture_angle);
660         immUniform2fv("texture_scale", gp_style->texture_scale);
661         immUniform2fv("texture_offset", gp_style->texture_offset);
662         immUniform1f("texture_opacity", gp_style->texture_opacity);
663         immUniform1i("t_mix", (gp_style->flag & GP_STYLE_COLOR_TEX_MIX) != 0);
664         immUniform1i("t_flip", (gp_style->flag & GP_STYLE_COLOR_FLIP_FILL) != 0);
665 #if 0 /* GPXX disabled, not used in annotations */
666         /* image texture */
667         if ((gp_style->fill_style == GP_STYLE_FILL_STYLE_TEXTURE) || (gp_style->flag & GP_STYLE_COLOR_TEX_MIX)) {
668                 gp_set_filling_texture(gp_style->ima, gp_style->flag);
669         }
670 #endif
671         /* Draw all triangles for filling the polygon (cache must be calculated before) */
672         immBegin(GPU_PRIM_TRIS, gps->tot_triangles * 3);
673         /* TODO: use batch instead of immediate mode, to share vertices */
674
675         const bGPDtriangle *stroke_triangle = gps->triangles;
676         for (int i = 0; i < gps->tot_triangles; i++, stroke_triangle++) {
677                 for (int j = 0; j < 3; j++) {
678                         gp_add_filldata_tobuffer(
679                                 &gps->points[stroke_triangle->verts[j]], stroke_triangle->uv[j],
680                                 pos, texcoord, gps->flag,
681                                 offsx, offsy, winx, winy, diff_mat);
682                 }
683         }
684
685         immEnd();
686         immUnbindProgram();
687 }
688
689 /* ----- Existing Strokes Drawing (3D and Point) ------ */
690
691 /* draw a given stroke - just a single dot (only one point) */
692 static void gp_draw_stroke_point(
693         const bGPDspoint *points, short thickness, short UNUSED(dflag), short sflag,
694         int offsx, int offsy, int winx, int winy, const float diff_mat[4][4], const float ink[4])
695 {
696         const bGPDspoint *pt = points;
697
698         /* get final position using parent matrix */
699         float fpt[3];
700         mul_v3_m4v3(fpt, diff_mat, &pt->x);
701
702         GPUVertFormat *format = immVertexFormat();
703         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
704
705         if (sflag & GP_STROKE_3DSPACE) {
706                 immBindBuiltinProgram(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA);
707         }
708         else {
709                 immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA);
710
711                 /* get 2D coordinates of point */
712                 float co[3] = { 0.0f };
713                 gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, co);
714                 copy_v3_v3(fpt, co);
715         }
716
717         gp_set_point_uniform_color(pt, ink);
718         /* set point thickness (since there's only one of these) */
719         immUniform1f("size", (float)(thickness + 2) * pt->pressure);
720
721         immBegin(GPU_PRIM_POINTS, 1);
722         immVertex3fv(pos, fpt);
723         immEnd();
724
725         immUnbindProgram();
726 }
727
728 /* draw a given stroke in 3d (i.e. in 3d-space) */
729 static void gp_draw_stroke_3d(tGPDdraw *tgpw, short thickness, const float ink[4], bool cyclic)
730 {
731         bGPDspoint *points = tgpw->gps->points;
732         int totpoints = tgpw->gps->totpoints;
733
734         const float viewport[2] = { (float)tgpw->winx, (float)tgpw->winy };
735         float curpressure = points[0].pressure;
736         float fpt[3];
737
738         /* if cyclic needs more vertex */
739         int cyclic_add = (cyclic) ? 1 : 0;
740
741         GPUVertFormat *format = immVertexFormat();
742         const struct {
743                 uint pos, color, thickness;
744         } attr_id = {
745                 .pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT),
746                 .color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT),
747                 .thickness = GPU_vertformat_attr_add(format, "thickness", GPU_COMP_F32, 1, GPU_FETCH_FLOAT),
748         };
749
750         immBindBuiltinProgram(GPU_SHADER_GPENCIL_STROKE);
751         immUniform2fv("Viewport", viewport);
752         immUniform1f("pixsize", tgpw->rv3d->pixsize);
753         float obj_scale = tgpw->ob ? (tgpw->ob->size[0] + tgpw->ob->size[1] + tgpw->ob->size[2]) / 3.0f : 1.0f;
754
755         immUniform1f("objscale", obj_scale);
756         int keep_size = (int)((tgpw->gpd) && (tgpw->gpd->flag & GP_DATA_STROKE_KEEPTHICKNESS));
757         immUniform1i("keep_size", keep_size);
758         immUniform1i("pixfactor", tgpw->gpd->pixfactor);
759         immUniform1i("xraymode", tgpw->gpd->xray_mode);
760
761         /* draw stroke curve */
762         GPU_line_width(max_ff(curpressure * thickness, 1.0f));
763         immBeginAtMost(GPU_PRIM_LINE_STRIP_ADJ, totpoints + cyclic_add + 2);
764         const bGPDspoint *pt = points;
765
766         for (int i = 0; i < totpoints; i++, pt++) {
767                 /* first point for adjacency (not drawn) */
768                 if (i == 0) {
769                         gp_set_point_varying_color(points, ink, attr_id.color);
770                         immAttr1f(attr_id.thickness, max_ff(curpressure * thickness, 1.0f));
771                         if ((cyclic) && (totpoints > 2)) {
772                                 mul_v3_m4v3(fpt, tgpw->diff_mat, &(points + totpoints - 1)->x);
773                         }
774                         else {
775                                 mul_v3_m4v3(fpt, tgpw->diff_mat, &(points + 1)->x);
776                         }
777                         mul_v3_fl(fpt, -1.0f);
778                         immVertex3fv(attr_id.pos, fpt);
779                 }
780                 /* set point */
781                 gp_set_point_varying_color(pt, ink, attr_id.color);
782                 immAttr1f(attr_id.thickness, max_ff(curpressure * thickness, 1.0f));
783                 mul_v3_m4v3(fpt, tgpw->diff_mat, &pt->x);
784                 immVertex3fv(attr_id.pos, fpt);
785
786                 curpressure = pt->pressure;
787         }
788
789         if (cyclic && totpoints > 2) {
790                 /* draw line to first point to complete the cycle */
791                 immAttr1f(attr_id.thickness, max_ff(points->pressure * thickness, 1.0f));
792                 mul_v3_m4v3(fpt, tgpw->diff_mat, &points->x);
793                 immVertex3fv(attr_id.pos, fpt);
794
795                 /* now add adjacency point (not drawn) */
796                 immAttr1f(attr_id.thickness, max_ff((points + 1)->pressure * thickness, 1.0f));
797                 mul_v3_m4v3(fpt, tgpw->diff_mat, &(points + 1)->x);
798                 immVertex3fv(attr_id.pos, fpt);
799         }
800         /* last adjacency point (not drawn) */
801         else {
802                 gp_set_point_varying_color(points + totpoints - 1, ink, attr_id.color);
803                 immAttr1f(attr_id.thickness, max_ff(curpressure * thickness, 1.0f));
804                 mul_v3_m4v3(fpt, tgpw->diff_mat, &(points + totpoints - 2)->x);
805                 mul_v3_fl(fpt, -1.0f);
806                 immVertex3fv(attr_id.pos, fpt);
807         }
808
809         immEnd();
810         immUnbindProgram();
811 }
812
813 /* ----- Fancy 2D-Stroke Drawing ------ */
814
815 /* draw a given stroke in 2d */
816 static void gp_draw_stroke_2d(
817         const bGPDspoint *points, int totpoints, short thickness_s, short dflag, short sflag,
818         bool UNUSED(debug), int offsx, int offsy, int winx, int winy, const float diff_mat[4][4], const float ink[4])
819 {
820         /* otherwise thickness is twice that of the 3D view */
821         float thickness = (float)thickness_s * 0.5f;
822
823         /* strokes in Image Editor need a scale factor, since units there are not pixels! */
824         float scalefac  = 1.0f;
825         if ((dflag & GP_DRAWDATA_IEDITHACK) && (dflag & GP_DRAWDATA_ONLYV2D)) {
826                 scalefac = 0.001f;
827         }
828
829         /* TODO: fancy++ with the magic of shaders */
830
831         /* tessellation code - draw stroke as series of connected quads (triangle strips in fact) with connection
832          * edges rotated to minimize shrinking artifacts, and rounded endcaps
833          */
834         {
835                 const bGPDspoint *pt1, *pt2;
836                 float s0[2], s1[2];     /* segment 'center' points */
837                 float pm[2];  /* normal from previous segment. */
838                 int i;
839                 float fpt[3];
840
841                 GPUVertFormat *format = immVertexFormat();
842                 const struct {
843                         uint pos, color;
844                 } attr_id = {
845                         .pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT),
846                         .color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT),
847                 };
848
849                 immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
850                 immBegin(GPU_PRIM_TRI_STRIP, totpoints * 2 + 4);
851
852                 /* get x and y coordinates from first point */
853                 mul_v3_m4v3(fpt, diff_mat, &points->x);
854                 gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, s0);
855
856                 for (i = 0, pt1 = points, pt2 = points + 1; i < (totpoints - 1); i++, pt1++, pt2++) {
857                         float t0[2], t1[2];     /* tessellated coordinates */
858                         float m1[2], m2[2];     /* gradient and normal */
859                         float mt[2], sc[2];     /* gradient for thickness, point for end-cap */
860                         float pthick;           /* thickness at segment point */
861
862                         /* get x and y coordinates from point2 (point1 has already been computed in previous iteration). */
863                         mul_v3_m4v3(fpt, diff_mat, &pt2->x);
864                         gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, s1);
865
866                         /* calculate gradient and normal - 'angle'=(ny/nx) */
867                         m1[1] = s1[1] - s0[1];
868                         m1[0] = s1[0] - s0[0];
869                         normalize_v2(m1);
870                         m2[1] = -m1[0];
871                         m2[0] = m1[1];
872
873                         /* always use pressure from first point here */
874                         pthick = (pt1->pressure * thickness * scalefac);
875
876                         /* color of point */
877                         gp_set_point_varying_color(pt1, ink, attr_id.color);
878
879                         /* if the first segment, start of segment is segment's normal */
880                         if (i == 0) {
881                                 /* draw start cap first
882                                  * - make points slightly closer to center (about halfway across)
883                                  */
884                                 mt[0] = m2[0] * pthick * 0.5f;
885                                 mt[1] = m2[1] * pthick * 0.5f;
886                                 sc[0] = s0[0] - (m1[0] * pthick * 0.75f);
887                                 sc[1] = s0[1] - (m1[1] * pthick * 0.75f);
888
889                                 t0[0] = sc[0] - mt[0];
890                                 t0[1] = sc[1] - mt[1];
891                                 t1[0] = sc[0] + mt[0];
892                                 t1[1] = sc[1] + mt[1];
893
894                                 /* First two points of cap. */
895                                 immVertex2fv(attr_id.pos, t0);
896                                 immVertex2fv(attr_id.pos, t1);
897
898                                 /* calculate points for start of segment */
899                                 mt[0] = m2[0] * pthick;
900                                 mt[1] = m2[1] * pthick;
901
902                                 t0[0] = s0[0] - mt[0];
903                                 t0[1] = s0[1] - mt[1];
904                                 t1[0] = s0[0] + mt[0];
905                                 t1[1] = s0[1] + mt[1];
906
907                                 /* Last two points of start cap (and first two points of first segment). */
908                                 immVertex2fv(attr_id.pos, t0);
909                                 immVertex2fv(attr_id.pos, t1);
910                         }
911                         /* if not the first segment, use bisector of angle between segments */
912                         else {
913                                 float mb[2];         /* bisector normal */
914                                 float athick, dfac;  /* actual thickness, difference between thicknesses */
915
916                                 /* calculate gradient of bisector (as average of normals) */
917                                 mb[0] = (pm[0] + m2[0]) / 2;
918                                 mb[1] = (pm[1] + m2[1]) / 2;
919                                 normalize_v2(mb);
920
921                                 /* calculate gradient to apply
922                                  * - as basis, use just pthick * bisector gradient
923                                  * - if cross-section not as thick as it should be, add extra padding to fix it
924                                  */
925                                 mt[0] = mb[0] * pthick;
926                                 mt[1] = mb[1] * pthick;
927                                 athick = len_v2(mt);
928                                 dfac = pthick - (athick * 2);
929
930                                 if (((athick * 2.0f) < pthick) && (IS_EQF(athick, pthick) == 0)) {
931                                         mt[0] += (mb[0] * dfac);
932                                         mt[1] += (mb[1] * dfac);
933                                 }
934
935                                 /* calculate points for start of segment */
936                                 t0[0] = s0[0] - mt[0];
937                                 t0[1] = s0[1] - mt[1];
938                                 t1[0] = s0[0] + mt[0];
939                                 t1[1] = s0[1] + mt[1];
940
941                                 /* Last two points of previous segment, and first two points of current segment. */
942                                 immVertex2fv(attr_id.pos, t0);
943                                 immVertex2fv(attr_id.pos, t1);
944                         }
945
946                         /* if last segment, also draw end of segment (defined as segment's normal) */
947                         if (i == totpoints - 2) {
948                                 /* for once, we use second point's pressure (otherwise it won't be drawn) */
949                                 pthick = (pt2->pressure * thickness * scalefac);
950
951                                 /* color of point */
952                                 gp_set_point_varying_color(pt2, ink, attr_id.color);
953
954                                 /* calculate points for end of segment */
955                                 mt[0] = m2[0] * pthick;
956                                 mt[1] = m2[1] * pthick;
957
958                                 t0[0] = s1[0] - mt[0];
959                                 t0[1] = s1[1] - mt[1];
960                                 t1[0] = s1[0] + mt[0];
961                                 t1[1] = s1[1] + mt[1];
962
963                                 /* Last two points of last segment (and first two points of end cap). */
964                                 immVertex2fv(attr_id.pos, t0);
965                                 immVertex2fv(attr_id.pos, t1);
966
967                                 /* draw end cap as last step
968                                  * - make points slightly closer to center (about halfway across)
969                                  */
970                                 mt[0] = m2[0] * pthick * 0.5f;
971                                 mt[1] = m2[1] * pthick * 0.5f;
972                                 sc[0] = s1[0] + (m1[0] * pthick * 0.75f);
973                                 sc[1] = s1[1] + (m1[1] * pthick * 0.75f);
974
975                                 t0[0] = sc[0] - mt[0];
976                                 t0[1] = sc[1] - mt[1];
977                                 t1[0] = sc[0] + mt[0];
978                                 t1[1] = sc[1] + mt[1];
979
980                                 /* Last two points of end cap. */
981                                 immVertex2fv(attr_id.pos, t0);
982                                 immVertex2fv(attr_id.pos, t1);
983                         }
984
985                         /* store computed point2 coordinates as point1 ones of next segment. */
986                         copy_v2_v2(s0, s1);
987                         /* store stroke's 'natural' normal for next stroke to use */
988                         copy_v2_v2(pm, m2);
989                 }
990
991                 immEnd();
992                 immUnbindProgram();
993         }
994 }
995
996 /* ----- Strokes Drawing ------ */
997
998 /* Helper for doing all the checks on whether a stroke can be drawn */
999 static bool gp_can_draw_stroke(const bGPDstroke *gps, const int dflag)
1000 {
1001         /* skip stroke if it isn't in the right display space for this drawing context */
1002         /* 1) 3D Strokes */
1003         if ((dflag & GP_DRAWDATA_ONLY3D) && !(gps->flag & GP_STROKE_3DSPACE))
1004                 return false;
1005         if (!(dflag & GP_DRAWDATA_ONLY3D) && (gps->flag & GP_STROKE_3DSPACE))
1006                 return false;
1007
1008         /* 2) Screen Space 2D Strokes */
1009         if ((dflag & GP_DRAWDATA_ONLYV2D) && !(gps->flag & GP_STROKE_2DSPACE))
1010                 return false;
1011         if (!(dflag & GP_DRAWDATA_ONLYV2D) && (gps->flag & GP_STROKE_2DSPACE))
1012                 return false;
1013
1014         /* 3) Image Space (2D) */
1015         if ((dflag & GP_DRAWDATA_ONLYI2D) && !(gps->flag & GP_STROKE_2DIMAGE))
1016                 return false;
1017         if (!(dflag & GP_DRAWDATA_ONLYI2D) && (gps->flag & GP_STROKE_2DIMAGE))
1018                 return false;
1019
1020         /* skip stroke if it doesn't have any valid data */
1021         if ((gps->points == NULL) || (gps->totpoints < 1))
1022                 return false;
1023
1024         /* stroke can be drawn */
1025         return true;
1026 }
1027
1028 /* draw a set of strokes */
1029 static void gp_draw_strokes(tGPDdraw *tgpw)
1030 {
1031         float tcolor[4];
1032         float tfill[4];
1033         short sthickness;
1034         float ink[4];
1035
1036         GPU_enable_program_point_size();
1037
1038         for (bGPDstroke *gps = tgpw->t_gpf->strokes.first; gps; gps = gps->next) {
1039                 /* check if stroke can be drawn */
1040                 if (gp_can_draw_stroke(gps, tgpw->dflag) == false) {
1041                         continue;
1042                 }
1043                 /* check if the color is visible */
1044                 Material *ma = tgpw->gpd->mat[gps->mat_nr];
1045                 MaterialGPencilStyle *gp_style = ma->gp_style;
1046
1047                 if ((gp_style == NULL) ||
1048                     (gp_style->flag & GP_STYLE_COLOR_HIDE) ||
1049                     /* if onion and ghost flag do not draw*/
1050                     (tgpw->onion && (gp_style->flag & GP_STYLE_COLOR_ONIONSKIN)))
1051                 {
1052                         continue;
1053                 }
1054
1055                 /* if disable fill, the colors with fill must be omitted too except fill boundary strokes */
1056                 if ((tgpw->disable_fill == 1) &&
1057                     (gp_style->fill_rgba[3] > 0.0f) &&
1058                     ((gps->flag & GP_STROKE_NOFILL) == 0))
1059                 {
1060                         continue;
1061                 }
1062
1063                 /* calculate thickness */
1064                 sthickness = gps->thickness + tgpw->lthick;
1065
1066                 if (sthickness <= 0) {
1067                         continue;
1068                 }
1069
1070                 /* check which stroke-drawer to use */
1071                 if (tgpw->dflag & GP_DRAWDATA_ONLY3D) {
1072                         const int no_xray = (tgpw->dflag & GP_DRAWDATA_NO_XRAY);
1073                         int mask_orig = 0;
1074
1075                         if (no_xray) {
1076                                 glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
1077                                 glDepthMask(0);
1078                                 GPU_depth_test(true);
1079
1080                                 /* first arg is normally rv3d->dist, but this isn't
1081                                  * available here and seems to work quite well without */
1082                                 bglPolygonOffset(1.0f, 1.0f);
1083                         }
1084
1085                         /* 3D Fill */
1086                         //if ((dflag & GP_DRAWDATA_FILL) && (gps->totpoints >= 3)) {
1087                         if ((gps->totpoints >= 3) && (tgpw->disable_fill != 1)) {
1088                                 /* set color using material, tint color and opacity */
1089                                 interp_v3_v3v3(tfill, gp_style->fill_rgba, tgpw->tintcolor, tgpw->tintcolor[3]);
1090                                 tfill[3] = gp_style->fill_rgba[3] * tgpw->opacity;
1091                                 if ((tfill[3] > GPENCIL_ALPHA_OPACITY_THRESH) || (gp_style->fill_style > 0)) {
1092                                         const float *color;
1093                                         if (!tgpw->onion) {
1094                                                 color = tfill;
1095                                         }
1096                                         else {
1097                                                 if (tgpw->custonion) {
1098                                                         color = tgpw->tintcolor;
1099                                                 }
1100                                                 else {
1101                                                         ARRAY_SET_ITEMS(tfill, UNPACK3(gp_style->fill_rgba), tgpw->tintcolor[3]);
1102                                                         color = tfill;
1103                                                 }
1104                                         }
1105                                         gp_draw_stroke_fill(
1106                                                 tgpw->gpd, gps,
1107                                                 tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy, tgpw->diff_mat, color);
1108                                 }
1109                         }
1110
1111                         /* 3D Stroke */
1112                         /* set color using material tint color and opacity */
1113                         if (!tgpw->onion) {
1114                                 interp_v3_v3v3(tcolor, gp_style->stroke_rgba, tgpw->tintcolor, tgpw->tintcolor[3]);
1115                                 tcolor[3] = gp_style->stroke_rgba[3] * tgpw->opacity;
1116                                 copy_v4_v4(ink, tcolor);
1117                         }
1118                         else {
1119                                 if (tgpw->custonion) {
1120                                         copy_v4_v4(ink, tgpw->tintcolor);
1121                                 }
1122                                 else {
1123                                         ARRAY_SET_ITEMS(tcolor, UNPACK3(gp_style->stroke_rgba), tgpw->opacity);
1124                                         copy_v4_v4(ink, tcolor);
1125                                 }
1126                         }
1127                         if (gp_style->mode == GP_STYLE_MODE_DOTS) {
1128                                 /* volumetric stroke drawing */
1129                                 if (tgpw->disable_fill != 1) {
1130                                         gp_draw_stroke_volumetric_3d(gps->points, gps->totpoints, sthickness, ink);
1131                                 }
1132                         }
1133                         else {
1134                                 /* 3D Lines - OpenGL primitives-based */
1135                                 if (gps->totpoints == 1) {
1136                                         if (tgpw->disable_fill != 1) {
1137                                                 gp_draw_stroke_point(gps->points, sthickness, tgpw->dflag, gps->flag,
1138                                                         tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy,
1139                                                         tgpw->diff_mat, ink);
1140                                         }
1141                                 }
1142                                 else {
1143                                         tgpw->gps = gps;
1144                                         gp_draw_stroke_3d(tgpw, sthickness, ink, gps->flag & GP_STROKE_CYCLIC);
1145                                 }
1146                         }
1147                         if (no_xray) {
1148                                 glDepthMask(mask_orig);
1149                                 GPU_depth_test(false);
1150
1151                                 bglPolygonOffset(0.0, 0.0);
1152                         }
1153                 }
1154                 else {
1155                         /* 2D - Fill */
1156                         if (gps->totpoints >= 3) {
1157                                 /* set color using material, tint color and opacity */
1158                                 interp_v3_v3v3(tfill, gp_style->fill_rgba, tgpw->tintcolor, tgpw->tintcolor[3]);
1159                                 tfill[3] = gp_style->fill_rgba[3] * tgpw->opacity;
1160                                 if ((tfill[3] > GPENCIL_ALPHA_OPACITY_THRESH) || (gp_style->fill_style > 0)) {
1161                                         const float *color;
1162                                         if (!tgpw->onion) {
1163                                                 color = tfill;
1164                                         }
1165                                         else {
1166                                                 if (tgpw->custonion) {
1167                                                         color = tgpw->tintcolor;
1168                                                 }
1169                                                 else {
1170                                                         ARRAY_SET_ITEMS(tfill, UNPACK3(gp_style->fill_rgba), tgpw->tintcolor[3]);
1171                                                         color = tfill;
1172                                                 }
1173                                         }
1174                                         gp_draw_stroke_fill(
1175                                                 tgpw->gpd, gps,
1176                                                 tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy, tgpw->diff_mat, color);
1177                                 }
1178                         }
1179
1180                         /* 2D Strokes... */
1181                         /* set color using material, tint color and opacity */
1182                         if (!tgpw->onion) {
1183                                 interp_v3_v3v3(tcolor, gp_style->stroke_rgba, tgpw->tintcolor, tgpw->tintcolor[3]);
1184                                 tcolor[3] = gp_style->stroke_rgba[3] * tgpw->opacity;
1185                                 copy_v4_v4(ink, tcolor);
1186                         }
1187                         else {
1188                                 if (tgpw->custonion) {
1189                                         copy_v4_v4(ink, tgpw->tintcolor);
1190                                 }
1191                                 else {
1192                                         ARRAY_SET_ITEMS(tcolor, UNPACK3(gp_style->stroke_rgba), tgpw->opacity);
1193                                         copy_v4_v4(ink, tcolor);
1194                                 }
1195                         }
1196                         if (gp_style->mode == GP_STYLE_MODE_DOTS) {
1197                                 /* blob/disk-based "volumetric" drawing */
1198                                 gp_draw_stroke_volumetric_2d(
1199                                         gps->points, gps->totpoints, sthickness, tgpw->dflag, gps->flag,
1200                                         tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy, tgpw->diff_mat, ink);
1201                         }
1202                         else {
1203                                 /* normal 2D strokes */
1204                                 if (gps->totpoints == 1) {
1205                                         gp_draw_stroke_point(
1206                                                 gps->points, sthickness, tgpw->dflag, gps->flag,
1207                                                 tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy,
1208                                                 tgpw->diff_mat, ink);
1209                                 }
1210                                 else {
1211                                         gp_draw_stroke_2d(
1212                                                 gps->points, gps->totpoints, sthickness, tgpw->dflag, gps->flag, false,
1213                                                 tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy, tgpw->diff_mat, ink);
1214                                 }
1215                         }
1216                 }
1217         }
1218
1219         GPU_disable_program_point_size();
1220 }
1221
1222 /* Draw selected verts for strokes being edited */
1223 static void gp_draw_strokes_edit(
1224         bGPdata *gpd, const bGPDframe *gpf, int offsx, int offsy, int winx, int winy, short dflag,
1225         short lflag, const float diff_mat[4][4], float alpha)
1226 {
1227         /* if alpha 0 do not draw */
1228         if (alpha == 0.0f)
1229                 return;
1230
1231         const bool no_xray = (dflag & GP_DRAWDATA_NO_XRAY) != 0;
1232         int mask_orig = 0;
1233
1234         /* set up depth masks... */
1235         if (dflag & GP_DRAWDATA_ONLY3D) {
1236                 if (no_xray) {
1237                         glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
1238                         glDepthMask(0);
1239                         GPU_depth_test(true);
1240
1241                         /* first arg is normally rv3d->dist, but this isn't
1242                          * available here and seems to work quite well without */
1243                         bglPolygonOffset(1.0f, 1.0f);
1244                 }
1245         }
1246
1247         GPU_enable_program_point_size();
1248
1249         /* draw stroke verts */
1250         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
1251                 /* check if stroke can be drawn */
1252                 if (gp_can_draw_stroke(gps, dflag) == false)
1253                         continue;
1254
1255                 /* Optimisation: only draw points for selected strokes
1256                  * We assume that selected points can only occur in
1257                  * strokes that are selected too.
1258                  */
1259                 if ((gps->flag & GP_STROKE_SELECT) == 0)
1260                         continue;
1261
1262                 /* verify color lock */
1263                 {
1264                         Material *ma = gpd->mat[gps->mat_nr];
1265                         MaterialGPencilStyle *gp_style = ma->gp_style;
1266
1267                         if (gp_style != NULL) {
1268                                 if (gp_style->flag & GP_STYLE_COLOR_HIDE) {
1269                                         continue;
1270                                 }
1271                                 if (((lflag & GP_LAYER_UNLOCK_COLOR) == 0) && (gp_style->flag & GP_STYLE_COLOR_LOCKED)) {
1272                                         continue;
1273                                 }
1274                         }
1275                 }
1276
1277                 /* Get size of verts:
1278                  * - The selected state needs to be larger than the unselected state so that
1279                  *   they stand out more.
1280                  * - We use the theme setting for size of the unselected verts
1281                  */
1282                 float bsize = UI_GetThemeValuef(TH_GP_VERTEX_SIZE);
1283                 float vsize;
1284                 if ((int)bsize > 8) {
1285                         vsize = 10.0f;
1286                         bsize = 8.0f;
1287                 }
1288                 else {
1289                         vsize = bsize + 2;
1290                 }
1291
1292                 /* for now, we assume that the base color of the points is not too close to the real color */
1293                 /* set color using material */
1294                 Material *ma = gpd->mat[gps->mat_nr];
1295                 MaterialGPencilStyle *gp_style = ma->gp_style;
1296
1297                 float selectColor[4];
1298                 UI_GetThemeColor3fv(TH_GP_VERTEX_SELECT, selectColor);
1299                 selectColor[3] = alpha;
1300
1301                 GPUVertFormat *format = immVertexFormat();
1302                 uint pos; /* specified later */
1303                 uint size = GPU_vertformat_attr_add(format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
1304                 uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
1305
1306                 if (gps->flag & GP_STROKE_3DSPACE) {
1307                         pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
1308                         immBindBuiltinProgram(GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR);
1309                 }
1310                 else {
1311                         pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
1312                         immBindBuiltinProgram(GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR);
1313                 }
1314
1315                 immBegin(GPU_PRIM_POINTS, gps->totpoints);
1316
1317                 /* Draw start and end point differently if enabled stroke direction hint */
1318                 bool show_direction_hint = (gpd->flag & GP_DATA_SHOW_DIRECTION) && (gps->totpoints > 1);
1319
1320                 /* Draw all the stroke points (selected or not) */
1321                 bGPDspoint *pt = gps->points;
1322                 float fpt[3];
1323                 for (int i = 0; i < gps->totpoints; i++, pt++) {
1324                         /* size and color first */
1325                         if (show_direction_hint && i == 0) {
1326                                 /* start point in green bigger */
1327                                 immAttr3f(color, 0.0f, 1.0f, 0.0f);
1328                                 immAttr1f(size, vsize + 4);
1329                         }
1330                         else if (show_direction_hint && (i == gps->totpoints - 1)) {
1331                                 /* end point in red smaller */
1332                                 immAttr3f(color, 1.0f, 0.0f, 0.0f);
1333                                 immAttr1f(size, vsize + 1);
1334                         }
1335                         else if (pt->flag & GP_SPOINT_SELECT) {
1336                                 immAttr3fv(color, selectColor);
1337                                 immAttr1f(size, vsize);
1338                         }
1339                         else {
1340                                 immAttr3fv(color, gp_style->stroke_rgba);
1341                                 immAttr1f(size, bsize);
1342                         }
1343
1344                         /* then position */
1345                         if (gps->flag & GP_STROKE_3DSPACE) {
1346                                 mul_v3_m4v3(fpt, diff_mat, &pt->x);
1347                                 immVertex3fv(pos, fpt);
1348                         }
1349                         else {
1350                                 float co[2];
1351                                 mul_v3_m4v3(fpt, diff_mat, &pt->x);
1352                                 gp_calc_2d_stroke_fxy(fpt, gps->flag, offsx, offsy, winx, winy, co);
1353                                 immVertex2fv(pos, co);
1354                         }
1355                 }
1356
1357                 immEnd();
1358                 immUnbindProgram();
1359         }
1360
1361         GPU_disable_program_point_size();
1362
1363         /* clear depth mask */
1364         if (dflag & GP_DRAWDATA_ONLY3D) {
1365                 if (no_xray) {
1366                         glDepthMask(mask_orig);
1367                         GPU_depth_test(false);
1368
1369                         bglPolygonOffset(0.0, 0.0);
1370 #if 0
1371                         glDisable(GL_POLYGON_OFFSET_LINE);
1372                         glPolygonOffset(0, 0);
1373 #endif
1374                 }
1375         }
1376 }
1377
1378 /* ----- General Drawing ------ */
1379
1380
1381 /* draw interpolate strokes (used only while operator is running) */
1382 void ED_gp_draw_interpolation(const bContext *C, tGPDinterpolate *tgpi, const int type)
1383 {
1384         tGPDdraw tgpw;
1385         ARegion *ar = CTX_wm_region(C);
1386         RegionView3D *rv3d = ar->regiondata;
1387         tGPDinterpolate_layer *tgpil;
1388         Object *obact = CTX_data_active_object(C);
1389         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1390
1391         float color[4];
1392
1393         UI_GetThemeColor3fv(TH_GP_VERTEX_SELECT, color);
1394         color[3] = 0.6f;
1395         int dflag = 0;
1396         /* if 3d stuff, enable flags */
1397         if (type == REGION_DRAW_POST_VIEW) {
1398                 dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS);
1399         }
1400
1401         tgpw.rv3d = rv3d;
1402         tgpw.depsgraph = depsgraph;
1403         tgpw.ob = obact;
1404         tgpw.gpd = tgpi->gpd;
1405         tgpw.offsx = 0;
1406         tgpw.offsy = 0;
1407         tgpw.winx = tgpi->ar->winx;
1408         tgpw.winy = tgpi->ar->winy;
1409         tgpw.dflag = dflag;
1410
1411         /* turn on alpha-blending */
1412         glEnable(GL_BLEND);
1413         for (tgpil = tgpi->ilayers.first; tgpil; tgpil = tgpil->next) {
1414                 /* calculate parent position */
1415                 ED_gpencil_parent_location(depsgraph, obact, tgpi->gpd, tgpil->gpl, tgpw.diff_mat);
1416                 if (tgpil->interFrame) {
1417                         tgpw.gpl = tgpil->gpl;
1418                         tgpw.gpf = tgpil->interFrame;
1419                         tgpw.t_gpf = tgpil->interFrame;
1420
1421                         tgpw.lthick = tgpil->gpl->line_change;
1422                         tgpw.opacity = 1.0;
1423                         copy_v4_v4(tgpw.tintcolor, color);
1424                         tgpw.onion = true;
1425                         tgpw.custonion = true;
1426
1427                         gp_draw_strokes(&tgpw);
1428                 }
1429         }
1430         glDisable(GL_BLEND);
1431 }
1432
1433 /* wrapper to draw strokes for filling operator */
1434 void ED_gp_draw_fill(tGPDdraw *tgpw)
1435 {
1436         gp_draw_strokes(tgpw);
1437 }
1438
1439 /* loop over gpencil data layers, drawing them */
1440 static void gp_draw_data_layers(RegionView3D *rv3d,
1441         const Brush *brush, float alpha, Object *ob, bGPdata *gpd,
1442         int offsx, int offsy, int winx, int winy, int cfra, int dflag)
1443 {
1444         float diff_mat[4][4];
1445         tGPDdraw tgpw;
1446
1447         tgpw.rv3d = rv3d;
1448         tgpw.depsgraph = NULL; /* XXX: This is not used here */
1449         tgpw.ob = ob;
1450         tgpw.gpd = gpd;
1451         tgpw.gpl = NULL;
1452         tgpw.gpf = NULL;
1453         tgpw.t_gpf = NULL;
1454         tgpw.offsx = offsx;
1455         tgpw.offsy = offsy;
1456         tgpw.winx = winx;
1457         tgpw.winy = winy;
1458         tgpw.dflag = dflag;
1459
1460         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1461                 /* calculate parent position */
1462                 ED_gpencil_parent_location(tgpw.depsgraph, ob, gpd, gpl, diff_mat);
1463
1464                 short lthick = brush->size + gpl->line_change;
1465
1466                 /* don't draw layer if hidden */
1467                 if (gpl->flag & GP_LAYER_HIDE)
1468                         continue;
1469
1470                 /* get frame to draw */
1471                 bGPDframe *gpf = BKE_gpencil_layer_getframe(gpl, cfra, GP_GETFRAME_USE_PREV);
1472                 if (gpf == NULL)
1473                         continue;
1474
1475                 /* set basic stroke thickness */
1476                 GPU_line_width(lthick);
1477
1478                 /* Add layer drawing settings to the set of "draw flags"
1479                  * NOTE: If the setting doesn't apply, it *must* be cleared,
1480                  *       as dflag's carry over from the previous layer
1481                  */
1482
1483                 /* xray... */
1484                 SET_FLAG_FROM_TEST(dflag, gpl->flag & GP_LAYER_NO_XRAY, GP_DRAWDATA_NO_XRAY);
1485
1486                 /* volumetric strokes... */
1487                 SET_FLAG_FROM_TEST(dflag, gpl->flag & GP_LAYER_VOLUMETRIC, GP_DRAWDATA_VOLUMETRIC);
1488
1489                 tgpw.gpl = gpl;
1490                 tgpw.gpf = gpf;
1491                 tgpw.t_gpf = gpf; // XXX?
1492                 tgpw.lthick = gpl->line_change;
1493                 tgpw.opacity = gpl->opacity;
1494                 copy_v4_v4(tgpw.tintcolor, gpl->tintcolor);
1495                 tgpw.onion = false;
1496                 tgpw.custonion = false;
1497                 copy_m4_m4(tgpw.diff_mat, diff_mat);
1498
1499                 /* draw the strokes already in active frame */
1500                 gp_draw_strokes(&tgpw);
1501
1502                 /* Draw verts of selected strokes
1503                  * - when doing OpenGL renders, we don't want to be showing these, as that ends up flickering
1504                  *   - locked layers can't be edited, so there's no point showing these verts
1505                  *     as they will have no bearings on what gets edited
1506                  * - only show when in editmode, since operators shouldn't work otherwise
1507                  *   (NOTE: doing it this way means that the toggling editmode shows visible change immediately)
1508                  */
1509                 /* XXX: perhaps we don't want to show these when users are drawing... */
1510                 if ((G.f & G_RENDER_OGL) == 0 &&
1511                     (gpl->flag & GP_LAYER_LOCKED) == 0 &&
1512                     (gpd->flag & GP_DATA_STROKE_EDITMODE))
1513                 {
1514                         gp_draw_strokes_edit(gpd, gpf, offsx, offsy, winx, winy, dflag, gpl->flag, diff_mat, alpha);
1515                 }
1516
1517                 /* Check if may need to draw the active stroke cache, only if this layer is the active layer
1518                  * that is being edited. (Stroke buffer is currently stored in gp-data)
1519                  */
1520                 if (ED_gpencil_session_active() && (gpl->flag & GP_LAYER_ACTIVE) &&
1521                     (gpf->flag & GP_FRAME_PAINT))
1522                 {
1523                         /* Buffer stroke needs to be drawn with a different linestyle
1524                          * to help differentiate them from normal strokes.
1525                          *
1526                          * It should also be noted that sbuffer contains temporary point types
1527                          * i.e. tGPspoints NOT bGPDspoints
1528                          */
1529                         if (gpd->runtime.mode == GP_STYLE_MODE_DOTS) {
1530                                 gp_draw_stroke_volumetric_buffer(
1531                                         gpd->runtime.sbuffer,
1532                                         gpd->runtime.sbuffer_size, lthick,
1533                                         dflag, gpd->runtime.scolor);
1534                         }
1535                         else {
1536                                 gp_draw_stroke_buffer(
1537                                         gpd->runtime.sbuffer,
1538                                         gpd->runtime.sbuffer_size, lthick,
1539                                         dflag, gpd->runtime.sbuffer_sflag,
1540                                         gpd->runtime.scolor, gpd->runtime.sfill);
1541                         }
1542                 }
1543         }
1544 }
1545
1546 /* draw a short status message in the top-right corner */
1547 static void UNUSED_FUNCTION(gp_draw_status_text)(const bGPdata *gpd, ARegion *ar)
1548 {
1549         rcti rect;
1550
1551         /* Cannot draw any status text when drawing OpenGL Renders */
1552         if (G.f & G_RENDER_OGL)
1553                 return;
1554
1555         /* Get bounds of region - Necessary to avoid problems with region overlap */
1556         ED_region_visible_rect(ar, &rect);
1557
1558         /* for now, this should only be used to indicate when we are in stroke editmode */
1559         if (gpd->flag & GP_DATA_STROKE_EDITMODE) {
1560                 const char *printable = IFACE_("GPencil Stroke Editing");
1561                 float       printable_size[2];
1562
1563                 int font_id = BLF_default();
1564
1565                 BLF_width_and_height(font_id, printable, BLF_DRAW_STR_DUMMY_MAX, &printable_size[0], &printable_size[1]);
1566
1567                 int xco = (rect.xmax - U.widget_unit) - (int)printable_size[0];
1568                 int yco = (rect.ymax - U.widget_unit);
1569
1570                 /* text label */
1571                 UI_FontThemeColor(font_id, TH_TEXT_HI);
1572 #ifdef WITH_INTERNATIONAL
1573                 BLF_draw_default(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1574 #else
1575                 BLF_draw_default_ascii(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1576 #endif
1577
1578                 /* grease pencil icon... */
1579                 // XXX: is this too intrusive?
1580                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1581                 GPU_blend(true);
1582
1583                 xco -= U.widget_unit;
1584                 yco -= (int)printable_size[1] / 2;
1585
1586                 UI_icon_draw(xco, yco, ICON_GREASEPENCIL);
1587
1588                 GPU_blend(false);
1589         }
1590 }
1591
1592 /* draw grease-pencil datablock */
1593 static void gp_draw_data(RegionView3D *rv3d,
1594         const Brush *brush, float alpha, Object *ob, bGPdata *gpd,
1595         int offsx, int offsy, int winx, int winy, int cfra, int dflag)
1596 {
1597         /* turn on smooth lines (i.e. anti-aliasing) */
1598         GPU_line_smooth(true);
1599
1600         /* XXX: turn on some way of ensuring that the polygon edges get smoothed
1601          *      GL_POLYGON_SMOOTH is nasty and shouldn't be used, as it ends up
1602          *      creating internal white rays due to the ways it accumulates stuff
1603          */
1604
1605         /* turn on alpha-blending */
1606         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1607         GPU_blend(true);
1608
1609         /* draw! */
1610         gp_draw_data_layers(rv3d, brush, alpha, ob, gpd, offsx, offsy, winx, winy, cfra, dflag);
1611
1612         /* turn off alpha blending, then smooth lines */
1613         GPU_blend(false); // alpha blending
1614         GPU_line_smooth(false); // smooth lines
1615 }
1616
1617 /* if we have strokes for scenes (3d view)/clips (movie clip editor)
1618  * and objects/tracks, multiple data blocks have to be drawn */
1619 static void gp_draw_data_all(
1620         ViewLayer *view_layer, RegionView3D *rv3d, Scene *scene, bGPdata *gpd,
1621         int offsx, int offsy, int winx, int winy,
1622         int cfra, int dflag, const char UNUSED(spacetype))
1623 {
1624         bGPdata *gpd_source = NULL;
1625         Brush *brush = NULL;
1626         Object *ob = OBACT(view_layer);
1627         if (scene) {
1628                 ToolSettings *ts = scene->toolsettings;
1629                 brush = BKE_paint_brush(&ts->gp_paint->paint);
1630
1631                 if (gpd_source) {
1632                         if (brush != NULL) {
1633                                 gp_draw_data(
1634                                         rv3d, brush, 1.0f, ob, gpd_source,
1635                                         offsx, offsy, winx, winy, cfra, dflag);
1636                         }
1637                 }
1638         }
1639
1640         /* scene/clip data has already been drawn, only object/track data is drawn here
1641          * if gpd_source == gpd, we don't have any object/track data and we can skip */
1642         if (gpd_source == NULL || (gpd_source && gpd_source != gpd)) {
1643                 if (brush != NULL) {
1644                         gp_draw_data(
1645                                 rv3d, brush, 1.0f, ob, gpd,
1646                                 offsx, offsy, winx, winy, cfra, dflag);
1647                 }
1648         }
1649 }
1650
1651 /* ----- Grease Pencil Sketches Drawing API ------ */
1652
1653
1654 /* draw grease-pencil sketches to specified 3d-view assuming that matrices are already set correctly
1655  * Note: this gets called twice - first time with only3d=true to draw 3d-strokes,
1656  * second time with only3d=false for screen-aligned strokes */
1657 void ED_gpencil_draw_view3d(
1658         wmWindowManager *wm,
1659         Scene *scene,
1660         ViewLayer *view_layer,
1661         struct Depsgraph *depsgraph,
1662         View3D *v3d,
1663         ARegion *ar,
1664         bool only3d)
1665 {
1666         int dflag = 0;
1667         RegionView3D *rv3d = ar->regiondata;
1668         int offsx,  offsy,  winx,  winy;
1669
1670         /* check that we have grease-pencil stuff to draw */
1671         // XXX: This is the only place that still uses this function
1672         bGPdata *gpd = ED_gpencil_data_get_active_v3d(view_layer, v3d);
1673         if (gpd == NULL) return;
1674
1675         /* when rendering to the offscreen buffer we don't want to
1676          * deal with the camera border, otherwise map the coords to the camera border. */
1677         if ((rv3d->persp == RV3D_CAMOB) && !(G.f & G_RENDER_OGL)) {
1678                 rctf rectf;
1679                 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &rectf, true); /* no shift */
1680
1681                 offsx = round_fl_to_int(rectf.xmin);
1682                 offsy = round_fl_to_int(rectf.ymin);
1683                 winx  = round_fl_to_int(rectf.xmax - rectf.xmin);
1684                 winy  = round_fl_to_int(rectf.ymax - rectf.ymin);
1685         }
1686         else {
1687                 offsx = 0;
1688                 offsy = 0;
1689                 winx  = ar->winx;
1690                 winy  = ar->winy;
1691         }
1692
1693         /* set flags */
1694         if (only3d) {
1695                 /* 3D strokes/3D space:
1696                  * - only 3D space points
1697                  * - don't status text either (as it's the wrong space)
1698                  */
1699                 dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS);
1700         }
1701
1702         if (v3d->flag2 & V3D_RENDER_OVERRIDE) {
1703                 /* don't draw status text when "only render" flag is set */
1704                 dflag |= GP_DRAWDATA_NOSTATUS;
1705         }
1706
1707         if ((wm == NULL) || ED_screen_animation_playing(wm)) {
1708                 /* don't show onion-skins during animation playback/scrub (i.e. it obscures the poses)
1709                  * OpenGL Renders (i.e. final output), or depth buffer (i.e. not real strokes)
1710                  */
1711                 dflag |= GP_DRAWDATA_NO_ONIONS;
1712         }
1713
1714         /* draw it! */
1715         gp_draw_data_all(view_layer, rv3d, scene, gpd, offsx, offsy, winx, winy, CFRA, dflag, v3d->spacetype);
1716 }
1717
1718 /* draw grease-pencil sketches to specified 3d-view for gp object
1719  * assuming that matrices are already set correctly
1720  */
1721 void ED_gpencil_draw_view3d_object(wmWindowManager *wm, Scene *scene, Depsgraph *depsgraph, Object *ob, View3D *v3d, ARegion *ar, bool only3d)
1722 {
1723         int dflag = 0;
1724         RegionView3D *rv3d = ar->regiondata;
1725         int offsx, offsy, winx, winy;
1726
1727         /* check that we have grease-pencil stuff to draw */
1728         bGPdata *gpd = ob->data;
1729         if (gpd == NULL) return;
1730
1731         /* when rendering to the offscreen buffer we don't want to
1732          * deal with the camera border, otherwise map the coords to the camera border. */
1733         if ((rv3d->persp == RV3D_CAMOB) && !(G.f & G_RENDER_OGL)) {
1734                 rctf rectf;
1735                 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &rectf, true); /* no shift */
1736
1737                 offsx = round_fl_to_int(rectf.xmin);
1738                 offsy = round_fl_to_int(rectf.ymin);
1739                 winx = round_fl_to_int(rectf.xmax - rectf.xmin);
1740                 winy = round_fl_to_int(rectf.ymax - rectf.ymin);
1741         }
1742         else {
1743                 offsx = 0;
1744                 offsy = 0;
1745                 winx = ar->winx;
1746                 winy = ar->winy;
1747         }
1748
1749         /* set flags */
1750         if (only3d) {
1751                 /* 3D strokes/3D space:
1752                  * - only 3D space points
1753                  * - don't status text either (as it's the wrong space)
1754                  */
1755                 dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS);
1756         }
1757
1758         if (v3d->flag2 & V3D_RENDER_OVERRIDE) {
1759                 /* don't draw status text when "only render" flag is set */
1760                 dflag |= GP_DRAWDATA_NOSTATUS;
1761         }
1762
1763         if ((wm == NULL) || ED_screen_animation_playing(wm)) {
1764                 /* don't show onionskins during animation playback/scrub (i.e. it obscures the poses)
1765                  * OpenGL Renders (i.e. final output), or depth buffer (i.e. not real strokes)
1766                  */
1767                 dflag |= GP_DRAWDATA_NO_ONIONS;
1768         }
1769
1770         /* draw it! */
1771         ToolSettings *ts = scene->toolsettings;
1772         Brush *brush = BKE_paint_brush(&ts->gp_paint->paint);
1773         if (brush != NULL) {
1774                 gp_draw_data(rv3d, brush, 1.0f, ob, gpd,
1775                         offsx, offsy, winx, winy, CFRA, dflag);
1776         }
1777 }
1778
1779 void ED_gpencil_draw_ex(
1780         ViewLayer *view_layer, RegionView3D *rv3d, Scene *scene,
1781         bGPdata *gpd, int winx, int winy, const int cfra, const char spacetype)
1782 {
1783         int dflag = GP_DRAWDATA_NOSTATUS | GP_DRAWDATA_ONLYV2D;
1784
1785         gp_draw_data_all(view_layer, rv3d, scene, gpd, 0, 0, winx, winy, cfra, dflag, spacetype);
1786 }