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