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