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