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