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