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