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