08629a9a2f7d9c2add572cfcc4c7df407cc09398
[blender.git] / source / blender / editors / space_view3d / drawarmature.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) 2005 by the Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <stdlib.h>
31 #include <string.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 "DNA_anim_types.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
44 #include "DNA_ID.h"
45 #include "DNA_nla_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_space_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_userdef_types.h"
52
53 #include "BLI_blenlib.h"
54 #include "BLI_math.h"
55 #include "BLI_dlrbTree.h"
56
57 #include "BKE_animsys.h"
58 #include "BKE_action.h"
59 #include "BKE_armature.h"
60 #include "BKE_constraint.h"
61 #include "BKE_context.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_global.h"
65 #include "BKE_main.h"
66 #include "BKE_modifier.h"
67 #include "BKE_nla.h"
68 #include "BKE_object.h"
69 #include "BKE_utildefines.h"
70
71 #include "BIF_gl.h"
72 #include "BIF_glutil.h"
73
74 #include "ED_armature.h"
75 #include "ED_keyframes_draw.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79 #include "BLF_api.h"
80
81 #include "UI_resources.h"
82
83 #include "view3d_intern.h"
84
85
86 /* *************** Armature Drawing - Coloring API ***************************** */
87
88 /* global here is reset before drawing each bone */
89 static ThemeWireColor *bcolor= NULL;
90
91 /* values of colCode for set_pchan_glcolor */
92 enum {
93         PCHAN_COLOR_NORMAL      = 0,            /* normal drawing */
94         PCHAN_COLOR_SOLID,                              /* specific case where "solid" color is needed */
95         PCHAN_COLOR_CONSTS,                             /* "constraint" colors (which may/may-not be suppressed) */
96         
97         PCHAN_COLOR_SPHEREBONE_BASE,    /* for the 'stick' of sphere (envelope) bones */
98         PCHAN_COLOR_SPHEREBONE_END,             /* for the ends of sphere (envelope) bones */
99         PCHAN_COLOR_LINEBONE                    /* for the middle of line-bones */
100 };      
101
102 /* This function sets the color-set for coloring a certain bone */
103 static void set_pchan_colorset (Object *ob, bPoseChannel *pchan)
104 {
105         bPose *pose= (ob) ? ob->pose : NULL;
106         bArmature *arm= (ob) ? ob->data : NULL;
107         bActionGroup *grp= NULL;
108         short color_index= 0;
109         
110         /* sanity check */
111         if (ELEM4(NULL, ob, arm, pose, pchan)) {
112                 bcolor= NULL;
113                 return;
114         }
115         
116         /* only try to set custom color if enabled for armature */
117         if (arm->flag & ARM_COL_CUSTOM) {       
118                 /* currently, a bone can only use a custom color set if it's group (if it has one),
119                  * has been set to use one
120                  */
121                 if (pchan->agrp_index) {
122                         grp= (bActionGroup *)BLI_findlink(&pose->agroups, (pchan->agrp_index - 1));
123                         if (grp)
124                                 color_index= grp->customCol;
125                 }
126         }
127         
128         /* bcolor is a pointer to the color set to use. If NULL, then the default
129          * color set (based on the theme colors for 3d-view) is used. 
130          */
131         if (color_index > 0) {
132                 bTheme *btheme= U.themes.first;
133                 bcolor= &btheme->tarm[(color_index - 1)];
134         }
135         else if (color_index == -1) {
136                 /* use the group's own custom color set */
137                 bcolor= (grp)? &grp->cs : NULL;
138         }
139         else 
140                 bcolor= NULL;
141 }
142
143 /* This function is for brightening/darkening a given color (like UI_ThemeColorShade()) */
144 static void cp_shade_color3ub (char cp[], int offset)
145 {
146         int r, g, b;
147         
148         r= offset + (int) cp[0];
149         CLAMP(r, 0, 255);
150         g= offset + (int) cp[1];
151         CLAMP(g, 0, 255);
152         b= offset + (int) cp[2];
153         CLAMP(b, 0, 255);
154         
155         cp[0]= r;
156         cp[1]= g;
157         cp[2]= b;
158 }
159
160 /* This function sets the gl-color for coloring a certain bone (based on bcolor) */
161 static short set_pchan_glColor (short colCode, int armflag, int boneflag, int constflag)
162 {
163         switch (colCode) {
164         case PCHAN_COLOR_NORMAL:
165         {
166                 if (bcolor) {
167                         char cp[3];
168                         
169                         if (boneflag & BONE_DRAW_ACTIVE) {
170                                 VECCOPY(cp, bcolor->active);
171                         }
172                         else if (boneflag & BONE_SELECTED) {
173                                 VECCOPY(cp, bcolor->select);
174                         }
175                         else {
176                                 /* a bit darker than solid */
177                                 VECCOPY(cp, bcolor->solid);
178                                 cp_shade_color3ub(cp, -50);
179                         }
180                         
181                         glColor3ub(cp[0], cp[1], cp[2]);
182                 }
183                 else {
184                         if (boneflag & BONE_DRAW_ACTIVE) UI_ThemeColorShade(TH_BONE_POSE, 40);
185                         else if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_BONE_POSE);
186                         else UI_ThemeColor(TH_WIRE);
187                 }
188                 
189                 return 1;
190         }
191                 break;
192         
193         case PCHAN_COLOR_SOLID:
194         {
195                 if (bcolor) {
196                         char *cp= bcolor->solid;
197                         glColor3ub(cp[0], cp[1], cp[2]);
198                 }
199                 else 
200                         UI_ThemeColor(TH_BONE_SOLID);
201                         
202                 return 1;
203         }
204                 break;
205                 
206         case PCHAN_COLOR_CONSTS:
207         {
208                 if ( (bcolor == NULL) || (bcolor->flag & TH_WIRECOLOR_CONSTCOLS) ) {
209                         if (constflag & PCHAN_HAS_STRIDE) glColor4ub(0, 0, 200, 80);
210                         else if (constflag & PCHAN_HAS_TARGET) glColor4ub(255, 150, 0, 80);
211                         else if (constflag & PCHAN_HAS_IK) glColor4ub(255, 255, 0, 80);
212                         else if (constflag & PCHAN_HAS_SPLINEIK) glColor4ub(200, 255, 0, 80);
213                         else if (constflag & PCHAN_HAS_CONST) glColor4ub(0, 255, 120, 80);
214                         else if (constflag) UI_ThemeColor4(TH_BONE_POSE);       // PCHAN_HAS_ACTION 
215                         
216                         return 1;
217                 }
218                 else 
219                         return 0;
220         }
221                 break;
222                 
223         case PCHAN_COLOR_SPHEREBONE_BASE:
224         {
225                 if (bcolor) {
226                         char cp[3];
227                         
228                         if (boneflag & BONE_DRAW_ACTIVE) {
229                                 VECCOPY(cp, bcolor->active);
230                         }
231                         else if (boneflag & BONE_SELECTED) {
232                                 VECCOPY(cp, bcolor->select);
233                         }
234                         else {
235                                 VECCOPY(cp, bcolor->solid);
236                         }
237                         
238                         glColor3ub(cp[0], cp[1], cp[2]);
239                 }
240                 else {
241                         if (boneflag & BONE_DRAW_ACTIVE) UI_ThemeColorShade(TH_BONE_POSE, 40);
242                         else if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_BONE_POSE);
243                         else UI_ThemeColor(TH_BONE_SOLID);
244                 }
245                 
246                 return 1;
247         }
248                 break;
249         case PCHAN_COLOR_SPHEREBONE_END:
250         {
251                 if (bcolor) {
252                         char cp[3];
253                         
254                         if (boneflag & BONE_DRAW_ACTIVE) {
255                                 VECCOPY(cp, bcolor->active);
256                                 cp_shade_color3ub(cp, 10);
257                         }
258                         else if (boneflag & BONE_SELECTED) {
259                                 VECCOPY(cp, bcolor->select);
260                                 cp_shade_color3ub(cp, -30);
261                         }
262                         else {
263                                 VECCOPY(cp, bcolor->solid);
264                                 cp_shade_color3ub(cp, -30);
265                         }
266                         
267                         glColor3ub(cp[0], cp[1], cp[2]);
268                 }
269                 else {
270                         if (boneflag & BONE_DRAW_ACTIVE) UI_ThemeColorShade(TH_BONE_POSE, 10);
271                         else if (boneflag & BONE_SELECTED) UI_ThemeColorShade(TH_BONE_POSE, -30);
272                         else UI_ThemeColorShade(TH_BONE_SOLID, -30);
273                 }
274         }
275                 break;
276                 
277         case PCHAN_COLOR_LINEBONE:
278         {
279                 /* inner part in background color or constraint */
280                 if ( (constflag) && ((bcolor==NULL) || (bcolor->flag & TH_WIRECOLOR_CONSTCOLS)) ) {
281                         if (constflag & PCHAN_HAS_STRIDE) glColor3ub(0, 0, 200);
282                         else if (constflag & PCHAN_HAS_TARGET) glColor3ub(255, 150, 0);
283                         else if (constflag & PCHAN_HAS_IK) glColor3ub(255, 255, 0);
284                         else if (constflag & PCHAN_HAS_SPLINEIK) glColor3ub(200, 255, 0);
285                         else if (constflag & PCHAN_HAS_CONST) glColor3ub(0, 255, 120);
286                         else if (constflag) UI_ThemeColor(TH_BONE_POSE);        /* PCHAN_HAS_ACTION */ 
287                 }
288                 else {
289                         if (bcolor) {
290                                 char *cp= bcolor->solid;
291                                 glColor4ub(cp[0], cp[1], cp[2], 204);   
292                         }
293                         else
294                                 UI_ThemeColorShade(TH_BACK, -30);
295                 }
296                 
297                 return 1;
298         }
299                 break;
300         }
301         
302         return 0;
303 }
304
305
306 /* *************** Armature drawing, helper calls for parts ******************* */
307
308 /* half the cube, in Y */
309 static float cube[8][3] = {
310 {-1.0,  0.0, -1.0},
311 {-1.0,  0.0,  1.0},
312 {-1.0,  1.0,  1.0},
313 {-1.0,  1.0, -1.0},
314 { 1.0,  0.0, -1.0},
315 { 1.0,  0.0,  1.0},
316 { 1.0,  1.0,  1.0},
317 { 1.0,  1.0, -1.0},
318 };
319
320 static void drawsolidcube_size(float xsize, float ysize, float zsize)
321 {
322         static GLuint displist=0;
323         float n[3];
324         
325         glScalef(xsize, ysize, zsize);
326         
327         n[0]=0; n[1]=0; n[2]=0;
328
329         if(displist==0) {
330                 displist= glGenLists(1);
331                 glNewList(displist, GL_COMPILE_AND_EXECUTE);
332
333                 glBegin(GL_QUADS);
334                 n[0]= -1.0;
335                 glNormal3fv(n); 
336                 glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
337                 n[0]=0;
338                 n[1]= -1.0;
339                 glNormal3fv(n); 
340                 glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
341                 n[1]=0;
342                 n[0]= 1.0;
343                 glNormal3fv(n); 
344                 glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
345                 n[0]=0;
346                 n[1]= 1.0;
347                 glNormal3fv(n); 
348                 glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
349                 n[1]=0;
350                 n[2]= 1.0;
351                 glNormal3fv(n); 
352                 glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
353                 n[2]=0;
354                 n[2]= -1.0;
355                 glNormal3fv(n); 
356                 glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
357                 glEnd();
358
359                 glEndList();
360         }
361         else glCallList(displist);
362         
363 }
364
365 static void drawcube_size(float xsize, float ysize, float zsize)
366 {
367         static GLuint displist=0;
368         
369         glScalef(xsize, ysize, zsize);
370         
371         if(displist == 0) {
372                 displist= glGenLists(1);
373                 glNewList(displist, GL_COMPILE_AND_EXECUTE);
374                 
375                 glBegin(GL_LINE_STRIP);
376                 glVertex3fv(cube[0]); glVertex3fv(cube[1]);glVertex3fv(cube[2]); glVertex3fv(cube[3]);
377                 glVertex3fv(cube[0]); glVertex3fv(cube[4]);glVertex3fv(cube[5]); glVertex3fv(cube[6]);
378                 glVertex3fv(cube[7]); glVertex3fv(cube[4]);
379                 glEnd();
380                 
381                 glBegin(GL_LINES);
382                 glVertex3fv(cube[1]); glVertex3fv(cube[5]);
383                 glVertex3fv(cube[2]); glVertex3fv(cube[6]);
384                 glVertex3fv(cube[3]); glVertex3fv(cube[7]);
385                 glEnd();
386                 
387                 glEndList();
388         }
389         else glCallList(displist);
390         
391 }
392
393
394 static void draw_bonevert(void)
395 {
396         static GLuint displist=0;
397         
398         if (displist == 0) {
399                 GLUquadricObj   *qobj;
400                 
401                 displist= glGenLists(1);
402                 glNewList(displist, GL_COMPILE_AND_EXECUTE);
403                         
404                 glPushMatrix();
405                 
406                 qobj    = gluNewQuadric(); 
407                 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
408                 gluDisk(qobj, 0.0,  0.05, 16, 1);
409                 
410                 glRotatef(90, 0, 1, 0);
411                 gluDisk(qobj, 0.0,  0.05, 16, 1);
412                 
413                 glRotatef(90, 1, 0, 0);
414                 gluDisk(qobj, 0.0,  0.05, 16, 1);
415                 
416                 gluDeleteQuadric(qobj);  
417                 
418                 glPopMatrix();
419                 glEndList();
420         }
421         else 
422                 glCallList(displist);
423 }
424
425 static void draw_bonevert_solid(void)
426 {
427         static GLuint displist=0;
428         
429         if (displist == 0) {
430                 GLUquadricObj   *qobj;
431                 
432                 displist= glGenLists(1);
433                 glNewList(displist, GL_COMPILE_AND_EXECUTE);
434                 
435                 qobj    = gluNewQuadric();
436                 gluQuadricDrawStyle(qobj, GLU_FILL); 
437                 glShadeModel(GL_SMOOTH);
438                 gluSphere(qobj, 0.05, 8, 5);
439                 glShadeModel(GL_FLAT);
440                 gluDeleteQuadric(qobj);  
441                 
442                 glEndList();
443         }
444         else 
445                 glCallList(displist);
446 }
447
448 static void draw_bone_octahedral()
449 {
450         static GLuint displist=0;
451         
452         if (displist == 0) {
453                 float vec[6][3];        
454                 
455                 displist= glGenLists(1);
456                 glNewList(displist, GL_COMPILE_AND_EXECUTE);
457                 
458                 vec[0][0]= vec[0][1]= vec[0][2]= 0.0f;
459                 vec[5][0]= vec[5][2]= 0.0f; vec[5][1]= 1.0f;
460                 
461                 vec[1][0]= 0.1f; vec[1][2]= 0.1f; vec[1][1]= 0.1f;
462                 vec[2][0]= 0.1f; vec[2][2]= -0.1f; vec[2][1]= 0.1f;
463                 vec[3][0]= -0.1f; vec[3][2]= -0.1f; vec[3][1]= 0.1f;
464                 vec[4][0]= -0.1f; vec[4][2]= 0.1f; vec[4][1]= 0.1f;
465                 
466                 /*      Section 1, sides */
467                 glBegin(GL_LINE_LOOP);
468                 glVertex3fv(vec[0]);
469                 glVertex3fv(vec[1]);
470                 glVertex3fv(vec[5]);
471                 glVertex3fv(vec[3]);
472                 glVertex3fv(vec[0]);
473                 glVertex3fv(vec[4]);
474                 glVertex3fv(vec[5]);
475                 glVertex3fv(vec[2]);
476                 glEnd();
477                 
478                 /*      Section 1, square */
479                 glBegin(GL_LINE_LOOP);
480                 glVertex3fv(vec[1]);
481                 glVertex3fv(vec[2]);
482                 glVertex3fv(vec[3]);
483                 glVertex3fv(vec[4]);
484                 glEnd();
485                 
486                 glEndList();
487         }
488         else 
489                 glCallList(displist);
490 }       
491
492 static void draw_bone_solid_octahedral(void)
493 {
494         static GLuint displist=0;
495         
496         if (displist == 0) {
497                 float vec[6][3], nor[3];        
498                 
499                 displist= glGenLists(1);
500                 glNewList(displist, GL_COMPILE_AND_EXECUTE);
501                 
502                 vec[0][0]= vec[0][1]= vec[0][2]= 0.0f;
503                 vec[5][0]= vec[5][2]= 0.0f; vec[5][1]= 1.0f;
504                 
505                 vec[1][0]= 0.1f; vec[1][2]= 0.1f; vec[1][1]= 0.1f;
506                 vec[2][0]= 0.1f; vec[2][2]= -0.1f; vec[2][1]= 0.1f;
507                 vec[3][0]= -0.1f; vec[3][2]= -0.1f; vec[3][1]= 0.1f;
508                 vec[4][0]= -0.1f; vec[4][2]= 0.1f; vec[4][1]= 0.1f;
509                 
510                 
511                 glBegin(GL_TRIANGLES);
512                 /* bottom */
513                 normal_tri_v3( nor,vec[2], vec[1], vec[0]);
514                 glNormal3fv(nor);
515                 glVertex3fv(vec[2]); glVertex3fv(vec[1]); glVertex3fv(vec[0]);
516                 
517                 normal_tri_v3( nor,vec[3], vec[2], vec[0]);
518                 glNormal3fv(nor);
519                 glVertex3fv(vec[3]); glVertex3fv(vec[2]); glVertex3fv(vec[0]);
520                 
521                 normal_tri_v3( nor,vec[4], vec[3], vec[0]);
522                 glNormal3fv(nor);
523                 glVertex3fv(vec[4]); glVertex3fv(vec[3]); glVertex3fv(vec[0]);
524
525                 normal_tri_v3( nor,vec[1], vec[4], vec[0]);
526                 glNormal3fv(nor);
527                 glVertex3fv(vec[1]); glVertex3fv(vec[4]); glVertex3fv(vec[0]);
528
529                 /* top */
530                 normal_tri_v3( nor,vec[5], vec[1], vec[2]);
531                 glNormal3fv(nor);
532                 glVertex3fv(vec[5]); glVertex3fv(vec[1]); glVertex3fv(vec[2]);
533                 
534                 normal_tri_v3( nor,vec[5], vec[2], vec[3]);
535                 glNormal3fv(nor);
536                 glVertex3fv(vec[5]); glVertex3fv(vec[2]); glVertex3fv(vec[3]);
537                 
538                 normal_tri_v3( nor,vec[5], vec[3], vec[4]);
539                 glNormal3fv(nor);
540                 glVertex3fv(vec[5]); glVertex3fv(vec[3]); glVertex3fv(vec[4]);
541                 
542                 normal_tri_v3( nor,vec[5], vec[4], vec[1]);
543                 glNormal3fv(nor);
544                 glVertex3fv(vec[5]); glVertex3fv(vec[4]); glVertex3fv(vec[1]);
545                 
546                 glEnd();
547                 
548                 glEndList();
549         }
550         else 
551                 glCallList(displist);
552 }       
553
554 /* *************** Armature drawing, bones ******************* */
555
556
557 static void draw_bone_points(int dt, int armflag, unsigned int boneflag, int id)
558 {
559         /*      Draw root point if we are not connected */
560         if ((boneflag & BONE_CONNECTED)==0) {
561                 if (id != -1)
562                         glLoadName(id | BONESEL_ROOT);
563                 
564                 if(dt <= OB_WIRE) {
565                         if (armflag & ARM_EDITMODE) {
566                                 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
567                                 else UI_ThemeColor(TH_VERTEX);
568                         }
569                 }
570                 else {
571                         if (armflag & ARM_POSEMODE) 
572                                 set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, 0);
573                         else
574                                 UI_ThemeColor(TH_BONE_SOLID);
575                 }
576                 
577                 if (dt > OB_WIRE) 
578                         draw_bonevert_solid();
579                 else 
580                         draw_bonevert();
581         }
582         
583         /*      Draw tip point */
584         if (id != -1)
585                 glLoadName(id | BONESEL_TIP);
586         
587         if (dt <= OB_WIRE) {
588                 if (armflag & ARM_EDITMODE) {
589                         if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
590                         else UI_ThemeColor(TH_VERTEX);
591                 }
592         }
593         else {
594                 if (armflag & ARM_POSEMODE) 
595                         set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, 0);
596                 else
597                         UI_ThemeColor(TH_BONE_SOLID);
598         }
599         
600         glTranslatef(0.0f, 1.0f, 0.0f);
601         if (dt > OB_WIRE) 
602                 draw_bonevert_solid();
603         else 
604                 draw_bonevert();
605         glTranslatef(0.0f, -1.0f, 0.0f);
606         
607 }
608
609 /* 16 values of sin function (still same result!) */
610 static float si[16] = {
611         0.00000000f,
612         0.20129852f, 0.39435585f,
613         0.57126821f, 0.72479278f,
614         0.84864425f, 0.93775213f,
615         0.98846832f, 0.99871650f,
616         0.96807711f, 0.89780453f,
617         0.79077573f, 0.65137248f,
618         0.48530196f, 0.29936312f,
619         0.10116832f
620 };
621 /* 16 values of cos function (still same result!) */
622 static float co[16] ={
623         1.00000000f,
624         0.97952994f, 0.91895781f,
625         0.82076344f, 0.68896691f,
626         0.52896401f, 0.34730525f,
627         0.15142777f, -0.05064916f,
628         -0.25065253f, -0.44039415f,
629         -0.61210598f, -0.75875812f,
630         -0.87434661f, -0.95413925f,
631         -0.99486932f
632 };
633
634
635
636 /* smat, imat = mat & imat to draw screenaligned */
637 static void draw_sphere_bone_dist(float smat[][4], float imat[][4], int boneflag, bPoseChannel *pchan, EditBone *ebone)
638 {
639         float head, tail, length, dist;
640         float *headvec, *tailvec, dirvec[3];
641         
642         /* figure out the sizes of spheres */
643         if (ebone) {
644                 /* this routine doesn't call get_matrix_editbone() that calculates it */
645                 ebone->length = len_v3v3(ebone->head, ebone->tail);
646                 
647                 length= ebone->length;
648                 tail= ebone->rad_tail;
649                 dist= ebone->dist;
650                 if (ebone->parent && (ebone->flag & BONE_CONNECTED))
651                         head= ebone->parent->rad_tail;
652                 else
653                         head= ebone->rad_head;
654                 headvec= ebone->head;
655                 tailvec= ebone->tail;
656         }
657         else {
658                 length= pchan->bone->length;
659                 tail= pchan->bone->rad_tail;
660                 dist= pchan->bone->dist;
661                 if (pchan->parent && (pchan->bone->flag & BONE_CONNECTED))
662                         head= pchan->parent->bone->rad_tail;
663                 else
664                         head= pchan->bone->rad_head;
665                 headvec= pchan->pose_head;
666                 tailvec= pchan->pose_tail;
667         }
668         
669         /* ***** draw it ***** */
670         
671         /* move vector to viewspace */
672         sub_v3_v3v3(dirvec, tailvec, headvec);
673         mul_mat3_m4_v3(smat, dirvec);
674         /* clear zcomp */
675         dirvec[2]= 0.0f;
676         /* move vector back */
677         mul_mat3_m4_v3(imat, dirvec);
678         
679         if (0.0f != normalize_v3(dirvec)) {
680                 float norvec[3], vec1[3], vec2[3], vec[3];
681                 int a;
682                 
683                 //mul_v3_fl(dirvec, head);
684                 cross_v3_v3v3(norvec, dirvec, imat[2]);
685                 
686                 glBegin(GL_QUAD_STRIP);
687                 
688                 for (a=0; a<16; a++) {
689                         vec[0]= - *(si+a) * dirvec[0] + *(co+a) * norvec[0];
690                         vec[1]= - *(si+a) * dirvec[1] + *(co+a) * norvec[1];
691                         vec[2]= - *(si+a) * dirvec[2] + *(co+a) * norvec[2];
692                         
693                         vec1[0]= headvec[0] + head*vec[0];
694                         vec1[1]= headvec[1] + head*vec[1];
695                         vec1[2]= headvec[2] + head*vec[2];
696                         vec2[0]= headvec[0] + (head+dist)*vec[0];
697                         vec2[1]= headvec[1] + (head+dist)*vec[1];
698                         vec2[2]= headvec[2] + (head+dist)*vec[2];
699                         
700                         glColor4ub(255, 255, 255, 50);
701                         glVertex3fv(vec1);
702                         //glColor4ub(255, 255, 255, 0);
703                         glVertex3fv(vec2);
704                 }
705                 
706                 for (a=15; a>=0; a--) {
707                         vec[0]= *(si+a) * dirvec[0] + *(co+a) * norvec[0];
708                         vec[1]= *(si+a) * dirvec[1] + *(co+a) * norvec[1];
709                         vec[2]= *(si+a) * dirvec[2] + *(co+a) * norvec[2];
710                         
711                         vec1[0]= tailvec[0] + tail*vec[0];
712                         vec1[1]= tailvec[1] + tail*vec[1];
713                         vec1[2]= tailvec[2] + tail*vec[2];
714                         vec2[0]= tailvec[0] + (tail+dist)*vec[0];
715                         vec2[1]= tailvec[1] + (tail+dist)*vec[1];
716                         vec2[2]= tailvec[2] + (tail+dist)*vec[2];
717                         
718                         //glColor4ub(255, 255, 255, 50);
719                         glVertex3fv(vec1);
720                         //glColor4ub(255, 255, 255, 0);
721                         glVertex3fv(vec2);
722                 }
723                 /* make it cyclic... */
724                 
725                 vec[0]= - *(si) * dirvec[0] + *(co) * norvec[0];
726                 vec[1]= - *(si) * dirvec[1] + *(co) * norvec[1];
727                 vec[2]= - *(si) * dirvec[2] + *(co) * norvec[2];
728                 
729                 vec1[0]= headvec[0] + head*vec[0];
730                 vec1[1]= headvec[1] + head*vec[1];
731                 vec1[2]= headvec[2] + head*vec[2];
732                 vec2[0]= headvec[0] + (head+dist)*vec[0];
733                 vec2[1]= headvec[1] + (head+dist)*vec[1];
734                 vec2[2]= headvec[2] + (head+dist)*vec[2];
735                 
736                 //glColor4ub(255, 255, 255, 50);
737                 glVertex3fv(vec1);
738                 //glColor4ub(255, 255, 255, 0);
739                 glVertex3fv(vec2);
740                 
741                 glEnd();
742         }
743 }
744
745
746 /* smat, imat = mat & imat to draw screenaligned */
747 static void draw_sphere_bone_wire(float smat[][4], float imat[][4], int armflag, int boneflag, int constflag, unsigned int id, bPoseChannel *pchan, EditBone *ebone)
748 {
749         float head, tail, length;
750         float *headvec, *tailvec, dirvec[3];
751         
752         /* figure out the sizes of spheres */
753         if (ebone) {
754                 /* this routine doesn't call get_matrix_editbone() that calculates it */
755                 ebone->length = len_v3v3(ebone->head, ebone->tail);
756                 
757                 length= ebone->length;
758                 tail= ebone->rad_tail;
759                 if (ebone->parent && (boneflag & BONE_CONNECTED))
760                         head= ebone->parent->rad_tail;
761                 else
762                         head= ebone->rad_head;
763                 headvec= ebone->head;
764                 tailvec= ebone->tail;
765         }
766         else {
767                 length= pchan->bone->length;
768                 tail= pchan->bone->rad_tail;
769                 if ((pchan->parent) && (boneflag & BONE_CONNECTED))
770                         head= pchan->parent->bone->rad_tail;
771                 else
772                         head= pchan->bone->rad_head;
773                 headvec= pchan->pose_head;
774                 tailvec= pchan->pose_tail;
775         }
776         
777         /* sphere root color */
778         if (armflag & ARM_EDITMODE) {
779                 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
780                 else UI_ThemeColor(TH_VERTEX);
781         }
782         else if (armflag & ARM_POSEMODE)
783                 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
784         
785         /*      Draw root point if we are not connected */
786         if ((boneflag & BONE_CONNECTED)==0) {
787                 if (id != -1)
788                         glLoadName(id | BONESEL_ROOT);
789                 
790                 drawcircball(GL_LINE_LOOP, headvec, head, imat);
791         }
792         
793         /*      Draw tip point */
794         if (armflag & ARM_EDITMODE) {
795                 if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
796                 else UI_ThemeColor(TH_VERTEX);
797         }
798         
799         if (id != -1)
800                 glLoadName(id | BONESEL_TIP);
801         
802         drawcircball(GL_LINE_LOOP, tailvec, tail, imat);
803         
804         /* base */
805         if (armflag & ARM_EDITMODE) {
806                 if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_SELECT);
807                 else UI_ThemeColor(TH_WIRE);
808         }
809         
810         sub_v3_v3v3(dirvec, tailvec, headvec);
811         
812         /* move vector to viewspace */
813         mul_mat3_m4_v3(smat, dirvec);
814         /* clear zcomp */
815         dirvec[2]= 0.0f;
816         /* move vector back */
817         mul_mat3_m4_v3(imat, dirvec);
818         
819         if (0.0f != normalize_v3(dirvec)) {
820                 float norvech[3], norvect[3], vec[3];
821                 
822                 VECCOPY(vec, dirvec);
823                 
824                 mul_v3_fl(dirvec, head);
825                 cross_v3_v3v3(norvech, dirvec, imat[2]);
826                 
827                 mul_v3_fl(vec, tail);
828                 cross_v3_v3v3(norvect, vec, imat[2]);
829                 
830                 if (id != -1)
831                         glLoadName(id | BONESEL_BONE);
832                 
833                 glBegin(GL_LINES);
834                 vec[0]= headvec[0] + norvech[0];
835                 vec[1]= headvec[1] + norvech[1];
836                 vec[2]= headvec[2] + norvech[2];
837                 glVertex3fv(vec);
838                 vec[0]= tailvec[0] + norvect[0];
839                 vec[1]= tailvec[1] + norvect[1];
840                 vec[2]= tailvec[2] + norvect[2];
841                 glVertex3fv(vec);
842                 vec[0]= headvec[0] - norvech[0];
843                 vec[1]= headvec[1] - norvech[1];
844                 vec[2]= headvec[2] - norvech[2];
845                 glVertex3fv(vec);
846                 vec[0]= tailvec[0] - norvect[0];
847                 vec[1]= tailvec[1] - norvect[1];
848                 vec[2]= tailvec[2] - norvect[2];
849                 glVertex3fv(vec);
850                 
851                 glEnd();
852         }
853 }
854
855 /* does wire only for outline selecting */
856 static void draw_sphere_bone(int dt, int armflag, int boneflag, int constflag, unsigned int id, bPoseChannel *pchan, EditBone *ebone)
857 {
858         GLUquadricObj   *qobj;
859         float head, tail, length;
860         float fac1, fac2;
861         
862         glPushMatrix();
863         qobj    = gluNewQuadric();
864
865         /* figure out the sizes of spheres */
866         if (ebone) {
867                 length= ebone->length;
868                 tail= ebone->rad_tail;
869                 if (ebone->parent && (boneflag & BONE_CONNECTED))
870                         head= ebone->parent->rad_tail;
871                 else
872                         head= ebone->rad_head;
873         }
874         else {
875                 length= pchan->bone->length;
876                 tail= pchan->bone->rad_tail;
877                 if (pchan->parent && (boneflag & BONE_CONNECTED))
878                         head= pchan->parent->bone->rad_tail;
879                 else
880                         head= pchan->bone->rad_head;
881         }
882         
883         /* move to z-axis space */
884         glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
885
886         if (dt==OB_SOLID) {
887                 /* set up solid drawing */
888                 glEnable(GL_COLOR_MATERIAL);
889                 glEnable(GL_LIGHTING);
890                 
891                 gluQuadricDrawStyle(qobj, GLU_FILL); 
892                 glShadeModel(GL_SMOOTH);
893         }
894         else {
895                 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
896         }
897         
898         /* sphere root color */
899         if (armflag & ARM_EDITMODE) {
900                 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
901                 else UI_ThemeColorShade(TH_BONE_SOLID, -30);
902         }
903         else if (armflag & ARM_POSEMODE)
904                 set_pchan_glColor(PCHAN_COLOR_SPHEREBONE_END, armflag, boneflag, constflag);
905         else if (dt==OB_SOLID) 
906                 UI_ThemeColorShade(TH_BONE_SOLID, -30);
907         
908         /*      Draw root point if we are not connected */
909         if ((boneflag & BONE_CONNECTED)==0) {
910                 if (id != -1)
911                         glLoadName(id | BONESEL_ROOT);
912                 gluSphere(qobj, head, 16, 10);
913         }
914         
915         /*      Draw tip point */
916         if (armflag & ARM_EDITMODE) {
917                 if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
918                 else UI_ThemeColorShade(TH_BONE_SOLID, -30);
919         }
920
921         if (id != -1)
922                 glLoadName(id | BONESEL_TIP);
923         
924         glTranslatef(0.0f, 0.0f, length);
925         gluSphere(qobj, tail, 16, 10);
926         glTranslatef(0.0f, 0.0f, -length);
927         
928         /* base */
929         if (armflag & ARM_EDITMODE) {
930                 if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_SELECT);
931                 else UI_ThemeColor(TH_BONE_SOLID);
932         }
933         else if (armflag & ARM_POSEMODE)
934                 set_pchan_glColor(PCHAN_COLOR_SPHEREBONE_BASE, armflag, boneflag, constflag);
935         else if (dt == OB_SOLID)
936                 UI_ThemeColor(TH_BONE_SOLID);
937         
938         fac1= (length-head)/length;
939         fac2= (length-tail)/length;
940         
941         if (length > (head+tail)) {
942                 if (id != -1)
943                         glLoadName (id | BONESEL_BONE);
944                 
945                 glEnable(GL_POLYGON_OFFSET_FILL);
946                 glPolygonOffset(-1.0f, -1.0f);
947                 
948                 glTranslatef(0.0f, 0.0f, head);
949                 gluCylinder(qobj, fac1*head + (1.0f-fac1)*tail, fac2*tail + (1.0f-fac2)*head, length-head-tail, 16, 1);
950                 glTranslatef(0.0f, 0.0f, -head);
951                 
952                 glDisable(GL_POLYGON_OFFSET_FILL);
953                 
954                 /* draw sphere on extrema */
955                 glTranslatef(0.0f, 0.0f, length-tail);
956                 gluSphere(qobj, fac2*tail + (1.0f-fac2)*head, 16, 10);
957                 glTranslatef(0.0f, 0.0f, -length+tail);
958                 
959                 glTranslatef(0.0f, 0.0f, head);
960                 gluSphere(qobj, fac1*head + (1.0f-fac1)*tail, 16, 10);
961         }
962         else {          
963                 /* 1 sphere in center */
964                 glTranslatef(0.0f, 0.0f, (head + length-tail)/2.0f);
965                 gluSphere(qobj, fac1*head + (1.0f-fac1)*tail, 16, 10);
966         }
967         
968         /* restore */
969         if (dt==OB_SOLID) {
970                 glShadeModel(GL_FLAT);
971                 glDisable(GL_LIGHTING);
972                 glDisable(GL_COLOR_MATERIAL);
973         }
974         
975         glPopMatrix();
976         gluDeleteQuadric(qobj);  
977 }
978
979 static GLubyte bm_dot6[]= {0x0, 0x18, 0x3C, 0x7E, 0x7E, 0x3C, 0x18, 0x0}; 
980 static GLubyte bm_dot8[]= {0x3C, 0x7E, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C}; 
981
982 static GLubyte bm_dot5[]= {0x0, 0x0, 0x10, 0x38, 0x7c, 0x38, 0x10, 0x0}; 
983 static GLubyte bm_dot7[]= {0x0, 0x38, 0x7C, 0xFE, 0xFE, 0xFE, 0x7C, 0x38}; 
984
985
986 static void draw_line_bone(int armflag, int boneflag, int constflag, unsigned int id, bPoseChannel *pchan, EditBone *ebone)
987 {
988         float length;
989         
990         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
991         
992         if (pchan) 
993                 length= pchan->bone->length;
994         else 
995                 length= ebone->length;
996         
997         glPushMatrix();
998         glScalef(length, length, length);
999         
1000         /* this chunk not in object mode */
1001         if (armflag & (ARM_EDITMODE|ARM_POSEMODE)) {
1002                 glLineWidth(4.0f);
1003                 if (armflag & ARM_POSEMODE)
1004                         set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1005                 else if (armflag & ARM_EDITMODE) {
1006                         UI_ThemeColor(TH_WIRE);
1007                 }
1008                 
1009                 /*      Draw root point if we are not connected */
1010                 if ((boneflag & BONE_CONNECTED)==0) {
1011                         if (G.f & G_PICKSEL) {  // no bitmap in selection mode, crashes 3d cards...
1012                                 glLoadName (id | BONESEL_ROOT);
1013                                 glBegin(GL_POINTS);
1014                                 glVertex3f(0.0f, 0.0f, 0.0f);
1015                                 glEnd();
1016                         }
1017                         else {
1018                                 glRasterPos3f(0.0f, 0.0f, 0.0f);
1019                                 glBitmap(8, 8, 4, 4, 0, 0, bm_dot8);
1020                         }
1021                 }
1022                 
1023                 if (id != -1)
1024                         glLoadName((GLuint) id|BONESEL_BONE);
1025                 
1026                 glBegin(GL_LINES);
1027                 glVertex3f(0.0f, 0.0f, 0.0f);
1028                 glVertex3f(0.0f, 1.0f, 0.0f);
1029                 glEnd();
1030                 
1031                 /* tip */
1032                 if (G.f & G_PICKSEL) {  
1033                         /* no bitmap in selection mode, crashes 3d cards... */
1034                         glLoadName(id | BONESEL_TIP);
1035                         glBegin(GL_POINTS);
1036                         glVertex3f(0.0f, 1.0f, 0.0f);
1037                         glEnd();
1038                 }
1039                 else {
1040                         glRasterPos3f(0.0f, 1.0f, 0.0f);
1041                         glBitmap(8, 8, 4, 4, 0, 0, bm_dot7);
1042                 }
1043                 
1044                 /* further we send no names */
1045                 if (id != -1)
1046                         glLoadName(id & 0xFFFF);        /* object tag, for bordersel optim */
1047                 
1048                 if (armflag & ARM_POSEMODE)
1049                         set_pchan_glColor(PCHAN_COLOR_LINEBONE, armflag, boneflag, constflag);
1050         }
1051         
1052         glLineWidth(2.0);
1053         
1054         /*Draw root point if we are not connected */
1055         if ((boneflag & BONE_CONNECTED)==0) {
1056                 if ((G.f & G_PICKSEL)==0) {     
1057                         /* no bitmap in selection mode, crashes 3d cards... */
1058                         if (armflag & ARM_EDITMODE) {
1059                                 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
1060                                 else UI_ThemeColor(TH_VERTEX);
1061                         }
1062                         glRasterPos3f(0.0f, 0.0f, 0.0f);
1063                         glBitmap(8, 8, 4, 4, 0, 0, bm_dot6);
1064                 }
1065         }
1066         
1067         if (armflag & ARM_EDITMODE) {
1068                 if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_EDGE_SELECT);
1069                 else UI_ThemeColorShade(TH_BACK, -30);
1070         }
1071         glBegin(GL_LINES);
1072         glVertex3f(0.0f, 0.0f, 0.0f);
1073         glVertex3f(0.0f, 1.0f, 0.0f);
1074         glEnd();
1075         
1076         /* tip */
1077         if ((G.f & G_PICKSEL)==0) {     
1078                 /* no bitmap in selection mode, crashes 3d cards... */
1079                 if (armflag & ARM_EDITMODE) {
1080                         if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
1081                         else UI_ThemeColor(TH_VERTEX);
1082                 }
1083                 glRasterPos3f(0.0f, 1.0f, 0.0f);
1084                 glBitmap(8, 8, 4, 4, 0, 0, bm_dot5);
1085         }
1086         
1087         glLineWidth(1.0);
1088         
1089         glPopMatrix();
1090 }
1091
1092 static void draw_b_bone_boxes(int dt, bPoseChannel *pchan, float xwidth, float length, float zwidth)
1093 {
1094         int segments= 0;
1095         
1096         if (pchan) 
1097                 segments= pchan->bone->segments;
1098         
1099         if ((segments > 1) && (pchan)) {
1100                 float dlen= length/(float)segments;
1101                 Mat4 *bbone= b_bone_spline_setup(pchan, 0);
1102                 int a;
1103                 
1104                 for (a=0; a<segments; a++, bbone++) {
1105                         glPushMatrix();
1106                         glMultMatrixf(bbone->mat);
1107                         if (dt==OB_SOLID) drawsolidcube_size(xwidth, dlen, zwidth);
1108                         else drawcube_size(xwidth, dlen, zwidth);
1109                         glPopMatrix();
1110                 }
1111         }
1112         else {
1113                 glPushMatrix();
1114                 if (dt==OB_SOLID) drawsolidcube_size(xwidth, length, zwidth);
1115                 else drawcube_size(xwidth, length, zwidth);
1116                 glPopMatrix();
1117         }
1118 }
1119
1120 static void draw_b_bone(int dt, int armflag, int boneflag, int constflag, unsigned int id, bPoseChannel *pchan, EditBone *ebone)
1121 {
1122         float xwidth, length, zwidth;
1123         
1124         if (pchan) {
1125                 xwidth= pchan->bone->xwidth;
1126                 length= pchan->bone->length;
1127                 zwidth= pchan->bone->zwidth;
1128         }
1129         else {
1130                 xwidth= ebone->xwidth;
1131                 length= ebone->length;
1132                 zwidth= ebone->zwidth;
1133         }
1134         
1135         /* draw points only if... */
1136         if (armflag & ARM_EDITMODE) {
1137                 /* move to unitspace */
1138                 glPushMatrix();
1139                 glScalef(length, length, length);
1140                 draw_bone_points(dt, armflag, boneflag, id);
1141                 glPopMatrix();
1142                 length*= 0.95f; // make vertices visible
1143         }
1144
1145         /* colors for modes */
1146         if (armflag & ARM_POSEMODE) {
1147                 if (dt <= OB_WIRE)
1148                         set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1149                 else 
1150                         set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, constflag);
1151         }
1152         else if (armflag & ARM_EDITMODE) {
1153                 if (dt==OB_WIRE) {
1154                         if (boneflag & BONE_DRAW_ACTIVE) UI_ThemeColor(TH_EDGE_SELECT);
1155                         else if (boneflag & BONE_SELECTED) UI_ThemeColorShade(TH_EDGE_SELECT, -20);
1156                         else UI_ThemeColor(TH_WIRE);
1157                 }
1158                 else 
1159                         UI_ThemeColor(TH_BONE_SOLID);
1160         }
1161         
1162         if (id != -1) {
1163                 glLoadName ((GLuint) id|BONESEL_BONE);
1164         }
1165         
1166         /* set up solid drawing */
1167         if (dt > OB_WIRE) {
1168                 glEnable(GL_COLOR_MATERIAL);
1169                 glEnable(GL_LIGHTING);
1170                 
1171                 if (armflag & ARM_POSEMODE)
1172                         set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, constflag);
1173                 else
1174                         UI_ThemeColor(TH_BONE_SOLID);
1175                 
1176                 draw_b_bone_boxes(OB_SOLID, pchan, xwidth, length, zwidth);
1177                 
1178                 /* disable solid drawing */
1179                 glDisable(GL_COLOR_MATERIAL);
1180                 glDisable(GL_LIGHTING);
1181         }
1182         else {  
1183                 /* wire */
1184                 if (armflag & ARM_POSEMODE) {
1185                         if (constflag) {
1186                                 /* set constraint colors */
1187                                 if (set_pchan_glColor(PCHAN_COLOR_CONSTS, armflag, boneflag, constflag)) {
1188                                         glEnable(GL_BLEND);
1189                                         
1190                                         draw_b_bone_boxes(OB_SOLID, pchan, xwidth, length, zwidth);
1191                                         
1192                                         glDisable(GL_BLEND);
1193                                 }
1194                                 
1195                                 /* restore colors */
1196                                 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1197                         }
1198                 }               
1199                 
1200                 draw_b_bone_boxes(OB_WIRE, pchan, xwidth, length, zwidth);              
1201         }
1202 }
1203
1204 static void draw_bone(int dt, int armflag, int boneflag, int constflag, unsigned int id, float length)
1205 {
1206         
1207         /*      Draw a 3d octahedral bone, we use normalized space based on length,
1208             for glDisplayLists */
1209         
1210         glScalef(length, length, length);
1211
1212         /* set up solid drawing */
1213         if (dt > OB_WIRE) {
1214                 glEnable(GL_COLOR_MATERIAL);
1215                 glEnable(GL_LIGHTING);
1216                 UI_ThemeColor(TH_BONE_SOLID);
1217         }
1218         
1219         /* colors for posemode */
1220         if (armflag & ARM_POSEMODE) {
1221                 if (dt <= OB_WIRE)
1222                         set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1223                 else 
1224                         set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, constflag);
1225         }
1226         
1227         
1228         draw_bone_points(dt, armflag, boneflag, id);
1229         
1230         /* now draw the bone itself */
1231         if (id != -1) {
1232                 glLoadName((GLuint) id|BONESEL_BONE);
1233         }
1234         
1235         /* wire? */
1236         if (dt <= OB_WIRE) {
1237                 /* colors */
1238                 if (armflag & ARM_EDITMODE) {
1239                         if (boneflag & BONE_DRAW_ACTIVE) UI_ThemeColor(TH_EDGE_SELECT);
1240                         else if (boneflag & BONE_SELECTED) UI_ThemeColorShade(TH_EDGE_SELECT, -20);
1241                         else UI_ThemeColor(TH_WIRE);
1242                 }
1243                 else if (armflag & ARM_POSEMODE) {
1244                         if (constflag) {
1245                                 /* draw constraint colors */
1246                                 if (set_pchan_glColor(PCHAN_COLOR_CONSTS, armflag, boneflag, constflag)) {      
1247                                         glEnable(GL_BLEND);
1248                                         
1249                                         draw_bone_solid_octahedral();
1250                                         
1251                                         glDisable(GL_BLEND);
1252                                 }
1253                                 
1254                                 /* restore colors */
1255                                 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1256                         }
1257                 }               
1258                 draw_bone_octahedral();
1259         }
1260         else {  
1261                 /* solid */
1262                 if (armflag & ARM_POSEMODE)
1263                         set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, constflag);
1264                 else
1265                         UI_ThemeColor(TH_BONE_SOLID);
1266                 draw_bone_solid_octahedral();
1267         }
1268
1269         /* disable solid drawing */
1270         if (dt > OB_WIRE) {
1271                 glDisable(GL_COLOR_MATERIAL);
1272                 glDisable(GL_LIGHTING);
1273         }
1274 }
1275
1276 static void draw_custom_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob, int dt, int armflag, int boneflag, unsigned int id, float length)
1277 {
1278         if(ob==NULL) return;
1279         
1280         glScalef(length, length, length);
1281         
1282         /* colors for posemode */
1283         if (armflag & ARM_POSEMODE) {
1284                 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, 0);
1285         }
1286         
1287         if (id != -1) {
1288                 glLoadName((GLuint) id|BONESEL_BONE);
1289         }
1290         
1291         draw_object_instance(scene, v3d, rv3d, ob, dt, armflag & ARM_POSEMODE);
1292 }
1293
1294
1295 static void pchan_draw_IK_root_lines(bPoseChannel *pchan, short only_temp)
1296 {
1297         bConstraint *con;
1298         bPoseChannel *parchan;
1299         
1300         for (con= pchan->constraints.first; con; con= con->next) {
1301                 if (con->enforce == 0.0f)
1302                         continue;
1303                 
1304                 switch (con->type) {
1305                         case CONSTRAINT_TYPE_KINEMATIC:
1306                         {
1307                                 bKinematicConstraint *data = (bKinematicConstraint*)con->data;
1308                                 int segcount= 0;
1309                                 
1310                                 /* if only_temp, only draw if it is a temporary ik-chain */
1311                                 if ((only_temp) && !(data->flag & CONSTRAINT_IK_TEMP))
1312                                         continue;
1313                                 
1314                                 setlinestyle(3);
1315                                 glBegin(GL_LINES);
1316                                 
1317                                 /* exclude tip from chain? */
1318                                 if ((data->flag & CONSTRAINT_IK_TIP)==0)
1319                                         parchan= pchan->parent;
1320                                 else
1321                                         parchan= pchan;
1322                                 
1323                                 glVertex3fv(parchan->pose_tail);
1324                                 
1325                                 /* Find the chain's root */
1326                                 while (parchan->parent) {
1327                                         segcount++;
1328                                         if(segcount==data->rootbone || segcount>255) break; // 255 is weak
1329                                         parchan= parchan->parent;
1330                                 }
1331                                 if (parchan)
1332                                         glVertex3fv(parchan->pose_head);
1333                                 
1334                                 glEnd();
1335                                 setlinestyle(0);
1336                         }
1337                                 break;
1338                         case CONSTRAINT_TYPE_SPLINEIK: 
1339                         {
1340                                 bSplineIKConstraint *data = (bSplineIKConstraint*)con->data;
1341                                 int segcount= 0;
1342                                 
1343                                 setlinestyle(3);
1344                                 glBegin(GL_LINES);
1345                                 
1346                                 parchan= pchan;
1347                                 glVertex3fv(parchan->pose_tail);
1348                                 
1349                                 /* Find the chain's root */
1350                                 while (parchan->parent) {
1351                                         segcount++;
1352                                         // FIXME: revise the breaking conditions
1353                                         if(segcount==data->chainlen || segcount>255) break; // 255 is weak
1354                                         parchan= parchan->parent;
1355                                 }
1356                                 if (parchan) // XXX revise the breaking conditions to only stop at the tail?
1357                                         glVertex3fv(parchan->pose_head);
1358                                 
1359                                 glEnd();
1360                                 setlinestyle(0);
1361                         }
1362                                 break;
1363                 }
1364         }
1365 }
1366
1367 static void bgl_sphere_project(float ax, float az)
1368 {
1369         float dir[3], sine, q3;
1370
1371         sine= 1.0f - ax*ax - az*az;
1372         q3= (sine < 0.0f)? 0.0f: (float)(2.0*sqrt(sine));
1373
1374         dir[0]= -az*q3;
1375         dir[1]= 1.0f - 2.0f*sine;
1376         dir[2]= ax*q3;
1377
1378         glVertex3fv(dir);
1379 }
1380
1381 static void draw_dof_ellipse(float ax, float az)
1382 {
1383         static float staticSine[16] = {
1384                 0.0f, 0.104528463268f, 0.207911690818f, 0.309016994375f,
1385                 0.406736643076f, 0.5f, 0.587785252292f, 0.669130606359f,
1386                 0.743144825477f, 0.809016994375f, 0.866025403784f,
1387                 0.913545457643f, 0.951056516295f, 0.978147600734f,
1388                 0.994521895368f, 1.0f
1389         };
1390
1391         int i, j, n=16;
1392         float x, z, px, pz;
1393
1394         glEnable(GL_BLEND);
1395         glDepthMask(0);
1396
1397         glColor4ub(70, 70, 70, 50);
1398
1399         glBegin(GL_QUADS);
1400         pz= 0.0f;
1401         for(i=1; i<n; i++) {
1402                 z= staticSine[i];
1403                 
1404                 px= 0.0f;
1405                 for(j=1; j<n-i+1; j++) {
1406                         x = staticSine[j];
1407                         
1408                         if(j == n-i) {
1409                                 glEnd();
1410                                 glBegin(GL_TRIANGLES);
1411                                 bgl_sphere_project(ax*px, az*z);
1412                                 bgl_sphere_project(ax*px, az*pz);
1413                                 bgl_sphere_project(ax*x, az*pz);
1414                                 glEnd();
1415                                 glBegin(GL_QUADS);
1416                         }
1417                         else {
1418                                 bgl_sphere_project(ax*x, az*z);
1419                                 bgl_sphere_project(ax*x, az*pz);
1420                                 bgl_sphere_project(ax*px, az*pz);
1421                                 bgl_sphere_project(ax*px, az*z);
1422                         }
1423                         
1424                         px= x;
1425                 }
1426                 pz= z;
1427         }
1428         glEnd();
1429
1430         glDisable(GL_BLEND);
1431         glDepthMask(1);
1432
1433         glColor3ub(0, 0, 0);
1434
1435         glBegin(GL_LINE_STRIP);
1436         for (i=0; i<n; i++)
1437                 bgl_sphere_project(staticSine[n-i-1]*ax, staticSine[i]*az);
1438         glEnd();
1439 }
1440
1441 static void draw_pose_dofs(Object *ob)
1442 {
1443         bArmature *arm= ob->data;
1444         bPoseChannel *pchan;
1445         Bone *bone;
1446         
1447         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1448                 bone= pchan->bone;
1449                 
1450                 if ( (bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))) {
1451                         if (bone->flag & BONE_SELECTED) {
1452                                 if (bone->layer & arm->layer) {
1453                                         if (pchan->ikflag & (BONE_IK_XLIMIT|BONE_IK_ZLIMIT)) {
1454                                                 if (ED_pose_channel_in_IK_chain(ob, pchan)) {
1455                                                         float corner[4][3], posetrans[3], mat[4][4];
1456                                                         float phi=0.0f, theta=0.0f, scale;
1457                                                         int a, i;
1458                                                         
1459                                                         /* in parent-bone pose, but own restspace */
1460                                                         glPushMatrix();
1461                                                         
1462                                                         VECCOPY(posetrans, pchan->pose_mat[3]);
1463                                                         glTranslatef(posetrans[0], posetrans[1], posetrans[2]);
1464                                                         
1465                                                         if (pchan->parent) {
1466                                                                 copy_m4_m4(mat, pchan->parent->pose_mat);
1467                                                                 mat[3][0]= mat[3][1]= mat[3][2]= 0.0f;
1468                                                                 glMultMatrixf(mat);
1469                                                         }
1470                                                         
1471                                                         copy_m4_m3(mat, pchan->bone->bone_mat);
1472                                                         glMultMatrixf(mat);
1473                                                         
1474                                                         scale= bone->length*pchan->size[1];
1475                                                         glScalef(scale, scale, scale);
1476                                                         
1477                                                         if (pchan->ikflag & BONE_IK_XLIMIT) {
1478                                                                 if (pchan->ikflag & BONE_IK_ZLIMIT) {
1479                                                                         float amin[3], amax[3];
1480                                                                         
1481                                                                         for (i=0; i<3; i++) {
1482                                                                                 amin[i]= (float)sin(pchan->limitmin[i]*M_PI/360.0);
1483                                                                                 amax[i]= (float)sin(pchan->limitmax[i]*M_PI/360.0);
1484                                                                         }
1485                                                                         
1486                                                                         glScalef(1.0f, -1.0f, 1.0f);
1487                                                                         if ((amin[0] != 0.0f) && (amin[2] != 0.0f))
1488                                                                                 draw_dof_ellipse(amin[0], amin[2]);
1489                                                                         if ((amin[0] != 0.0f) && (amax[2] != 0.0f))
1490                                                                                 draw_dof_ellipse(amin[0], amax[2]);
1491                                                                         if ((amax[0] != 0.0f) && (amin[2] != 0.0f))
1492                                                                                 draw_dof_ellipse(amax[0], amin[2]);
1493                                                                         if ((amax[0] != 0.0f) && (amax[2] != 0.0f))
1494                                                                                 draw_dof_ellipse(amax[0], amax[2]);
1495                                                                         glScalef(1.0f, -1.0f, 1.0f);
1496                                                                 }
1497                                                         }
1498                                                         
1499                                                         /* arcs */
1500                                                         if (pchan->ikflag & BONE_IK_ZLIMIT) {
1501                                                                 theta= 0.5f*(pchan->limitmin[2]+pchan->limitmax[2]);
1502                                                                 glRotatef(theta, 0.0f, 0.0f, 1.0f);
1503                                                                 
1504                                                                 glColor3ub(50, 50, 255);        // blue, Z axis limit
1505                                                                 glBegin(GL_LINE_STRIP);
1506                                                                 for (a=-16; a<=16; a++) {
1507                                                                         float fac= ((float)a)/16.0f;
1508                                                                         
1509                                                                         phi= fac * (float)(M_PI/360.0f) * (pchan->limitmax[2] - pchan->limitmin[2]);
1510                                                                         
1511                                                                         i= (a == -16) ? 0 : 1;
1512                                                                         corner[i][0]= (float)sin(phi);
1513                                                                         corner[i][1]= (float)cos(phi);
1514                                                                         corner[i][2]= 0.0f;
1515                                                                         glVertex3fv(corner[i]);
1516                                                                 }
1517                                                                 glEnd();
1518                                                                 
1519                                                                 glRotatef(-theta, 0.0f, 0.0f, 1.0f);
1520                                                         }                                       
1521                                                         
1522                                                         if (pchan->ikflag & BONE_IK_XLIMIT) {
1523                                                                 theta= 0.5f * (pchan->limitmin[0] + pchan->limitmax[0]);
1524                                                                 glRotatef(theta, 1.0f, 0.0f, 0.0f);
1525                                                                 
1526                                                                 glColor3ub(255, 50, 50);        // Red, X axis limit
1527                                                                 glBegin(GL_LINE_STRIP);
1528                                                                 for (a=-16; a<=16; a++) {
1529                                                                         float fac= ((float)a)/16.0f;
1530                                                                         phi= (float)(0.5*M_PI) + fac * (float)(M_PI/360.0f) * (pchan->limitmax[0] - pchan->limitmin[0]);
1531                                                                         
1532                                                                         i= (a == -16) ? 2 : 3;
1533                                                                         corner[i][0]= 0.0f;
1534                                                                         corner[i][1]= (float)sin(phi);
1535                                                                         corner[i][2]= (float)cos(phi);
1536                                                                         glVertex3fv(corner[i]);
1537                                                                 }
1538                                                                 glEnd();
1539                                                                 
1540                                                                 glRotatef(-theta, 1.0f, 0.0f, 0.0f);
1541                                                         }
1542                                                         
1543                                                         /* out of cone, out of bone */
1544                                                         glPopMatrix(); 
1545                                                 }
1546                                         }
1547                                 }
1548                         }
1549                 }
1550         }
1551 }
1552
1553 static void bone_matrix_translate_y(float mat[][4], float y)
1554 {
1555         float trans[3];
1556
1557         VECCOPY(trans, mat[1]);
1558         mul_v3_fl(trans, y);
1559         add_v3_v3v3(mat[3], mat[3], trans);
1560 }
1561
1562 /* assumes object is Armature with pose */
1563 static void draw_pose_channels(Scene *scene, View3D *v3d, ARegion *ar, Base *base, int dt)
1564 {
1565         RegionView3D *rv3d= ar->regiondata;
1566         Object *ob= base->object;
1567         bArmature *arm= ob->data;
1568         bPoseChannel *pchan;
1569         Bone *bone;
1570         GLfloat tmp;
1571         float smat[4][4], imat[4][4], bmat[4][4];
1572         int index= -1;
1573         short do_dashed= 3, draw_wire= 0;
1574         short flag, constflag;
1575         
1576         /* hacky... prevent outline select from drawing dashed helplines */
1577         glGetFloatv(GL_LINE_WIDTH, &tmp);
1578         if (tmp > 1.1) do_dashed &= ~1;
1579         if (v3d->flag & V3D_HIDE_HELPLINES) do_dashed &= ~2;
1580         
1581         /* precalc inverse matrix for drawing screen aligned */
1582         if (arm->drawtype==ARM_ENVELOPE) {
1583                 /* precalc inverse matrix for drawing screen aligned */
1584                 wmGetMatrix(smat);
1585                 mul_mat3_m4_fl(smat, 1.0f/len_v3(ob->obmat[0]));
1586                 invert_m4_m4(imat, smat);
1587                 
1588                 /* and draw blended distances */
1589                 if (arm->flag & ARM_POSEMODE) {
1590                         glEnable(GL_BLEND);
1591                         //glShadeModel(GL_SMOOTH);
1592                         
1593                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1594                         
1595                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1596                                 bone= pchan->bone;
1597                                 if (bone && !(bone->flag & (BONE_HIDDEN_P|BONE_NO_DEFORM|BONE_HIDDEN_PG))) {
1598                                         if (bone->flag & (BONE_SELECTED)) {
1599                                                 if (bone->layer & arm->layer)
1600                                                         draw_sphere_bone_dist(smat, imat, bone->flag, pchan, NULL);
1601                                         }
1602                                 }
1603                         }
1604                         
1605                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
1606                         glDisable(GL_BLEND);
1607                         //glShadeModel(GL_FLAT);
1608                 }
1609         }
1610         
1611         /* little speedup, also make sure transparent only draws once */
1612         glCullFace(GL_BACK); 
1613         glEnable(GL_CULL_FACE);
1614         
1615         /* if solid we draw that first, with selection codes, but without names, axes etc */
1616         if (dt > OB_WIRE) {
1617                 if (arm->flag & ARM_POSEMODE) 
1618                         index= base->selcol;
1619                 
1620                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1621                         bone= pchan->bone;
1622                         
1623                         if ( (bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)) ) {
1624                                 if (bone->layer & arm->layer) {
1625                                         glPushMatrix();
1626                                         glMultMatrixf(pchan->pose_mat);
1627                                         
1628                                         /* catch exception for bone with hidden parent */
1629                                         flag= bone->flag;
1630                                         if ( (bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)) )
1631                                                 flag &= ~BONE_CONNECTED;
1632                                         
1633                                         /* set temporary flag for drawing bone as active */
1634                                         if (bone == arm->act_bone)
1635                                                 flag |= BONE_DRAW_ACTIVE;
1636                                         
1637                                         /* set color-set to use */
1638                                         set_pchan_colorset(ob, pchan);
1639                                         
1640                                         if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM)) {
1641                                                 /* if drawwire, don't try to draw in solid */
1642                                                 if (pchan->bone->flag & BONE_DRAWWIRE) 
1643                                                         draw_wire= 1;
1644                                                 else
1645                                                         draw_custom_bone(scene, v3d, rv3d, pchan->custom, OB_SOLID, arm->flag, flag, index, bone->length);
1646                                         }
1647                                         else if (arm->drawtype==ARM_LINE)
1648                                                 ;       /* nothing in solid */
1649                                         else if (arm->drawtype==ARM_ENVELOPE)
1650                                                 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
1651                                         else if (arm->drawtype==ARM_B_BONE)
1652                                                 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
1653                                         else
1654                                                 draw_bone(OB_SOLID, arm->flag, flag, 0, index, bone->length);
1655                                                 
1656                                         glPopMatrix();
1657                                 }
1658                         }
1659                         
1660                         if (index!= -1) 
1661                                 index+= 0x10000;        // pose bones count in higher 2 bytes only
1662                 }
1663                 
1664                 /* very very confusing... but in object mode, solid draw, we cannot do glLoadName yet,
1665                  * stick bones and/or wire custom-shapes are drawn in next loop 
1666                  */
1667                 if ((arm->drawtype != ARM_LINE) && (draw_wire == 0)) {
1668                         /* object tag, for bordersel optim */
1669                         glLoadName(index & 0xFFFF);     
1670                         index= -1;
1671                 }
1672         }
1673         
1674         /* draw custom bone shapes as wireframes */
1675         if ( !(arm->flag & ARM_NO_CUSTOM) &&
1676                  ((draw_wire) || (dt <= OB_WIRE)) ) 
1677         {
1678                 if (arm->flag & ARM_POSEMODE)
1679                         index= base->selcol;
1680                         
1681                 /* only draw custom bone shapes that need to be drawn as wires */
1682                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1683                         bone= pchan->bone;
1684                         
1685                         if ((bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))) {
1686                                 if (bone->layer & arm->layer) {
1687                                         if (pchan->custom) {
1688                                                 if ((dt < OB_SOLID) || (bone->flag & BONE_DRAWWIRE)) {
1689                                                         glPushMatrix();
1690                                                         glMultMatrixf(pchan->pose_mat);
1691                                                         
1692                                                         /* prepare colors */
1693                                                         if (arm->flag & ARM_POSEMODE)   
1694                                                                 set_pchan_colorset(ob, pchan);
1695 #if 0 // XXX - 13 October 2009, Disabled this to make ghosting show the right colors (Aligorith)
1696                                                         else {
1697                                                                 if ((scene->basact)==base) {
1698                                                                         if (base->flag & (SELECT+BA_WAS_SEL)) UI_ThemeColor(TH_ACTIVE);
1699                                                                         else UI_ThemeColor(TH_WIRE);
1700                                                                 }
1701                                                                 else {
1702                                                                         if (base->flag & (SELECT+BA_WAS_SEL)) UI_ThemeColor(TH_SELECT);
1703                                                                         else UI_ThemeColor(TH_WIRE);
1704                                                                 }
1705                                                         }
1706 #endif
1707                                                                 
1708                                                         /* catch exception for bone with hidden parent */
1709                                                         flag= bone->flag;
1710                                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)))
1711                                                                 flag &= ~BONE_CONNECTED;
1712                                                                 
1713                                                         /* set temporary flag for drawing bone as active */
1714                                                         if (bone == arm->act_bone)
1715                                                                 flag |= BONE_DRAW_ACTIVE;
1716                                                         
1717                                                         draw_custom_bone(scene, v3d, rv3d, pchan->custom, OB_WIRE, arm->flag, flag, index, bone->length);
1718                                                         
1719                                                         glPopMatrix();
1720                                                 }
1721                                         }
1722                                 }
1723                         }
1724                         
1725                         if (index != -1) 
1726                                 index+= 0x10000;        // pose bones count in higher 2 bytes only
1727                 }
1728                 
1729                 if (draw_wire) {
1730                         /* object tag, for bordersel optim */
1731                         glLoadName(index & 0xFFFF);     
1732                         index= -1;
1733                 }
1734         }
1735         
1736         /* wire draw over solid only in posemode */
1737         if ((dt <= OB_WIRE) || (arm->flag & ARM_POSEMODE) || (arm->drawtype==ARM_LINE)) {
1738                 /* draw line check first. we do selection indices */
1739                 if (arm->drawtype==ARM_LINE) {
1740                         if (arm->flag & ARM_POSEMODE) 
1741                                 index= base->selcol;
1742                 }
1743                 /* if solid && posemode, we draw again with polygonoffset */
1744                 else if ((dt > OB_WIRE) && (arm->flag & ARM_POSEMODE)) {
1745                         bglPolygonOffset(rv3d->dist, 1.0);
1746                 }
1747                 else {
1748                         /* and we use selection indices if not done yet */
1749                         if (arm->flag & ARM_POSEMODE) 
1750                                 index= base->selcol;
1751                 }
1752                 
1753                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1754                         bone= pchan->bone;
1755                         
1756                         if ((bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))) {
1757                                 if (bone->layer & arm->layer) {
1758                                         if ((do_dashed & 1) && (bone->parent)) {
1759                                                 /* Draw a line from our root to the parent's tip 
1760                                                  *      - only if V3D_HIDE_HELPLINES is enabled...
1761                                                  */
1762                                                 if ( (do_dashed & 2) && ((bone->flag & BONE_CONNECTED)==0) ) {
1763                                                         if (arm->flag & ARM_POSEMODE) {
1764                                                                 glLoadName(index & 0xFFFF);     // object tag, for bordersel optim
1765                                                                 UI_ThemeColor(TH_WIRE);
1766                                                         }
1767                                                         setlinestyle(3);
1768                                                         glBegin(GL_LINES);
1769                                                         glVertex3fv(pchan->pose_head);
1770                                                         glVertex3fv(pchan->parent->pose_tail);
1771                                                         glEnd();
1772                                                         setlinestyle(0);
1773                                                 }
1774                                                 
1775                                                 /* Draw a line to IK root bone 
1776                                                  *      - only if temporary chain (i.e. "autoik")
1777                                                  */
1778                                                 if (arm->flag & ARM_POSEMODE) {
1779                                                         if (pchan->constflag & PCHAN_HAS_IK) {
1780                                                                 if (bone->flag & BONE_SELECTED) {
1781                                                                         if (pchan->constflag & PCHAN_HAS_TARGET) glColor3ub(200, 120, 0);
1782                                                                         else glColor3ub(200, 200, 50);  // add theme!
1783                                                                         
1784                                                                         glLoadName(index & 0xFFFF);
1785                                                                         pchan_draw_IK_root_lines(pchan, !(do_dashed & 2));
1786                                                                 }
1787                                                         }
1788                                                         else if (pchan->constflag & PCHAN_HAS_SPLINEIK) {
1789                                                                 if (bone->flag & BONE_SELECTED) {
1790                                                                         glColor3ub(150, 200, 50);       // add theme!
1791                                                                         
1792                                                                         glLoadName(index & 0xFFFF);
1793                                                                         pchan_draw_IK_root_lines(pchan, !(do_dashed & 2));
1794                                                                 }
1795                                                         }       
1796                                                 }
1797                                         }
1798                                         
1799                                         glPushMatrix();
1800                                         if (arm->drawtype != ARM_ENVELOPE)
1801                                                 glMultMatrixf(pchan->pose_mat);
1802                                         
1803                                         /* catch exception for bone with hidden parent */
1804                                         flag= bone->flag;
1805                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)))
1806                                                 flag &= ~BONE_CONNECTED;
1807                                         
1808                                         /* set temporary flag for drawing bone as active */
1809                                         if (bone == arm->act_bone)
1810                                                 flag |= BONE_DRAW_ACTIVE;
1811                                         
1812                                         /* extra draw service for pose mode */
1813                                         constflag= pchan->constflag;
1814                                         if (pchan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE))
1815                                                 constflag |= PCHAN_HAS_ACTION;
1816                                         if (pchan->flag & POSE_STRIDE)
1817                                                 constflag |= PCHAN_HAS_STRIDE;
1818                                                 
1819                                         /* set color-set to use */
1820                                         set_pchan_colorset(ob, pchan);
1821                                         
1822                                         if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM))
1823                                                 ; // custom bone shapes should not be drawn here!
1824                                         else if (arm->drawtype==ARM_ENVELOPE) {
1825                                                 if (dt < OB_SOLID)
1826                                                         draw_sphere_bone_wire(smat, imat, arm->flag, flag, constflag, index, pchan, NULL);
1827                                         }
1828                                         else if (arm->drawtype==ARM_LINE)
1829                                                 draw_line_bone(arm->flag, flag, constflag, index, pchan, NULL);
1830                                         else if (arm->drawtype==ARM_B_BONE)
1831                                                 draw_b_bone(OB_WIRE, arm->flag, flag, constflag, index, pchan, NULL);
1832                                         else
1833                                                 draw_bone(OB_WIRE, arm->flag, flag, constflag, index, bone->length);
1834                                         
1835                                         glPopMatrix();
1836                                 }
1837                         }
1838                         
1839                         /* pose bones count in higher 2 bytes only */
1840                         if (index != -1) 
1841                                 index+= 0x10000;        
1842                 }
1843                 /* restore things */
1844                 if ((arm->drawtype!=ARM_LINE)&& (dt>OB_WIRE) && (arm->flag & ARM_POSEMODE))
1845                         bglPolygonOffset(rv3d->dist, 0.0);
1846         }       
1847         
1848         /* restore */
1849         glDisable(GL_CULL_FACE);
1850         
1851         /* draw DoFs */
1852         if (arm->flag & ARM_POSEMODE)
1853                 draw_pose_dofs(ob);
1854
1855         /* finally names and axes */
1856         if (arm->flag & (ARM_DRAWNAMES|ARM_DRAWAXES)) {
1857                 /* patch for several 3d cards (IBM mostly) that crash on glSelect with text drawing */
1858                 if ((G.f & G_PICKSEL) == 0) {
1859                         float vec[3];
1860                         
1861                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1862                         
1863                         for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1864                                 if ((pchan->bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))==0) {
1865                                         if (pchan->bone->layer & arm->layer) {
1866                                                 if (arm->flag & (ARM_EDITMODE|ARM_POSEMODE)) {
1867                                                         bone= pchan->bone;
1868                                                         
1869                                                         if (bone->flag & BONE_SELECTED) UI_ThemeColor(TH_TEXT_HI);
1870                                                         else UI_ThemeColor(TH_TEXT);
1871                                                 }
1872                                                 else if (dt > OB_WIRE)
1873                                                         UI_ThemeColor(TH_TEXT);
1874                                                 
1875                                                 /*      Draw names of bone      */
1876                                                 if (arm->flag & ARM_DRAWNAMES) {
1877                                                         mid_v3_v3v3(vec, pchan->pose_head, pchan->pose_tail);
1878                                                         view3d_cached_text_draw_add(vec[0], vec[1], vec[2], pchan->name, 10);
1879                                                 }       
1880                                                 
1881                                                 /*      Draw additional axes on the bone tail  */
1882                                                 if ( (arm->flag & ARM_DRAWAXES) && (arm->flag & ARM_POSEMODE) ) {
1883                                                         glPushMatrix();
1884                                                         copy_m4_m4(bmat, pchan->pose_mat);
1885                                                         bone_matrix_translate_y(bmat, pchan->bone->length);
1886                                                         glMultMatrixf(bmat);
1887                                                         
1888                                                         /* do cached text draw immediate to include transform */
1889                                                         view3d_cached_text_draw_begin();
1890                                                         drawaxes(pchan->bone->length*0.25f, 0, OB_ARROWS);
1891                                                         view3d_cached_text_draw_end(v3d, ar, 1, bmat);
1892                                                         
1893                                                         glPopMatrix();
1894                                                 }
1895                                         }
1896                                 }
1897                         }
1898                         
1899                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
1900                 }
1901         }
1902 }
1903
1904 /* in editmode, we don't store the bone matrix... */
1905 static void get_matrix_editbone(EditBone *eBone, float bmat[][4])
1906 {
1907         float           delta[3];
1908         float           mat[3][3];
1909         
1910         /* Compose the parent transforms (i.e. their translations) */
1911         sub_v3_v3v3(delta, eBone->tail, eBone->head);   
1912         
1913         eBone->length = (float)sqrt(delta[0]*delta[0] + delta[1]*delta[1] +delta[2]*delta[2]);
1914         
1915         vec_roll_to_mat3(delta, eBone->roll, mat);
1916         copy_m4_m3(bmat, mat);
1917
1918         add_v3_v3v3(bmat[3], bmat[3], eBone->head);
1919 }
1920
1921 static void draw_ebones(View3D *v3d, ARegion *ar, Object *ob, int dt)
1922 {
1923         RegionView3D *rv3d= ar->regiondata;
1924         EditBone *eBone;
1925         bArmature *arm= ob->data;
1926         float smat[4][4], imat[4][4], bmat[4][4];
1927         unsigned int index;
1928         int flag;
1929         
1930         /* envelope (deform distance) */
1931         if(arm->drawtype==ARM_ENVELOPE) {
1932                 /* precalc inverse matrix for drawing screen aligned */
1933                 wmGetMatrix(smat);
1934                 mul_mat3_m4_fl(smat, 1.0f/len_v3(ob->obmat[0]));
1935                 invert_m4_m4(imat, smat);
1936                 
1937                 /* and draw blended distances */
1938                 glEnable(GL_BLEND);
1939                 //glShadeModel(GL_SMOOTH);
1940                 
1941                 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1942
1943                 for (eBone=arm->edbo->first, index=0; eBone; eBone=eBone->next, index++) {
1944                         if (eBone->layer & arm->layer) {
1945                                 if ((eBone->flag & (BONE_HIDDEN_A|BONE_NO_DEFORM))==0) {
1946                                         if (eBone->flag & (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL))
1947                                                 draw_sphere_bone_dist(smat, imat, eBone->flag, NULL, eBone);
1948                                 }
1949                         }
1950                 }
1951                 
1952                 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
1953                 glDisable(GL_BLEND);
1954                 //glShadeModel(GL_FLAT);
1955         }
1956         
1957         /* if solid we draw it first */
1958         if ((dt > OB_WIRE) && (arm->drawtype!=ARM_LINE)) {
1959                 index= 0;
1960                 for (eBone=arm->edbo->first, index=0; eBone; eBone=eBone->next, index++) {
1961                         if (eBone->layer & arm->layer) {
1962                                 if ((eBone->flag & BONE_HIDDEN_A)==0) {
1963                                         glPushMatrix();
1964                                         get_matrix_editbone(eBone, bmat);
1965                                         glMultMatrixf(bmat);
1966                                         
1967                                         /* catch exception for bone with hidden parent */
1968                                         flag= eBone->flag;
1969                                         if ( (eBone->parent) && ((eBone->parent->flag & BONE_HIDDEN_A) || (eBone->parent->layer & arm->layer)==0) )
1970                                                 flag &= ~BONE_CONNECTED;
1971                                                 
1972                                         /* set temporary flag for drawing bone as active */
1973                                         if (eBone == arm->act_edbone)
1974                                                 flag |= BONE_DRAW_ACTIVE;
1975                                         
1976                                         if (arm->drawtype==ARM_ENVELOPE)
1977                                                 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
1978                                         else if(arm->drawtype==ARM_B_BONE)
1979                                                 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
1980                                         else {
1981                                                 draw_bone(OB_SOLID, arm->flag, flag, 0, index, eBone->length);
1982                                         }
1983                                         
1984                                         glPopMatrix();
1985                                 }
1986                         }
1987                 }
1988         }
1989         
1990         /* if wire over solid, set offset */
1991         index= -1;
1992         glLoadName(-1);
1993         if (arm->drawtype==ARM_LINE) {
1994                 if(G.f & G_PICKSEL)
1995                         index= 0;
1996         }
1997         else if (dt > OB_WIRE) 
1998                 bglPolygonOffset(rv3d->dist, 1.0f);
1999         else if (arm->flag & ARM_EDITMODE) 
2000                 index= 0;       /* do selection codes */
2001         
2002         for (eBone=arm->edbo->first; eBone; eBone=eBone->next) {
2003                 if (eBone->layer & arm->layer) {
2004                         if ((eBone->flag & BONE_HIDDEN_A)==0) {
2005                                 
2006                                 /* catch exception for bone with hidden parent */
2007                                 flag= eBone->flag;
2008                                 if ( (eBone->parent) && ((eBone->parent->flag & BONE_HIDDEN_A) || (eBone->parent->layer & arm->layer)==0) )
2009                                         flag &= ~BONE_CONNECTED;
2010                                         
2011                                 /* set temporary flag for drawing bone as active */
2012                                 if (eBone == arm->act_edbone)
2013                                         flag |= BONE_DRAW_ACTIVE;
2014                                 
2015                                 if (arm->drawtype == ARM_ENVELOPE) {
2016                                         if (dt < OB_SOLID)
2017                                                 draw_sphere_bone_wire(smat, imat, arm->flag, flag, 0, index, NULL, eBone);
2018                                 }
2019                                 else {
2020                                         glPushMatrix();
2021                                         get_matrix_editbone(eBone, bmat);
2022                                         glMultMatrixf(bmat);
2023                                         
2024                                         if (arm->drawtype == ARM_LINE) 
2025                                                 draw_line_bone(arm->flag, flag, 0, index, NULL, eBone);
2026                                         else if (arm->drawtype == ARM_B_BONE)
2027                                                 draw_b_bone(OB_WIRE, arm->flag, flag, 0, index, NULL, eBone);
2028                                         else
2029                                                 draw_bone(OB_WIRE, arm->flag, flag, 0, index, eBone->length);
2030                                         
2031                                         glPopMatrix();
2032                                 }
2033                                 
2034                                 /* offset to parent */
2035                                 if (eBone->parent) {
2036                                         UI_ThemeColor(TH_WIRE);
2037                                         glLoadName (-1);                // -1 here is OK!
2038                                         setlinestyle(3);
2039                                         
2040                                         glBegin(GL_LINES);
2041                                         glVertex3fv(eBone->parent->tail);
2042                                         glVertex3fv(eBone->head);
2043                                         glEnd();
2044                                         
2045                                         setlinestyle(0);
2046                                 }
2047                         }
2048                 }
2049                 if(index!=-1) index++;
2050         }
2051         
2052         /* restore */
2053         if (arm->drawtype==ARM_LINE);
2054         else if (dt>OB_WIRE) bglPolygonOffset(rv3d->dist, 0.0f);
2055         
2056         /* finally names and axes */
2057         if (arm->flag & (ARM_DRAWNAMES|ARM_DRAWAXES)) {
2058                 // patch for several 3d cards (IBM mostly) that crash on glSelect with text drawing
2059                 if ((G.f & G_PICKSEL) == 0) {
2060                         float vec[3];
2061                         
2062                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2063                         
2064                         for (eBone=arm->edbo->first, index=0; eBone; eBone=eBone->next, index++) {
2065                                 if(eBone->layer & arm->layer) {
2066                                         if ((eBone->flag & BONE_HIDDEN_A)==0) {
2067                                                 
2068                                                 if (eBone->flag & BONE_SELECTED) UI_ThemeColor(TH_TEXT_HI);
2069                                                 else UI_ThemeColor(TH_TEXT);
2070                                                 
2071                                                 /*      Draw name */
2072                                                 if (arm->flag & ARM_DRAWNAMES) {
2073                                                         mid_v3_v3v3(vec, eBone->head, eBone->tail);
2074                                                         glRasterPos3fv(vec);
2075                                                         view3d_cached_text_draw_add(vec[0], vec[1], vec[2], eBone->name, 10);
2076                                                 }                                       
2077                                                 /*      Draw additional axes */
2078                                                 if (arm->flag & ARM_DRAWAXES) {
2079                                                         glPushMatrix();
2080                                                         get_matrix_editbone(eBone, bmat);
2081                                                         bone_matrix_translate_y(bmat, eBone->length);
2082                                                         glMultMatrixf(bmat);
2083                                                         
2084                                                         /* do cached text draw immediate to include transform */
2085                                                         view3d_cached_text_draw_begin();
2086                                                         drawaxes(eBone->length*0.25f, 0, OB_ARROWS);
2087                                                         view3d_cached_text_draw_end(v3d, ar, 1, bmat);
2088                                                         
2089                                                         glPopMatrix();
2090                                                 }
2091                                                 
2092                                         }
2093                                 }
2094                         }
2095                         
2096                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2097                 }
2098         }
2099 }
2100
2101 /* ****************************** Armature Visualisation ******************************** */
2102
2103 /* ---------- Paths --------- */
2104
2105 /* draw bone paths
2106  *      - in view space 
2107  */
2108 static void draw_pose_paths(Scene *scene, View3D *v3d, ARegion *ar, Object *ob)
2109 {
2110         RegionView3D *rv3d= ar->regiondata;
2111         AnimData *adt= BKE_animdata_from_id(&ob->id);
2112         bArmature *arm= ob->data;
2113         bPoseChannel *pchan;
2114         ActKeyColumn *ak;
2115         DLRBT_Tree keys;
2116         float *fp, *fp_start;
2117         int a, stepsize;
2118         int sfra, efra, len;
2119         
2120         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2121         
2122         glPushMatrix();
2123         glLoadMatrixf(rv3d->viewmat);
2124         
2125         /* version patch here - cannot access frame info from file reading */
2126         if (arm->pathsize == 0) arm->pathsize= 1;
2127         stepsize = arm->pathsize;
2128         
2129         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2130                 if (pchan->bone->layer & arm->layer) {
2131                         if (pchan->path) {
2132                                 /* version patch here - cannot access frame info from file reading */
2133                                 if ((pchan->pathsf == 0) || (pchan->pathef == 0)) {
2134                                         pchan->pathsf= SFRA;
2135                                         pchan->pathef= EFRA;
2136                                 }
2137                                 
2138                                 /* get frame ranges */
2139                                 if (arm->pathflag & ARM_PATH_ACFRA) {
2140                                         int sind;
2141                                         
2142                                         /* With "Around Current", we only choose frames from around 
2143                                          * the current frame to draw. However, this range is still 
2144                                          * restricted by the limits of the original path.
2145                                          */
2146                                         sfra= CFRA - arm->pathbc;
2147                                         efra= CFRA + arm->pathac;
2148                                         if (sfra < pchan->pathsf) sfra= pchan->pathsf;
2149                                         if (efra > pchan->pathef) efra= pchan->pathef;
2150                                         
2151                                         len= efra - sfra;
2152                                         
2153                                         sind= sfra - pchan->pathsf;
2154                                         fp_start= (pchan->path + (3*sind));
2155                                 }
2156                                 else {
2157                                         sfra= pchan->pathsf;
2158                                         efra = sfra + pchan->pathlen;
2159                                         len = pchan->pathlen;
2160                                         fp_start = pchan->path;
2161                                 }
2162                                 
2163                                 /* draw curve-line of path */
2164                                 glShadeModel(GL_SMOOTH);
2165                                 
2166                                 glBegin(GL_LINE_STRIP);                                 
2167                                 for (a=0, fp=fp_start; a<len; a++, fp+=3) {
2168                                         float intensity; /* how faint */
2169                                         
2170                                         /* set color
2171                                          *      - more intense for active/selected bones, less intense for unselected bones
2172                                          *      - black for before current frame, green for current frame, blue for after current frame
2173                                          *      - intensity decreases as distance from current frame increases
2174                                          */
2175                                         #define SET_INTENSITY(A, B, C, min, max) (((1.0f - ((C - B) / (C - A))) * (max-min)) + min) 
2176                                         if ((a+sfra) < CFRA) {
2177                                                 /* black - before cfra */
2178                                                 if (pchan->bone->flag & BONE_SELECTED) {
2179                                                         // intensity= 0.5f;
2180                                                         intensity = SET_INTENSITY(sfra, a, CFRA, 0.25f, 0.75f);
2181                                                 }
2182                                                 else {
2183                                                         //intensity= 0.8f;
2184                                                         intensity = SET_INTENSITY(sfra, a, CFRA, 0.68f, 0.92f);
2185                                                 }
2186                                                 UI_ThemeColorBlend(TH_WIRE, TH_BACK, intensity);
2187                                         }
2188                                         else if ((a+sfra) > CFRA) {
2189                                                 /* blue - after cfra */
2190                                                 if (pchan->bone->flag & BONE_SELECTED) {
2191                                                         //intensity = 0.5f;
2192                                                         intensity = SET_INTENSITY(CFRA, a, efra, 0.25f, 0.75f);
2193                                                 }
2194                                                 else {
2195                                                         //intensity = 0.8f;
2196                                                         intensity = SET_INTENSITY(CFRA, a, efra, 0.68f, 0.92f);
2197                                                 }
2198                                                 UI_ThemeColorBlend(TH_BONE_POSE, TH_BACK, intensity);
2199                                         }
2200                                         else {
2201                                                 /* green - on cfra */
2202                                                 if (pchan->bone->flag & BONE_SELECTED) {
2203                                                         intensity= 0.5f;
2204                                                 }
2205                                                 else {
2206                                                         intensity= 0.99f;
2207                                                 }
2208                                                 UI_ThemeColorBlendShade(TH_CFRAME, TH_BACK, intensity, 10);
2209                                         }       
2210                                         
2211                                         /* draw a vertex with this color */ 
2212                                         glVertex3fv(fp);
2213                                 }
2214                                 
2215                                 glEnd();
2216                                 glShadeModel(GL_FLAT);
2217                                 
2218                                 glPointSize(1.0);
2219                                 
2220                                 /* draw little black point at each frame
2221                                  * NOTE: this is not really visible/noticable
2222                                  */
2223                                 glBegin(GL_POINTS);
2224                                 for (a=0, fp=fp_start; a<len; a++, fp+=3) 
2225                                         glVertex3fv(fp);
2226                                 glEnd();
2227                                 
2228                                 /* Draw little white dots at each framestep value */
2229                                 UI_ThemeColor(TH_TEXT_HI);
2230                                 glBegin(GL_POINTS);
2231                                 for (a=0, fp=fp_start; a<len; a+=stepsize, fp+=(stepsize*3)) 
2232                                         glVertex3fv(fp);
2233                                 glEnd();
2234                                 
2235                                 /* Draw frame numbers at each framestep value */
2236                                 if (arm->pathflag & ARM_PATH_FNUMS) {
2237                                         for (a=0, fp=fp_start; a<len; a+=stepsize, fp+=(stepsize*3)) {
2238                                                 char str[32];
2239                                                 
2240                                                 /* only draw framenum if several consecutive highlighted points don't occur on same point */
2241                                                 if (a == 0) {
2242                                                         sprintf(str, "%d", (a+sfra));
2243                                                         view3d_cached_text_draw_add(fp[0], fp[1], fp[2], str, 0);
2244                                                 }
2245                                                 else if ((a > stepsize) && (a < len-stepsize)) { 
2246                                                         if ((equals_v3v3(fp, fp-(stepsize*3))==0) || (equals_v3v3(fp, fp+(stepsize*3))==0)) {
2247                                                                 sprintf(str, "%d", (a+sfra));
2248                                                                 view3d_cached_text_draw_add(fp[0], fp[1], fp[2], str, 0);
2249                                                         }
2250                                                 }
2251                                         }
2252                                 }
2253                                 
2254                                 /* Keyframes - dots and numbers */
2255                                 if (arm->pathflag & ARM_PATH_KFRAS) {
2256                                         /* build list of all keyframes in active action for pchan */
2257                                         BLI_dlrbTree_init(&keys);
2258                                         
2259                                         if (adt) {
2260                                                 bActionGroup *agrp= action_groups_find_named(adt->action, pchan->name);
2261                                                 if (agrp) {
2262                                                         agroup_to_keylist(adt, agrp, &keys, NULL);
2263                                                         BLI_dlrbTree_linkedlist_sync(&keys);
2264                                                 }
2265                                         }
2266                                         
2267                                         /* Draw slightly-larger yellow dots at each keyframe */
2268                                         UI_ThemeColor(TH_VERTEX_SELECT);
2269                                         glPointSize(5.0f);
2270                                         
2271                                         glBegin(GL_POINTS);
2272                                         for (a=0, fp=fp_start; a<len; a++, fp+=3) {
2273                                                 for (ak= keys.first; ak; ak= ak->next) {
2274                                                         if (ak->cfra == (a+sfra))
2275                                                                 glVertex3fv(fp);
2276                                                 }
2277                                         }
2278                                         glEnd();
2279                                         
2280                                         glPointSize(1.0f);
2281                                         
2282                                         /* Draw frame numbers of keyframes  */
2283                                         if ((arm->pathflag & ARM_PATH_FNUMS) || (arm->pathflag & ARM_PATH_KFNOS)) {
2284                                                 for(a=0, fp=fp_start; a<len; a++, fp+=3) {
2285                                                         for (ak= keys.first; ak; ak= ak->next) {
2286                                                                 if (ak->cfra == (a+sfra)) {
2287                                                                         char str[32];
2288                                                                         
2289                                                                         sprintf(str, "%d", (a+sfra));
2290                                                                         view3d_cached_text_draw_add(fp[0], fp[1], fp[2], str, 0);
2291                                                                 }
2292                                                         }
2293                                                 }
2294                                         }
2295                                         
2296                                         BLI_dlrbTree_free(&keys);
2297                                 }
2298                         }
2299                 }
2300         }
2301         
2302         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2303         glPopMatrix();
2304 }
2305
2306
2307 /* ---------- Ghosts --------- */
2308
2309 /* helper function for ghost drawing - sets/removes flags for temporarily 
2310  * hiding unselected bones while drawing ghosts
2311  */
2312 static void ghost_poses_tag_unselected(Object *ob, short unset)
2313 {
2314         bArmature *arm= ob->data;
2315         bPose *pose= ob->pose;
2316         bPoseChannel *pchan;
2317         
2318         /* don't do anything if no hiding any bones */
2319         if ((arm->flag & ARM_GHOST_ONLYSEL)==0)
2320                 return;
2321                 
2322         /* loop over all pchans, adding/removing tags as appropriate */
2323         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
2324                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
2325                         if (unset) {
2326                                 /* remove tags from all pchans if cleaning up */
2327                                 pchan->bone->flag &= ~BONE_HIDDEN_PG;
2328                         }
2329                         else {
2330                                 /* set tags on unselected pchans only */
2331                                 if ((pchan->bone->flag & BONE_SELECTED)==0)
2332                                         pchan->bone->flag |= BONE_HIDDEN_PG;
2333                         }
2334                 }
2335         }
2336 }
2337
2338 /* draw ghosts that occur within a frame range 
2339  *      note: object should be in posemode 
2340  */
2341 static void draw_ghost_poses_range(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2342 {
2343         Object *ob= base->object;
2344         AnimData *adt= BKE_animdata_from_id(&ob->id);
2345         bArmature *arm= ob->data;
2346         bPose *posen, *poseo;
2347         float start, end, stepsize, range, colfac;
2348         int cfrao, flago, ipoflago;
2349         
2350         start = (float)arm->ghostsf;
2351         end = (float)arm->ghostef;
2352         if (end <= start)
2353                 return;
2354         
2355         stepsize= (float)(arm->ghostsize);
2356         range= (float)(end - start);
2357         
2358         /* store values */
2359         ob->mode &= ~OB_MODE_POSE;
2360         cfrao= CFRA;
2361         flago= arm->flag;
2362         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2363         ipoflago= ob->ipoflag; 
2364         ob->ipoflag |= OB_DISABLE_PATH;
2365         
2366         /* copy the pose */
2367         poseo= ob->pose;
2368         copy_pose(&posen, ob->pose, 1);
2369         ob->pose= posen;
2370         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
2371         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
2372         
2373         glEnable(GL_BLEND);
2374         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2375         
2376         /* draw from first frame of range to last */
2377         for (CFRA= (int)start; CFRA < end; CFRA += (int)stepsize) {
2378                 colfac = (end - (float)CFRA) / range;
2379                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
2380                 
2381                 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2382                 where_is_pose(scene, ob);
2383                 draw_pose_channels(scene, v3d, ar, base, OB_WIRE);
2384         }
2385         glDisable(GL_BLEND);
2386         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2387
2388         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
2389         free_pose(posen);
2390         
2391         /* restore */
2392         CFRA= cfrao;
2393         ob->pose= poseo;
2394         arm->flag= flago;
2395         armature_rebuild_pose(ob, ob->data);
2396         ob->mode |= OB_MODE_POSE;
2397         ob->ipoflag= ipoflago; 
2398 }
2399
2400 /* draw ghosts on keyframes in action within range 
2401  *      - object should be in posemode 
2402  */
2403 static void draw_ghost_poses_keys(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2404 {
2405         Object *ob= base->object;
2406         AnimData *adt= BKE_animdata_from_id(&ob->id);
2407         bAction *act= (adt) ? adt->action : NULL;
2408         bArmature *arm= ob->data;
2409         bPose *posen, *poseo;
2410         DLRBT_Tree keys;
2411         ActKeyColumn *ak, *akn;
2412         float start, end, range, colfac, i;
2413         int cfrao, flago;
2414         
2415         start = (float)arm->ghostsf;
2416         end = (float)arm->ghostef;
2417         if (end <= start)
2418                 return;
2419         
2420         /* get keyframes - then clip to only within range */
2421         BLI_dlrbTree_init(&keys);
2422         action_to_keylist(adt, act, &keys, NULL);
2423         BLI_dlrbTree_linkedlist_sync(&keys);
2424         
2425         range= 0;
2426         for (ak= keys.first; ak; ak= akn) {
2427                 akn= ak->next;
2428                 
2429                 if ((ak->cfra < start) || (ak->cfra > end))
2430                         BLI_freelinkN((ListBase *)&keys, ak);
2431                 else
2432                         range++;
2433         }
2434         if (range == 0) return;
2435         
2436         /* store values */
2437         ob->mode &= ~OB_MODE_POSE;
2438         cfrao= CFRA;
2439         flago= arm->flag;
2440         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2441         ob->ipoflag |= OB_DISABLE_PATH;
2442         
2443         /* copy the pose */
2444         poseo= ob->pose;
2445         copy_pose(&posen, ob->pose, 1);
2446         ob->pose= posen;
2447         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
2448         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
2449         
2450         glEnable(GL_BLEND);
2451         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2452         
2453         /* draw from first frame of range to last */
2454         for (ak=keys.first, i=0; ak; ak=ak->next, i++) {
2455                 colfac = i/range;
2456                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
2457                 
2458                 CFRA= (int)ak->cfra;
2459                 
2460                 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2461                 where_is_pose(scene, ob);
2462                 draw_pose_channels(scene, v3d, ar, base, OB_WIRE);
2463         }
2464         glDisable(GL_BLEND);
2465         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2466
2467         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
2468         BLI_dlrbTree_free(&keys);
2469         free_pose(posen);
2470         
2471         /* restore */
2472         CFRA= cfrao;
2473         ob->pose= poseo;
2474         arm->flag= flago;
2475         armature_rebuild_pose(ob, ob->data);
2476         ob->mode |= OB_MODE_POSE;
2477 }
2478
2479 /* draw ghosts around current frame
2480  *      - object is supposed to be armature in posemode 
2481  */
2482 static void draw_ghost_poses(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2483 {
2484         Object *ob= base->object;
2485         AnimData *adt= BKE_animdata_from_id(&ob->id);
2486         bArmature *arm= ob->data;
2487         bPose *posen, *poseo;
2488         float cur, start, end, stepsize, range, colfac, actframe, ctime;
2489         int cfrao, flago;
2490         
2491         /* pre conditions, get an action with sufficient frames */
2492         if ELEM(NULL, adt, adt->action)
2493                 return;
2494
2495         calc_action_range(adt->action, &start, &end, 0);
2496         if (start == end)
2497                 return;
2498
2499         stepsize= (float)(arm->ghostsize);
2500         range= (float)(arm->ghostep)*stepsize + 0.5f;   /* plus half to make the for loop end correct */
2501         
2502         /* store values */
2503         ob->mode &= ~OB_MODE_POSE;
2504         cfrao= CFRA;
2505         actframe= BKE_nla_tweakedit_remap(adt, (float)CFRA, 0);
2506         flago= arm->flag;
2507         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2508         
2509         /* copy the pose */
2510         poseo= ob->pose;
2511         copy_pose(&posen, ob->pose, 1);
2512         ob->pose= posen;
2513         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
2514         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
2515         
2516         glEnable(GL_BLEND);
2517         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2518         
2519         /* draw from darkest blend to lowest */
2520         for(cur= stepsize; cur<range; cur+=stepsize) {
2521                 ctime= cur - (float)fmod(cfrao, stepsize);      /* ensures consistant stepping */
2522                 colfac= ctime/range;
2523                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
2524                 
2525                 /* only within action range */
2526                 if (actframe+ctime >= start && actframe+ctime <= end) {
2527                         CFRA= (int)BKE_nla_tweakedit_remap(adt, actframe+ctime, NLATIME_CONVERT_MAP);
2528                         
2529                         if (CFRA != cfrao) {
2530                                 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2531                                 where_is_pose(scene, ob);
2532                                 draw_pose_channels(scene, v3d, ar, base, OB_WIRE);
2533                         }
2534                 }
2535                 
2536                 ctime= cur + (float)fmod((float)cfrao, stepsize) - stepsize+1.0f;       /* ensures consistant stepping */
2537                 colfac= ctime/range;
2538                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
2539                 
2540                 /* only within action range */
2541                 if ((actframe-ctime >= start) && (actframe-ctime <= end)) {
2542                         CFRA= (int)BKE_nla_tweakedit_remap(adt, actframe-ctime, NLATIME_CONVERT_MAP);
2543                         
2544                         if (CFRA != cfrao) {
2545                                 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2546                                 where_is_pose(scene, ob);
2547                                 draw_pose_channels(scene, v3d, ar, base, OB_WIRE);
2548                         }
2549                 }
2550         }
2551         glDisable(GL_BLEND);
2552         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2553
2554         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
2555         free_pose(posen);
2556         
2557         /* restore */
2558         CFRA= cfrao;
2559         ob->pose= poseo;
2560         arm->flag= flago;
2561         armature_rebuild_pose(ob, ob->data);
2562         ob->mode |= OB_MODE_POSE;
2563 }
2564
2565 /* ********************************** Armature Drawing - Main ************************* */
2566
2567 /* called from drawobject.c, return 1 if nothing was drawn */
2568 int draw_armature(Scene *scene, View3D *v3d, ARegion *ar, Base *base, int dt, int flag)
2569 {
2570         Object *ob= base->object;
2571         bArmature *arm= ob->data;
2572         int retval= 0;
2573
2574         if(G.f & G_RENDER_SHADOW)
2575                 return 1;
2576         
2577         if(dt>OB_WIRE && arm->drawtype!=ARM_LINE) {
2578                 /* we use color for solid lighting */
2579                 glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
2580                 glEnable(GL_COLOR_MATERIAL);
2581                 glColor3ub(0,0,0);      // clear spec
2582                 glDisable(GL_COLOR_MATERIAL);
2583                 
2584                 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
2585                 glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW); // only for lighting...
2586         }
2587         
2588         /* arm->flag is being used to detect mode... */
2589         /* editmode? */
2590         if(arm->edbo) {
2591                 arm->flag |= ARM_EDITMODE;
2592                 draw_ebones(v3d, ar, ob, dt);
2593                 arm->flag &= ~ARM_EDITMODE;
2594         }
2595         else{
2596                 /*      Draw Pose */
2597                 if(ob->pose && ob->pose->chanbase.first) {
2598                         /* drawing posemode selection indices or colors only in these cases */
2599                         if(!(base->flag & OB_FROMDUPLI)) {
2600                                 if(G.f & G_PICKSEL) {
2601                                         if(OBACT && (OBACT->mode & OB_MODE_WEIGHT_PAINT)) {
2602                                                 if(ob==modifiers_isDeformedByArmature(OBACT))
2603                                                         arm->flag |= ARM_POSEMODE;
2604                                         }
2605                                         else if(ob->mode & OB_MODE_POSE) 
2606                                                 arm->flag |= ARM_POSEMODE;
2607                                 }
2608                                 else if(ob->mode & OB_MODE_POSE) {
2609                                         if (arm->ghosttype == ARM_GHOST_RANGE) {
2610                                                 draw_ghost_poses_range(scene, v3d, ar, base);
2611                                         }
2612                                         else if (arm->ghosttype == ARM_GHOST_KEYS) {
2613                                                 draw_ghost_poses_keys(scene, v3d, ar, base);
2614                                         }
2615                                         else if (arm->ghosttype == ARM_GHOST_CUR) {
2616                                                 if (arm->ghostep)
2617                                                         draw_ghost_poses(scene, v3d, ar, base);
2618                                         }
2619                                         if ((flag & DRAW_SCENESET)==0) {
2620                                                 if(ob==OBACT) 
2621                                                         arm->flag |= ARM_POSEMODE;
2622                                                 else if(OBACT && (OBACT->mode & OB_MODE_WEIGHT_PAINT)) {
2623                                                         if(ob==modifiers_isDeformedByArmature(OBACT))
2624                                                                 arm->flag |= ARM_POSEMODE;
2625                                                 }
2626                                                 draw_pose_paths(scene, v3d, ar, ob);
2627                                         }
2628                                 }       
2629                         }
2630                         draw_pose_channels(scene, v3d, ar, base, dt);
2631                         arm->flag &= ~ARM_POSEMODE; 
2632                         
2633                         if(ob->mode & OB_MODE_POSE)
2634                                 UI_ThemeColor(TH_WIRE); /* restore, for extra draw stuff */
2635                 }
2636                 else retval= 1;
2637         }
2638         /* restore */
2639         glFrontFace(GL_CCW);
2640
2641         return retval;
2642 }
2643
2644 /* *************** END Armature drawing ******************* */
2645
2646