4b6b5ab26c8d757c5962c42625e8c6ddb16c5b3b
[blender.git] / source / blender / src / drawgpencil.c
1 /**
2  * $Id: drawgpencil.c 14881 2008-05-18 10:41:42Z aligorith $
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include "MEM_guardedalloc.h"
39
40 #include "IMB_imbuf.h"
41 #include "IMB_imbuf_types.h"
42
43 #include "BMF_Api.h"
44
45 #include "BLI_arithb.h"
46 #include "BLI_blenlib.h"
47
48 #include "DNA_listBase.h"
49 #include "DNA_gpencil_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_screen_types.h"
52 #include "DNA_space_types.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_view3d_types.h"
55
56 #include "BKE_global.h"
57 #include "BKE_utildefines.h"
58 #include "BKE_blender.h"
59
60 #include "BIF_gl.h"
61 #include "BIF_glutil.h"
62 #include "BIF_butspace.h"
63 #include "BIF_graphics.h"
64 #include "BIF_interface.h"
65 #include "BIF_mywindow.h"
66 #include "BIF_resources.h"
67 #include "BIF_space.h"
68 #include "BIF_screen.h"
69 #include "BIF_toolbox.h"
70 #include "BIF_toets.h"
71
72 #include "BDR_gpencil.h"
73 #include "BIF_drawgpencil.h"
74
75 #include "BSE_drawipo.h"
76 #include "BSE_headerbuttons.h"
77 #include "BSE_view.h"
78
79 #include "blendef.h"
80 #include "butspace.h"
81
82 #include "PIL_time.h"                   /* sleep                                */
83 #include "mydevice.h"
84
85 /* ************************************************** */
86 /* GREASE PENCIL PANEL-UI DRAWING */
87
88 /* Every space which implements Grease-Pencil functionality should have a panel
89  * for the settings. All of the space-dependent parts should be coded in the panel
90  * code for that space, but the rest is all handled by generic panel here.
91  */
92
93 /* ------- Callbacks ----------- */
94 /* These are just 'dummy wrappers' around gpencil api calls */
95
96 /* make layer active one after being clicked on */
97 void gp_ui_activelayer_cb (void *gpd, void *gpl)
98 {
99         gpencil_layer_setactive(gpd, gpl);
100         allqueue(REDRAWACTION, 0);
101 }
102
103 /* rename layer and set active */
104 void gp_ui_renamelayer_cb (void *gpd_arg, void *gpl_arg)
105 {
106         bGPdata *gpd= (bGPdata *)gpd_arg;
107         bGPDlayer *gpl= (bGPDlayer *)gpl_arg;
108         
109         BLI_uniquename(&gpd->layers, gpl, "GP_Layer", offsetof(bGPDlayer, info[0]), 128);
110         gpencil_layer_setactive(gpd, gpl);
111         allqueue(REDRAWACTION, 0);
112 }
113
114 /* add a new layer */
115 void gp_ui_addlayer_cb (void *gpd, void *dummy)
116 {
117         gpencil_layer_addnew(gpd);
118         allqueue(REDRAWACTION, 0);
119 }
120
121 /* delete active layer */
122 void gp_ui_dellayer_cb (void *gpd, void *dummy)
123 {
124         gpencil_layer_delactive(gpd);
125         allqueue(REDRAWACTION, 0);
126 }
127
128 /* delete last stroke of active layer */
129 void gp_ui_delstroke_cb (void *gpd, void *gpl)
130 {
131         bGPDframe *gpf= gpencil_layer_getframe(gpl, CFRA, 0);
132         
133         gpencil_layer_setactive(gpd, gpl);
134         gpencil_frame_delete_laststroke(gpf);
135 }
136
137 /* delete active frame of active layer */
138 void gp_ui_delframe_cb (void *gpd, void *gpl)
139 {
140         bGPDframe *gpf= gpencil_layer_getframe(gpl, CFRA, 0);
141         
142         gpencil_layer_setactive(gpd, gpl);
143         gpencil_layer_delframe(gpl, gpf);
144         
145         allqueue(REDRAWACTION, 0);
146 }
147
148 /* ------- Drawing Code ------- */
149
150 /* draw the controls for a given layer */
151 static void gp_drawui_layer (uiBlock *block, bGPdata *gpd, bGPDlayer *gpl, short *xco, short *yco)
152 {
153         uiBut *but;
154         short active= (gpl->flag & GP_LAYER_ACTIVE);
155         short width= 314;
156         short height;
157         int rb_col;
158         
159         /* unless button has own callback, it adds this callback to button */
160         uiBlockSetFunc(block, gp_ui_activelayer_cb, gpd, gpl);
161         
162         /* draw header */
163         {
164                 uiBlockSetEmboss(block, UI_EMBOSSN);
165                 
166                 /* rounded header */
167                 if (active) uiBlockSetCol(block, TH_BUT_ACTION);
168                         rb_col= (active)?-20:20;
169                         uiDefBut(block, ROUNDBOX, B_DIFF, "", *xco-8, *yco-2, width, 24, NULL, 5.0, 0.0, 15 , rb_col-20, ""); 
170                 if (active) uiBlockSetCol(block, TH_AUTO);
171                 
172                 /* lock toggle */
173                 uiDefIconButBitI(block, ICONTOG, GP_LAYER_LOCKED, B_REDR, ICON_UNLOCKED,        *xco-7, *yco-1, 20, 20, &gpl->flag, 0.0, 0.0, 0, 0, "Layer cannot be modified");
174         }
175         
176         /* when layer is locked or hidden, only draw header */
177         if (gpl->flag & (GP_LAYER_LOCKED|GP_LAYER_HIDE)) {
178                 char name[256]; /* gpl->info is 128, but we need space for 'locked/hidden' as well */
179                 
180                 height= 0;
181                 
182                 /* visibility button (only if hidden but not locked!) */
183                 if ((gpl->flag & GP_LAYER_HIDE) && !(gpl->flag & GP_LAYER_LOCKED))
184                         uiDefIconButBitI(block, ICONTOG, GP_LAYER_HIDE, B_REDR, ICON_RESTRICT_VIEW_OFF, *xco+12, *yco-1, 20, 20, &gpl->flag, 0.0, 0.0, 0, 0, "Visibility of layer");
185                 
186                 /* name */
187                 if (gpl->flag & GP_LAYER_HIDE)
188                         sprintf(name, "%s (Hidden)", gpl->info);
189                 else
190                         sprintf(name, "%s (Locked)", gpl->info);
191                 uiDefBut(block, LABEL, 1, name, *xco+35, *yco, 240, 20, NULL, 0.0, 0.0, 0, 0, "Short description of what this layer is for (optional)");
192                         
193                 /* delete button (only if hidden but not locked!) */
194                 if ((gpl->flag & GP_LAYER_HIDE) & !(gpl->flag & GP_LAYER_LOCKED)) {
195                         but= uiDefIconBut(block, BUT, B_REDR, ICON_X, *xco+(width-30), *yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Delete layer");
196                         uiButSetFunc(but, gp_ui_dellayer_cb, gpd, NULL);
197                 }       
198                 uiBlockSetEmboss(block, UI_EMBOSS);
199         }
200         else {
201                 height= 97;
202                 
203                 /* draw rest of header */
204                 {
205                         /* visibility button */
206                         uiDefIconButBitI(block, ICONTOG, GP_LAYER_HIDE, B_REDR, ICON_RESTRICT_VIEW_OFF, *xco+12, *yco-1, 20, 20, &gpl->flag, 0.0, 0.0, 0, 0, "Visibility of layer");
207                         
208                         uiBlockSetEmboss(block, UI_EMBOSS);
209                         
210                         /* name */
211                         but= uiDefButC(block, TEX, B_REDR, "Info:",     *xco+36, *yco, 240, 19, gpl->info, 0, 127, 0, 0, "Short description of what this layer is for (optional)");
212                         uiButSetFunc(but, gp_ui_renamelayer_cb, gpd, gpl);
213                         
214                         /* delete 'button' */
215                         uiBlockSetEmboss(block, UI_EMBOSSN);
216                         
217                         but= uiDefIconBut(block, BUT, B_REDR, ICON_X, *xco+(width-30), *yco, 19, 19, NULL, 0.0, 0.0, 0.0, 0.0, "Delete layer");
218                         uiButSetFunc(but, gp_ui_dellayer_cb, gpd, NULL);
219                         
220                         uiBlockSetEmboss(block, UI_EMBOSS);
221                 }
222                 
223                 /* draw backdrop */
224                 if (active) uiBlockSetCol(block, TH_BUT_ACTION);
225                         uiDefBut(block, ROUNDBOX, B_DIFF, "", *xco-8, *yco-height, width, height-1, NULL, 5.0, 0.0, 12, rb_col, ""); 
226                 if (active) uiBlockSetCol(block, TH_AUTO);
227                 
228                 /* draw settings */
229                 {
230                         /* color */
231                         uiBlockBeginAlign(block);
232                                 uiDefButF(block, COL, B_REDR, "",               *xco, *yco-26, 150, 19, gpl->color, 0, 0, 0, 0, "Color to use for all strokes on this Grease Pencil Layer");
233                                 uiDefButF(block, NUMSLI, B_REDR, "Opacity: ",           *xco,*yco-45,150,19, &gpl->color[3], 0.3, 1.0, 0, 0, "Visibility of stroke (0.3 to 1.0)");
234                         uiBlockEndAlign(block);
235                         
236                         /* stroke thickness */
237                         uiDefButS(block, NUMSLI, B_REDR, "Thickness:",  *xco, *yco-75, 150, 20, &gpl->thickness, 1, 10, 0, 0, "Thickness of strokes (in pixels)");
238                         
239                         /* debugging options */
240                         if (G.f & G_DEBUG) {
241                                 uiDefButBitI(block, TOG, GP_LAYER_DRAWDEBUG, B_REDR, "Show Points", *xco, *yco-95, 150, 20, &gpl->flag, 0, 0, 0, 0, "Show points which form the strokes");
242                         }
243                         
244                         /* onion-skinning */
245                         uiBlockBeginAlign(block);
246                                 uiDefButBitI(block, TOG, GP_LAYER_ONIONSKIN, B_REDR, "Onion-Skin", *xco+160, *yco-26, 140, 20, &gpl->flag, 0, 0, 0, 0, "Ghost frames on either side of frame");
247                                 uiDefButS(block, NUMSLI, B_REDR, "GStep:",      *xco+160, *yco-46, 140, 20, &gpl->gstep, 0, 120, 0, 0, "Max number of frames on either side of active frame to show (0 = just 'first' available sketch on either side)");
248                         uiBlockEndAlign(block);
249                         
250                         /* options */
251                         uiBlockBeginAlign(block);
252                                 but= uiDefBut(block, BUT, B_REDR, "Del Active Frame", *xco+160, *yco-75, 140, 20, NULL, 0, 0, 0, 0, "Erases the the active frame for this layer (Hotkey = Alt-XKEY/DEL)");
253                                 uiButSetFunc(but, gp_ui_delframe_cb, gpd, gpl);
254                                 
255                                 but= uiDefBut(block, BUT, B_REDR, "Del Last Stroke", *xco+160, *yco-95, 140, 20, NULL, 0, 0, 0, 0, "Erases the last stroke from the active frame (Hotkey = Alt-XKEY/DEL)");
256                                 uiButSetFunc(but, gp_ui_delstroke_cb, gpd, gpl);
257                         uiBlockEndAlign(block);
258                 }
259         }
260         
261         /* adjust height for new to start */
262         (*yco) -= (height + 27); 
263
264
265 /* Draw the contents for a grease-pencil panel. This assumes several things:
266  *      - that panel has been created, is 318 x 204. max yco is 225
267  *      - that a toggle for turning on/off gpencil drawing is 150 x 20, starting from (10,225)
268  *              which is basically the top left-hand corner
269  * It will return the amount of extra space to extend the panel by
270  */
271 short draw_gpencil_panel (uiBlock *block, bGPdata *gpd, ScrArea *sa)
272 {
273         uiBut *but;
274         bGPDlayer *gpl;
275         short xco= 10, yco= 170;
276         
277         /* draw gpd settings first */
278         {
279                 /* add new layer buttons */
280                 but= uiDefBut(block, BUT, B_REDR, "Add New Layer", 10,205,150,20, 0, 0, 0, 0, 0, "Adds a new Grease Pencil Layer");
281                 uiButSetFunc(but, gp_ui_addlayer_cb, gpd, NULL);
282                 
283                 
284                 /* show override lmb-clicks button + painting lock */
285                 uiBlockBeginAlign(block);
286                         if ((gpd->flag & GP_DATA_EDITPAINT)==0) {
287                                 uiDefButBitI(block, TOG, GP_DATA_EDITPAINT, B_REDR, "Draw Mode", 170, 225, 130, 20, &gpd->flag, 0, 0, 0, 0, "Interpret click-drag as new strokes");
288                                 
289                                 uiBlockSetCol(block, TH_BUT_SETTING);
290                                         uiDefIconButBitI(block, ICONTOG, GP_DATA_LMBPLOCK, B_REDR, ICON_UNLOCKED,       300, 225, 20, 20, &gpd->flag, 0.0, 0.0, 0, 0, "Painting cannot occur with Shift-LMB (when making selections)");
291                                 uiBlockSetCol(block, TH_AUTO);
292                         }
293                         else
294                                 uiDefButBitI(block, TOG, GP_DATA_EDITPAINT, B_REDR, "Draw Mode", 170, 225, 150, 20, &gpd->flag, 0, 0, 0, 0, "Interpret click-drag as new strokes");
295                 uiBlockEndAlign(block);
296                 
297                 /* 'view align' button (naming depends on context) */
298                 if (sa->spacetype == SPACE_VIEW3D)
299                         uiDefButBitI(block, TOG, GP_DATA_VIEWALIGN, B_REDR, "Sketch in 3D", 170, 205, 150, 20, &gpd->flag, 0, 0, 0, 0, "New strokes are added in 3D-space");
300                 else if (sa->spacetype != SPACE_SEQ) /* not available for sequencer yet */
301                         uiDefButBitI(block, TOG, GP_DATA_VIEWALIGN, B_REDR, "Stick to View", 170, 205, 150, 20, &gpd->flag, 0, 0, 0, 0, "New strokes are added on 2d-canvas");
302         }
303         
304         /* draw for each layer */
305         for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
306                 gp_drawui_layer(block, gpd, gpl, &xco, &yco);
307         }
308         
309         /* return new height if necessary */
310         return (yco < 0) ? (204 - yco) : 204;
311 }       
312
313 /* ************************************************** */
314 /* GREASE PENCIL DRAWING */
315
316 /* ----- General Defines ------ */
317
318 /* flags for sflag */
319 enum {
320         GP_DRAWDATA_NOSTATUS    = (1<<0),       /* don't draw status info */
321         GP_DRAWDATA_ONLY3D              = (1<<1),       /* only draw 3d-strokes */
322         GP_DRAWDATA_ONLYV2D             = (1<<2),       /* only draw 'canvas' strokes */
323         GP_DRAWDATA_ONLYI2D             = (1<<3),       /* only draw 'image' strokes */
324 };
325
326 /* thickness above which we should use special drawing */
327 #define GP_DRAWTHICKNESS_SPECIAL        3
328
329 /* ----- Tool Buffer Drawing ------ */
330
331 /* draw stroke defined in buffer (simple ogl lines/points for now, as dotted lines) */
332 static void gp_draw_stroke_buffer (tGPspoint *points, int totpoints, short thickness, short dflag, short sflag)
333 {
334         tGPspoint *pt;
335         int i;
336         
337         /* error checking */
338         if ((points == NULL) || (totpoints <= 0))
339                 return;
340         
341         /* check if buffer can be drawn */
342         if (dflag & (GP_DRAWDATA_ONLY3D|GP_DRAWDATA_ONLYV2D))
343                 return;
344         
345         /* if drawing a single point, draw it larger */ 
346         if (totpoints == 1) {           
347                 /* draw point */
348                 glBegin(GL_POINTS);
349                         glVertex2f(points->x, points->y);
350                 glEnd();
351         }
352         else if (sflag & GP_STROKE_ERASER) {
353                 /* don't draw stroke at all! */
354         }
355         else {
356                 float oldpressure = 0.0f;
357                 
358                 /* draw stroke curve */
359                 if (G.f & G_DEBUG) setlinestyle(2);
360                 
361                 glBegin(GL_LINE_STRIP);
362                 for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
363                         if (fabs(pt->pressure - oldpressure) > 0.2f) {
364                                 glEnd();
365                                 glLineWidth(pt->pressure * thickness);
366                                 glBegin(GL_LINE_STRIP);
367                                 
368                                 glVertex2f(pt->x, pt->y);
369                                 
370                                 oldpressure = pt->pressure;
371                         }
372                         else
373                                 glVertex2f(pt->x, pt->y);
374                 }
375                 glEnd();
376                 
377                 if (G.f & G_DEBUG) setlinestyle(0);
378         }
379 }
380
381 /* ----- Existing Strokes Drawing (3D and Point) ------ */
382
383 /* draw a given stroke - just a single dot (only one point) */
384 static void gp_draw_stroke_point (bGPDspoint *points, short thickness, short sflag, int winx, int winy)
385 {
386         /* draw point */
387         if (sflag & GP_STROKE_3DSPACE) {
388                 glBegin(GL_POINTS);
389                         glVertex3f(points->x, points->y, points->z);
390                 glEnd();
391         }
392         else {
393                 float co[2];
394                 
395                 /* get coordinates of point */
396                 if (sflag & GP_STROKE_2DSPACE) {
397                         co[0]= points->x;
398                         co[1]= points->y;
399                 }
400                 else {
401                         co[0]= (points->x / 1000 * winx);
402                         co[1]= (points->y / 1000 * winy);
403                 }
404                 
405                 /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, simple opengl point will do */
406                 if (thickness < GP_DRAWTHICKNESS_SPECIAL) {
407                         glBegin(GL_POINTS);
408                                 glVertex2fv(co);
409                         glEnd();
410                 }
411                 else {
412                         /* draw filled circle as is done in circf (but without the matrix push/pops which screwed things up) */
413                         GLUquadricObj *qobj = gluNewQuadric(); 
414                         
415                         gluQuadricDrawStyle(qobj, GLU_FILL); 
416                         
417                         /* need to translate drawing position, but must reset after too! */
418                         glTranslatef(co[0],  co[1], 0.); 
419                         gluDisk( qobj, 0.0,  thickness, 32, 1); 
420                         glTranslatef(-co[0],  -co[1], 0.);
421                         
422                         gluDeleteQuadric(qobj);
423                 }
424         }
425 }
426
427 /* draw a given stroke in 3d (i.e. in 3d-space), using simple ogl lines */
428 static void gp_draw_stroke_3d (bGPDspoint *points, int totpoints, short thickness, short dflag, short sflag, short debug, int winx, int winy)
429 {
430         bGPDspoint *pt;
431         float oldpressure = 0.0f;
432         int i;
433         
434         /* draw stroke curve */
435         glBegin(GL_LINE_STRIP);
436         for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
437                 if (fabs(pt->pressure - oldpressure) > 0.2f) {
438                         glEnd();
439                         glLineWidth(pt->pressure * thickness);
440                         glBegin(GL_LINE_STRIP);
441                         
442                         glVertex3f(pt->x, pt->y, pt->z);
443                         
444                         oldpressure = pt->pressure;
445                 }
446                 else
447                         glVertex3f(pt->x, pt->y, pt->z);
448         }
449         glEnd();
450         
451         /* draw debug points of curve on top? */
452         if (debug) {
453                 glBegin(GL_POINTS);
454                 for (i=0, pt=points; i < totpoints && pt; i++, pt++)
455                         glVertex3f(pt->x, pt->y, pt->z);
456                 glEnd();
457         }
458 }
459
460 /* ----- Fancy 2D-Stroke Drawing ------ */
461
462 /* draw a given stroke in 2d */
463 static void gp_draw_stroke (bGPDspoint *points, int totpoints, short thickness, short dflag, short sflag, short debug, int winx, int winy)
464 {       
465         /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, 'smooth' opengl lines look better */
466         if (thickness < GP_DRAWTHICKNESS_SPECIAL) {
467                 bGPDspoint *pt;
468                 int i;
469                 
470                 glBegin(GL_LINE_STRIP);
471                 for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
472                         if (sflag & GP_STROKE_2DSPACE) {
473                                 glVertex2f(pt->x, pt->y);
474                         }
475                         else {
476                                 const float x= (pt->x / 1000 * winx);
477                                 const float y= (pt->y / 1000 * winy);
478                                 
479                                 glVertex2f(x, y);
480                         }
481                 }
482                 glEnd();
483         }
484         else { /* tesselation code: currently only enabled with rt != 0 */
485                 bGPDspoint *pt1, *pt2;
486                 float pm[2];
487                 int i;
488                 
489                 glShadeModel(GL_FLAT);
490                 glBegin(GL_QUADS);
491                 
492                 for (i=0, pt1=points, pt2=points+1; i < (totpoints-1); i++, pt1++, pt2++) {
493                         float s0[2], s1[2];             /* segment 'center' points */
494                         float t0[2], t1[2];             /* tesselated coordinates */
495                         float m1[2], m2[2];             /* gradient and normal */
496                         float mt[2], sc[2];             /* gradient for thickness, point for end-cap */
497                         float pthick;                   /* thickness at segment point */
498                         
499                         /* get x and y coordinates from points */
500                         if (sflag & GP_STROKE_2DSPACE) {
501                                 s0[0]= pt1->x;          s0[1]= pt1->y;
502                                 s1[0]= pt2->x;          s1[1]= pt2->y;
503                         }
504                         else {
505                                 s0[0]= (pt1->x / 1000 * winx);
506                                 s0[1]= (pt1->y / 1000 * winy);
507                                 s1[0]= (pt2->x / 1000 * winx);
508                                 s1[1]= (pt2->y / 1000 * winy);
509                         }               
510                         
511                         /* calculate gradient and normal - 'angle'=(ny/nx) */
512                         m1[1]= s1[1] - s0[1];           
513                         m1[0]= s1[0] - s0[0];
514                         Normalize2(m1);
515                         m2[1]= -m1[0];
516                         m2[0]= m1[1];
517                         
518                         /* always use pressure from first point here */
519                         pthick= (pt1->pressure * thickness);
520                         
521                         /* if the first segment, start of segment is segment's normal */
522                         if (i == 0) {
523                                 /* draw start cap first 
524                                  *      - make points slightly closer to center (about halfway across) 
525                                  */                             
526                                 mt[0]= m2[0] * pthick * 0.5;
527                                 mt[1]= m2[1] * pthick * 0.5;
528                                 sc[0]= s0[0] - (m1[0] * pthick * 0.75);
529                                 sc[1]= s0[1] - (m1[1] * pthick * 0.75);
530                                 
531                                 t0[0]= sc[0] - mt[0];
532                                 t0[1]= sc[1] - mt[1];
533                                 t1[0]= sc[0] + mt[0];
534                                 t1[1]= sc[1] + mt[1];
535                                 
536                                 glVertex2fv(t0);
537                                 glVertex2fv(t1);
538                                 
539                                 /* calculate points for start of segment */
540                                 mt[0]= m2[0] * pthick;
541                                 mt[1]= m2[1] * pthick;
542                                 
543                                 t0[0]= s0[0] - mt[0];
544                                 t0[1]= s0[1] - mt[1];
545                                 t1[0]= s0[0] + mt[0];
546                                 t1[1]= s0[1] + mt[1];
547                                 
548                                 /* draw this line twice (first to finish off start cap, then for stroke) */
549                                 glVertex2fv(t1);
550                                 glVertex2fv(t0);
551                                 glVertex2fv(t0);
552                                 glVertex2fv(t1);
553                         }
554                         /* if not the first segment, use bisector of angle between segments */
555                         else {
556                                 float mb[2];            /* bisector normal */
557                                 float athick, dfac;             /* actual thickness, difference between thicknesses */
558                                 
559                                 /* calculate gradient of bisector (as average of normals) */
560                                 mb[0]= (pm[0] + m2[0]) / 2;
561                                 mb[1]= (pm[1] + m2[1]) / 2;
562                                 Normalize2(mb);
563                                 
564                                 /* calculate gradient to apply 
565                                  *      - as basis, use just pthick * bisector gradient
566                                  *      - if cross-section not as thick as it should be, add extra padding to fix it
567                                  */
568                                 mt[0]= mb[0] * pthick;
569                                 mt[1]= mb[1] * pthick;
570                                 athick= Vec2Length(mt);
571                                 dfac= pthick - (athick * 2);
572                                 if ( ((athick * 2) < pthick) && (IS_EQ(athick, pthick)==0) ) 
573                                 {
574                                         mt[0] += (mb[0] * dfac);
575                                         mt[1] += (mb[1] * dfac);
576                                 }       
577                                 
578                                 /* calculate points for start of segment */
579                                 t0[0]= s0[0] - mt[0];
580                                 t0[1]= s0[1] - mt[1];
581                                 t1[0]= s0[0] + mt[0];
582                                 t1[1]= s0[1] + mt[1];
583                                 
584                                 /* draw this line twice (once for end of current segment, and once for start of next) */
585                                 glVertex2fv(t1);
586                                 glVertex2fv(t0);
587                                 glVertex2fv(t0);
588                                 glVertex2fv(t1);
589                         }
590                         
591                         /* if last segment, also draw end of segment (defined as segment's normal) */
592                         if (i == totpoints-2) {
593                                 /* for once, we use second point's pressure (otherwise it won't be drawn) */
594                                 pthick= (pt2->pressure * thickness);
595                                 
596                                 /* calculate points for end of segment */
597                                 mt[0]= m2[0] * pthick;
598                                 mt[1]= m2[1] * pthick;
599                                 
600                                 t0[0]= s1[0] - mt[0];
601                                 t0[1]= s1[1] - mt[1];
602                                 t1[0]= s1[0] + mt[0];
603                                 t1[1]= s1[1] + mt[1];
604                                 
605                                 /* draw this line twice (once for end of stroke, and once for endcap)*/
606                                 glVertex2fv(t1);
607                                 glVertex2fv(t0);
608                                 glVertex2fv(t0);
609                                 glVertex2fv(t1);
610                                 
611                                 
612                                 /* draw end cap as last step 
613                                  *      - make points slightly closer to center (about halfway across) 
614                                  */                             
615                                 mt[0]= m2[0] * pthick * 0.5;
616                                 mt[1]= m2[1] * pthick * 0.5;
617                                 sc[0]= s1[0] + (m1[0] * pthick * 0.75);
618                                 sc[1]= s1[1] + (m1[1] * pthick * 0.75);
619                                 
620                                 t0[0]= sc[0] - mt[0];
621                                 t0[1]= sc[1] - mt[1];
622                                 t1[0]= sc[0] + mt[0];
623                                 t1[1]= sc[1] + mt[1];
624                                 
625                                 glVertex2fv(t1);
626                                 glVertex2fv(t0);
627                         }
628                         
629                         /* store stroke's 'natural' normal for next stroke to use */
630                         Vec2Copyf(pm, m2);
631                 }
632                 
633                 glEnd();
634         }
635         
636         /* draw debug points of curve on top? (original stroke points) */
637         if (debug) {
638                 bGPDspoint *pt;
639                 int i;
640                 
641                 glBegin(GL_POINTS);
642                 for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
643                         if (sflag & GP_STROKE_2DSPACE) {
644                                 glVertex2f(pt->x, pt->y);
645                         }
646                         else {
647                                 const float x= (pt->x / 1000 * winx);
648                                 const float y= (pt->y / 1000 * winy);
649                                 
650                                 glVertex2f(x, y);
651                         }
652                 }
653                 glEnd();
654         }
655 }
656
657 /* ----- General Drawing ------ */
658
659 /* draw a set of strokes */
660 static void gp_draw_strokes (bGPDframe *gpf, int winx, int winy, int dflag, short debug, 
661                                                          short lthick, float color[4])
662 {
663         bGPDstroke *gps;
664         
665         /* set color first (may need to reset it again later too) */
666         glColor4f(color[0], color[1], color[2], color[3]);
667         
668         for (gps= gpf->strokes.first; gps; gps= gps->next) {
669                 /* check if stroke can be drawn */
670                 if ((dflag & GP_DRAWDATA_ONLY3D) && !(gps->flag & GP_STROKE_3DSPACE))
671                         continue;
672                 if (!(dflag & GP_DRAWDATA_ONLY3D) && (gps->flag & GP_STROKE_3DSPACE))
673                         continue;
674                 if ((dflag & GP_DRAWDATA_ONLYV2D) && !(gps->flag & GP_STROKE_2DSPACE))
675                         continue;
676                 if (!(dflag & GP_DRAWDATA_ONLYV2D) && (gps->flag & GP_STROKE_2DSPACE))
677                         continue;
678                 if ((dflag & GP_DRAWDATA_ONLYI2D) && !(gps->flag & GP_STROKE_2DIMAGE))
679                         continue;
680                 if (!(dflag & GP_DRAWDATA_ONLYI2D) && (gps->flag & GP_STROKE_2DIMAGE))
681                         continue;
682                 if ((gps->points == 0) || (gps->totpoints < 1))
683                         continue;
684                 
685                 /* check which stroke-drawer to use */
686                 if (gps->totpoints == 1)
687                         gp_draw_stroke_point(gps->points, lthick, gps->flag, winx, winy);
688                 else if (dflag & GP_DRAWDATA_ONLY3D)
689                         gp_draw_stroke_3d(gps->points, gps->totpoints, lthick, dflag, gps->flag, debug, winx, winy);
690                 else if (gps->totpoints > 1)    
691                         gp_draw_stroke(gps->points, gps->totpoints, lthick, dflag, gps->flag, debug, winx, winy);
692         }
693 }
694
695 /* draw grease-pencil datablock */
696 static void gp_draw_data (bGPdata *gpd, int winx, int winy, int dflag)
697 {
698         bGPDlayer *gpl, *actlay=NULL;
699         
700         /* turn on smooth lines (i.e. anti-aliasing) */
701         glEnable(GL_LINE_SMOOTH);
702         
703         /* turn on alpha-blending */
704         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
705         glEnable(GL_BLEND);
706                 
707         /* loop over layers, drawing them */
708         for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
709                 bGPDframe *gpf;
710                 
711                 short debug = (gpl->flag & GP_LAYER_DRAWDEBUG) ? 1 : 0;
712                 short lthick= gpl->thickness;
713                 float color[4], tcolor[4];
714                 
715                 /* don't draw layer if hidden */
716                 if (gpl->flag & GP_LAYER_HIDE) 
717                         continue;
718                 
719                 /* if layer is active one, store pointer to it */
720                 if (gpl->flag & GP_LAYER_ACTIVE)
721                         actlay= gpl;
722                 
723                 /* get frame to draw */
724                 gpf= gpencil_layer_getframe(gpl, CFRA, 0);
725                 if (gpf == NULL) 
726                         continue;
727                 
728                 /* set color, stroke thickness, and point size */
729                 glLineWidth(lthick);
730                 QUATCOPY(color, gpl->color); // just for copying 4 array elements
731                 QUATCOPY(tcolor, gpl->color); // additional copy of color (for ghosting)
732                 glColor4f(color[0], color[1], color[2], color[3]);
733                 glPointSize(gpl->thickness + 2);
734                 
735                 /* draw 'onionskins' (frame left + right) */
736                 if (gpl->flag & GP_LAYER_ONIONSKIN) {
737                         /* drawing method - only immediately surrounding (gstep = 0), or within a frame range on either side (gstep > 0)*/                      
738                         if (gpl->gstep) {
739                                 bGPDframe *gf;
740                                 short i;
741                                 
742                                 /* draw previous frames first */
743                                 for (gf=gpf->prev, i=0; gf; gf=gf->prev, i++) {
744                                         /* check if frame is drawable */
745                                         if ((gpf->framenum - gf->framenum) <= gpl->gstep) {
746                                                 /* alpha decreases with distance from curframe index */
747                                                 tcolor[3] = color[3] - (i/gpl->gstep);
748                                                 gp_draw_strokes(gf, winx, winy, dflag, debug, lthick, tcolor);
749                                         }
750                                         else 
751                                                 break;
752                                 }
753                                 
754                                 /* now draw next frames */
755                                 for (gf= gpf->next, i=0; gf; gf=gf->next, i++) {
756                                         /* check if frame is drawable */
757                                         if ((gf->framenum - gpf->framenum) <= gpl->gstep) {
758                                                 /* alpha decreases with distance from curframe index */
759                                                 tcolor[3] = color[3] - (i/gpl->gstep);
760                                                 gp_draw_strokes(gf, winx, winy, dflag, debug, lthick, tcolor);
761                                         }
762                                         else 
763                                                 break;
764                                 }       
765                                 
766                                 /* restore alpha */
767                                 glColor4f(color[0], color[1], color[2], color[3]);
768                         }
769                         else {
770                                 /* draw the strokes for the ghost frames (at half of the alpha set by user) */
771                                 if (gpf->prev) {
772                                         tcolor[3] = (color[3] / 7);
773                                         gp_draw_strokes(gpf->prev, winx, winy, dflag, debug, lthick, tcolor);
774                                 }
775                                 
776                                 if (gpf->next) {
777                                         tcolor[3] = (color[3] / 4);
778                                         gp_draw_strokes(gpf->next, winx, winy, dflag, debug, lthick, tcolor);
779                                 }
780                                 
781                                 /* restore alpha */
782                                 glColor4f(color[0], color[1], color[2], color[3]);
783                         }
784                 }
785                 
786                 /* draw the strokes already in active frame */
787                 tcolor[3]= color[3];
788                 gp_draw_strokes(gpf, winx, winy, dflag, debug, lthick, tcolor);
789                 
790                 /* Check if may need to draw the active stroke cache, only if this layer is the active layer
791                  * that is being edited. (Stroke buffer is currently stored in gp-data)
792                  */
793                 if ((G.f & G_GREASEPENCIL) && (gpl->flag & GP_LAYER_ACTIVE) &&
794                         (gpf->flag & GP_FRAME_PAINT)) 
795                 {
796                         /* Buffer stroke needs to be drawn with a different linestyle to help differentiate them from normal strokes. */
797                         gp_draw_stroke_buffer(gpd->sbuffer, gpd->sbuffer_size, lthick, dflag, gpd->sbuffer_sflag);
798                 }
799         }
800         
801         /* turn off alpha blending, then smooth lines */
802         glDisable(GL_BLEND); // alpha blending
803         glDisable(GL_LINE_SMOOTH); // smooth lines
804         
805         /* show info for debugging the status of gpencil */
806         if ( ((dflag & GP_DRAWDATA_NOSTATUS)==0) && (gpd->flag & GP_DATA_DISPINFO) ) {
807                 char printable[256];
808                 short xmax;
809                 
810                 /* get text to display */
811                 if (actlay) {
812                         if (gpd->flag & GP_DATA_EDITPAINT)
813                                 BIF_ThemeColor(TH_BONE_POSE); // should be blue-ish
814                         else if (actlay->actframe == NULL)
815                                 BIF_ThemeColor(TH_REDALERT);
816                         else if (actlay->actframe->framenum == CFRA)
817                                 BIF_ThemeColor(TH_VERTEX_SELECT); // should be yellow
818                         else
819                                 BIF_ThemeColor(TH_TEXT_HI);
820                         
821                         if (actlay->actframe) {
822                                 sprintf(printable, "GPencil: Layer ('%s'), Frame (%d) %s", 
823                                         actlay->info, actlay->actframe->framenum,
824                                         ((gpd->flag & GP_DATA_EDITPAINT)?", Draw Mode On":"") );
825                         }
826                         else {
827                                 sprintf(printable, "GPencil: Layer ('%s'), Frame <None> %s", 
828                                         actlay->info, ((gpd->flag & GP_DATA_EDITPAINT)?", Draw Mode On":"") );
829                         }
830                 }
831                 else {
832                         BIF_ThemeColor(TH_REDALERT);
833                         sprintf(printable, "GPencil: Layer <None>");
834                 }
835                 xmax= GetButStringLength(printable);
836                 
837                 /* only draw it if view is wide enough (assume padding of 20 is enough for now) */
838                 if (winx > (xmax + 20)) { 
839                         glRasterPos2i(winx-xmax, winy-20);
840                         BMF_DrawString(G.fonts, printable);
841                 }
842         }
843         
844         /* restore initial gl conditions */
845         glLineWidth(1.0);
846         glPointSize(1.0);
847         glColor4f(0, 0, 0, 1);
848 }
849
850 /* ----- Grease Pencil Sketches Drawing API ------ */
851
852 /* draw grease-pencil sketches to specified 2d-view that uses ibuf corrections */
853 void draw_gpencil_2dimage (ScrArea *sa, ImBuf *ibuf)
854 {
855         bGPdata *gpd;
856         int dflag = 0;
857         
858         /* check that we have grease-pencil stuff to draw */
859         if (ELEM(NULL, sa, ibuf)) return;
860         gpd= gpencil_data_getactive(sa);
861         if (gpd == NULL) return;
862         
863         /* draw it! */
864         dflag = (GP_DRAWDATA_ONLYI2D|GP_DRAWDATA_NOSTATUS);
865         gp_draw_data(gpd, sa->winx, sa->winy, dflag);
866 }
867
868 /* draw grease-pencil sketches to specified 2d-view assuming that matrices are already set correctly 
869  * Note: this gets called twice - first time with onlyv2d=1 to draw 'canvas' strokes, second time with onlyv2d=0 for screen-aligned strokes
870  */
871 void draw_gpencil_2dview (ScrArea *sa, short onlyv2d)
872 {
873         bGPdata *gpd;
874         int dflag = 0;
875         
876         /* check that we have grease-pencil stuff to draw */
877         if (sa == NULL) return;
878         gpd= gpencil_data_getactive(sa);
879         if (gpd == NULL) return;
880         
881         /* draw it! */
882         if (onlyv2d) dflag |= (GP_DRAWDATA_ONLYV2D|GP_DRAWDATA_NOSTATUS);
883         gp_draw_data(gpd, sa->winx, sa->winy, dflag);
884 }
885
886 /* draw grease-pencil sketches to specified 3d-view assuming that matrices are already set correctly 
887  * Note: this gets called twice - first time with only3d=1 to draw 3d-strokes, second time with only3d=0 for screen-aligned strokes
888  */
889 void draw_gpencil_3dview (ScrArea *sa, short only3d)
890 {
891         bGPdata *gpd;
892         int dflag = 0;
893         
894         /* check that we have grease-pencil stuff to draw */
895         gpd= gpencil_data_getactive(sa);
896         if (gpd == NULL) return;
897         
898         /* draw it! */
899         if (only3d) dflag |= (GP_DRAWDATA_ONLY3D|GP_DRAWDATA_NOSTATUS);
900         gp_draw_data(gpd, sa->winx, sa->winy, dflag);
901 }
902
903 /* draw grease-pencil sketches to opengl render window assuming that matrices are already set correctly */
904 void draw_gpencil_oglrender (View3D *v3d, int winx, int winy)
905 {
906         bGPdata *gpd;
907         
908         /* assume gpencil data comes from v3d */
909         if (v3d == NULL) return;
910         gpd= v3d->gpd;
911         if (gpd == NULL) return;
912         
913         /* pass 1: draw 3d-strokes ------------ > */
914         gp_draw_data(gpd, winx, winy, (GP_DRAWDATA_NOSTATUS|GP_DRAWDATA_ONLY3D));
915         
916         /* pass 2: draw 2d-strokes ------------ > */
917                 /* adjust view matrices */
918         myortho2(-0.375, (float)(winx)-0.375, -0.375, (float)(winy)-0.375);
919         glLoadIdentity();
920         
921                 /* draw it! */
922         gp_draw_data(gpd, winx, winy, GP_DRAWDATA_NOSTATUS);
923 }
924
925 /* ************************************************** */