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