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