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