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