5bb2d374c804ac1af13fcc6ca87e775eab44e850
[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 /* Triangulate stroke for high quality fill (this is done only if cache is null or stroke was modified) */
463 static void gp_triangulate_stroke_fill(bGPDstroke *gps)
464 {
465         BLI_assert(gps->totpoints >= 3);
466
467         /* allocate memory for temporary areas */
468         gps->tot_triangles = gps->totpoints - 2;
469         uint (*tmp_triangles)[3] = MEM_mallocN(sizeof(*tmp_triangles) * gps->tot_triangles, "GP Stroke temp triangulation");
470         float (*points2d)[2] = MEM_mallocN(sizeof(*points2d) * gps->totpoints, "GP Stroke temp 2d points");
471         float(*uv)[2] = MEM_mallocN(sizeof(*uv) * gps->totpoints, "GP Stroke temp 2d uv data");
472
473         int direction = 0;
474
475         /* convert to 2d and triangulate */
476         BKE_gpencil_stroke_2d_flat(gps->points, gps->totpoints, points2d, &direction);
477         BLI_polyfill_calc(points2d, (uint)gps->totpoints, direction, tmp_triangles);
478
479         /* calc texture coordinates automatically */
480         float minv[2];
481         float maxv[2];
482         /* first needs bounding box data */
483         gp_calc_2d_bounding_box((const float(*)[2])points2d, gps->totpoints, minv, maxv, false);
484         /* calc uv data */
485         gp_calc_stroke_text_coordinates((const float(*)[2])points2d, gps->totpoints, minv, maxv, uv);
486
487         /* Number of triangles */
488         gps->tot_triangles = gps->totpoints - 2;
489         /* save triangulation data in stroke cache */
490         if (gps->tot_triangles > 0) {
491                 if (gps->triangles == NULL) {
492                         gps->triangles = MEM_callocN(sizeof(*gps->triangles) * gps->tot_triangles, "GP Stroke triangulation");
493                 }
494                 else {
495                         gps->triangles = MEM_recallocN(gps->triangles, sizeof(*gps->triangles) * gps->tot_triangles);
496                 }
497
498                 for (int i = 0; i < gps->tot_triangles; i++) {
499                         bGPDtriangle *stroke_triangle = &gps->triangles[i];
500                         memcpy(stroke_triangle->verts, tmp_triangles[i], sizeof(uint[3]));
501                         /* copy texture coordinates */
502                         copy_v2_v2(stroke_triangle->uv[0], uv[tmp_triangles[i][0]]);
503                         copy_v2_v2(stroke_triangle->uv[1], uv[tmp_triangles[i][1]]);
504                         copy_v2_v2(stroke_triangle->uv[2], uv[tmp_triangles[i][2]]);
505                 }
506         }
507         else {
508                 /* No triangles needed - Free anything allocated previously */
509                 if (gps->triangles)
510                         MEM_freeN(gps->triangles);
511
512                 gps->triangles = NULL;
513         }
514
515         /* disable recalculation flag */
516         if (gps->flag & GP_STROKE_RECALC_GEOMETRY) {
517                 gps->flag &= ~GP_STROKE_RECALC_GEOMETRY;
518         }
519
520         /* clear memory */
521         MEM_SAFE_FREE(tmp_triangles);
522         MEM_SAFE_FREE(points2d);
523         MEM_SAFE_FREE(uv);
524 }
525
526 /* add a new fill point and texture coordinates to vertex buffer */
527 static void gp_add_filldata_tobuffer(
528         const bGPDspoint *pt, const float uv[2], uint pos, uint texcoord, short flag,
529         int offsx, int offsy, int winx, int winy, const float diff_mat[4][4])
530 {
531         float fpt[3];
532         float co[2];
533
534         mul_v3_m4v3(fpt, diff_mat, &pt->x);
535         /* if 2d, need conversion */
536         if (!(flag & GP_STROKE_3DSPACE)) {
537                 gp_calc_2d_stroke_fxy(fpt, flag, offsx, offsy, winx, winy, co);
538                 copy_v2_v2(fpt, co);
539                 fpt[2] = 0.0f; /* 2d always is z=0.0f */
540         }
541
542         immAttr2f(texcoord, uv[0], uv[1]); /* texture coordinates */
543         immVertex3fv(pos, fpt); /* position */
544 }
545
546 #if 0 /* GPXX disabled, not used in annotations */
547 /* assign image texture for filling stroke */
548 static int gp_set_filling_texture(Image *image, short flag)
549 {
550         ImBuf *ibuf;
551         uint *bind = &image->bindcode[TEXTARGET_TEXTURE_2D];
552         int error = GL_NO_ERROR;
553         ImageUser iuser = { NULL };
554         void *lock;
555
556         iuser.ok = true;
557
558         ibuf = BKE_image_acquire_ibuf(image, &iuser, &lock);
559
560         if (ibuf == NULL || ibuf->rect == NULL) {
561                 BKE_image_release_ibuf(image, ibuf, NULL);
562                 return (int)GL_INVALID_OPERATION;
563         }
564
565         GPU_create_gl_tex(bind, ibuf->rect, ibuf->rect_float, ibuf->x, ibuf->y, GL_TEXTURE_2D,
566                 false, false, image);
567
568         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
569         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
570         if (flag & GP_STYLE_COLOR_TEX_CLAMP) {
571                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
572                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
573         }
574         else {
575                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
576                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
577         }
578         BKE_image_release_ibuf(image, ibuf, NULL);
579
580         return error;
581 }
582 #endif
583
584 /* draw fills for shapes */
585 static void gp_draw_stroke_fill(
586         bGPdata *gpd, bGPDstroke *gps,
587         int offsx, int offsy, int winx, int winy, const float diff_mat[4][4], const float color[4])
588 {
589         BLI_assert(gps->totpoints >= 3);
590         Material *ma = gpd->mat[gps->mat_nr];
591         MaterialGPencilStyle *gp_style = ma->gp_style;
592
593         /* Calculate triangles cache for filling area (must be done only after changes) */
594         if ((gps->flag & GP_STROKE_RECALC_GEOMETRY) || (gps->tot_triangles == 0) || (gps->triangles == NULL)) {
595                 gp_triangulate_stroke_fill(gps);
596         }
597         BLI_assert(gps->tot_triangles >= 1);
598
599         GPUVertFormat *format = immVertexFormat();
600         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
601         uint texcoord = GPU_vertformat_attr_add(format, "texCoord", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
602         immBindBuiltinProgram(GPU_SHADER_GPENCIL_FILL);
603
604         immUniformColor4fv(color);
605         immUniform4fv("color2", gp_style->mix_rgba);
606         immUniform1i("fill_type", gp_style->fill_style);
607         immUniform1f("mix_factor", gp_style->mix_factor);
608
609         immUniform1f("gradient_angle", gp_style->gradient_angle);
610         immUniform1f("gradient_radius", gp_style->gradient_radius);
611         immUniform1f("pattern_gridsize", gp_style->pattern_gridsize);
612         immUniform2fv("gradient_scale", gp_style->gradient_scale);
613         immUniform2fv("gradient_shift", gp_style->gradient_shift);
614
615         immUniform1f("texture_angle", gp_style->texture_angle);
616         immUniform2fv("texture_scale", gp_style->texture_scale);
617         immUniform2fv("texture_offset", gp_style->texture_offset);
618         immUniform1f("texture_opacity", gp_style->texture_opacity);
619         immUniform1i("t_mix", (gp_style->flag & GP_STYLE_COLOR_TEX_MIX) != 0);
620         immUniform1i("t_flip", (gp_style->flag & GP_STYLE_COLOR_FLIP_FILL) != 0);
621 #if 0 /* GPXX disabled, not used in annotations */
622         /* image texture */
623         if ((gp_style->fill_style == GP_STYLE_FILL_STYLE_TEXTURE) || (gp_style->flag & GP_STYLE_COLOR_TEX_MIX)) {
624                 gp_set_filling_texture(gp_style->ima, gp_style->flag);
625         }
626 #endif
627         /* Draw all triangles for filling the polygon (cache must be calculated before) */
628         immBegin(GPU_PRIM_TRIS, gps->tot_triangles * 3);
629         /* TODO: use batch instead of immediate mode, to share vertices */
630
631         const bGPDtriangle *stroke_triangle = gps->triangles;
632         for (int i = 0; i < gps->tot_triangles; i++, stroke_triangle++) {
633                 for (int j = 0; j < 3; j++) {
634                         gp_add_filldata_tobuffer(
635                                 &gps->points[stroke_triangle->verts[j]], stroke_triangle->uv[j],
636                                 pos, texcoord, gps->flag,
637                                 offsx, offsy, winx, winy, diff_mat);
638                 }
639         }
640
641         immEnd();
642         immUnbindProgram();
643 }
644
645 /* ----- Existing Strokes Drawing (3D and Point) ------ */
646
647 /* draw a given stroke - just a single dot (only one point) */
648 static void gp_draw_stroke_point(
649         const bGPDspoint *points, short thickness, short UNUSED(dflag), short sflag,
650         int offsx, int offsy, int winx, int winy, const float diff_mat[4][4], const float ink[4])
651 {
652         const bGPDspoint *pt = points;
653
654         /* get final position using parent matrix */
655         float fpt[3];
656         mul_v3_m4v3(fpt, diff_mat, &pt->x);
657
658         GPUVertFormat *format = immVertexFormat();
659         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
660
661         if (sflag & GP_STROKE_3DSPACE) {
662                 immBindBuiltinProgram(GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA);
663         }
664         else {
665                 immBindBuiltinProgram(GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA);
666
667                 /* get 2D coordinates of point */
668                 float co[3] = { 0.0f };
669                 gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, co);
670                 copy_v3_v3(fpt, co);
671         }
672
673         gp_set_point_uniform_color(pt, ink);
674         /* set point thickness (since there's only one of these) */
675         immUniform1f("size", (float)(thickness + 2) * pt->pressure);
676
677         immBegin(GPU_PRIM_POINTS, 1);
678         immVertex3fv(pos, fpt);
679         immEnd();
680
681         immUnbindProgram();
682 }
683
684 /* draw a given stroke in 3d (i.e. in 3d-space) */
685 static void gp_draw_stroke_3d(tGPDdraw *tgpw, short thickness, const float ink[4], bool cyclic)
686 {
687         bGPDspoint *points = tgpw->gps->points;
688         int totpoints = tgpw->gps->totpoints;
689
690         const float viewport[2] = { (float)tgpw->winx, (float)tgpw->winy };
691         float curpressure = points[0].pressure;
692         float fpt[3];
693
694         /* if cyclic needs more vertex */
695         int cyclic_add = (cyclic) ? 1 : 0;
696
697         GPUVertFormat *format = immVertexFormat();
698         const struct {
699                 uint pos, color, thickness;
700         } attr_id = {
701                 .pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT),
702                 .color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT),
703                 .thickness = GPU_vertformat_attr_add(format, "thickness", GPU_COMP_F32, 1, GPU_FETCH_FLOAT),
704         };
705
706         immBindBuiltinProgram(GPU_SHADER_GPENCIL_STROKE);
707         immUniform2fv("Viewport", viewport);
708         immUniform1f("pixsize", tgpw->rv3d->pixsize);
709         float obj_scale = tgpw->ob ? (tgpw->ob->size[0] + tgpw->ob->size[1] + tgpw->ob->size[2]) / 3.0f : 1.0f;
710
711         immUniform1f("objscale", obj_scale);
712         int keep_size = (int)((tgpw->gpd) && (tgpw->gpd->flag & GP_DATA_STROKE_KEEPTHICKNESS));
713         immUniform1i("keep_size", keep_size);
714         immUniform1i("pixfactor", tgpw->gpd->pixfactor);
715         immUniform1i("xraymode", tgpw->gpd->xray_mode);
716
717         /* draw stroke curve */
718         GPU_line_width(max_ff(curpressure * thickness, 1.0f));
719         immBeginAtMost(GPU_PRIM_LINE_STRIP_ADJ, totpoints + cyclic_add + 2);
720         const bGPDspoint *pt = points;
721
722         for (int i = 0; i < totpoints; i++, pt++) {
723                 /* first point for adjacency (not drawn) */
724                 if (i == 0) {
725                         gp_set_point_varying_color(points, ink, attr_id.color);
726                         immAttr1f(attr_id.thickness, max_ff(curpressure * thickness, 1.0f));
727                         if ((cyclic) && (totpoints > 2)) {
728                                 mul_v3_m4v3(fpt, tgpw->diff_mat, &(points + totpoints - 1)->x);
729                         }
730                         else {
731                                 mul_v3_m4v3(fpt, tgpw->diff_mat, &(points + 1)->x);
732                         }
733                         mul_v3_fl(fpt, -1.0f);
734                         immVertex3fv(attr_id.pos, fpt);
735                 }
736                 /* set point */
737                 gp_set_point_varying_color(pt, ink, attr_id.color);
738                 immAttr1f(attr_id.thickness, max_ff(curpressure * thickness, 1.0f));
739                 mul_v3_m4v3(fpt, tgpw->diff_mat, &pt->x);
740                 immVertex3fv(attr_id.pos, fpt);
741
742                 curpressure = pt->pressure;
743         }
744
745         if (cyclic && totpoints > 2) {
746                 /* draw line to first point to complete the cycle */
747                 immAttr1f(attr_id.thickness, max_ff(points->pressure * thickness, 1.0f));
748                 mul_v3_m4v3(fpt, tgpw->diff_mat, &points->x);
749                 immVertex3fv(attr_id.pos, fpt);
750
751                 /* now add adjacency point (not drawn) */
752                 immAttr1f(attr_id.thickness, max_ff((points + 1)->pressure * thickness, 1.0f));
753                 mul_v3_m4v3(fpt, tgpw->diff_mat, &(points + 1)->x);
754                 immVertex3fv(attr_id.pos, fpt);
755         }
756         /* last adjacency point (not drawn) */
757         else {
758                 gp_set_point_varying_color(points + totpoints - 1, ink, attr_id.color);
759                 immAttr1f(attr_id.thickness, max_ff(curpressure * thickness, 1.0f));
760                 mul_v3_m4v3(fpt, tgpw->diff_mat, &(points + totpoints - 2)->x);
761                 mul_v3_fl(fpt, -1.0f);
762                 immVertex3fv(attr_id.pos, fpt);
763         }
764
765         immEnd();
766         immUnbindProgram();
767 }
768
769 /* ----- Fancy 2D-Stroke Drawing ------ */
770
771 /* draw a given stroke in 2d */
772 static void gp_draw_stroke_2d(
773         const bGPDspoint *points, int totpoints, short thickness_s, short dflag, short sflag,
774         bool UNUSED(debug), int offsx, int offsy, int winx, int winy, const float diff_mat[4][4], const float ink[4])
775 {
776         /* otherwise thickness is twice that of the 3D view */
777         float thickness = (float)thickness_s * 0.5f;
778
779         /* strokes in Image Editor need a scale factor, since units there are not pixels! */
780         float scalefac  = 1.0f;
781         if ((dflag & GP_DRAWDATA_IEDITHACK) && (dflag & GP_DRAWDATA_ONLYV2D)) {
782                 scalefac = 0.001f;
783         }
784
785         /* TODO: fancy++ with the magic of shaders */
786
787         /* tessellation code - draw stroke as series of connected quads (triangle strips in fact) with connection
788          * edges rotated to minimize shrinking artifacts, and rounded endcaps
789          */
790         {
791                 const bGPDspoint *pt1, *pt2;
792                 float s0[2], s1[2];     /* segment 'center' points */
793                 float pm[2];  /* normal from previous segment. */
794                 int i;
795                 float fpt[3];
796
797                 GPUVertFormat *format = immVertexFormat();
798                 const struct {
799                         uint pos, color;
800                 } attr_id = {
801                         .pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT),
802                         .color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT),
803                 };
804
805                 immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
806                 immBegin(GPU_PRIM_TRI_STRIP, totpoints * 2 + 4);
807
808                 /* get x and y coordinates from first point */
809                 mul_v3_m4v3(fpt, diff_mat, &points->x);
810                 gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, s0);
811
812                 for (i = 0, pt1 = points, pt2 = points + 1; i < (totpoints - 1); i++, pt1++, pt2++) {
813                         float t0[2], t1[2];     /* tessellated coordinates */
814                         float m1[2], m2[2];     /* gradient and normal */
815                         float mt[2], sc[2];     /* gradient for thickness, point for end-cap */
816                         float pthick;           /* thickness at segment point */
817
818                         /* get x and y coordinates from point2 (point1 has already been computed in previous iteration). */
819                         mul_v3_m4v3(fpt, diff_mat, &pt2->x);
820                         gp_calc_2d_stroke_fxy(fpt, sflag, offsx, offsy, winx, winy, s1);
821
822                         /* calculate gradient and normal - 'angle'=(ny/nx) */
823                         m1[1] = s1[1] - s0[1];
824                         m1[0] = s1[0] - s0[0];
825                         normalize_v2(m1);
826                         m2[1] = -m1[0];
827                         m2[0] = m1[1];
828
829                         /* always use pressure from first point here */
830                         pthick = (pt1->pressure * thickness * scalefac);
831
832                         /* color of point */
833                         gp_set_point_varying_color(pt1, ink, attr_id.color);
834
835                         /* if the first segment, start of segment is segment's normal */
836                         if (i == 0) {
837                                 /* draw start cap first
838                                  * - make points slightly closer to center (about halfway across)
839                                  */
840                                 mt[0] = m2[0] * pthick * 0.5f;
841                                 mt[1] = m2[1] * pthick * 0.5f;
842                                 sc[0] = s0[0] - (m1[0] * pthick * 0.75f);
843                                 sc[1] = s0[1] - (m1[1] * pthick * 0.75f);
844
845                                 t0[0] = sc[0] - mt[0];
846                                 t0[1] = sc[1] - mt[1];
847                                 t1[0] = sc[0] + mt[0];
848                                 t1[1] = sc[1] + mt[1];
849
850                                 /* First two points of cap. */
851                                 immVertex2fv(attr_id.pos, t0);
852                                 immVertex2fv(attr_id.pos, t1);
853
854                                 /* calculate points for start of segment */
855                                 mt[0] = m2[0] * pthick;
856                                 mt[1] = m2[1] * pthick;
857
858                                 t0[0] = s0[0] - mt[0];
859                                 t0[1] = s0[1] - mt[1];
860                                 t1[0] = s0[0] + mt[0];
861                                 t1[1] = s0[1] + mt[1];
862
863                                 /* Last two points of start cap (and first two points of first segment). */
864                                 immVertex2fv(attr_id.pos, t0);
865                                 immVertex2fv(attr_id.pos, t1);
866                         }
867                         /* if not the first segment, use bisector of angle between segments */
868                         else {
869                                 float mb[2];         /* bisector normal */
870                                 float athick, dfac;  /* actual thickness, difference between thicknesses */
871
872                                 /* calculate gradient of bisector (as average of normals) */
873                                 mb[0] = (pm[0] + m2[0]) / 2;
874                                 mb[1] = (pm[1] + m2[1]) / 2;
875                                 normalize_v2(mb);
876
877                                 /* calculate gradient to apply
878                                  * - as basis, use just pthick * bisector gradient
879                                  * - if cross-section not as thick as it should be, add extra padding to fix it
880                                  */
881                                 mt[0] = mb[0] * pthick;
882                                 mt[1] = mb[1] * pthick;
883                                 athick = len_v2(mt);
884                                 dfac = pthick - (athick * 2);
885
886                                 if (((athick * 2.0f) < pthick) && (IS_EQF(athick, pthick) == 0)) {
887                                         mt[0] += (mb[0] * dfac);
888                                         mt[1] += (mb[1] * dfac);
889                                 }
890
891                                 /* calculate points for start of segment */
892                                 t0[0] = s0[0] - mt[0];
893                                 t0[1] = s0[1] - mt[1];
894                                 t1[0] = s0[0] + mt[0];
895                                 t1[1] = s0[1] + mt[1];
896
897                                 /* Last two points of previous segment, and first two points of current segment. */
898                                 immVertex2fv(attr_id.pos, t0);
899                                 immVertex2fv(attr_id.pos, t1);
900                         }
901
902                         /* if last segment, also draw end of segment (defined as segment's normal) */
903                         if (i == totpoints - 2) {
904                                 /* for once, we use second point's pressure (otherwise it won't be drawn) */
905                                 pthick = (pt2->pressure * thickness * scalefac);
906
907                                 /* color of point */
908                                 gp_set_point_varying_color(pt2, ink, attr_id.color);
909
910                                 /* calculate points for end of segment */
911                                 mt[0] = m2[0] * pthick;
912                                 mt[1] = m2[1] * pthick;
913
914                                 t0[0] = s1[0] - mt[0];
915                                 t0[1] = s1[1] - mt[1];
916                                 t1[0] = s1[0] + mt[0];
917                                 t1[1] = s1[1] + mt[1];
918
919                                 /* Last two points of last segment (and first two points of end cap). */
920                                 immVertex2fv(attr_id.pos, t0);
921                                 immVertex2fv(attr_id.pos, t1);
922
923                                 /* draw end cap as last step
924                                  * - make points slightly closer to center (about halfway across)
925                                  */
926                                 mt[0] = m2[0] * pthick * 0.5f;
927                                 mt[1] = m2[1] * pthick * 0.5f;
928                                 sc[0] = s1[0] + (m1[0] * pthick * 0.75f);
929                                 sc[1] = s1[1] + (m1[1] * pthick * 0.75f);
930
931                                 t0[0] = sc[0] - mt[0];
932                                 t0[1] = sc[1] - mt[1];
933                                 t1[0] = sc[0] + mt[0];
934                                 t1[1] = sc[1] + mt[1];
935
936                                 /* Last two points of end cap. */
937                                 immVertex2fv(attr_id.pos, t0);
938                                 immVertex2fv(attr_id.pos, t1);
939                         }
940
941                         /* store computed point2 coordinates as point1 ones of next segment. */
942                         copy_v2_v2(s0, s1);
943                         /* store stroke's 'natural' normal for next stroke to use */
944                         copy_v2_v2(pm, m2);
945                 }
946
947                 immEnd();
948                 immUnbindProgram();
949         }
950 }
951
952 /* ----- Strokes Drawing ------ */
953
954 /* Helper for doing all the checks on whether a stroke can be drawn */
955 static bool gp_can_draw_stroke(const bGPDstroke *gps, const int dflag)
956 {
957         /* skip stroke if it isn't in the right display space for this drawing context */
958         /* 1) 3D Strokes */
959         if ((dflag & GP_DRAWDATA_ONLY3D) && !(gps->flag & GP_STROKE_3DSPACE))
960                 return false;
961         if (!(dflag & GP_DRAWDATA_ONLY3D) && (gps->flag & GP_STROKE_3DSPACE))
962                 return false;
963
964         /* 2) Screen Space 2D Strokes */
965         if ((dflag & GP_DRAWDATA_ONLYV2D) && !(gps->flag & GP_STROKE_2DSPACE))
966                 return false;
967         if (!(dflag & GP_DRAWDATA_ONLYV2D) && (gps->flag & GP_STROKE_2DSPACE))
968                 return false;
969
970         /* 3) Image Space (2D) */
971         if ((dflag & GP_DRAWDATA_ONLYI2D) && !(gps->flag & GP_STROKE_2DIMAGE))
972                 return false;
973         if (!(dflag & GP_DRAWDATA_ONLYI2D) && (gps->flag & GP_STROKE_2DIMAGE))
974                 return false;
975
976         /* skip stroke if it doesn't have any valid data */
977         if ((gps->points == NULL) || (gps->totpoints < 1))
978                 return false;
979
980         /* stroke can be drawn */
981         return true;
982 }
983
984 /* draw a set of strokes */
985 static void gp_draw_strokes(tGPDdraw *tgpw)
986 {
987         float tcolor[4];
988         float tfill[4];
989         short sthickness;
990         float ink[4];
991
992         GPU_enable_program_point_size();
993
994         for (bGPDstroke *gps = tgpw->t_gpf->strokes.first; gps; gps = gps->next) {
995                 /* check if stroke can be drawn */
996                 if (gp_can_draw_stroke(gps, tgpw->dflag) == false) {
997                         continue;
998                 }
999                 /* check if the color is visible */
1000                 Material *ma = tgpw->gpd->mat[gps->mat_nr];
1001                 MaterialGPencilStyle *gp_style = ma->gp_style;
1002
1003                 if ((gp_style == NULL) ||
1004                     (gp_style->flag & GP_STYLE_COLOR_HIDE) ||
1005                     /* if onion and ghost flag do not draw*/
1006                     (tgpw->onion && (gp_style->flag & GP_STYLE_COLOR_ONIONSKIN)))
1007                 {
1008                         continue;
1009                 }
1010
1011                 /* if disable fill, the colors with fill must be omitted too except fill boundary strokes */
1012                 if ((tgpw->disable_fill == 1) &&
1013                     (gp_style->fill_rgba[3] > 0.0f) &&
1014                     ((gps->flag & GP_STROKE_NOFILL) == 0))
1015                 {
1016                         continue;
1017                 }
1018
1019                 /* calculate thickness */
1020                 sthickness = gps->thickness + tgpw->lthick;
1021
1022                 if (sthickness <= 0) {
1023                         continue;
1024                 }
1025
1026                 /* check which stroke-drawer to use */
1027                 if (tgpw->dflag & GP_DRAWDATA_ONLY3D) {
1028                         const int no_xray = (tgpw->dflag & GP_DRAWDATA_NO_XRAY);
1029                         int mask_orig = 0;
1030
1031                         if (no_xray) {
1032                                 glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
1033                                 glDepthMask(0);
1034                                 GPU_depth_test(true);
1035
1036                                 /* first arg is normally rv3d->dist, but this isn't
1037                                  * available here and seems to work quite well without */
1038                                 bglPolygonOffset(1.0f, 1.0f);
1039                         }
1040
1041                         /* 3D Fill */
1042                         //if ((dflag & GP_DRAWDATA_FILL) && (gps->totpoints >= 3)) {
1043                         if ((gps->totpoints >= 3) && (tgpw->disable_fill != 1)) {
1044                                 /* set color using material, tint color and opacity */
1045                                 interp_v3_v3v3(tfill, gp_style->fill_rgba, tgpw->tintcolor, tgpw->tintcolor[3]);
1046                                 tfill[3] = gp_style->fill_rgba[3] * tgpw->opacity;
1047                                 if ((tfill[3] > GPENCIL_ALPHA_OPACITY_THRESH) || (gp_style->fill_style > 0)) {
1048                                         const float *color;
1049                                         if (!tgpw->onion) {
1050                                                 color = tfill;
1051                                         }
1052                                         else {
1053                                                 if (tgpw->custonion) {
1054                                                         color = tgpw->tintcolor;
1055                                                 }
1056                                                 else {
1057                                                         ARRAY_SET_ITEMS(tfill, UNPACK3(gp_style->fill_rgba), tgpw->tintcolor[3]);
1058                                                         color = tfill;
1059                                                 }
1060                                         }
1061                                         gp_draw_stroke_fill(
1062                                                 tgpw->gpd, gps,
1063                                                 tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy, tgpw->diff_mat, color);
1064                                 }
1065                         }
1066
1067                         /* 3D Stroke */
1068                         /* set color using material tint color and opacity */
1069                         if (!tgpw->onion) {
1070                                 interp_v3_v3v3(tcolor, gp_style->stroke_rgba, tgpw->tintcolor, tgpw->tintcolor[3]);
1071                                 tcolor[3] = gp_style->stroke_rgba[3] * tgpw->opacity;
1072                                 copy_v4_v4(ink, tcolor);
1073                         }
1074                         else {
1075                                 if (tgpw->custonion) {
1076                                         copy_v4_v4(ink, tgpw->tintcolor);
1077                                 }
1078                                 else {
1079                                         ARRAY_SET_ITEMS(tcolor, UNPACK3(gp_style->stroke_rgba), tgpw->opacity);
1080                                         copy_v4_v4(ink, tcolor);
1081                                 }
1082                         }
1083                         if (gp_style->mode == GP_STYLE_MODE_DOTS) {
1084                                 /* volumetric stroke drawing */
1085                                 if (tgpw->disable_fill != 1) {
1086                                         gp_draw_stroke_volumetric_3d(gps->points, gps->totpoints, sthickness, ink);
1087                                 }
1088                         }
1089                         else {
1090                                 /* 3D Lines - OpenGL primitives-based */
1091                                 if (gps->totpoints == 1) {
1092                                         if (tgpw->disable_fill != 1) {
1093                                                 gp_draw_stroke_point(gps->points, sthickness, tgpw->dflag, gps->flag,
1094                                                         tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy,
1095                                                         tgpw->diff_mat, ink);
1096                                         }
1097                                 }
1098                                 else {
1099                                         tgpw->gps = gps;
1100                                         gp_draw_stroke_3d(tgpw, sthickness, ink, gps->flag & GP_STROKE_CYCLIC);
1101                                 }
1102                         }
1103                         if (no_xray) {
1104                                 glDepthMask(mask_orig);
1105                                 GPU_depth_test(false);
1106
1107                                 bglPolygonOffset(0.0, 0.0);
1108                         }
1109                 }
1110                 else {
1111                         /* 2D - Fill */
1112                         if (gps->totpoints >= 3) {
1113                                 /* set color using material, tint color and opacity */
1114                                 interp_v3_v3v3(tfill, gp_style->fill_rgba, tgpw->tintcolor, tgpw->tintcolor[3]);
1115                                 tfill[3] = gp_style->fill_rgba[3] * tgpw->opacity;
1116                                 if ((tfill[3] > GPENCIL_ALPHA_OPACITY_THRESH) || (gp_style->fill_style > 0)) {
1117                                         const float *color;
1118                                         if (!tgpw->onion) {
1119                                                 color = tfill;
1120                                         }
1121                                         else {
1122                                                 if (tgpw->custonion) {
1123                                                         color = tgpw->tintcolor;
1124                                                 }
1125                                                 else {
1126                                                         ARRAY_SET_ITEMS(tfill, UNPACK3(gp_style->fill_rgba), tgpw->tintcolor[3]);
1127                                                         color = tfill;
1128                                                 }
1129                                         }
1130                                         gp_draw_stroke_fill(
1131                                                 tgpw->gpd, gps,
1132                                                 tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy, tgpw->diff_mat, color);
1133                                 }
1134                         }
1135
1136                         /* 2D Strokes... */
1137                         /* set color using material, tint color and opacity */
1138                         if (!tgpw->onion) {
1139                                 interp_v3_v3v3(tcolor, gp_style->stroke_rgba, tgpw->tintcolor, tgpw->tintcolor[3]);
1140                                 tcolor[3] = gp_style->stroke_rgba[3] * tgpw->opacity;
1141                                 copy_v4_v4(ink, tcolor);
1142                         }
1143                         else {
1144                                 if (tgpw->custonion) {
1145                                         copy_v4_v4(ink, tgpw->tintcolor);
1146                                 }
1147                                 else {
1148                                         ARRAY_SET_ITEMS(tcolor, UNPACK3(gp_style->stroke_rgba), tgpw->opacity);
1149                                         copy_v4_v4(ink, tcolor);
1150                                 }
1151                         }
1152                         if (gp_style->mode == GP_STYLE_MODE_DOTS) {
1153                                 /* blob/disk-based "volumetric" drawing */
1154                                 gp_draw_stroke_volumetric_2d(
1155                                         gps->points, gps->totpoints, sthickness, tgpw->dflag, gps->flag,
1156                                         tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy, tgpw->diff_mat, ink);
1157                         }
1158                         else {
1159                                 /* normal 2D strokes */
1160                                 if (gps->totpoints == 1) {
1161                                         gp_draw_stroke_point(
1162                                                 gps->points, sthickness, tgpw->dflag, gps->flag,
1163                                                 tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy,
1164                                                 tgpw->diff_mat, ink);
1165                                 }
1166                                 else {
1167                                         gp_draw_stroke_2d(
1168                                                 gps->points, gps->totpoints, sthickness, tgpw->dflag, gps->flag, false,
1169                                                 tgpw->offsx, tgpw->offsy, tgpw->winx, tgpw->winy, tgpw->diff_mat, ink);
1170                                 }
1171                         }
1172                 }
1173         }
1174
1175         GPU_disable_program_point_size();
1176 }
1177
1178 /* Draw selected verts for strokes being edited */
1179 static void gp_draw_strokes_edit(
1180         bGPdata *gpd, const bGPDframe *gpf, int offsx, int offsy, int winx, int winy, short dflag,
1181         short lflag, const float diff_mat[4][4], float alpha)
1182 {
1183         /* if alpha 0 do not draw */
1184         if (alpha == 0.0f)
1185                 return;
1186
1187         const bool no_xray = (dflag & GP_DRAWDATA_NO_XRAY) != 0;
1188         int mask_orig = 0;
1189
1190         /* set up depth masks... */
1191         if (dflag & GP_DRAWDATA_ONLY3D) {
1192                 if (no_xray) {
1193                         glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
1194                         glDepthMask(0);
1195                         GPU_depth_test(true);
1196
1197                         /* first arg is normally rv3d->dist, but this isn't
1198                          * available here and seems to work quite well without */
1199                         bglPolygonOffset(1.0f, 1.0f);
1200                 }
1201         }
1202
1203         GPU_enable_program_point_size();
1204
1205         /* draw stroke verts */
1206         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
1207                 /* check if stroke can be drawn */
1208                 if (gp_can_draw_stroke(gps, dflag) == false)
1209                         continue;
1210
1211                 /* Optimisation: only draw points for selected strokes
1212                  * We assume that selected points can only occur in
1213                  * strokes that are selected too.
1214                  */
1215                 if ((gps->flag & GP_STROKE_SELECT) == 0)
1216                         continue;
1217
1218                 /* verify color lock */
1219                 {
1220                         Material *ma = gpd->mat[gps->mat_nr];
1221                         MaterialGPencilStyle *gp_style = ma->gp_style;
1222
1223                         if (gp_style != NULL) {
1224                                 if (gp_style->flag & GP_STYLE_COLOR_HIDE) {
1225                                         continue;
1226                                 }
1227                                 if (((lflag & GP_LAYER_UNLOCK_COLOR) == 0) && (gp_style->flag & GP_STYLE_COLOR_LOCKED)) {
1228                                         continue;
1229                                 }
1230                         }
1231                 }
1232
1233                 /* Get size of verts:
1234                  * - The selected state needs to be larger than the unselected state so that
1235                  *   they stand out more.
1236                  * - We use the theme setting for size of the unselected verts
1237                  */
1238                 float bsize = UI_GetThemeValuef(TH_GP_VERTEX_SIZE);
1239                 float vsize;
1240                 if ((int)bsize > 8) {
1241                         vsize = 10.0f;
1242                         bsize = 8.0f;
1243                 }
1244                 else {
1245                         vsize = bsize + 2;
1246                 }
1247
1248                 /* for now, we assume that the base color of the points is not too close to the real color */
1249                 /* set color using material */
1250                 Material *ma = gpd->mat[gps->mat_nr];
1251                 MaterialGPencilStyle *gp_style = ma->gp_style;
1252
1253                 float selectColor[4];
1254                 UI_GetThemeColor3fv(TH_GP_VERTEX_SELECT, selectColor);
1255                 selectColor[3] = alpha;
1256
1257                 GPUVertFormat *format = immVertexFormat();
1258                 uint pos; /* specified later */
1259                 uint size = GPU_vertformat_attr_add(format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);
1260                 uint color = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);
1261
1262                 if (gps->flag & GP_STROKE_3DSPACE) {
1263                         pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
1264                         immBindBuiltinProgram(GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR);
1265                 }
1266                 else {
1267                         pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
1268                         immBindBuiltinProgram(GPU_SHADER_2D_POINT_VARYING_SIZE_VARYING_COLOR);
1269                 }
1270
1271                 immBegin(GPU_PRIM_POINTS, gps->totpoints);
1272
1273                 /* Draw start and end point differently if enabled stroke direction hint */
1274                 bool show_direction_hint = (gpd->flag & GP_DATA_SHOW_DIRECTION) && (gps->totpoints > 1);
1275
1276                 /* Draw all the stroke points (selected or not) */
1277                 bGPDspoint *pt = gps->points;
1278                 float fpt[3];
1279                 for (int i = 0; i < gps->totpoints; i++, pt++) {
1280                         /* size and color first */
1281                         if (show_direction_hint && i == 0) {
1282                                 /* start point in green bigger */
1283                                 immAttr3f(color, 0.0f, 1.0f, 0.0f);
1284                                 immAttr1f(size, vsize + 4);
1285                         }
1286                         else if (show_direction_hint && (i == gps->totpoints - 1)) {
1287                                 /* end point in red smaller */
1288                                 immAttr3f(color, 1.0f, 0.0f, 0.0f);
1289                                 immAttr1f(size, vsize + 1);
1290                         }
1291                         else if (pt->flag & GP_SPOINT_SELECT) {
1292                                 immAttr3fv(color, selectColor);
1293                                 immAttr1f(size, vsize);
1294                         }
1295                         else {
1296                                 immAttr3fv(color, gp_style->stroke_rgba);
1297                                 immAttr1f(size, bsize);
1298                         }
1299
1300                         /* then position */
1301                         if (gps->flag & GP_STROKE_3DSPACE) {
1302                                 mul_v3_m4v3(fpt, diff_mat, &pt->x);
1303                                 immVertex3fv(pos, fpt);
1304                         }
1305                         else {
1306                                 float co[2];
1307                                 mul_v3_m4v3(fpt, diff_mat, &pt->x);
1308                                 gp_calc_2d_stroke_fxy(fpt, gps->flag, offsx, offsy, winx, winy, co);
1309                                 immVertex2fv(pos, co);
1310                         }
1311                 }
1312
1313                 immEnd();
1314                 immUnbindProgram();
1315         }
1316
1317         GPU_disable_program_point_size();
1318
1319         /* clear depth mask */
1320         if (dflag & GP_DRAWDATA_ONLY3D) {
1321                 if (no_xray) {
1322                         glDepthMask(mask_orig);
1323                         GPU_depth_test(false);
1324
1325                         bglPolygonOffset(0.0, 0.0);
1326 #if 0
1327                         glDisable(GL_POLYGON_OFFSET_LINE);
1328                         glPolygonOffset(0, 0);
1329 #endif
1330                 }
1331         }
1332 }
1333
1334 /* ----- General Drawing ------ */
1335
1336
1337 /* draw interpolate strokes (used only while operator is running) */
1338 void ED_gp_draw_interpolation(const bContext *C, tGPDinterpolate *tgpi, const int type)
1339 {
1340         tGPDdraw tgpw;
1341         ARegion *ar = CTX_wm_region(C);
1342         RegionView3D *rv3d = ar->regiondata;
1343         tGPDinterpolate_layer *tgpil;
1344         Object *obact = CTX_data_active_object(C);
1345         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1346
1347         float color[4];
1348
1349         UI_GetThemeColor3fv(TH_GP_VERTEX_SELECT, color);
1350         color[3] = 0.6f;
1351         int dflag = 0;
1352         /* if 3d stuff, enable flags */
1353         if (type == REGION_DRAW_POST_VIEW) {
1354                 dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS);
1355         }
1356
1357         tgpw.rv3d = rv3d;
1358         tgpw.depsgraph = depsgraph;
1359         tgpw.ob = obact;
1360         tgpw.gpd = tgpi->gpd;
1361         tgpw.offsx = 0;
1362         tgpw.offsy = 0;
1363         tgpw.winx = tgpi->ar->winx;
1364         tgpw.winy = tgpi->ar->winy;
1365         tgpw.dflag = dflag;
1366
1367         /* turn on alpha-blending */
1368         glEnable(GL_BLEND);
1369         for (tgpil = tgpi->ilayers.first; tgpil; tgpil = tgpil->next) {
1370                 /* calculate parent position */
1371                 ED_gpencil_parent_location(depsgraph, obact, tgpi->gpd, tgpil->gpl, tgpw.diff_mat);
1372                 if (tgpil->interFrame) {
1373                         tgpw.gpl = tgpil->gpl;
1374                         tgpw.gpf = tgpil->interFrame;
1375                         tgpw.t_gpf = tgpil->interFrame;
1376
1377                         tgpw.lthick = tgpil->gpl->line_change;
1378                         tgpw.opacity = 1.0;
1379                         copy_v4_v4(tgpw.tintcolor, color);
1380                         tgpw.onion = true;
1381                         tgpw.custonion = true;
1382
1383                         gp_draw_strokes(&tgpw);
1384                 }
1385         }
1386         glDisable(GL_BLEND);
1387 }
1388
1389 /* wrapper to draw strokes for filling operator */
1390 void ED_gp_draw_fill(tGPDdraw *tgpw)
1391 {
1392         gp_draw_strokes(tgpw);
1393 }
1394
1395 /* loop over gpencil data layers, drawing them */
1396 static void gp_draw_data_layers(RegionView3D *rv3d,
1397         const Brush *brush, float alpha, Object *ob, bGPdata *gpd,
1398         int offsx, int offsy, int winx, int winy, int cfra, int dflag)
1399 {
1400         float diff_mat[4][4];
1401         tGPDdraw tgpw;
1402
1403         tgpw.rv3d = rv3d;
1404         tgpw.depsgraph = NULL; /* XXX: This is not used here */
1405         tgpw.ob = ob;
1406         tgpw.gpd = gpd;
1407         tgpw.gpl = NULL;
1408         tgpw.gpf = NULL;
1409         tgpw.t_gpf = NULL;
1410         tgpw.offsx = offsx;
1411         tgpw.offsy = offsy;
1412         tgpw.winx = winx;
1413         tgpw.winy = winy;
1414         tgpw.dflag = dflag;
1415
1416         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
1417                 /* calculate parent position */
1418                 ED_gpencil_parent_location(tgpw.depsgraph, ob, gpd, gpl, diff_mat);
1419
1420                 short lthick = brush->size + gpl->line_change;
1421
1422                 /* don't draw layer if hidden */
1423                 if (gpl->flag & GP_LAYER_HIDE)
1424                         continue;
1425
1426                 /* get frame to draw */
1427                 bGPDframe *gpf = BKE_gpencil_layer_getframe(gpl, cfra, GP_GETFRAME_USE_PREV);
1428                 if (gpf == NULL)
1429                         continue;
1430
1431                 /* set basic stroke thickness */
1432                 GPU_line_width(lthick);
1433
1434                 /* Add layer drawing settings to the set of "draw flags"
1435                  * NOTE: If the setting doesn't apply, it *must* be cleared,
1436                  *       as dflag's carry over from the previous layer
1437                  */
1438
1439                 /* xray... */
1440                 SET_FLAG_FROM_TEST(dflag, gpl->flag & GP_LAYER_NO_XRAY, GP_DRAWDATA_NO_XRAY);
1441
1442                 /* volumetric strokes... */
1443                 SET_FLAG_FROM_TEST(dflag, gpl->flag & GP_LAYER_VOLUMETRIC, GP_DRAWDATA_VOLUMETRIC);
1444
1445                 tgpw.gpl = gpl;
1446                 tgpw.gpf = gpf;
1447                 tgpw.t_gpf = gpf; // XXX?
1448                 tgpw.lthick = gpl->line_change;
1449                 tgpw.opacity = gpl->opacity;
1450                 copy_v4_v4(tgpw.tintcolor, gpl->tintcolor);
1451                 tgpw.onion = false;
1452                 tgpw.custonion = false;
1453                 copy_m4_m4(tgpw.diff_mat, diff_mat);
1454
1455                 /* draw the strokes already in active frame */
1456                 gp_draw_strokes(&tgpw);
1457
1458                 /* Draw verts of selected strokes
1459                  * - when doing OpenGL renders, we don't want to be showing these, as that ends up flickering
1460                  *   - locked layers can't be edited, so there's no point showing these verts
1461                  *     as they will have no bearings on what gets edited
1462                  * - only show when in editmode, since operators shouldn't work otherwise
1463                  *   (NOTE: doing it this way means that the toggling editmode shows visible change immediately)
1464                  */
1465                 /* XXX: perhaps we don't want to show these when users are drawing... */
1466                 if ((G.f & G_RENDER_OGL) == 0 &&
1467                     (gpl->flag & GP_LAYER_LOCKED) == 0 &&
1468                     (gpd->flag & GP_DATA_STROKE_EDITMODE))
1469                 {
1470                         gp_draw_strokes_edit(gpd, gpf, offsx, offsy, winx, winy, dflag, gpl->flag, diff_mat, alpha);
1471                 }
1472
1473                 /* Check if may need to draw the active stroke cache, only if this layer is the active layer
1474                  * that is being edited. (Stroke buffer is currently stored in gp-data)
1475                  */
1476                 if (ED_gpencil_session_active() && (gpl->flag & GP_LAYER_ACTIVE) &&
1477                     (gpf->flag & GP_FRAME_PAINT))
1478                 {
1479                         /* Buffer stroke needs to be drawn with a different linestyle
1480                          * to help differentiate them from normal strokes.
1481                          *
1482                          * It should also be noted that sbuffer contains temporary point types
1483                          * i.e. tGPspoints NOT bGPDspoints
1484                          */
1485                         if (gpd->runtime.mode == GP_STYLE_MODE_DOTS) {
1486                                 gp_draw_stroke_volumetric_buffer(
1487                                         gpd->runtime.sbuffer,
1488                                         gpd->runtime.sbuffer_size, lthick,
1489                                         dflag, gpd->runtime.scolor);
1490                         }
1491                         else {
1492                                 gp_draw_stroke_buffer(
1493                                         gpd->runtime.sbuffer,
1494                                         gpd->runtime.sbuffer_size, lthick,
1495                                         dflag, gpd->runtime.sbuffer_sflag,
1496                                         gpd->runtime.scolor, gpd->runtime.sfill);
1497                         }
1498                 }
1499         }
1500 }
1501
1502 /* draw a short status message in the top-right corner */
1503 static void UNUSED_FUNCTION(gp_draw_status_text)(const bGPdata *gpd, ARegion *ar)
1504 {
1505         rcti rect;
1506
1507         /* Cannot draw any status text when drawing OpenGL Renders */
1508         if (G.f & G_RENDER_OGL)
1509                 return;
1510
1511         /* Get bounds of region - Necessary to avoid problems with region overlap */
1512         ED_region_visible_rect(ar, &rect);
1513
1514         /* for now, this should only be used to indicate when we are in stroke editmode */
1515         if (gpd->flag & GP_DATA_STROKE_EDITMODE) {
1516                 const char *printable = IFACE_("GPencil Stroke Editing");
1517                 float       printable_size[2];
1518
1519                 int font_id = BLF_default();
1520
1521                 BLF_width_and_height(font_id, printable, BLF_DRAW_STR_DUMMY_MAX, &printable_size[0], &printable_size[1]);
1522
1523                 int xco = (rect.xmax - U.widget_unit) - (int)printable_size[0];
1524                 int yco = (rect.ymax - U.widget_unit);
1525
1526                 /* text label */
1527                 UI_FontThemeColor(font_id, TH_TEXT_HI);
1528 #ifdef WITH_INTERNATIONAL
1529                 BLF_draw_default(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1530 #else
1531                 BLF_draw_default_ascii(xco, yco, 0.0f, printable, BLF_DRAW_STR_DUMMY_MAX);
1532 #endif
1533
1534                 /* grease pencil icon... */
1535                 // XXX: is this too intrusive?
1536                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1537                 GPU_blend(true);
1538
1539                 xco -= U.widget_unit;
1540                 yco -= (int)printable_size[1] / 2;
1541
1542                 UI_icon_draw(xco, yco, ICON_GREASEPENCIL);
1543
1544                 GPU_blend(false);
1545         }
1546 }
1547
1548 /* draw grease-pencil datablock */
1549 static void gp_draw_data(RegionView3D *rv3d,
1550         const Brush *brush, float alpha, Object *ob, bGPdata *gpd,
1551         int offsx, int offsy, int winx, int winy, int cfra, int dflag)
1552 {
1553         /* turn on smooth lines (i.e. anti-aliasing) */
1554         GPU_line_smooth(true);
1555
1556         /* XXX: turn on some way of ensuring that the polygon edges get smoothed
1557          *      GL_POLYGON_SMOOTH is nasty and shouldn't be used, as it ends up
1558          *      creating internal white rays due to the ways it accumulates stuff
1559          */
1560
1561         /* turn on alpha-blending */
1562         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
1563         GPU_blend(true);
1564
1565         /* draw! */
1566         gp_draw_data_layers(rv3d, brush, alpha, ob, gpd, offsx, offsy, winx, winy, cfra, dflag);
1567
1568         /* turn off alpha blending, then smooth lines */
1569         GPU_blend(false); // alpha blending
1570         GPU_line_smooth(false); // smooth lines
1571 }
1572
1573 /* if we have strokes for scenes (3d view)/clips (movie clip editor)
1574  * and objects/tracks, multiple data blocks have to be drawn */
1575 static void gp_draw_data_all(
1576         ViewLayer *view_layer, RegionView3D *rv3d, Scene *scene, bGPdata *gpd,
1577         int offsx, int offsy, int winx, int winy,
1578         int cfra, int dflag, const char UNUSED(spacetype))
1579 {
1580         bGPdata *gpd_source = NULL;
1581         Brush *brush = NULL;
1582         Object *ob = OBACT(view_layer);
1583         if (scene) {
1584                 ToolSettings *ts = scene->toolsettings;
1585                 brush = BKE_paint_brush(&ts->gp_paint->paint);
1586
1587                 if (gpd_source) {
1588                         if (brush != NULL) {
1589                                 gp_draw_data(
1590                                         rv3d, brush, 1.0f, ob, gpd_source,
1591                                         offsx, offsy, winx, winy, cfra, dflag);
1592                         }
1593                 }
1594         }
1595
1596         /* scene/clip data has already been drawn, only object/track data is drawn here
1597          * if gpd_source == gpd, we don't have any object/track data and we can skip */
1598         if (gpd_source == NULL || (gpd_source && gpd_source != gpd)) {
1599                 if (brush != NULL) {
1600                         gp_draw_data(
1601                                 rv3d, brush, 1.0f, ob, gpd,
1602                                 offsx, offsy, winx, winy, cfra, dflag);
1603                 }
1604         }
1605 }
1606
1607 /* ----- Grease Pencil Sketches Drawing API ------ */
1608
1609
1610 /* draw grease-pencil sketches to specified 3d-view assuming that matrices are already set correctly
1611  * Note: this gets called twice - first time with only3d=true to draw 3d-strokes,
1612  * second time with only3d=false for screen-aligned strokes */
1613 void ED_gpencil_draw_view3d(
1614         wmWindowManager *wm,
1615         Scene *scene,
1616         ViewLayer *view_layer,
1617         struct Depsgraph *depsgraph,
1618         View3D *v3d,
1619         ARegion *ar,
1620         bool only3d)
1621 {
1622         int dflag = 0;
1623         RegionView3D *rv3d = ar->regiondata;
1624         int offsx,  offsy,  winx,  winy;
1625
1626         /* check that we have grease-pencil stuff to draw */
1627         // XXX: This is the only place that still uses this function
1628         bGPdata *gpd = ED_gpencil_data_get_active_v3d(view_layer, v3d);
1629         if (gpd == NULL) return;
1630
1631         /* when rendering to the offscreen buffer we don't want to
1632          * deal with the camera border, otherwise map the coords to the camera border. */
1633         if ((rv3d->persp == RV3D_CAMOB) && !(G.f & G_RENDER_OGL)) {
1634                 rctf rectf;
1635                 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &rectf, true); /* no shift */
1636
1637                 offsx = round_fl_to_int(rectf.xmin);
1638                 offsy = round_fl_to_int(rectf.ymin);
1639                 winx  = round_fl_to_int(rectf.xmax - rectf.xmin);
1640                 winy  = round_fl_to_int(rectf.ymax - rectf.ymin);
1641         }
1642         else {
1643                 offsx = 0;
1644                 offsy = 0;
1645                 winx  = ar->winx;
1646                 winy  = ar->winy;
1647         }
1648
1649         /* set flags */
1650         if (only3d) {
1651                 /* 3D strokes/3D space:
1652                  * - only 3D space points
1653                  * - don't status text either (as it's the wrong space)
1654                  */
1655                 dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS);
1656         }
1657
1658         if (v3d->flag2 & V3D_RENDER_OVERRIDE) {
1659                 /* don't draw status text when "only render" flag is set */
1660                 dflag |= GP_DRAWDATA_NOSTATUS;
1661         }
1662
1663         if ((wm == NULL) || ED_screen_animation_playing(wm)) {
1664                 /* don't show onion-skins during animation playback/scrub (i.e. it obscures the poses)
1665                  * OpenGL Renders (i.e. final output), or depth buffer (i.e. not real strokes)
1666                  */
1667                 dflag |= GP_DRAWDATA_NO_ONIONS;
1668         }
1669
1670         /* draw it! */
1671         gp_draw_data_all(view_layer, rv3d, scene, gpd, offsx, offsy, winx, winy, CFRA, dflag, v3d->spacetype);
1672 }
1673
1674 /* draw grease-pencil sketches to specified 3d-view for gp object
1675  * assuming that matrices are already set correctly
1676  */
1677 void ED_gpencil_draw_view3d_object(wmWindowManager *wm, Scene *scene, Depsgraph *depsgraph, Object *ob, View3D *v3d, ARegion *ar, bool only3d)
1678 {
1679         int dflag = 0;
1680         RegionView3D *rv3d = ar->regiondata;
1681         int offsx, offsy, winx, winy;
1682
1683         /* check that we have grease-pencil stuff to draw */
1684         bGPdata *gpd = ob->data;
1685         if (gpd == NULL) return;
1686
1687         /* when rendering to the offscreen buffer we don't want to
1688          * deal with the camera border, otherwise map the coords to the camera border. */
1689         if ((rv3d->persp == RV3D_CAMOB) && !(G.f & G_RENDER_OGL)) {
1690                 rctf rectf;
1691                 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &rectf, true); /* no shift */
1692
1693                 offsx = round_fl_to_int(rectf.xmin);
1694                 offsy = round_fl_to_int(rectf.ymin);
1695                 winx = round_fl_to_int(rectf.xmax - rectf.xmin);
1696                 winy = round_fl_to_int(rectf.ymax - rectf.ymin);
1697         }
1698         else {
1699                 offsx = 0;
1700                 offsy = 0;
1701                 winx = ar->winx;
1702                 winy = ar->winy;
1703         }
1704
1705         /* set flags */
1706         if (only3d) {
1707                 /* 3D strokes/3D space:
1708                  * - only 3D space points
1709                  * - don't status text either (as it's the wrong space)
1710                  */
1711                 dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS);
1712         }
1713
1714         if (v3d->flag2 & V3D_RENDER_OVERRIDE) {
1715                 /* don't draw status text when "only render" flag is set */
1716                 dflag |= GP_DRAWDATA_NOSTATUS;
1717         }
1718
1719         if ((wm == NULL) || ED_screen_animation_playing(wm)) {
1720                 /* don't show onionskins during animation playback/scrub (i.e. it obscures the poses)
1721                  * OpenGL Renders (i.e. final output), or depth buffer (i.e. not real strokes)
1722                  */
1723                 dflag |= GP_DRAWDATA_NO_ONIONS;
1724         }
1725
1726         /* draw it! */
1727         ToolSettings *ts = scene->toolsettings;
1728         Brush *brush = BKE_paint_brush(&ts->gp_paint->paint);
1729         if (brush != NULL) {
1730                 gp_draw_data(rv3d, brush, 1.0f, ob, gpd,
1731                         offsx, offsy, winx, winy, CFRA, dflag);
1732         }
1733 }
1734
1735 void ED_gpencil_draw_ex(
1736         ViewLayer *view_layer, RegionView3D *rv3d, Scene *scene,
1737         bGPdata *gpd, int winx, int winy, const int cfra, const char spacetype)
1738 {
1739         int dflag = GP_DRAWDATA_NOSTATUS | GP_DRAWDATA_ONLYV2D;
1740
1741         gp_draw_data_all(view_layer, rv3d, scene, gpd, 0, 0, winx, winy, cfra, dflag, spacetype);
1742 }