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