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