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