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