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