d1c1e4a5ac68ad5d1586c8d51a42ff355fc390fe
[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 offsx, int offsy, 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 if (sflag & GP_STROKE_2DIMAGE) {
414                         co[0]= points->x;
415                         co[1]= points->y;
416                 }
417                 else {
418                         co[0]= (points->x / 1000 * winx);
419                         co[1]= (points->y / 1000 * winy);
420                 }
421                 
422                 /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, simple opengl point will do */
423                 if (thickness < GP_DRAWTHICKNESS_SPECIAL) {
424                         glBegin(GL_POINTS);
425                                 glVertex2fv(co);
426                         glEnd();
427                 }
428                 else {
429                         /* draw filled circle as is done in circf (but without the matrix push/pops which screwed things up) */
430                         GLUquadricObj *qobj = gluNewQuadric(); 
431                         
432                         gluQuadricDrawStyle(qobj, GLU_FILL); 
433                         
434                         /* need to translate drawing position, but must reset after too! */
435                         glTranslatef(co[0],  co[1], 0.); 
436                         gluDisk( qobj, 0.0,  thickness, 32, 1); 
437                         glTranslatef(-co[0],  -co[1], 0.);
438                         
439                         gluDeleteQuadric(qobj);
440                 }
441         }
442 }
443
444 /* draw a given stroke in 3d (i.e. in 3d-space), using simple ogl lines */
445 static void gp_draw_stroke_3d (bGPDspoint *points, int totpoints, short thickness, short dflag, short sflag, short debug, int winx, int winy)
446 {
447         bGPDspoint *pt;
448         float oldpressure = 0.0f;
449         int i;
450         
451         /* draw stroke curve */
452         glBegin(GL_LINE_STRIP);
453         for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
454                 if (fabs(pt->pressure - oldpressure) > 0.2f) {
455                         glEnd();
456                         glLineWidth(pt->pressure * thickness);
457                         glBegin(GL_LINE_STRIP);
458                         
459                         glVertex3f(pt->x, pt->y, pt->z);
460                         
461                         oldpressure = pt->pressure;
462                 }
463                 else
464                         glVertex3f(pt->x, pt->y, pt->z);
465         }
466         glEnd();
467         
468         /* draw debug points of curve on top? */
469         if (debug) {
470                 glBegin(GL_POINTS);
471                 for (i=0, pt=points; i < totpoints && pt; i++, pt++)
472                         glVertex3f(pt->x, pt->y, pt->z);
473                 glEnd();
474         }
475 }
476
477 /* ----- Fancy 2D-Stroke Drawing ------ */
478
479 /* draw a given stroke in 2d */
480 static void gp_draw_stroke (bGPDspoint *points, int totpoints, short thickness, short dflag, short sflag, 
481                                                         short debug, int offsx, int offsy, int winx, int winy)
482 {       
483         /* if thickness is less than GP_DRAWTHICKNESS_SPECIAL, 'smooth' opengl lines look better */
484         if ((thickness < GP_DRAWTHICKNESS_SPECIAL) || (dflag & GP_DRAWDATA_ONLYI2D)) {
485                 bGPDspoint *pt;
486                 int i;
487                 
488                 glBegin(GL_LINE_STRIP);
489                 for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
490                         if (sflag & GP_STROKE_2DSPACE) {
491                                 glVertex2f(pt->x, pt->y);
492                         }
493                         else if (sflag & GP_STROKE_2DIMAGE) {
494                                 const float x= pt->x;
495                                 const float y= pt->y;
496                                 
497                                 glVertex2f(x, y);
498                         }
499                         else {
500                                 const float x= (pt->x / 1000 * winx);
501                                 const float y= (pt->y / 1000 * winy);
502                                 
503                                 glVertex2f(x, y);
504                         }
505                 }
506                 glEnd();
507         }
508         else { /* tesselation code: currently only enabled with rt != 0 */
509                 bGPDspoint *pt1, *pt2;
510                 float pm[2];
511                 int i;
512                 
513                 glShadeModel(GL_FLAT);
514                 glBegin(GL_QUADS);
515                 
516                 for (i=0, pt1=points, pt2=points+1; i < (totpoints-1); i++, pt1++, pt2++) {
517                         float s0[2], s1[2];             /* segment 'center' points */
518                         float t0[2], t1[2];             /* tesselated coordinates */
519                         float m1[2], m2[2];             /* gradient and normal */
520                         float mt[2], sc[2];             /* gradient for thickness, point for end-cap */
521                         float pthick;                   /* thickness at segment point */
522                         
523                         /* get x and y coordinates from points */
524                         if (sflag & GP_STROKE_2DSPACE) {
525                                 s0[0]= pt1->x;          s0[1]= pt1->y;
526                                 s1[0]= pt2->x;          s1[1]= pt2->y;
527                         }
528                         else if (sflag & GP_STROKE_2DIMAGE) {
529                                 s0[0]= pt1->x;          s0[1]= pt1->y;
530                                 s1[0]= pt2->x;          s1[1]= pt2->y;
531                         }
532                         else {
533                                 s0[0]= (pt1->x / 1000 * winx);
534                                 s0[1]= (pt1->y / 1000 * winy);
535                                 s1[0]= (pt2->x / 1000 * winx);
536                                 s1[1]= (pt2->y / 1000 * winy);
537                         }               
538                         
539                         /* calculate gradient and normal - 'angle'=(ny/nx) */
540                         m1[1]= s1[1] - s0[1];           
541                         m1[0]= s1[0] - s0[0];
542                         Normalize2(m1);
543                         m2[1]= -m1[0];
544                         m2[0]= m1[1];
545                         
546                         /* always use pressure from first point here */
547                         pthick= (pt1->pressure * thickness);
548                         
549                         /* if the first segment, start of segment is segment's normal */
550                         if (i == 0) {
551                                 /* draw start cap first 
552                                  *      - make points slightly closer to center (about halfway across) 
553                                  */                             
554                                 mt[0]= m2[0] * pthick * 0.5;
555                                 mt[1]= m2[1] * pthick * 0.5;
556                                 sc[0]= s0[0] - (m1[0] * pthick * 0.75);
557                                 sc[1]= s0[1] - (m1[1] * pthick * 0.75);
558                                 
559                                 t0[0]= sc[0] - mt[0];
560                                 t0[1]= sc[1] - mt[1];
561                                 t1[0]= sc[0] + mt[0];
562                                 t1[1]= sc[1] + mt[1];
563                                 
564                                 glVertex2fv(t0);
565                                 glVertex2fv(t1);
566                                 
567                                 /* calculate points for start of segment */
568                                 mt[0]= m2[0] * pthick;
569                                 mt[1]= m2[1] * pthick;
570                                 
571                                 t0[0]= s0[0] - mt[0];
572                                 t0[1]= s0[1] - mt[1];
573                                 t1[0]= s0[0] + mt[0];
574                                 t1[1]= s0[1] + mt[1];
575                                 
576                                 /* draw this line twice (first to finish off start cap, then for stroke) */
577                                 glVertex2fv(t1);
578                                 glVertex2fv(t0);
579                                 glVertex2fv(t0);
580                                 glVertex2fv(t1);
581                         }
582                         /* if not the first segment, use bisector of angle between segments */
583                         else {
584                                 float mb[2];            /* bisector normal */
585                                 float athick, dfac;             /* actual thickness, difference between thicknesses */
586                                 
587                                 /* calculate gradient of bisector (as average of normals) */
588                                 mb[0]= (pm[0] + m2[0]) / 2;
589                                 mb[1]= (pm[1] + m2[1]) / 2;
590                                 Normalize2(mb);
591                                 
592                                 /* calculate gradient to apply 
593                                  *      - as basis, use just pthick * bisector gradient
594                                  *      - if cross-section not as thick as it should be, add extra padding to fix it
595                                  */
596                                 mt[0]= mb[0] * pthick;
597                                 mt[1]= mb[1] * pthick;
598                                 athick= Vec2Length(mt);
599                                 dfac= pthick - (athick * 2);
600                                 if ( ((athick * 2) < pthick) && (IS_EQ(athick, pthick)==0) ) 
601                                 {
602                                         mt[0] += (mb[0] * dfac);
603                                         mt[1] += (mb[1] * dfac);
604                                 }       
605                                 
606                                 /* calculate points for start of segment */
607                                 t0[0]= s0[0] - mt[0];
608                                 t0[1]= s0[1] - mt[1];
609                                 t1[0]= s0[0] + mt[0];
610                                 t1[1]= s0[1] + mt[1];
611                                 
612                                 /* draw this line twice (once for end of current segment, and once for start of next) */
613                                 glVertex2fv(t1);
614                                 glVertex2fv(t0);
615                                 glVertex2fv(t0);
616                                 glVertex2fv(t1);
617                         }
618                         
619                         /* if last segment, also draw end of segment (defined as segment's normal) */
620                         if (i == totpoints-2) {
621                                 /* for once, we use second point's pressure (otherwise it won't be drawn) */
622                                 pthick= (pt2->pressure * thickness);
623                                 
624                                 /* calculate points for end of segment */
625                                 mt[0]= m2[0] * pthick;
626                                 mt[1]= m2[1] * pthick;
627                                 
628                                 t0[0]= s1[0] - mt[0];
629                                 t0[1]= s1[1] - mt[1];
630                                 t1[0]= s1[0] + mt[0];
631                                 t1[1]= s1[1] + mt[1];
632                                 
633                                 /* draw this line twice (once for end of stroke, and once for endcap)*/
634                                 glVertex2fv(t1);
635                                 glVertex2fv(t0);
636                                 glVertex2fv(t0);
637                                 glVertex2fv(t1);
638                                 
639                                 
640                                 /* draw end cap as last step 
641                                  *      - make points slightly closer to center (about halfway across) 
642                                  */                             
643                                 mt[0]= m2[0] * pthick * 0.5;
644                                 mt[1]= m2[1] * pthick * 0.5;
645                                 sc[0]= s1[0] + (m1[0] * pthick * 0.75);
646                                 sc[1]= s1[1] + (m1[1] * pthick * 0.75);
647                                 
648                                 t0[0]= sc[0] - mt[0];
649                                 t0[1]= sc[1] - mt[1];
650                                 t1[0]= sc[0] + mt[0];
651                                 t1[1]= sc[1] + mt[1];
652                                 
653                                 glVertex2fv(t1);
654                                 glVertex2fv(t0);
655                         }
656                         
657                         /* store stroke's 'natural' normal for next stroke to use */
658                         Vec2Copyf(pm, m2);
659                 }
660                 
661                 glEnd();
662         }
663         
664         /* draw debug points of curve on top? (original stroke points) */
665         if (debug) {
666                 bGPDspoint *pt;
667                 int i;
668                 
669                 glBegin(GL_POINTS);
670                 for (i=0, pt=points; i < totpoints && pt; i++, pt++) {
671                         if (sflag & GP_STROKE_2DSPACE) {
672                                 glVertex2f(pt->x, pt->y);
673                         }
674                         else if (sflag & GP_STROKE_2DIMAGE) {
675                                         // fixme
676                                 const float x= pt->x;
677                                 const float y= pt->y;
678                                 
679                                 glVertex2f(x, y);
680                         }
681                         else {
682                                 const float x= (pt->x / 1000 * winx);
683                                 const float y= (pt->y / 1000 * winy);
684                                 
685                                 glVertex2f(x, y);
686                         }
687                 }
688                 glEnd();
689         }
690 }
691
692 /* ----- General Drawing ------ */
693
694 /* draw a set of strokes */
695 static void gp_draw_strokes (bGPDframe *gpf, int offsx, int offsy, int winx, int winy, int dflag,  
696                                                          short debug, short lthick, float color[4])
697 {
698         bGPDstroke *gps;
699         
700         /* set color first (may need to reset it again later too) */
701         glColor4f(color[0], color[1], color[2], color[3]);
702         
703         for (gps= gpf->strokes.first; gps; gps= gps->next) {
704                 /* check if stroke can be drawn */
705                 if ((dflag & GP_DRAWDATA_ONLY3D) && !(gps->flag & GP_STROKE_3DSPACE))
706                         continue;
707                 if (!(dflag & GP_DRAWDATA_ONLY3D) && (gps->flag & GP_STROKE_3DSPACE))
708                         continue;
709                 if ((dflag & GP_DRAWDATA_ONLYV2D) && !(gps->flag & GP_STROKE_2DSPACE))
710                         continue;
711                 if (!(dflag & GP_DRAWDATA_ONLYV2D) && (gps->flag & GP_STROKE_2DSPACE))
712                         continue;
713                 if ((dflag & GP_DRAWDATA_ONLYI2D) && !(gps->flag & GP_STROKE_2DIMAGE))
714                         continue;
715                 if (!(dflag & GP_DRAWDATA_ONLYI2D) && (gps->flag & GP_STROKE_2DIMAGE))
716                         continue;
717                 if ((gps->points == 0) || (gps->totpoints < 1))
718                         continue;
719                 
720                 /* check which stroke-drawer to use */
721                 if (gps->totpoints == 1)
722                         gp_draw_stroke_point(gps->points, lthick, gps->flag, offsx, offsy, winx, winy);
723                 else if (dflag & GP_DRAWDATA_ONLY3D)
724                         gp_draw_stroke_3d(gps->points, gps->totpoints, lthick, dflag, gps->flag, debug, winx, winy);
725                 else if (gps->totpoints > 1)    
726                         gp_draw_stroke(gps->points, gps->totpoints, lthick, dflag, gps->flag, debug, offsx, offsy, winx, winy);
727         }
728 }
729
730 /* draw grease-pencil datablock */
731 static void gp_draw_data (bGPdata *gpd, int offsx, int offsy, int winx, int winy, int dflag)
732 {
733         bGPDlayer *gpl, *actlay=NULL;
734         
735         /* turn on smooth lines (i.e. anti-aliasing) */
736         glEnable(GL_LINE_SMOOTH);
737         
738         /* turn on alpha-blending */
739         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
740         glEnable(GL_BLEND);
741                 
742         /* loop over layers, drawing them */
743         for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
744                 bGPDframe *gpf;
745                 
746                 short debug = (gpl->flag & GP_LAYER_DRAWDEBUG) ? 1 : 0;
747                 short lthick= gpl->thickness;
748                 float color[4], tcolor[4];
749                 
750                 /* don't draw layer if hidden */
751                 if (gpl->flag & GP_LAYER_HIDE) 
752                         continue;
753                 
754                 /* if layer is active one, store pointer to it */
755                 if (gpl->flag & GP_LAYER_ACTIVE)
756                         actlay= gpl;
757                 
758                 /* get frame to draw */
759                 gpf= gpencil_layer_getframe(gpl, CFRA, 0);
760                 if (gpf == NULL) 
761                         continue;
762                 
763                 /* set color, stroke thickness, and point size */
764                 glLineWidth(lthick);
765                 QUATCOPY(color, gpl->color); // just for copying 4 array elements
766                 QUATCOPY(tcolor, gpl->color); // additional copy of color (for ghosting)
767                 glColor4f(color[0], color[1], color[2], color[3]);
768                 glPointSize(gpl->thickness + 2);
769                 
770                 /* draw 'onionskins' (frame left + right) */
771                 if (gpl->flag & GP_LAYER_ONIONSKIN) {
772                         /* drawing method - only immediately surrounding (gstep = 0), or within a frame range on either side (gstep > 0)*/                      
773                         if (gpl->gstep) {
774                                 bGPDframe *gf;
775                                 short i;
776                                 
777                                 /* draw previous frames first */
778                                 for (gf=gpf->prev, i=0; gf; gf=gf->prev, i++) {
779                                         /* check if frame is drawable */
780                                         if ((gpf->framenum - gf->framenum) <= gpl->gstep) {
781                                                 /* alpha decreases with distance from curframe index */
782                                                 tcolor[3] = color[3] - (i/gpl->gstep);
783                                                 gp_draw_strokes(gf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
784                                         }
785                                         else 
786                                                 break;
787                                 }
788                                 
789                                 /* now draw next frames */
790                                 for (gf= gpf->next, i=0; gf; gf=gf->next, i++) {
791                                         /* check if frame is drawable */
792                                         if ((gf->framenum - gpf->framenum) <= gpl->gstep) {
793                                                 /* alpha decreases with distance from curframe index */
794                                                 tcolor[3] = color[3] - (i/gpl->gstep);
795                                                 gp_draw_strokes(gf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
796                                         }
797                                         else 
798                                                 break;
799                                 }       
800                                 
801                                 /* restore alpha */
802                                 glColor4f(color[0], color[1], color[2], color[3]);
803                         }
804                         else {
805                                 /* draw the strokes for the ghost frames (at half of the alpha set by user) */
806                                 if (gpf->prev) {
807                                         tcolor[3] = (color[3] / 7);
808                                         gp_draw_strokes(gpf->prev, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
809                                 }
810                                 
811                                 if (gpf->next) {
812                                         tcolor[3] = (color[3] / 4);
813                                         gp_draw_strokes(gpf->next, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
814                                 }
815                                 
816                                 /* restore alpha */
817                                 glColor4f(color[0], color[1], color[2], color[3]);
818                         }
819                 }
820                 
821                 /* draw the strokes already in active frame */
822                 tcolor[3]= color[3];
823                 gp_draw_strokes(gpf, offsx, offsy, winx, winy, dflag, debug, lthick, tcolor);
824                 
825                 /* Check if may need to draw the active stroke cache, only if this layer is the active layer
826                  * that is being edited. (Stroke buffer is currently stored in gp-data)
827                  */
828                 if ((G.f & G_GREASEPENCIL) && (gpl->flag & GP_LAYER_ACTIVE) &&
829                         (gpf->flag & GP_FRAME_PAINT)) 
830                 {
831                         /* Buffer stroke needs to be drawn with a different linestyle to help differentiate them from normal strokes. */
832                         gp_draw_stroke_buffer(gpd->sbuffer, gpd->sbuffer_size, lthick, dflag, gpd->sbuffer_sflag);
833                 }
834         }
835         
836         /* turn off alpha blending, then smooth lines */
837         glDisable(GL_BLEND); // alpha blending
838         glDisable(GL_LINE_SMOOTH); // smooth lines
839         
840         /* show info for debugging the status of gpencil */
841         if ( ((dflag & GP_DRAWDATA_NOSTATUS)==0) && (gpd->flag & GP_DATA_DISPINFO) ) {
842                 char printable[256];
843                 short xmax;
844                 
845                 /* get text to display */
846                 if (actlay) {
847                         if (gpd->flag & GP_DATA_EDITPAINT)
848                                 BIF_ThemeColor(TH_BONE_POSE); // should be blue-ish
849                         else if (actlay->actframe == NULL)
850                                 BIF_ThemeColor(TH_REDALERT);
851                         else if (actlay->actframe->framenum == CFRA)
852                                 BIF_ThemeColor(TH_VERTEX_SELECT); // should be yellow
853                         else
854                                 BIF_ThemeColor(TH_TEXT_HI);
855                         
856                         if (actlay->actframe) {
857                                 sprintf(printable, "GPencil: Layer ('%s'), Frame (%d) %s", 
858                                         actlay->info, actlay->actframe->framenum,
859                                         ((gpd->flag & GP_DATA_EDITPAINT)?", Draw Mode On":"") );
860                         }
861                         else {
862                                 sprintf(printable, "GPencil: Layer ('%s'), Frame <None> %s", 
863                                         actlay->info, ((gpd->flag & GP_DATA_EDITPAINT)?", Draw Mode On":"") );
864                         }
865                 }
866                 else {
867                         BIF_ThemeColor(TH_REDALERT);
868                         sprintf(printable, "GPencil: Layer <None>");
869                 }
870                 xmax= GetButStringLength(printable);
871                 
872                 /* only draw it if view is wide enough (assume padding of 20 is enough for now) */
873                 if (winx > (xmax + 20)) { 
874                         glRasterPos2i(winx-xmax, winy-20);
875                         BMF_DrawString(G.fonts, printable);
876                 }
877         }
878         
879         /* restore initial gl conditions */
880         glLineWidth(1.0);
881         glPointSize(1.0);
882         glColor4f(0, 0, 0, 1);
883 }
884
885 /* ----- Grease Pencil Sketches Drawing API ------ */
886
887 /* draw grease-pencil sketches to specified 2d-view that uses ibuf corrections */
888 void draw_gpencil_2dimage (ScrArea *sa, ImBuf *ibuf)
889 {
890         bGPdata *gpd;
891         int offsx, offsy, sizex, sizey;
892         int dflag = 0;
893         
894         /* check that we have grease-pencil stuff to draw */
895         if (ELEM(NULL, sa, ibuf)) return;
896         gpd= gpencil_data_getactive(sa);
897         if (gpd == NULL) return;
898         
899         /* calculate rect */
900         switch (sa->spacetype) {
901                 case SPACE_IMAGE: /* image */
902                 {
903                         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
904                         
905                         // fixme... are these settings still needed?
906                         offsx= 0;
907                         offsy= 0;
908                         sizex= sa->winx;
909                         sizey= sa->winy;
910                         
911                         myortho2(sima->v2d.cur.xmin, sima->v2d.cur.xmax, sima->v2d.cur.ymin, sima->v2d.cur.ymax);
912                 }
913                         break;
914                         
915                 default: /* for spacetype not yet handled */
916                         offsx= 0;
917                         offsy= 0;
918                         sizex= sa->winx;
919                         sizey= sa->winy;
920                         break;
921         }
922         
923         
924         /* draw it! */
925         dflag = (GP_DRAWDATA_ONLYI2D|GP_DRAWDATA_NOSTATUS);
926         gp_draw_data(gpd, offsx, offsy, sizex, sizey, dflag);
927 }
928
929 /* draw grease-pencil sketches to specified 2d-view assuming that matrices are already set correctly 
930  * Note: this gets called twice - first time with onlyv2d=1 to draw 'canvas' strokes, second time with onlyv2d=0 for screen-aligned strokes
931  */
932 void draw_gpencil_2dview (ScrArea *sa, short onlyv2d)
933 {
934         bGPdata *gpd;
935         int dflag = 0;
936         
937         /* check that we have grease-pencil stuff to draw */
938         if (sa == NULL) return;
939         gpd= gpencil_data_getactive(sa);
940         if (gpd == NULL) return;
941         
942         /* draw it! */
943         if (onlyv2d) dflag |= (GP_DRAWDATA_ONLYV2D|GP_DRAWDATA_NOSTATUS);
944         gp_draw_data(gpd, 0, 0, sa->winx, sa->winy, dflag);
945 }
946
947 /* draw grease-pencil sketches to specified 3d-view assuming that matrices are already set correctly 
948  * Note: this gets called twice - first time with only3d=1 to draw 3d-strokes, second time with only3d=0 for screen-aligned strokes
949  */
950 void draw_gpencil_3dview (ScrArea *sa, short only3d)
951 {
952         bGPdata *gpd;
953         int dflag = 0;
954         
955         /* check that we have grease-pencil stuff to draw */
956         gpd= gpencil_data_getactive(sa);
957         if (gpd == NULL) return;
958         
959         /* draw it! */
960         if (only3d) dflag |= (GP_DRAWDATA_ONLY3D|GP_DRAWDATA_NOSTATUS);
961         gp_draw_data(gpd, 0, 0, sa->winx, sa->winy, dflag);
962 }
963
964 /* draw grease-pencil sketches to opengl render window assuming that matrices are already set correctly */
965 void draw_gpencil_oglrender (View3D *v3d, int winx, int winy)
966 {
967         bGPdata *gpd;
968         
969         /* assume gpencil data comes from v3d */
970         if (v3d == NULL) return;
971         gpd= v3d->gpd;
972         if (gpd == NULL) return;
973         
974         /* pass 1: draw 3d-strokes ------------ > */
975         gp_draw_data(gpd, 0, 0, winx, winy, (GP_DRAWDATA_NOSTATUS|GP_DRAWDATA_ONLY3D));
976         
977         /* pass 2: draw 2d-strokes ------------ > */
978                 /* adjust view matrices */
979         myortho2(-0.375, (float)(winx)-0.375, -0.375, (float)(winy)-0.375);
980         glLoadIdentity();
981         
982                 /* draw it! */
983         gp_draw_data(gpd, 0, 0, winx, winy, GP_DRAWDATA_NOSTATUS);
984 }
985
986 /* ************************************************** */