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