Merging r39948 through r39988 from trunk into vgroup_modifiers.
[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                         glVertex2f(points->x, points->y);
111                 glEnd();
112         }
113         else if (sflag & GP_STROKE_ERASER) {
114                 /* don't draw stroke at all! */
115         }
116         else {
117                 float oldpressure = 0.0f;
118                 
119                 /* draw stroke curve */
120                 if (G.f & G_DEBUG) setlinestyle(2);
121                 
122                 glBegin(GL_LINE_STRIP);
123                 for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
124                         /* if there was a significant pressure change, stop the curve, change the thickness of the stroke,
125                          * and continue drawing again (since line-width cannot change in middle of GL_LINE_STRIP)
126                          */
127                         if (fabsf(pt->pressure - oldpressure) > 0.2f) {
128                                 glEnd();
129                                 glLineWidth(pt->pressure * thickness);
130                                 glBegin(GL_LINE_STRIP);
131                                 
132                                 /* need to roll-back one point to ensure that there are no gaps in the stroke */
133                                 if (i != 0) {
134                                         pt--;
135                                         glVertex2f(pt->x, pt->y);
136                                         pt++;
137                                 }
138                                 /* now the point we want... */
139                                 glVertex2f(pt->x, pt->y);
140                                 
141                                 oldpressure = pt->pressure;
142                         }
143                         else
144                                 glVertex2f(pt->x, pt->y);
145                 }
146                 glEnd();
147                 
148                 if (G.f & G_DEBUG) setlinestyle(0);
149         }
150 }
151
152 /* ----- Existing Strokes Drawing (3D and Point) ------ */
153
154 /* draw a given stroke - just a single dot (only one point) */
155 static void gp_draw_stroke_point (bGPDspoint *points, short thickness, short dflag, short sflag, int offsx, int offsy, int winx, int winy)
156 {
157         /* draw point */
158         if (sflag & GP_STROKE_3DSPACE) {
159                 glBegin(GL_POINTS);
160                         glVertex3f(points->x, points->y, points->z);
161                 glEnd();
162         }
163         else {
164                 float co[2];
165                 
166                 /* get coordinates of point */
167                 if (sflag & GP_STROKE_2DSPACE) {
168                         co[0]= points->x;
169                         co[1]= points->y;
170                 }
171                 else if (sflag & GP_STROKE_2DIMAGE) {
172                         co[0]= (points->x * winx) + offsx;
173                         co[1]= (points->y * winy) + offsy;
174                 }
175                 else {
176                         co[0]= (points->x / 100 * winx) + offsx;
177                         co[1]= (points->y / 100 * winy) + offsy;
178                 }
179                 
180                 /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, simple dot looks ok
181                  *      - also mandatory in if Image Editor 'image-based' dot
182                  */
183                 if ( (thickness < GP_DRAWTHICKNESS_SPECIAL) ||
184                          ((dflag & GP_DRAWDATA_IEDITHACK) && (sflag & GP_STROKE_2DSPACE)) )
185                 {
186                         glBegin(GL_POINTS);
187                                 glVertex2fv(co);
188                         glEnd();
189                 }
190                 else 
191                 {
192                         /* draw filled circle as is done in circf (but without the matrix push/pops which screwed things up) */
193                         GLUquadricObj *qobj = gluNewQuadric(); 
194                         
195                         gluQuadricDrawStyle(qobj, GLU_FILL); 
196                         
197                         /* need to translate drawing position, but must reset after too! */
198                         glTranslatef(co[0],  co[1], 0.); 
199                         gluDisk(qobj, 0.0,  thickness, 32, 1); 
200                         glTranslatef(-co[0],  -co[1], 0.);
201                         
202                         gluDeleteQuadric(qobj);
203                 }
204         }
205 }
206
207 /* draw a given stroke in 3d (i.e. in 3d-space), using simple ogl lines */
208 static void gp_draw_stroke_3d (bGPDspoint *points, int totpoints, short thickness, short debug)
209 {
210         bGPDspoint *pt;
211         float oldpressure = 0.0f;
212         int i;
213         
214         /* draw stroke curve */
215         glBegin(GL_LINE_STRIP);
216         for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
217                 /* if there was a significant pressure change, stop the curve, change the thickness of the stroke,
218                  * and continue drawing again (since line-width cannot change in middle of GL_LINE_STRIP)
219                  */
220                 if (fabsf(pt->pressure - oldpressure) > 0.2f) {
221                         glEnd();
222                         glLineWidth(pt->pressure * thickness);
223                         glBegin(GL_LINE_STRIP);
224                         
225                         /* need to roll-back one point to ensure that there are no gaps in the stroke */
226                         if (i != 0) {
227                                 pt--;
228                                 glVertex3f(pt->x, pt->y, pt->z);
229                                 pt++;
230                         }
231                         /* now the point we want... */
232                         glVertex3f(pt->x, pt->y, pt->z);
233                         
234                         oldpressure = pt->pressure;
235                 }
236                 else
237                         glVertex3f(pt->x, pt->y, pt->z);
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                         glVertex3f(pt->x, pt->y, pt->z);
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                                 glVertex2f(pt->x, pt->y);
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                 QUATCOPY(color, gpl->color); // just for copying 4 array elements
579                 QUATCOPY(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                 {
692                         
693                         /* just draw using standard scaling (settings here are currently ignored anyways) */
694                         // FIXME: the opengl poly-strokes don't draw at right thickness when done this way, so disabled
695                         offsx= 0;
696                         offsy= 0;
697                         sizex= ar->winx;
698                         sizey= ar->winy;
699                         
700                         wmOrtho2(ar->v2d.cur.xmin, ar->v2d.cur.xmax, ar->v2d.cur.ymin, ar->v2d.cur.ymax);
701                         
702                         dflag |= GP_DRAWDATA_ONLYV2D|GP_DRAWDATA_IEDITHACK;
703                 }
704                         break;
705                         
706                 case SPACE_SEQ: /* sequence */
707                 {
708                         SpaceSeq *sseq= (SpaceSeq *)sa->spacedata.first;
709                         float zoom, zoomx, zoomy;
710                         
711                         /* calculate accessory values */
712                         zoom= (float)(SEQ_ZOOM_FAC(sseq->zoom));
713                         if (sseq->mainb == SEQ_DRAW_IMG_IMBUF) {
714                                 /* XXX sequencer zoom should store it? */
715                                 zoomx = zoom; //  * (G.scene->r.xasp / G.scene->r.yasp);
716                                 zoomy = zoom;
717                         } 
718                         else
719                                 zoomx = zoomy = zoom;
720                         
721                         /* calculate transforms (Note: we use ibuf here, as we have it) */
722                         sizex= (int)(zoomx * ibuf->x);
723                         sizey= (int)(zoomy * ibuf->y);
724                         offsx= (int)( (ar->winx-sizex)/2 + sseq->xof );
725                         offsy= (int)( (ar->winy-sizey)/2 + sseq->yof );
726                         
727                         dflag |= GP_DRAWDATA_ONLYI2D;
728                 }
729                         break;
730                         
731                 default: /* for spacetype not yet handled */
732                         offsx= 0;
733                         offsy= 0;
734                         sizex= ar->winx;
735                         sizey= ar->winy;
736                         
737                         dflag |= GP_DRAWDATA_ONLYI2D;
738                         break;
739         }
740         
741         
742         /* draw it! */
743         gp_draw_data(gpd, offsx, offsy, sizex, sizey, CFRA, dflag);
744 }
745
746 /* draw grease-pencil sketches to specified 2d-view assuming that matrices are already set correctly 
747  * Note: this gets called twice - first time with onlyv2d=1 to draw 'canvas' strokes, second time with onlyv2d=0 for screen-aligned strokes
748  */
749 void draw_gpencil_view2d (bContext *C, short onlyv2d)
750 {
751         ScrArea *sa= CTX_wm_area(C);
752         ARegion *ar= CTX_wm_region(C);
753         Scene *scene= CTX_data_scene(C);
754         bGPdata *gpd;
755         int dflag = 0;
756         
757         /* check that we have grease-pencil stuff to draw */
758         if (sa == NULL) return;
759         gpd= gpencil_data_get_active(C); // XXX
760         if (gpd == NULL) return;
761         
762         /* special hack for Image Editor */
763         // FIXME: the opengl poly-strokes don't draw at right thickness when done this way, so disabled
764         if (sa->spacetype == SPACE_IMAGE)
765                 dflag |= GP_DRAWDATA_IEDITHACK;
766         
767         /* draw it! */
768         if (onlyv2d) dflag |= (GP_DRAWDATA_ONLYV2D|GP_DRAWDATA_NOSTATUS);
769         gp_draw_data(gpd, 0, 0, ar->winx, ar->winy, CFRA, dflag);
770 }
771
772 /* draw grease-pencil sketches to specified 3d-view assuming that matrices are already set correctly 
773  * Note: this gets called twice - first time with only3d=1 to draw 3d-strokes, second time with only3d=0 for screen-aligned strokes
774  */
775
776 void draw_gpencil_view3d_ext (Scene *scene, View3D *v3d, ARegion *ar, short only3d)
777 {
778         bGPdata *gpd;
779         int dflag = 0;
780         rcti rect;
781         RegionView3D *rv3d= ar->regiondata;
782
783         /* check that we have grease-pencil stuff to draw */
784         gpd= gpencil_data_get_active_v3d(scene); // XXX
785         if (gpd == NULL) return;
786
787         /* when rendering to the offscreen buffer we dont want to
788          * deal with the camera border, otherwise map the coords to the camera border. */
789         if ((rv3d->persp == RV3D_CAMOB) && !(G.f & G_RENDER_OGL)) {
790                 rctf rectf;
791                 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &rectf, -1); /* negative shift */
792                 BLI_copy_rcti_rctf(&rect, &rectf);
793         }
794         else {
795                 rect.xmin= 0;
796                 rect.ymin= 0;
797                 rect.xmax= ar->winx;
798                 rect.ymax= ar->winy;
799         }
800         
801         /* draw it! */
802         if (only3d) dflag |= (GP_DRAWDATA_ONLY3D|GP_DRAWDATA_NOSTATUS);
803
804         gp_draw_data(gpd, rect.xmin, rect.ymin, rect.xmax, rect.ymax, CFRA, dflag);
805 }
806
807 void draw_gpencil_view3d (bContext *C, short only3d)
808 {
809         ARegion *ar= CTX_wm_region(C);
810         View3D *v3d= CTX_wm_view3d(C);
811         Scene *scene= CTX_data_scene(C);
812         draw_gpencil_view3d_ext(scene, v3d, ar, only3d);
813 }
814
815 /* ************************************************** */