76502a43eb78f12d5a8f0f556b1b4005a48ae0f5
[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
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/gpencil/drawgpencil.c
27  *  \ingroup edgpencil
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stddef.h>
34 #include <math.h>
35 #include <float.h>
36
37 #include "BLI_sys_types.h"
38
39 #include "BLI_math.h"
40 #include "BLI_utildefines.h"
41
42 #include "DNA_gpencil_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_screen_types.h"
45 #include "DNA_space_types.h"
46 #include "DNA_view3d_types.h"
47 #include "DNA_userdef_types.h"
48
49 #include "BKE_context.h"
50 #include "BKE_global.h"
51 #include "BKE_gpencil.h"
52
53 #include "WM_api.h"
54
55 #include "BIF_gl.h"
56 #include "BIF_glutil.h"
57
58 #include "ED_gpencil.h"
59 #include "ED_view3d.h"
60
61 /* ************************************************** */
62 /* GREASE PENCIL DRAWING */
63
64 /* ----- General Defines ------ */
65
66 /* flags for sflag */
67 typedef enum eDrawStrokeFlags {
68         GP_DRAWDATA_NOSTATUS    = (1 << 0),   /* don't draw status info */
69         GP_DRAWDATA_ONLY3D      = (1 << 1),   /* only draw 3d-strokes */
70         GP_DRAWDATA_ONLYV2D     = (1 << 2),   /* only draw 'canvas' strokes */
71         GP_DRAWDATA_ONLYI2D     = (1 << 3),   /* only draw 'image' strokes */
72         GP_DRAWDATA_IEDITHACK   = (1 << 4),   /* special hack for drawing strokes in Image Editor (weird coordinates) */
73         GP_DRAWDATA_NO_XRAY     = (1 << 5),   /* don't draw xray in 3D view (which is default) */
74 } eDrawStrokeFlags;
75
76
77
78 /* thickness above which we should use special drawing */
79 #define GP_DRAWTHICKNESS_SPECIAL    3
80
81 /* ----- Tool Buffer Drawing ------ */
82
83 /* draw stroke defined in buffer (simple ogl lines/points for now, as dotted lines) */
84 static void gp_draw_stroke_buffer(tGPspoint *points, int totpoints, short thickness, short dflag, short sflag)
85 {
86         tGPspoint *pt;
87         int i;
88         
89         /* error checking */
90         if ((points == NULL) || (totpoints <= 0))
91                 return;
92         
93         /* check if buffer can be drawn */
94         if (dflag & (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_ONLYV2D))
95                 return;
96         
97         /* if drawing a single point, draw it larger */
98         if (totpoints == 1) {
99                 /* draw point */
100                 glBegin(GL_POINTS);
101                 glVertex2iv(&points->x);
102                 glEnd();
103         }
104         else if (sflag & GP_STROKE_ERASER) {
105                 /* don't draw stroke at all! */
106         }
107         else {
108                 float oldpressure = points[0].pressure;
109                 
110                 /* draw stroke curve */
111                 if (G.debug & G_DEBUG) setlinestyle(2);
112
113                 glLineWidth(oldpressure * thickness);
114                 glBegin(GL_LINE_STRIP);
115
116                 for (i = 0, pt = points; i < totpoints && pt; i++, pt++) {
117                         /* if there was a significant pressure change, stop the curve, change the thickness of the stroke,
118                          * and continue drawing again (since line-width cannot change in middle of GL_LINE_STRIP)
119                          */
120                         if (fabsf(pt->pressure - oldpressure) > 0.2f) {
121                                 glEnd();
122                                 glLineWidth(pt->pressure * thickness);
123                                 glBegin(GL_LINE_STRIP);
124                                 
125                                 /* need to roll-back one point to ensure that there are no gaps in the stroke */
126                                 if (i != 0) glVertex2iv(&(pt - 1)->x);
127
128                                 /* now the point we want... */
129                                 glVertex2iv(&pt->x);
130                                 
131                                 oldpressure = pt->pressure;
132                         }
133                         else
134                                 glVertex2iv(&pt->x);
135                 }
136                 glEnd();
137
138                 /* reset for predictable OpenGL context */
139                 glLineWidth(1.0f);
140                 
141                 if (G.debug & G_DEBUG) setlinestyle(0);
142         }
143 }
144
145 /* ----- Existing Strokes Drawing (3D and Point) ------ */
146
147 /* draw a given stroke - just a single dot (only one point) */
148 static void gp_draw_stroke_point(bGPDspoint *points, short thickness, short dflag, short sflag,
149                                  int offsx, int offsy, int winx, int winy)
150 {
151         /* draw point */
152         if (sflag & GP_STROKE_3DSPACE) {
153                 glBegin(GL_POINTS);
154                 glVertex3fv(&points->x);
155                 glEnd();
156         }
157         else {
158                 float co[2];
159                 
160                 /* get coordinates of point */
161                 if (sflag & GP_STROKE_2DSPACE) {
162                         co[0] = points->x;
163                         co[1] = points->y;
164                 }
165                 else if (sflag & GP_STROKE_2DIMAGE) {
166                         co[0] = (points->x * winx) + offsx;
167                         co[1] = (points->y * winy) + offsy;
168                 }
169                 else {
170                         co[0] = (points->x / 100 * winx) + offsx;
171                         co[1] = (points->y / 100 * winy) + offsy;
172                 }
173                 
174                 /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, simple dot looks ok
175                  *  - also mandatory in if Image Editor 'image-based' dot
176                  */
177                 if ((thickness < GP_DRAWTHICKNESS_SPECIAL) ||
178                     ((dflag & GP_DRAWDATA_IEDITHACK) && (sflag & GP_STROKE_2DSPACE)))
179                 {
180                         glBegin(GL_POINTS);
181                         glVertex2fv(co);
182                         glEnd();
183                 }
184                 else {
185                         /* draw filled circle as is done in circf (but without the matrix push/pops which screwed things up) */
186                         GLUquadricObj *qobj = gluNewQuadric(); 
187                         
188                         gluQuadricDrawStyle(qobj, GLU_FILL); 
189                         
190                         /* need to translate drawing position, but must reset after too! */
191                         glTranslatef(co[0], co[1], 0.0);
192                         gluDisk(qobj, 0.0,  thickness, 32, 1); 
193                         glTranslatef(-co[0], -co[1], 0.0);
194                         
195                         gluDeleteQuadric(qobj);
196                 }
197         }
198 }
199
200 /* draw a given stroke in 3d (i.e. in 3d-space), using simple ogl lines */
201 static void gp_draw_stroke_3d(bGPDspoint *points, int totpoints, short thickness, short debug)
202 {
203         bGPDspoint *pt;
204         float curpressure = points[0].pressure;
205         int i;
206         
207         /* draw stroke curve */
208         glLineWidth(curpressure * thickness);
209         glBegin(GL_LINE_STRIP);
210         for (i = 0, pt = points; i < totpoints && pt; i++, pt++) {
211                 /* if there was a significant pressure change, stop the curve, change the thickness of the stroke,
212                  * and continue drawing again (since line-width cannot change in middle of GL_LINE_STRIP)
213                  * Note: we want more visible levels of pressures when thickness is bigger.
214                  */
215                 if (fabsf(pt->pressure - curpressure) > 0.2f / (float)thickness) {
216                         glEnd();
217                         curpressure = pt->pressure;
218                         glLineWidth(curpressure * thickness);
219                         glBegin(GL_LINE_STRIP);
220                         
221                         /* need to roll-back one point to ensure that there are no gaps in the stroke */
222                         if (i != 0) glVertex3fv(&(pt - 1)->x);
223                         
224                         /* now the point we want... */
225                         glVertex3fv(&pt->x);
226                 }
227                 else {
228                         glVertex3fv(&pt->x);
229                 }
230         }
231         glEnd();
232         
233         /* draw debug points of curve on top? */
234         if (debug) {
235                 glBegin(GL_POINTS);
236                 for (i = 0, pt = points; i < totpoints && pt; i++, pt++)
237                         glVertex3fv(&pt->x);
238                 glEnd();
239         }
240 }
241
242 /* ----- Fancy 2D-Stroke Drawing ------ */
243
244 /* draw a given stroke in 2d */
245 static void gp_draw_stroke(bGPDspoint *points, int totpoints, short thickness_s, short dflag, short sflag,
246                            short debug, int offsx, int offsy, int winx, int winy)
247 {
248         /* otherwise thickness is twice that of the 3D view */
249         float thickness = (float)thickness_s * 0.5f;
250
251         /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, 'smooth' opengl lines look better
252          *  - 'smooth' opengl lines are also required if Image Editor 'image-based' stroke
253          */
254         if ((thickness < GP_DRAWTHICKNESS_SPECIAL) ||
255             ((dflag & GP_DRAWDATA_IEDITHACK) && (dflag & GP_DRAWDATA_ONLYV2D)))
256         {
257                 bGPDspoint *pt;
258                 int i;
259                 
260                 glBegin(GL_LINE_STRIP);
261                 for (i = 0, pt = points; i < totpoints && pt; i++, pt++) {
262                         if (sflag & GP_STROKE_2DSPACE) {
263                                 glVertex2f(pt->x, pt->y);
264                         }
265                         else if (sflag & GP_STROKE_2DIMAGE) {
266                                 const float x = (pt->x * winx) + offsx;
267                                 const float y = (pt->y * winy) + offsy;
268                                 
269                                 glVertex2f(x, y);
270                         }
271                         else {
272                                 const float x = (pt->x / 100 * winx) + offsx;
273                                 const float y = (pt->y / 100 * winy) + offsy;
274                                 
275                                 glVertex2f(x, y);
276                         }
277                 }
278                 glEnd();
279         }
280         
281         /* tessellation code - draw stroke as series of connected quads with connection
282          * edges rotated to minimize shrinking artifacts, and rounded endcaps
283          */
284         else {
285                 bGPDspoint *pt1, *pt2;
286                 float pm[2];
287                 int i;
288                 
289                 glShadeModel(GL_FLAT);
290                 glBegin(GL_QUADS);
291                 
292                 for (i = 0, pt1 = points, pt2 = points + 1; i < (totpoints - 1); i++, pt1++, pt2++) {
293                         float s0[2], s1[2];     /* segment 'center' points */
294                         float t0[2], t1[2];     /* tessellated coordinates */
295                         float m1[2], m2[2];     /* gradient and normal */
296                         float mt[2], sc[2];     /* gradient for thickness, point for end-cap */
297                         float pthick;           /* thickness at segment point */
298                         
299                         /* get x and y coordinates from points */
300                         if (sflag & GP_STROKE_2DSPACE) {
301                                 s0[0] = pt1->x;      s0[1] = pt1->y;
302                                 s1[0] = pt2->x;      s1[1] = pt2->y;
303                         }
304                         else if (sflag & GP_STROKE_2DIMAGE) {
305                                 s0[0] = (pt1->x * winx) + offsx;
306                                 s0[1] = (pt1->y * winy) + offsy;
307                                 s1[0] = (pt2->x * winx) + offsx;
308                                 s1[1] = (pt2->y * winy) + offsy;
309                         }
310                         else {
311                                 s0[0] = (pt1->x / 100 * winx) + offsx;
312                                 s0[1] = (pt1->y / 100 * winy) + offsy;
313                                 s1[0] = (pt2->x / 100 * winx) + offsx;
314                                 s1[1] = (pt2->y / 100 * winy) + offsy;
315                         }
316                         
317                         /* calculate gradient and normal - 'angle'=(ny/nx) */
318                         m1[1] = s1[1] - s0[1];
319                         m1[0] = s1[0] - s0[0];
320                         normalize_v2(m1);
321                         m2[1] = -m1[0];
322                         m2[0] = m1[1];
323                         
324                         /* always use pressure from first point here */
325                         pthick = (pt1->pressure * thickness);
326                         
327                         /* if the first segment, start of segment is segment's normal */
328                         if (i == 0) {
329                                 /* draw start cap first 
330                                  *      - make points slightly closer to center (about halfway across) 
331                                  */
332                                 mt[0] = m2[0] * pthick * 0.5f;
333                                 mt[1] = m2[1] * pthick * 0.5f;
334                                 sc[0] = s0[0] - (m1[0] * pthick * 0.75f);
335                                 sc[1] = s0[1] - (m1[1] * pthick * 0.75f);
336                                 
337                                 t0[0] = sc[0] - mt[0];
338                                 t0[1] = sc[1] - mt[1];
339                                 t1[0] = sc[0] + mt[0];
340                                 t1[1] = sc[1] + mt[1];
341                                 
342                                 glVertex2fv(t0);
343                                 glVertex2fv(t1);
344                                 
345                                 /* calculate points for start of segment */
346                                 mt[0] = m2[0] * pthick;
347                                 mt[1] = m2[1] * pthick;
348                                 
349                                 t0[0] = s0[0] - mt[0];
350                                 t0[1] = s0[1] - mt[1];
351                                 t1[0] = s0[0] + mt[0];
352                                 t1[1] = s0[1] + mt[1];
353                                 
354                                 /* draw this line twice (first to finish off start cap, then for stroke) */
355                                 glVertex2fv(t1);
356                                 glVertex2fv(t0);
357                                 glVertex2fv(t0);
358                                 glVertex2fv(t1);
359                         }
360                         /* if not the first segment, use bisector of angle between segments */
361                         else {
362                                 float mb[2];         /* bisector normal */
363                                 float athick, dfac;  /* actual thickness, difference between thicknesses */
364                                 
365                                 /* calculate gradient of bisector (as average of normals) */
366                                 mb[0] = (pm[0] + m2[0]) / 2;
367                                 mb[1] = (pm[1] + m2[1]) / 2;
368                                 normalize_v2(mb);
369                                 
370                                 /* calculate gradient to apply 
371                                  *  - as basis, use just pthick * bisector gradient
372                                  *      - if cross-section not as thick as it should be, add extra padding to fix it
373                                  */
374                                 mt[0] = mb[0] * pthick;
375                                 mt[1] = mb[1] * pthick;
376                                 athick = len_v2(mt);
377                                 dfac = pthick - (athick * 2);
378                                 
379                                 if (((athick * 2.0f) < pthick) && (IS_EQF(athick, pthick) == 0)) {
380                                         mt[0] += (mb[0] * dfac);
381                                         mt[1] += (mb[1] * dfac);
382                                 }
383                                 
384                                 /* calculate points for start of segment */
385                                 t0[0] = s0[0] - mt[0];
386                                 t0[1] = s0[1] - mt[1];
387                                 t1[0] = s0[0] + mt[0];
388                                 t1[1] = s0[1] + mt[1];
389                                 
390                                 /* draw this line twice (once for end of current segment, and once for start of next) */
391                                 glVertex2fv(t1);
392                                 glVertex2fv(t0);
393                                 glVertex2fv(t0);
394                                 glVertex2fv(t1);
395                         }
396                         
397                         /* if last segment, also draw end of segment (defined as segment's normal) */
398                         if (i == totpoints - 2) {
399                                 /* for once, we use second point's pressure (otherwise it won't be drawn) */
400                                 pthick = (pt2->pressure * thickness);
401                                 
402                                 /* calculate points for end of segment */
403                                 mt[0] = m2[0] * pthick;
404                                 mt[1] = m2[1] * pthick;
405                                 
406                                 t0[0] = s1[0] - mt[0];
407                                 t0[1] = s1[1] - mt[1];
408                                 t1[0] = s1[0] + mt[0];
409                                 t1[1] = s1[1] + mt[1];
410                                 
411                                 /* draw this line twice (once for end of stroke, and once for endcap)*/
412                                 glVertex2fv(t1);
413                                 glVertex2fv(t0);
414                                 glVertex2fv(t0);
415                                 glVertex2fv(t1);
416                                 
417                                 
418                                 /* draw end cap as last step 
419                                  *      - make points slightly closer to center (about halfway across) 
420                                  */
421                                 mt[0] = m2[0] * pthick * 0.5f;
422                                 mt[1] = m2[1] * pthick * 0.5f;
423                                 sc[0] = s1[0] + (m1[0] * pthick * 0.75f);
424                                 sc[1] = s1[1] + (m1[1] * pthick * 0.75f);
425                                 
426                                 t0[0] = sc[0] - mt[0];
427                                 t0[1] = sc[1] - mt[1];
428                                 t1[0] = sc[0] + mt[0];
429                                 t1[1] = sc[1] + mt[1];
430                                 
431                                 glVertex2fv(t1);
432                                 glVertex2fv(t0);
433                         }
434                         
435                         /* store stroke's 'natural' normal for next stroke to use */
436                         copy_v2_v2(pm, m2);
437                 }
438                 
439                 glEnd();
440         }
441         
442         /* draw debug points of curve on top? (original stroke points) */
443         if (debug) {
444                 bGPDspoint *pt;
445                 int i;
446                 
447                 glBegin(GL_POINTS);
448                 for (i = 0, pt = points; i < totpoints && pt; i++, pt++) {
449                         if (sflag & GP_STROKE_2DSPACE) {
450                                 glVertex2fv(&pt->x);
451                         }
452                         else if (sflag & GP_STROKE_2DIMAGE) {
453                                 const float x = (float)((pt->x * winx) + offsx);
454                                 const float y = (float)((pt->y * winy) + offsy);
455                                 
456                                 glVertex2f(x, y);
457                         }
458                         else {
459                                 const float x = (float)(pt->x / 100 * winx) + offsx;
460                                 const float y = (float)(pt->y / 100 * winy) + offsy;
461                                 
462                                 glVertex2f(x, y);
463                         }
464                 }
465                 glEnd();
466         }
467 }
468
469 /* ----- General Drawing ------ */
470
471 /* draw a set of strokes */
472 static void gp_draw_strokes(bGPDframe *gpf, int offsx, int offsy, int winx, int winy, int dflag,
473                             short debug, short lthick, const float color[4])
474 {
475         bGPDstroke *gps;
476         
477         /* set color first (may need to reset it again later too) */
478         glColor4fv(color);
479         
480         for (gps = gpf->strokes.first; gps; gps = gps->next) {
481                 /* check if stroke can be drawn - checks here generally fall into pairs */
482                 if ((dflag & GP_DRAWDATA_ONLY3D) && !(gps->flag & GP_STROKE_3DSPACE))
483                         continue;
484                 if (!(dflag & GP_DRAWDATA_ONLY3D) && (gps->flag & GP_STROKE_3DSPACE))
485                         continue;
486                 if ((dflag & GP_DRAWDATA_ONLYV2D) && !(gps->flag & GP_STROKE_2DSPACE))
487                         continue;
488                 if (!(dflag & GP_DRAWDATA_ONLYV2D) && (gps->flag & GP_STROKE_2DSPACE))
489                         continue;
490                 if ((dflag & GP_DRAWDATA_ONLYI2D) && !(gps->flag & GP_STROKE_2DIMAGE))
491                         continue;
492                 if (!(dflag & GP_DRAWDATA_ONLYI2D) && (gps->flag & GP_STROKE_2DIMAGE))
493                         continue;
494                 if ((gps->points == NULL) || (gps->totpoints < 1))
495                         continue;
496                 
497                 /* check which stroke-drawer to use */
498                 if (dflag & GP_DRAWDATA_ONLY3D) {
499                         const int no_xray = (dflag & GP_DRAWDATA_NO_XRAY);
500                         int mask_orig = 0;
501                         
502                         if (no_xray) {
503                                 glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
504                                 glDepthMask(0);
505                                 glEnable(GL_DEPTH_TEST);
506                                 
507                                 /* first arg is normally rv3d->dist, but this isn't
508                                  * available here and seems to work quite well without */
509                                 bglPolygonOffset(1.0f, 1.0f);
510 #if 0
511                                 glEnable(GL_POLYGON_OFFSET_LINE);
512                                 glPolygonOffset(-1.0f, -1.0f);
513 #endif
514                         }
515                         
516                         if (gps->totpoints == 1) {
517                                 gp_draw_stroke_point(gps->points, lthick, dflag, gps->flag, offsx, offsy, winx, winy);
518                         }
519                         else {
520                                 gp_draw_stroke_3d(gps->points, gps->totpoints, lthick, debug);
521                         }
522                         
523                         if (no_xray) {
524                                 glDepthMask(mask_orig);
525                                 glDisable(GL_DEPTH_TEST);
526                                 
527                                 bglPolygonOffset(0.0, 0.0);
528 #if 0
529                                 glDisable(GL_POLYGON_OFFSET_LINE);
530                                 glPolygonOffset(0, 0);
531 #endif
532                         }
533                 }
534                 else {
535                         if (gps->totpoints == 1) {
536                                 gp_draw_stroke_point(gps->points, lthick, dflag, gps->flag, offsx, offsy, winx, winy);
537                         }
538                         else {
539                                 gp_draw_stroke(gps->points, gps->totpoints, lthick, dflag, gps->flag, debug, offsx, offsy, winx, winy);
540                         }
541                 }
542         }
543 }
544
545 /* draw grease-pencil datablock */
546 static void gp_draw_data(bGPdata *gpd, int offsx, int offsy, int winx, int winy, int cfra, int dflag)
547 {
548         bGPDlayer *gpl;
549         
550         /* reset line drawing style (in case previous user didn't reset) */
551         setlinestyle(0);
552         
553         /* turn on smooth lines (i.e. anti-aliasing) */
554         glEnable(GL_LINE_SMOOTH);
555         
556         /* turn on alpha-blending */
557         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
558         glEnable(GL_BLEND);
559                 
560         /* loop over layers, drawing them */
561         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
562                 bGPDframe *gpf;
563                 
564                 short debug = (gpl->flag & GP_LAYER_DRAWDEBUG) ? 1 : 0;
565                 short lthick = gpl->thickness;
566                 float color[4], tcolor[4];
567                 
568                 /* don't draw layer if hidden */
569                 if (gpl->flag & GP_LAYER_HIDE) 
570                         continue;
571                 
572                 /* get frame to draw */
573                 gpf = gpencil_layer_getframe(gpl, cfra, 0);
574                 if (gpf == NULL) 
575                         continue;
576                 
577                 /* set color, stroke thickness, and point size */
578                 glLineWidth(lthick);
579                 copy_v4_v4(color, gpl->color); // just for copying 4 array elements
580                 copy_v4_v4(tcolor, gpl->color); // additional copy of color (for ghosting)
581                 glColor4fv(color);
582                 glPointSize((float)(gpl->thickness + 2));
583                 
584                 /* apply xray layer setting */
585                 if (gpl->flag & GP_LAYER_NO_XRAY) dflag |=  GP_DRAWDATA_NO_XRAY;
586                 else dflag &= ~GP_DRAWDATA_NO_XRAY;
587                 
588                 /* draw 'onionskins' (frame left + right) */
589                 if (gpl->flag & GP_LAYER_ONIONSKIN) {
590                         /* drawing method - only immediately surrounding (gstep = 0),
591                          * or within a frame range on either side (gstep > 0)*/
592                         if (gpl->gstep) {
593                                 bGPDframe *gf;
594                                 float fac;
595                                 
596                                 /* draw previous frames first */
597                                 for (gf = gpf->prev; gf; gf = gf->prev) {
598                                         /* check if frame is drawable */
599                                         if ((gpf->framenum - gf->framenum) <= gpl->gstep) {
600                                                 /* alpha decreases with distance from curframe index */
601                                                 fac = 1.0f - ((float)(gpf->framenum - gf->framenum) / (float)(gpl->gstep + 1));
602                                                 tcolor[3] = color[3] * fac * 0.66f;
603                                                 gp_draw_strokes(gf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
604                                         }
605                                         else 
606                                                 break;
607                                 }
608                                 
609                                 /* now draw next frames */
610                                 for (gf = gpf->next; gf; gf = gf->next) {
611                                         /* check if frame is drawable */
612                                         if ((gf->framenum - gpf->framenum) <= gpl->gstep) {
613                                                 /* alpha decreases with distance from curframe index */
614                                                 fac = 1.0f - ((float)(gf->framenum - gpf->framenum) / (float)(gpl->gstep + 1));
615                                                 tcolor[3] = color[3] * fac * 0.66f;
616                                                 gp_draw_strokes(gf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
617                                         }
618                                         else 
619                                                 break;
620                                 }
621                                 
622                                 /* restore alpha */
623                                 glColor4fv(color);
624                         }
625                         else {
626                                 /* draw the strokes for the ghost frames (at half of the alpha set by user) */
627                                 if (gpf->prev) {
628                                         tcolor[3] = (color[3] / 7);
629                                         gp_draw_strokes(gpf->prev, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
630                                 }
631                                 
632                                 if (gpf->next) {
633                                         tcolor[3] = (color[3] / 4);
634                                         gp_draw_strokes(gpf->next, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
635                                 }
636                                 
637                                 /* restore alpha */
638                                 glColor4fv(color);
639                         }
640                 }
641                 
642                 /* draw the strokes already in active frame */
643                 tcolor[3] = color[3];
644                 gp_draw_strokes(gpf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
645                 
646                 /* Check if may need to draw the active stroke cache, only if this layer is the active layer
647                  * that is being edited. (Stroke buffer is currently stored in gp-data)
648                  */
649                 if (ED_gpencil_session_active() && (gpl->flag & GP_LAYER_ACTIVE) &&
650                     (gpf->flag & GP_FRAME_PAINT))
651                 {
652                         /* Buffer stroke needs to be drawn with a different linestyle
653                          * to help differentiate them from normal strokes. */
654                         gp_draw_stroke_buffer(gpd->sbuffer, gpd->sbuffer_size, lthick, dflag, gpd->sbuffer_sflag);
655                 }
656         }
657         
658         /* turn off alpha blending, then smooth lines */
659         glDisable(GL_BLEND); // alpha blending
660         glDisable(GL_LINE_SMOOTH); // smooth lines
661                 
662         /* restore initial gl conditions */
663         glLineWidth(1.0);
664         glPointSize(1.0);
665         glColor4f(0, 0, 0, 1);
666 }
667
668 /* ----- Grease Pencil Sketches Drawing API ------ */
669
670 /* ............................
671  * XXX
672  *      We need to review the calls below, since they may be/are not that suitable for
673  *      the new ways that we intend to be drawing data...
674  * ............................ */
675
676 /* draw grease-pencil sketches to specified 2d-view that uses ibuf corrections */
677 void ED_gpencil_draw_2dimage(const bContext *C)
678 {
679         ScrArea *sa = CTX_wm_area(C);
680         ARegion *ar = CTX_wm_region(C);
681         Scene *scene = CTX_data_scene(C);
682         bGPdata *gpd;
683         int offsx, offsy, sizex, sizey;
684         int dflag = GP_DRAWDATA_NOSTATUS;
685         
686         gpd = ED_gpencil_data_get_active(C); // XXX
687         if (gpd == NULL) return;
688         
689         /* calculate rect */
690         switch (sa->spacetype) {
691                 case SPACE_IMAGE: /* image */
692                 case SPACE_CLIP: /* clip */
693                 {
694                         
695                         /* just draw using standard scaling (settings here are currently ignored anyways) */
696                         /* FIXME: the opengl poly-strokes don't draw at right thickness when done this way, so disabled */
697                         offsx = 0;
698                         offsy = 0;
699                         sizex = ar->winx;
700                         sizey = ar->winy;
701                         
702                         wmOrtho2(ar->v2d.cur.xmin, ar->v2d.cur.xmax, ar->v2d.cur.ymin, ar->v2d.cur.ymax);
703                         
704                         dflag |= GP_DRAWDATA_ONLYV2D | GP_DRAWDATA_IEDITHACK;
705                         break;
706                 }
707                 case SPACE_SEQ: /* sequence */
708                 {
709                         /* just draw using standard scaling (settings here are currently ignored anyways) */
710                         offsx = 0;
711                         offsy = 0;
712                         sizex = ar->winx;
713                         sizey = ar->winy;
714                         
715                         /* NOTE: I2D was used in 2.4x, but the old settings for that have been deprecated 
716                          * and everything moved to standard View2d 
717                          */
718                         dflag |= GP_DRAWDATA_ONLYV2D;
719                         break;
720                 }
721                 default: /* for spacetype not yet handled */
722                         offsx = 0;
723                         offsy = 0;
724                         sizex = ar->winx;
725                         sizey = ar->winy;
726                         
727                         dflag |= GP_DRAWDATA_ONLYI2D;
728                         break;
729         }
730         
731         
732         /* draw it! */
733         gp_draw_data(gpd, offsx, offsy, sizex, sizey, CFRA, dflag);
734 }
735
736 /* draw grease-pencil sketches to specified 2d-view assuming that matrices are already set correctly 
737  * Note: this gets called twice - first time with onlyv2d=1 to draw 'canvas' strokes,
738  * second time with onlyv2d=0 for screen-aligned strokes */
739 void ED_gpencil_draw_view2d(const bContext *C, bool onlyv2d)
740 {
741         ScrArea *sa = CTX_wm_area(C);
742         ARegion *ar = CTX_wm_region(C);
743         Scene *scene = CTX_data_scene(C);
744         bGPdata *gpd;
745         int dflag = 0;
746         
747         /* check that we have grease-pencil stuff to draw */
748         if (sa == NULL) return;
749         gpd = ED_gpencil_data_get_active(C); // XXX
750         if (gpd == NULL) return;
751         
752         /* special hack for Image Editor */
753         /* FIXME: the opengl poly-strokes don't draw at right thickness when done this way, so disabled */
754         if (ELEM(sa->spacetype, SPACE_IMAGE, SPACE_CLIP))
755                 dflag |= GP_DRAWDATA_IEDITHACK;
756         
757         /* draw it! */
758         if (onlyv2d) dflag |= (GP_DRAWDATA_ONLYV2D | GP_DRAWDATA_NOSTATUS);
759         gp_draw_data(gpd, 0, 0, ar->winx, ar->winy, CFRA, dflag);
760 }
761
762 /* draw grease-pencil sketches to specified 3d-view assuming that matrices are already set correctly 
763  * Note: this gets called twice - first time with only3d=1 to draw 3d-strokes,
764  * second time with only3d=0 for screen-aligned strokes */
765 void ED_gpencil_draw_view3d(Scene *scene, View3D *v3d, ARegion *ar, bool only3d)
766 {
767         bGPdata *gpd;
768         int dflag = 0;
769         RegionView3D *rv3d = ar->regiondata;
770         int offsx,  offsy,  winx,  winy;
771
772         /* check that we have grease-pencil stuff to draw */
773         gpd = ED_gpencil_data_get_active_v3d(scene, v3d);
774         if (gpd == NULL) return;
775
776         /* when rendering to the offscreen buffer we don't want to
777          * deal with the camera border, otherwise map the coords to the camera border. */
778         if ((rv3d->persp == RV3D_CAMOB) && !(G.f & G_RENDER_OGL)) {
779                 rctf rectf;
780                 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &rectf, true); /* no shift */
781
782                 offsx = iroundf(rectf.xmin);
783                 offsy = iroundf(rectf.ymin);
784                 winx  = iroundf(rectf.xmax - rectf.xmin);
785                 winy  = iroundf(rectf.ymax - rectf.ymin);
786         }
787         else {
788                 offsx = 0;
789                 offsy = 0;
790                 winx  = ar->winx;
791                 winy  = ar->winy;
792         }
793         
794         /* draw it! */
795         if (only3d) dflag |= (GP_DRAWDATA_ONLY3D | GP_DRAWDATA_NOSTATUS);
796
797         gp_draw_data(gpd, offsx, offsy, winx, winy, CFRA, dflag);
798 }
799
800 void ED_gpencil_draw_ex(bGPdata *gpd, int winx, int winy, const int cfra)
801 {
802         int dflag = GP_DRAWDATA_NOSTATUS | GP_DRAWDATA_ONLYV2D;
803
804         gp_draw_data(gpd, 0, 0, winx, winy, cfra, dflag);
805 }
806
807 /* ************************************************** */