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