svn merge -r 13357:13382 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / src / 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 "BMF_Api.h"
41
42 #include "DNA_action_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_constraint_types.h"
45 #include "DNA_ID.h"
46 #include "DNA_nla_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_screen_types.h"
50 #include "DNA_space_types.h"
51 #include "DNA_view3d_types.h"
52 #include "DNA_userdef_types.h"
53
54 #include "BLI_blenlib.h"
55 #include "BLI_arithb.h"
56
57 #include "BKE_action.h"
58 #include "BKE_armature.h"
59 #include "BKE_constraint.h"
60 #include "BKE_depsgraph.h"
61 #include "BKE_DerivedMesh.h"
62 #include "BKE_global.h"
63 #include "BKE_main.h"
64 #include "BKE_object.h"
65 #include "BKE_ipo.h"
66 #include "BKE_utildefines.h"
67
68 #include "BIF_editaction.h"
69 #include "BIF_editarmature.h"
70 #include "BIF_gl.h"
71 #include "BIF_glutil.h"
72 #include "BIF_graphics.h"
73 #include "BIF_interface.h"
74 #include "BIF_poseobject.h"
75 #include "BIF_mywindow.h"
76 #include "BIF_resources.h"
77 #include "BIF_screen.h"
78
79 #include "BDR_editobject.h"
80 #include "BDR_drawobject.h"
81 #include "BDR_drawaction.h"
82
83 #include "BSE_edit.h"
84 #include "BSE_view.h"
85
86 #include "mydevice.h"
87 #include "blendef.h"
88 #include "nla.h"
89
90
91 /* *************** Armature Drawing - Coloring API ***************************** */
92
93 /* global here is reset before drawing each bone */
94 static ThemeWireColor *bcolor= NULL;
95
96 /* values of colCode for set_pchan_glcolor */
97 enum {
98         PCHAN_COLOR_NORMAL      = 0,            /* normal drawing */
99         PCHAN_COLOR_SOLID,                              /* specific case where "solid" colour is needed */
100         PCHAN_COLOR_CONSTS,                             /* "constraint" colors (which may/may-not be suppressed) */
101         
102         PCHAN_COLOR_SPHEREBONE_BASE,    /* for the 'stick' of sphere (envelope) bones */
103         PCHAN_COLOR_SPHEREBONE_END,             /* for the ends of sphere (envelope) bones */
104         PCHAN_COLOR_LINEBONE                    /* for the middle of line-bones */
105 };      
106
107 /* This function sets the color-set for coloring a certain bone */
108 static void set_pchan_colorset (Object *ob, bPoseChannel *pchan)
109 {
110         bPose *pose= (ob) ? ob->pose : NULL;
111         bArmature *arm= (ob) ? ob->data : NULL;
112         short color_index= 0;
113         
114         /* sanity check */
115         if (ELEM4(NULL, ob, arm, pose, pchan)) {
116                 bcolor= NULL;
117                 return;
118         }
119         
120         /* only try to set custom color if enabled for armature */
121         if (arm->flag & ARM_COL_CUSTOM) {       
122                 /* currently, a bone can only use a custom color set if it's group (if it has one),
123                  * has been set to use one
124                  */
125                 if (pchan->agrp_index) {
126                         bActionGroup *grp= (bActionGroup *)BLI_findlink(&pose->agroups, (pchan->agrp_index - 1));
127                         if (grp)
128                                 color_index= grp->customCol;
129                 }
130         }
131         
132         /* bcolor is a pointer to the color set to use. If NULL, then the default
133          * color set (based on the theme colors for 3d-view) is used. 
134          */
135         if (color_index) {
136                 bTheme *btheme= U.themes.first;
137                 bcolor= &btheme->tarm[(color_index - 1)];
138         }
139         else 
140                 bcolor= NULL;
141 }
142
143 /* This function is for brightening/darkening a given color (like BIF_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) BIF_ThemeColorShade(TH_BONE_POSE, 40);
185                         else if (boneflag & BONE_SELECTED) BIF_ThemeColor(TH_BONE_POSE);
186                         else BIF_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                         BIF_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) BIF_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) BIF_ThemeColorShade(TH_BONE_POSE, 40);
241                         else if (boneflag & BONE_SELECTED) BIF_ThemeColor(TH_BONE_POSE);
242                         else BIF_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) BIF_ThemeColorShade(TH_BONE_POSE, 10);
270                         else if (boneflag & BONE_SELECTED) BIF_ThemeColorShade(TH_BONE_POSE, -30);
271                         else BIF_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 ((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) BIF_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], 0.8);   
290                         }
291                         else
292                                 BIF_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.0;
457                 vec[5][0]= vec[5][2]= 0.0; vec[5][1]= 1.0;
458                 
459                 vec[1][0]= 0.1; vec[1][2]= 0.1; vec[1][1]= 0.1;
460                 vec[2][0]= 0.1; vec[2][2]= -0.1; vec[2][1]= 0.1;
461                 vec[3][0]= -0.1; vec[3][2]= -0.1; vec[3][1]= 0.1;
462                 vec[4][0]= -0.1; vec[4][2]= 0.1; vec[4][1]= 0.1;
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.0;
501                 vec[5][0]= vec[5][2]= 0.0; vec[5][1]= 1.0;
502                 
503                 vec[1][0]= 0.1; vec[1][2]= 0.1; vec[1][1]= 0.1;
504                 vec[2][0]= 0.1; vec[2][2]= -0.1; vec[2][1]= 0.1;
505                 vec[3][0]= -0.1; vec[3][2]= -0.1; vec[3][1]= 0.1;
506                 vec[4][0]= -0.1; vec[4][2]= 0.1; vec[4][1]= 0.1;
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) BIF_ThemeColor(TH_VERTEX_SELECT);
565                                 else BIF_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                                 BIF_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) BIF_ThemeColor(TH_VERTEX_SELECT);
588                         else BIF_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                         BIF_ThemeColor(TH_BONE_SOLID);
596         }
597         
598         glTranslatef(0.0, 1.0, 0.0);
599         if (dt > OB_WIRE) 
600                 draw_bonevert_solid();
601         else 
602                 draw_bonevert();
603         glTranslatef(0.0, -1.0, 0.0);
604         
605 }
606
607 /* 16 values of sin function (still same result!) */
608 static float si[16] = {
609         0.00000000,
610         0.20129852, 0.39435585,
611         0.57126821, 0.72479278,
612         0.84864425, 0.93775213,
613         0.98846832, 0.99871650,
614         0.96807711, 0.89780453,
615         0.79077573, 0.65137248,
616         0.48530196, 0.29936312,
617         0.10116832
618 };
619 /* 16 values of cos function (still same result!) */
620 static float co[16] ={
621         1.00000000,
622         0.97952994, 0.91895781,
623         0.82076344, 0.68896691,
624         0.52896401, 0.34730525,
625         0.15142777, -0.05064916,
626         -0.25065253, -0.44039415,
627         -0.61210598, -0.75875812,
628         -0.87434661, -0.95413925,
629         -0.99486932
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.0;
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) BIF_ThemeColor(TH_VERTEX_SELECT);
778                 else BIF_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) BIF_ThemeColor(TH_VERTEX_SELECT);
794                 else BIF_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) BIF_ThemeColor(TH_SELECT);
805                 else BIF_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.0;
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) BIF_ThemeColor(TH_VERTEX_SELECT);
899                 else BIF_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                 BIF_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) BIF_ThemeColor(TH_VERTEX_SELECT);
916                 else BIF_ThemeColorShade(TH_BONE_SOLID, -30);
917         }
918
919         if (id != -1)
920                 glLoadName(id | BONESEL_TIP);
921         
922         glTranslatef(0.0, 0.0, length);
923         gluSphere(qobj, tail, 16, 10);
924         glTranslatef(0.0, 0.0, -length);
925         
926         /* base */
927         if (armflag & ARM_EDITMODE) {
928                 if (boneflag & BONE_SELECTED) BIF_ThemeColor(TH_SELECT);
929                 else BIF_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                 BIF_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.0, -1.0);
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.0);
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.0);
1001                 if (armflag & ARM_POSEMODE)
1002                         set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1003                 else if (armflag & ARM_EDITMODE) {
1004                         BIF_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) BIF_ThemeColor(TH_VERTEX_SELECT);
1058                                 else BIF_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) BIF_ThemeColor(TH_EDGE_SELECT);
1067                 else BIF_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) BIF_ThemeColor(TH_VERTEX_SELECT);
1079                         else BIF_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) BIF_ThemeColor(TH_EDGE_SELECT);
1153                         else if (boneflag & BONE_SELECTED) BIF_ThemeColorShade(TH_EDGE_SELECT, -20);
1154                         else BIF_ThemeColor(TH_WIRE);
1155                 }
1156                 else 
1157                         BIF_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                         BIF_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 colours */
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                 BIF_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) BIF_ThemeColor(TH_EDGE_SELECT);
1238                         else if (boneflag & BONE_SELECTED) BIF_ThemeColorShade(TH_EDGE_SELECT, -20);
1239                         else BIF_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                         BIF_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(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(ob, dt, armflag & ARM_POSEMODE);
1290 }
1291
1292
1293 static void pchan_draw_IK_root_lines(bPoseChannel *pchan)
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) {
1300                         bKinematicConstraint *data = (bKinematicConstraint*)con->data;
1301                         int segcount= 0;
1302                         
1303                         setlinestyle(3);
1304                         glBegin(GL_LINES);
1305                         
1306                         /* exclude tip from chain? */
1307                         if ((data->flag & CONSTRAINT_IK_TIP)==0)
1308                                 parchan= pchan->parent;
1309                         else
1310                                 parchan= pchan;
1311                         
1312                         glVertex3fv(parchan->pose_tail);
1313                         
1314                         /* Find the chain's root */
1315                         while (parchan->parent) {
1316                                 segcount++;
1317                                 if(segcount==data->rootbone || segcount>255) break; // 255 is weak
1318                                 parchan= parchan->parent;
1319                         }
1320                         if (parchan)
1321                                 glVertex3fv(parchan->pose_head);
1322                         
1323                         glEnd();
1324                         setlinestyle(0);
1325                 }
1326         }
1327 }
1328
1329 static void bgl_sphere_project(float ax, float az)
1330 {
1331         float dir[3], sine, q3;
1332
1333         sine= 1.0f-ax*ax-az*az;
1334         q3= (sine < 0.0f)? 0.0f: 2.0f*sqrt(sine);
1335
1336         dir[0]= -az*q3;
1337         dir[1]= 1.0f-2.0f*sine;
1338         dir[2]= ax*q3;
1339
1340         glVertex3fv(dir);
1341 }
1342
1343 static void draw_dof_ellipse(float ax, float az)
1344 {
1345         static float staticSine[16] = {
1346                 0.0, 0.104528463268, 0.207911690818, 0.309016994375,
1347                 0.406736643076, 0.5, 0.587785252292, 0.669130606359,
1348                 0.743144825477, 0.809016994375, 0.866025403784,
1349                 0.913545457643, 0.951056516295, 0.978147600734,
1350                 0.994521895368, 1.0
1351         };
1352
1353         int i, j, n=16;
1354         float x, z, px, pz;
1355
1356         glEnable(GL_BLEND);
1357         glDepthMask(0);
1358
1359         glColor4ub(70, 70, 70, 50);
1360
1361         glBegin(GL_QUADS);
1362         pz= 0.0f;
1363         for(i=1; i<n; i++) {
1364                 z= staticSine[i];
1365
1366                 px= 0.0f;
1367                 for(j=1; j<n-i+1; j++) {
1368                         x = staticSine[j];
1369
1370                         if(j == n-i) {
1371                                 glEnd();
1372                                 glBegin(GL_TRIANGLES);
1373                                 bgl_sphere_project(ax*px, az*z);
1374                                 bgl_sphere_project(ax*px, az*pz);
1375                                 bgl_sphere_project(ax*x, az*pz);
1376                                 glEnd();
1377                                 glBegin(GL_QUADS);
1378                         }
1379                         else {
1380                                 bgl_sphere_project(ax*x, az*z);
1381                                 bgl_sphere_project(ax*x, az*pz);
1382                                 bgl_sphere_project(ax*px, az*pz);
1383                                 bgl_sphere_project(ax*px, az*z);
1384                         }
1385
1386                         px= x;
1387                 }
1388                 pz= z;
1389         }
1390         glEnd();
1391
1392         glDisable(GL_BLEND);
1393         glDepthMask(1);
1394
1395         glColor3ub(0, 0, 0);
1396
1397         glBegin(GL_LINE_STRIP);
1398         for (i=0; i<n; i++)
1399                 bgl_sphere_project(staticSine[n-i-1]*ax, staticSine[i]*az);
1400         glEnd();
1401 }
1402
1403 static void draw_pose_dofs(Object *ob)
1404 {
1405         bArmature *arm= ob->data;
1406         bPoseChannel *pchan;
1407         Bone *bone;
1408         
1409         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1410                 bone= pchan->bone;
1411                 
1412                 if ( (bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))) {
1413                         if (bone->flag & BONE_SELECTED) {
1414                                 if (bone->layer & arm->layer) {
1415                                         if (pchan->ikflag & (BONE_IK_XLIMIT|BONE_IK_ZLIMIT)) {
1416                                                 if (pose_channel_in_IK_chain(ob, pchan)) {
1417                                                         float corner[4][3], posetrans[3], mat[4][4];
1418                                                         float phi=0.0f, theta=0.0f, scale;
1419                                                         int a, i;
1420                                                         
1421                                                         /* in parent-bone pose, but own restspace */
1422                                                         glPushMatrix();
1423                                                         
1424                                                         VECCOPY(posetrans, pchan->pose_mat[3]);
1425                                                         glTranslatef(posetrans[0], posetrans[1], posetrans[2]);
1426                                                         
1427                                                         if (pchan->parent) {
1428                                                                 Mat4CpyMat4(mat, pchan->parent->pose_mat);
1429                                                                 mat[3][0]= mat[3][1]= mat[3][2]= 0.0f;
1430                                                                 glMultMatrixf(mat);
1431                                                         }
1432                                                         
1433                                                         Mat4CpyMat3(mat, pchan->bone->bone_mat);
1434                                                         glMultMatrixf(mat);
1435                                                         
1436                                                         scale= bone->length*pchan->size[1];
1437                                                         glScalef(scale, scale, scale);
1438                                                         
1439                                                         if (pchan->ikflag & BONE_IK_XLIMIT) {
1440                                                                 if (pchan->ikflag & BONE_IK_ZLIMIT) {
1441                                                                         float amin[3], amax[3];
1442                                                                         
1443                                                                         for (i=0; i<3; i++) {
1444                                                                                 amin[i]= sin(pchan->limitmin[i]*M_PI/360.0);
1445                                                                                 amax[i]= sin(pchan->limitmax[i]*M_PI/360.0);
1446                                                                         }
1447                                                                         
1448                                                                         glScalef(1.0, -1.0, 1.0);
1449                                                                         if (amin[0] != 0.0 && amin[2] != 0.0)
1450                                                                                 draw_dof_ellipse(amin[0], amin[2]);
1451                                                                         if (amin[0] != 0.0 && amax[2] != 0.0)
1452                                                                                 draw_dof_ellipse(amin[0], amax[2]);
1453                                                                         if (amax[0] != 0.0 && amin[2] != 0.0)
1454                                                                                 draw_dof_ellipse(amax[0], amin[2]);
1455                                                                         if (amax[0] != 0.0 && amax[2] != 0.0)
1456                                                                                 draw_dof_ellipse(amax[0], amax[2]);
1457                                                                         glScalef(1.0, -1.0, 1.0);
1458                                                                 }
1459                                                         }
1460                                                         
1461                                                         /* arcs */
1462                                                         if (pchan->ikflag & BONE_IK_ZLIMIT) {
1463                                                                 theta= 0.5*(pchan->limitmin[2]+pchan->limitmax[2]);
1464                                                                 glRotatef(theta, 0.0f, 0.0f, 1.0f);
1465                                                                 
1466                                                                 glColor3ub(50, 50, 255);        // blue, Z axis limit
1467                                                                 glBegin(GL_LINE_STRIP);
1468                                                                 for (a=-16; a<=16; a++) {
1469                                                                         float fac= ((float)a)/16.0f;
1470                                                                         phi= fac*(M_PI/360.0f)*(pchan->limitmax[2]-pchan->limitmin[2]);
1471                                                                         
1472                                                                         i= (a == -16) ? 0 : 1;
1473                                                                         corner[i][0]= sin(phi);
1474                                                                         corner[i][1]= cos(phi);
1475                                                                         corner[i][2]= 0.0f;
1476                                                                         glVertex3fv(corner[i]);
1477                                                                 }
1478                                                                 glEnd();
1479                                                                 
1480                                                                 glRotatef(-theta, 0.0f, 0.0f, 1.0f);
1481                                                         }                                       
1482                                                         
1483                                                         if (pchan->ikflag & BONE_IK_XLIMIT) {
1484                                                                 theta=  0.5*(pchan->limitmin[0]+pchan->limitmax[0]);
1485                                                                 glRotatef(theta, 1.0f, 0.0f, 0.0f);
1486                                                                 
1487                                                                 glColor3ub(255, 50, 50);        // Red, X axis limit
1488                                                                 glBegin(GL_LINE_STRIP);
1489                                                                 for (a=-16; a<=16; a++) {
1490                                                                         float fac= ((float)a)/16.0f;
1491                                                                         phi= 0.5f*M_PI + fac*(M_PI/360.0f)*(pchan->limitmax[0]-pchan->limitmin[0]);
1492                                                                         
1493                                                                         i= (a == -16) ? 2 : 3;
1494                                                                         corner[i][0]= 0.0f;
1495                                                                         corner[i][1]= sin(phi);
1496                                                                         corner[i][2]= cos(phi);
1497                                                                         glVertex3fv(corner[i]);
1498                                                                 }
1499                                                                 glEnd();
1500                                                                 
1501                                                                 glRotatef(-theta, 1.0f, 0.0f, 0.0f);
1502                                                         }
1503                                                         
1504                                                         /* out of cone, out of bone */
1505                                                         glPopMatrix(); 
1506                                                 }
1507                                         }
1508                                 }
1509                         }
1510                 }
1511         }
1512 }
1513
1514 /* assumes object is Armature with pose */
1515 static void draw_pose_channels(Base *base, int dt)
1516 {
1517         Object *ob= base->object;
1518         bArmature *arm= ob->data;
1519         bPoseChannel *pchan;
1520         Bone *bone;
1521         GLfloat tmp;
1522         float smat[4][4], imat[4][4];
1523         int index= -1;
1524         int do_dashed= 1;
1525         short flag, constflag;
1526         
1527         /* hacky... prevent outline select from drawing dashed helplines */
1528         glGetFloatv(GL_LINE_WIDTH, &tmp);
1529         if (tmp > 1.1) do_dashed= 0;
1530         if (G.vd->flag & V3D_HIDE_HELPLINES) do_dashed= 0;
1531         
1532         /* precalc inverse matrix for drawing screen aligned */
1533         if (arm->drawtype==ARM_ENVELOPE) {
1534                 /* precalc inverse matrix for drawing screen aligned */
1535                 mygetmatrix(smat);
1536                 Mat4MulFloat3(smat[0], 1.0f/VecLength(ob->obmat[0]));
1537                 Mat4Invert(imat, smat);
1538                 
1539                 /* and draw blended distances */
1540                 if (arm->flag & ARM_POSEMODE) {
1541                         glEnable(GL_BLEND);
1542                         //glShadeModel(GL_SMOOTH);
1543                         
1544                         if (G.vd->zbuf) glDisable(GL_DEPTH_TEST);
1545                         
1546                         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1547                                 bone= pchan->bone;
1548                                 if (bone && !(bone->flag & (BONE_HIDDEN_P|BONE_NO_DEFORM|BONE_HIDDEN_PG))) {
1549                                         if (bone->flag & (BONE_SELECTED)) {
1550                                                 if (bone->layer & arm->layer)
1551                                                         draw_sphere_bone_dist(smat, imat, bone->flag, pchan, NULL);
1552                                         }
1553                                 }
1554                         }
1555                         
1556                         if (G.vd->zbuf) glEnable(GL_DEPTH_TEST);
1557                         glDisable(GL_BLEND);
1558                         //glShadeModel(GL_FLAT);
1559                 }
1560         }
1561         
1562         /* little speedup, also make sure transparent only draws once */
1563         glCullFace(GL_BACK); 
1564         glEnable(GL_CULL_FACE);
1565         
1566         /* if solid we draw that first, with selection codes, but without names, axes etc */
1567         if (dt > OB_WIRE) {
1568                 if (arm->flag & ARM_POSEMODE) 
1569                         index= base->selcol;
1570                 
1571                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1572                         bone= pchan->bone;
1573                         
1574                         if ( (bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)) ) {
1575                                 if (bone->layer & arm->layer) {
1576                                         glPushMatrix();
1577                                         glMultMatrixf(pchan->pose_mat);
1578                                         
1579                                         /* catch exception for bone with hidden parent */
1580                                         flag= bone->flag;
1581                                         if ( (bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)) )
1582                                                 flag &= ~BONE_CONNECTED;
1583                                                 
1584                                         /* set color-set to use */
1585                                         set_pchan_colorset(ob, pchan);
1586                                         
1587                                         if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM))
1588                                                 draw_custom_bone(pchan->custom, OB_SOLID, arm->flag, flag, index, bone->length);
1589                                         else if (arm->drawtype==ARM_LINE)
1590                                                 ;       /* nothing in solid */
1591                                         else if (arm->drawtype==ARM_ENVELOPE)
1592                                                 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
1593                                         else if (arm->drawtype==ARM_B_BONE)
1594                                                 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
1595                                         else
1596                                                 draw_bone(OB_SOLID, arm->flag, flag, 0, index, bone->length);
1597                                                 
1598                                         glPopMatrix();
1599                                 }
1600                         }
1601                         
1602                         if (index!= -1) 
1603                                 index+= 0x10000;        // pose bones count in higher 2 bytes only
1604                 }
1605                 
1606                 /* very very confusing... but in object mode, solid draw, we cannot do glLoadName yet, stick bones are drawn in next loop */
1607                 if (arm->drawtype != ARM_LINE) {
1608                         /* object tag, for bordersel optim */
1609                         glLoadName(index & 0xFFFF);     
1610                         index= -1;
1611                 }
1612         }
1613         
1614         /* wire draw over solid only in posemode */
1615         if ((dt <= OB_WIRE) || (arm->flag & ARM_POSEMODE) || (arm->drawtype==ARM_LINE)) {
1616                 /* draw line check first. we do selection indices */
1617                 if (arm->drawtype==ARM_LINE) {
1618                         if (arm->flag & ARM_POSEMODE) 
1619                                 index= base->selcol;
1620                 }
1621                 /* if solid && posemode, we draw again with polygonoffset */
1622                 else if ((dt > OB_WIRE) && (arm->flag & ARM_POSEMODE)) {
1623                         bglPolygonOffset(1.0);
1624                 }
1625                 else {
1626                         /* and we use selection indices if not done yet */
1627                         if (arm->flag & ARM_POSEMODE) 
1628                                 index= base->selcol;
1629                 }
1630                 
1631                 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1632                         bone= pchan->bone;
1633                         
1634                         if ((bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))) {
1635                                 if (bone->layer & arm->layer) {
1636                                         if (do_dashed && bone->parent) {
1637                                                 /*      Draw a line from our root to the parent's tip */
1638                                                 if ((bone->flag & BONE_CONNECTED)==0) {
1639                                                         if (arm->flag & ARM_POSEMODE) {
1640                                                                 glLoadName(index & 0xFFFF);     // object tag, for bordersel optim
1641                                                                 BIF_ThemeColor(TH_WIRE);
1642                                                         }
1643                                                         setlinestyle(3);
1644                                                         glBegin(GL_LINES);
1645                                                         glVertex3fv(pchan->pose_head);
1646                                                         glVertex3fv(pchan->parent->pose_tail);
1647                                                         glEnd();
1648                                                         setlinestyle(0);
1649                                                 }
1650                                                 
1651                                                 /*      Draw a line to IK root bone */ 
1652                                                 //              TODO: make this draw with do_dashed off (V3D_HIDE_HELPLINES)
1653                                                 if (arm->flag & ARM_POSEMODE) {
1654                                                         if (pchan->constflag & PCHAN_HAS_IK) {
1655                                                                 if (bone->flag & BONE_SELECTED) {
1656                                                                         if (pchan->constflag & PCHAN_HAS_TARGET) glColor3ub(200, 120, 0);
1657                                                                         else glColor3ub(200, 200, 50);  // add theme!
1658                                                                         
1659                                                                         glLoadName(index & 0xFFFF);
1660                                                                         pchan_draw_IK_root_lines(pchan);
1661                                                                 }
1662                                                         }
1663                                                 }
1664                                         }
1665                                         
1666                                         glPushMatrix();
1667                                         if (arm->drawtype != ARM_ENVELOPE)
1668                                                 glMultMatrixf(pchan->pose_mat);
1669                                         
1670                                         /* catch exception for bone with hidden parent */
1671                                         flag= bone->flag;
1672                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)))
1673                                                 flag &= ~BONE_CONNECTED;
1674                                         
1675                                         /* extra draw service for pose mode */
1676                                         constflag= pchan->constflag;
1677                                         if(pchan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE))
1678                                                 constflag |= PCHAN_HAS_ACTION;
1679                                         if(pchan->flag & POSE_STRIDE)
1680                                                 constflag |= PCHAN_HAS_STRIDE;
1681                                                 
1682                                         /* set color-set to use */
1683                                         set_pchan_colorset(ob, pchan);
1684
1685                                         if (pchan->custom && !(arm->flag & ARM_NO_CUSTOM)) {
1686                                                 if (dt < OB_SOLID)
1687                                                         draw_custom_bone(pchan->custom, OB_WIRE, arm->flag, flag, index, bone->length);
1688                                         }
1689                                         else if (arm->drawtype==ARM_ENVELOPE) {
1690                                                 if (dt < OB_SOLID)
1691                                                         draw_sphere_bone_wire(smat, imat, arm->flag, flag, constflag, index, pchan, NULL);
1692                                         }
1693                                         else if (arm->drawtype==ARM_LINE)
1694                                                 draw_line_bone(arm->flag, flag, constflag, index, pchan, NULL);
1695                                         else if (arm->drawtype==ARM_B_BONE)
1696                                                 draw_b_bone(OB_WIRE, arm->flag, flag, constflag, index, pchan, NULL);
1697                                         else
1698                                                 draw_bone(OB_WIRE, arm->flag, flag, constflag, index, bone->length);
1699                                         
1700                                         glPopMatrix();
1701                                 }
1702                         }
1703                         
1704                         /* pose bones count in higher 2 bytes only */
1705                         if (index != -1) 
1706                                 index+= 0x10000;        
1707                 }
1708                 /* restore things */
1709                 if ((arm->drawtype!=ARM_LINE) && (dt>OB_WIRE) && (arm->flag & ARM_POSEMODE))
1710                         bglPolygonOffset(0.0);
1711         }       
1712         
1713         /* restore */
1714         glDisable(GL_CULL_FACE);
1715         
1716         /* draw DoFs */
1717         if (arm->flag & ARM_POSEMODE)
1718                 draw_pose_dofs(ob);
1719
1720         /* finally names and axes */
1721         if (arm->flag & (ARM_DRAWNAMES|ARM_DRAWAXES)) {
1722                 /* patch for several 3d cards (IBM mostly) that crash on glSelect with text drawing */
1723                 if ((G.f & G_PICKSEL) == 0) {
1724                         float vec[3];
1725                         
1726                         if (G.vd->zbuf) glDisable(GL_DEPTH_TEST);
1727                         
1728                         for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1729                                 if ((pchan->bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))==0) {
1730                                         if (pchan->bone->layer & arm->layer) {
1731                                                 if (arm->flag & (ARM_EDITMODE|ARM_POSEMODE)) {
1732                                                         bone= pchan->bone;
1733                                                         
1734                                                         if (bone->flag & BONE_SELECTED) BIF_ThemeColor(TH_TEXT_HI);
1735                                                         else BIF_ThemeColor(TH_TEXT);
1736                                                 }
1737                                                 else if (dt > OB_WIRE)
1738                                                         BIF_ThemeColor(TH_TEXT);
1739                                                 
1740                                                 /*      Draw names of bone      */
1741                                                 if (arm->flag & ARM_DRAWNAMES) {
1742                                                         VecMidf(vec, pchan->pose_head, pchan->pose_tail);
1743                                                         glRasterPos3fv(vec);
1744                                                         BMF_DrawString(G.font, " ");
1745                                                         BMF_DrawString(G.font, pchan->name);
1746                                                 }       
1747                                                 
1748                                                 /*      Draw additional axes on the bone tail  */
1749                                                 if ( (arm->flag & ARM_DRAWAXES) && (arm->flag & ARM_POSEMODE) ) {
1750                                                         glPushMatrix();
1751                                                         glMultMatrixf(pchan->pose_mat);
1752                                                         glTranslatef(0.0f, pchan->bone->length, 0.0f);
1753                                                         drawaxes(0.25f*pchan->bone->length, 0, OB_ARROWS);
1754                                                         glPopMatrix();
1755                                                 }
1756                                         }
1757                                 }
1758                         }
1759                         
1760                         if (G.vd->zbuf) glEnable(GL_DEPTH_TEST);
1761                 }
1762         }
1763 }
1764
1765 /* in editmode, we don't store the bone matrix... */
1766 static void set_matrix_editbone(EditBone *eBone)
1767 {
1768         float           delta[3],offset[3];
1769         float           mat[3][3], bmat[4][4];
1770         
1771         /* Compose the parent transforms (i.e. their translations) */
1772         VECCOPY(offset, eBone->head);
1773         
1774         glTranslatef(offset[0],offset[1],offset[2]);
1775         
1776         VecSubf(delta, eBone->tail, eBone->head);       
1777         
1778         eBone->length = sqrt (delta[0]*delta[0] + delta[1]*delta[1] +delta[2]*delta[2]);
1779         
1780         vec_roll_to_mat3(delta, eBone->roll, mat);
1781         Mat4CpyMat3(bmat, mat);
1782                 
1783         glMultMatrixf(bmat);
1784         
1785 }
1786
1787 static void draw_ebones(Object *ob, int dt)
1788 {
1789         EditBone *eBone;
1790         bArmature *arm= ob->data;
1791         float smat[4][4], imat[4][4];
1792         unsigned int index;
1793         int flag;
1794         
1795         /* envelope (deform distance) */
1796         if(arm->drawtype==ARM_ENVELOPE) {
1797                 /* precalc inverse matrix for drawing screen aligned */
1798                 mygetmatrix(smat);
1799                 Mat4MulFloat3(smat[0], 1.0f/VecLength(ob->obmat[0]));
1800                 Mat4Invert(imat, smat);
1801                 
1802                 /* and draw blended distances */
1803                 glEnable(GL_BLEND);
1804                 //glShadeModel(GL_SMOOTH);
1805                 
1806                 if (G.vd->zbuf) glDisable(GL_DEPTH_TEST);
1807
1808                 for (eBone=G.edbo.first, index=0; eBone; eBone=eBone->next, index++) {
1809                         if (eBone->layer & arm->layer) {
1810                                 if ((eBone->flag & (BONE_HIDDEN_A|BONE_NO_DEFORM))==0) {
1811                                         if (eBone->flag & (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL))
1812                                                 draw_sphere_bone_dist(smat, imat, eBone->flag, NULL, eBone);
1813                                 }
1814                         }
1815                 }
1816                 
1817                 if (G.vd->zbuf) glEnable(GL_DEPTH_TEST);
1818                 glDisable(GL_BLEND);
1819                 //glShadeModel(GL_FLAT);
1820         }
1821         
1822         /* if solid we draw it first */
1823         if ((dt > OB_WIRE) && (arm->drawtype!=ARM_LINE)) {
1824                 index= 0;
1825                 for (eBone=G.edbo.first, index=0; eBone; eBone=eBone->next, index++) {
1826                         if (eBone->layer & arm->layer) {
1827                                 if ((eBone->flag & BONE_HIDDEN_A)==0) {
1828                                         glPushMatrix();
1829                                         set_matrix_editbone(eBone);
1830                                         
1831                                         /* catch exception for bone with hidden parent */
1832                                         flag= eBone->flag;
1833                                         if ( (eBone->parent) && ((eBone->parent->flag & BONE_HIDDEN_A) || (eBone->parent->layer & arm->layer)==0) )
1834                                                 flag &= ~BONE_CONNECTED;
1835                                         
1836                                         if (arm->drawtype==ARM_ENVELOPE)
1837                                                 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
1838                                         else if(arm->drawtype==ARM_B_BONE)
1839                                                 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
1840                                         else {
1841                                                 draw_bone(OB_SOLID, arm->flag, flag, 0, index, eBone->length);
1842                                         }
1843                                         
1844                                         glPopMatrix();
1845                                 }
1846                         }
1847                 }
1848         }
1849         
1850         /* if wire over solid, set offset */
1851         index= -1;
1852         glLoadName(-1);
1853         if (arm->drawtype==ARM_LINE) {
1854                 if(G.f & G_PICKSEL)
1855                         index= 0;
1856         }
1857         else if (dt > OB_WIRE) 
1858                 bglPolygonOffset(1.0);
1859         else if (arm->flag & ARM_EDITMODE) 
1860                 index= 0;       /* do selection codes */
1861         
1862         for (eBone=G.edbo.first; eBone; eBone=eBone->next) {
1863                 if (eBone->layer & arm->layer) {
1864                         if ((eBone->flag & BONE_HIDDEN_A)==0) {
1865                                 
1866                                 /* catch exception for bone with hidden parent */
1867                                 flag= eBone->flag;
1868                                 if ( (eBone->parent) && ((eBone->parent->flag & BONE_HIDDEN_A) || (eBone->parent->layer & arm->layer)==0) )
1869                                         flag &= ~BONE_CONNECTED;
1870                                 
1871                                 if (arm->drawtype == ARM_ENVELOPE) {
1872                                         if (dt < OB_SOLID)
1873                                                 draw_sphere_bone_wire(smat, imat, arm->flag, flag, 0, index, NULL, eBone);
1874                                 }
1875                                 else {
1876                                         glPushMatrix();
1877                                         set_matrix_editbone(eBone);
1878                                         
1879                                         if (arm->drawtype == ARM_LINE) 
1880                                                 draw_line_bone(arm->flag, flag, 0, index, NULL, eBone);
1881                                         else if (arm->drawtype == ARM_B_BONE)
1882                                                 draw_b_bone(OB_WIRE, arm->flag, flag, 0, index, NULL, eBone);
1883                                         else
1884                                                 draw_bone(OB_WIRE, arm->flag, flag, 0, index, eBone->length);
1885                                         
1886                                         glPopMatrix();
1887                                 }
1888                                 
1889                                 /* offset to parent */
1890                                 if (eBone->parent) {
1891                                         BIF_ThemeColor(TH_WIRE);
1892                                         glLoadName (-1);                // -1 here is OK!
1893                                         setlinestyle(3);
1894                                         
1895                                         glBegin(GL_LINES);
1896                                         glVertex3fv(eBone->parent->tail);
1897                                         glVertex3fv(eBone->head);
1898                                         glEnd();
1899                                         
1900                                         setlinestyle(0);
1901                                 }
1902                         }
1903                 }
1904                 if(index!=-1) index++;
1905         }
1906         
1907         /* restore */
1908         if (arm->drawtype==ARM_LINE);
1909         else if (dt>OB_WIRE) bglPolygonOffset(0.0);
1910         
1911         /* finally names and axes */
1912         if (arm->flag & (ARM_DRAWNAMES|ARM_DRAWAXES)) {
1913                 // patch for several 3d cards (IBM mostly) that crash on glSelect with text drawing
1914                 if ((G.f & G_PICKSEL) == 0) {
1915                         float vec[3];
1916                         
1917                         if (G.vd->zbuf) glDisable(GL_DEPTH_TEST);
1918                         
1919                         for (eBone=G.edbo.first, index=0; eBone; eBone=eBone->next, index++) {
1920                                 if(eBone->layer & arm->layer) {
1921                                         if ((eBone->flag & BONE_HIDDEN_A)==0) {
1922                                                 
1923                                                 if (eBone->flag & BONE_SELECTED) BIF_ThemeColor(TH_TEXT_HI);
1924                                                 else BIF_ThemeColor(TH_TEXT);
1925                                                 
1926                                                 /*      Draw name */
1927                                                 if (arm->flag & ARM_DRAWNAMES) {
1928                                                         VecMidf(vec, eBone->head, eBone->tail);
1929                                                         glRasterPos3fv(vec);
1930                                                         BMF_DrawString(G.font, " ");
1931                                                         BMF_DrawString(G.font, eBone->name);
1932                                                 }                                       
1933                                                 /*      Draw additional axes */
1934                                                 if (arm->flag & ARM_DRAWAXES) {
1935                                                         glPushMatrix();
1936                                                         set_matrix_editbone(eBone);
1937                                                         glTranslatef(0.0f, eBone->length, 0.0f);
1938                                                         drawaxes(eBone->length*0.25f, 0, OB_ARROWS);
1939                                                         glPopMatrix();
1940                                                 }
1941                                                 
1942                                         }
1943                                 }
1944                         }
1945                         
1946                         if (G.vd->zbuf) glEnable(GL_DEPTH_TEST);
1947                 }
1948         }
1949 }
1950
1951 /* ****************************** Armature Visualisation ******************************** */
1952
1953 /* ---------- Paths --------- */
1954
1955 /* draw bone paths
1956  *      - in view space 
1957  */
1958 static void draw_pose_paths(Object *ob)
1959 {
1960         bArmature *arm= ob->data;
1961         bPoseChannel *pchan;
1962         bAction *act;
1963         bActionChannel *achan;
1964         ActKeyColumn *ak;
1965         ListBase keys;
1966         float *fp, *fp_start;
1967         int a, stepsize;
1968         int sfra, efra, len;
1969         
1970         if (G.vd->zbuf) glDisable(GL_DEPTH_TEST);
1971         
1972         glPushMatrix();
1973         glLoadMatrixf(G.vd->viewmat);
1974         
1975         /* version patch here - cannot access frame info from file reading */
1976         if (arm->pathsize == 0) arm->pathsize= 1;
1977         stepsize = arm->pathsize;
1978         
1979         for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1980                 if (pchan->bone->layer & arm->layer) {
1981                         if (pchan->path) {
1982                                 /* version patch here - cannot access frame info from file reading */
1983                                 if ((pchan->pathsf == 0) || (pchan->pathef == 0)) {
1984                                         pchan->pathsf= SFRA;
1985                                         pchan->pathef= EFRA;
1986                                 }
1987                                 
1988                                 /* get frame ranges */
1989                                 if (arm->pathflag & ARM_PATH_ACFRA) {
1990                                         int sind;
1991                                         
1992                                         /* With "Around Current", we only choose frames from around 
1993                                          * the current frame to draw. However, this range is still 
1994                                          * restricted by the limits of the original path.
1995                                          */
1996                                         sfra= CFRA - arm->pathbc;
1997                                         efra= CFRA + arm->pathac;
1998                                         if (sfra < pchan->pathsf) sfra= pchan->pathsf;
1999                                         if (efra > pchan->pathef) efra= pchan->pathef;
2000                                         
2001                                         len= efra - sfra;
2002                                         
2003                                         sind= sfra - pchan->pathsf;
2004                                         fp_start= (pchan->path + (3*sind));
2005                                 }
2006                                 else {
2007                                         sfra= pchan->pathsf;
2008                                         efra = sfra + pchan->pathlen;
2009                                         len = pchan->pathlen;
2010                                         fp_start = pchan->path;
2011                                 }
2012                                 
2013                                 /* draw curve-line of path */
2014                                 glShadeModel(GL_SMOOTH);
2015                                 
2016                                 glBegin(GL_LINE_STRIP);                                 
2017                                 for (a=0, fp=fp_start; a<len; a++, fp+=3) {
2018                                         float intensity; /* how faint */
2019                                         
2020                                         /* set colour
2021                                          *      - more intense for active/selected bones, less intense for unselected bones
2022                                          *      - black for before current frame, green for current frame, blue for after current frame
2023                                          *      - intensity decreases as distance from current frame increases
2024                                          */
2025                                         #define SET_INTENSITY(A, B, C, min, max) (((1.0f - ((C - B) / (C - A))) * (max-min)) + min) 
2026                                         if ((a+sfra) < CFRA) {
2027                                                 /* black - before cfra */
2028                                                 if (pchan->bone->flag & BONE_SELECTED) {
2029                                                         // intensity= 0.5;
2030                                                         intensity = SET_INTENSITY(sfra, a, CFRA, 0.25f, 0.75f);
2031                                                 }
2032                                                 else {
2033                                                         //intensity= 0.8;
2034                                                         intensity = SET_INTENSITY(sfra, a, CFRA, 0.68f, 0.92f);
2035                                                 }
2036                                                 BIF_ThemeColorBlend(TH_WIRE, TH_BACK, intensity);
2037                                         }
2038                                         else if ((a+sfra) > CFRA) {
2039                                                 /* blue - after cfra */
2040                                                 if (pchan->bone->flag & BONE_SELECTED) {
2041                                                         //intensity = 0.5;
2042                                                         intensity = SET_INTENSITY(CFRA, a, efra, 0.25f, 0.75f);
2043                                                 }
2044                                                 else {
2045                                                         //intensity = 0.8;
2046                                                         intensity = SET_INTENSITY(CFRA, a, efra, 0.68f, 0.92f);
2047                                                 }
2048                                                 BIF_ThemeColorBlend(TH_BONE_POSE, TH_BACK, intensity);
2049                                         }
2050                                         else {
2051                                                 /* green - on cfra */
2052                                                 if (pchan->bone->flag & BONE_SELECTED) {
2053                                                         intensity= 0.5;
2054                                                 }
2055                                                 else {
2056                                                         intensity= 0.99;
2057                                                 }
2058                                                 BIF_ThemeColorBlendShade(TH_CFRAME, TH_BACK, intensity, 10);
2059                                         }       
2060                                         
2061                                         /* draw a vertex with this colour */ 
2062                                         glVertex3fv(fp);
2063                                 }
2064                                 
2065                                 glEnd();
2066                                 glShadeModel(GL_FLAT);
2067                                 
2068                                 glPointSize(1.0);
2069                                 
2070                                 /* draw little black point at each frame
2071                                  * NOTE: this is not really visible/noticable
2072                                  */
2073                                 glBegin(GL_POINTS);
2074                                 for (a=0, fp=fp_start; a<len; a++, fp+=3) 
2075                                         glVertex3fv(fp);
2076                                 glEnd();
2077                                 
2078                                 /* Draw little white dots at each framestep value */
2079                                 BIF_ThemeColor(TH_TEXT_HI);
2080                                 glBegin(GL_POINTS);
2081                                 for (a=0, fp=fp_start; a<len; a+=stepsize, fp+=(stepsize*3)) 
2082                                         glVertex3fv(fp);
2083                                 glEnd();
2084                                 
2085                                 /* Draw frame numbers at each framestep value */
2086                                 if (arm->pathflag & ARM_PATH_FNUMS) {
2087                                         for (a=0, fp=fp_start; a<len; a+=stepsize, fp+=(stepsize*3)) {
2088                                                 char str[32];
2089                                                 
2090                                                 /* only draw framenum if several consecutive highlighted points don't occur on same point */
2091                                                 if (a == 0) {
2092                                                         glRasterPos3fv(fp);
2093                                                         sprintf(str, "  %d\n", (a+sfra));
2094                                                         BMF_DrawString(G.font, str);
2095                                                 }
2096                                                 else if ((a > stepsize) && (a < len-stepsize)) { 
2097                                                         if ((VecEqual(fp, fp-(stepsize*3))==0) || (VecEqual(fp, fp+(stepsize*3))==0)) {
2098                                                                 glRasterPos3fv(fp);
2099                                                                 sprintf(str, "  %d\n", (a+sfra));
2100                                                                 BMF_DrawString(G.font, str);
2101                                                         }
2102                                                 }
2103                                         }
2104                                 }
2105                                 
2106                                 /* Keyframes - dots and numbers */
2107                                 if (arm->pathflag & ARM_PATH_KFRAS) {
2108                                         /* build list of all keyframes in active action for pchan */
2109                                         keys.first = keys.last = NULL;  
2110                                         act= ob_get_action(ob);
2111                                         if (act) {
2112                                                 achan= get_action_channel(act, pchan->name);
2113                                                 if (achan) 
2114                                                         ipo_to_keylist(achan->ipo, &keys, NULL);
2115                                         }
2116                                         
2117                                         /* Draw slightly-larger yellow dots at each keyframe */
2118                                         BIF_ThemeColor(TH_VERTEX_SELECT);
2119                                         glPointSize(5.0);
2120                                         
2121                                         glBegin(GL_POINTS);
2122                                         for (a=0, fp=fp_start; a<len; a++, fp+=3) {
2123                                                 for (ak= keys.first; ak; ak= ak->next) {
2124                                                         if (ak->cfra == (a+sfra))
2125                                                                 glVertex3fv(fp);
2126                                                 }
2127                                         }
2128                                         glEnd();
2129                                         
2130                                         glPointSize(1.0);
2131                                         
2132                                         /* Draw frame numbers of keyframes  */
2133                                         if ((arm->pathflag & ARM_PATH_FNUMS) || (arm->pathflag & ARM_PATH_KFNOS)) {
2134                                                 for(a=0, fp=fp_start; a<len; a++, fp+=3) {
2135                                                         for (ak= keys.first; ak; ak= ak->next) {
2136                                                                 if (ak->cfra == (a+sfra)) {
2137                                                                         char str[32];
2138                                                                         
2139                                                                         glRasterPos3fv(fp);
2140                                                                         sprintf(str, "  %d\n", (a+sfra));
2141                                                                         BMF_DrawString(G.font, str);
2142                                                                 }
2143                                                         }
2144                                                 }
2145                                         }
2146                                         
2147                                         BLI_freelistN(&keys);
2148                                 }
2149                         }
2150                 }
2151         }
2152         
2153         if (G.vd->zbuf) glEnable(GL_DEPTH_TEST);
2154         glPopMatrix();
2155 }
2156
2157
2158 /* ---------- Ghosts --------- */
2159
2160 /* helper function for ghost drawing - sets/removes flags for temporarily 
2161  * hiding unselected bones while drawing ghosts
2162  */
2163 static void ghost_poses_tag_unselected(Object *ob, short unset)
2164 {
2165         bArmature *arm= ob->data;
2166         bPose *pose= ob->pose;
2167         bPoseChannel *pchan;
2168         
2169         /* don't do anything if no hiding any bones */
2170         if ((arm->flag & ARM_GHOST_ONLYSEL)==0)
2171                 return;
2172                 
2173         /* loop over all pchans, adding/removing tags as appropriate */
2174         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
2175                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
2176                         if (unset) {
2177                                 /* remove tags from all pchans if cleaning up */
2178                                 pchan->bone->flag &= ~BONE_HIDDEN_PG;
2179                         }
2180                         else {
2181                                 /* set tags on unselected pchans only */
2182                                 if ((pchan->bone->flag & BONE_SELECTED)==0)
2183                                         pchan->bone->flag |= BONE_HIDDEN_PG;
2184                         }
2185                 }
2186         }
2187 }
2188
2189 /* draw ghosts that occur within a frame range 
2190  *      note: object should be in posemode 
2191  */
2192 static void draw_ghost_poses_range(Base *base)
2193 {
2194         Object *ob= base->object;
2195         bArmature *arm= ob->data;
2196         bPose *posen, *poseo;
2197         float start, end, stepsize, range, colfac;
2198         int cfrao, flago, ipoflago;
2199         
2200         start = arm->ghostsf;
2201         end = arm->ghostef;
2202         if (end <= start)
2203                 return;
2204         
2205         stepsize= (float)(arm->ghostsize);
2206         range= (float)(end - start);
2207         
2208         /* store values */
2209         ob->flag &= ~OB_POSEMODE;
2210         cfrao= CFRA;
2211         flago= arm->flag;
2212         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2213         ipoflago= ob->ipoflag; 
2214         ob->ipoflag |= OB_DISABLE_PATH;
2215         
2216         /* copy the pose */
2217         poseo= ob->pose;
2218         copy_pose(&posen, ob->pose, 1);
2219         ob->pose= posen;
2220         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
2221         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
2222         
2223         glEnable(GL_BLEND);
2224         if (G.vd->zbuf) glDisable(GL_DEPTH_TEST);
2225         
2226         /* draw from first frame of range to last */
2227         for (CFRA= start; CFRA<end; CFRA+=stepsize) {
2228                 colfac = (end-CFRA)/range;
2229                 BIF_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0f*sqrt(colfac)));
2230                 
2231                 do_all_pose_actions(ob);
2232                 where_is_pose(ob);
2233                 draw_pose_channels(base, OB_WIRE);
2234         }
2235         glDisable(GL_BLEND);
2236         if (G.vd->zbuf) glEnable(GL_DEPTH_TEST);
2237
2238         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
2239         free_pose(posen);
2240         
2241         /* restore */
2242         CFRA= cfrao;
2243         ob->pose= poseo;
2244         arm->flag= flago;
2245         armature_rebuild_pose(ob, ob->data);
2246         ob->flag |= OB_POSEMODE;
2247         ob->ipoflag= ipoflago; 
2248 }
2249
2250 /* draw ghosts on keyframes in action within range 
2251  *      - object should be in posemode 
2252  */
2253 static void draw_ghost_poses_keys(Base *base)
2254 {
2255         Object *ob= base->object;
2256         bAction *act= ob_get_action(ob);
2257         bArmature *arm= ob->data;
2258         bPose *posen, *poseo;
2259         ListBase keys= {NULL, NULL};
2260         ActKeyColumn *ak, *akn;
2261         float start, end, range, colfac, i;
2262         int cfrao, flago, ipoflago;
2263         
2264         start = arm->ghostsf;
2265         end = arm->ghostef;
2266         if (end <= start)
2267                 return;
2268         
2269         /* get keyframes - then clip to only within range */
2270         action_to_keylist(act, &keys, NULL);
2271         range= 0;
2272         for (ak= keys.first; ak; ak= akn) {
2273                 akn= ak->next;
2274                 
2275                 if ((ak->cfra < start) || (ak->cfra > end))
2276                         BLI_freelinkN(&keys, ak);
2277                 else
2278                         range++;
2279         }
2280         if (range == 0) return;
2281         
2282         /* store values */
2283         ob->flag &= ~OB_POSEMODE;
2284         cfrao= CFRA;
2285         flago= arm->flag;
2286         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2287         ipoflago= ob->ipoflag; 
2288         ob->ipoflag |= OB_DISABLE_PATH;
2289         
2290         /* copy the pose */
2291         poseo= ob->pose;
2292         copy_pose(&posen, ob->pose, 1);
2293         ob->pose= posen;
2294         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
2295         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
2296         
2297         glEnable(GL_BLEND);
2298         if (G.vd->zbuf) glDisable(GL_DEPTH_TEST);
2299         
2300         /* draw from first frame of range to last */
2301         for (ak=keys.first, i=0; ak; ak=ak->next, i++) {
2302                 colfac = i/range;
2303                 BIF_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0f*sqrt(colfac)));
2304                 
2305                 CFRA= (int)ak->cfra;
2306                 
2307                 do_all_pose_actions(ob);
2308                 where_is_pose(ob);
2309                 draw_pose_channels(base, OB_WIRE);
2310         }
2311         glDisable(GL_BLEND);
2312         if (G.vd->zbuf) glEnable(GL_DEPTH_TEST);
2313
2314         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
2315         BLI_freelistN(&keys);
2316         free_pose(posen);
2317         
2318         /* restore */
2319         CFRA= cfrao;
2320         ob->pose= poseo;
2321         arm->flag= flago;
2322         armature_rebuild_pose(ob, ob->data);
2323         ob->flag |= OB_POSEMODE;
2324         ob->ipoflag= ipoflago; 
2325 }
2326
2327 /* draw ghosts around current frame
2328  *      - object is supposed to be armature in posemode 
2329  */
2330 static void draw_ghost_poses(Base *base)
2331 {
2332         Object *ob= base->object;
2333         bArmature *arm= ob->data;
2334         bPose *posen, *poseo;
2335         bActionStrip *strip;
2336         float cur, start, end, stepsize, range, colfac, actframe, ctime;
2337         int cfrao, maptime, flago, ipoflago;
2338         
2339         /* pre conditions, get an action with sufficient frames */
2340         if (ob->action==NULL)
2341                 return;
2342
2343         calc_action_range(ob->action, &start, &end, 0);
2344         if (start == end)
2345                 return;
2346
2347         stepsize= (float)(arm->ghostsize);
2348         range= (float)(arm->ghostep)*stepsize + 0.5f;   /* plus half to make the for loop end correct */
2349         
2350         /* we only map time for armature when an active strip exists */
2351         for (strip=ob->nlastrips.first; strip; strip=strip->next)
2352                 if (strip->flag & ACTSTRIP_ACTIVE)
2353                         break;
2354         
2355         maptime= (strip!=NULL);
2356         
2357         /* store values */
2358         ob->flag &= ~OB_POSEMODE;
2359         cfrao= CFRA;
2360         if (maptime) actframe= get_action_frame(ob, (float)CFRA);
2361         else actframe= CFRA;
2362         flago= arm->flag;
2363         arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2364         ipoflago= ob->ipoflag; 
2365         ob->ipoflag |= OB_DISABLE_PATH;
2366         
2367         /* copy the pose */
2368         poseo= ob->pose;
2369         copy_pose(&posen, ob->pose, 1);
2370         ob->pose= posen;
2371         armature_rebuild_pose(ob, ob->data);    /* child pointers for IK */
2372         ghost_poses_tag_unselected(ob, 0);              /* hide unselected bones if need be */
2373         
2374         glEnable(GL_BLEND);
2375         if (G.vd->zbuf) glDisable(GL_DEPTH_TEST);
2376         
2377         /* draw from darkest blend to lowest */
2378         for(cur= stepsize; cur<range; cur+=stepsize) {
2379                 ctime= cur - fmod((float)cfrao, stepsize);      /* ensures consistant stepping */
2380                 colfac= ctime/range;
2381                 BIF_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0f*sqrt(colfac)));
2382                 
2383                 /* only within action range */
2384                 if (actframe+ctime >= start && actframe+ctime <= end) {
2385                         if (maptime) CFRA= (int)get_action_frame_inv(ob, actframe+ctime);
2386                         else CFRA= (int)floor(actframe+ctime);
2387                         
2388                         if (CFRA!=cfrao) {
2389                                 do_all_pose_actions(ob);
2390                                 where_is_pose(ob);
2391                                 draw_pose_channels(base, OB_WIRE);
2392                         }
2393                 }
2394                 
2395                 ctime= cur + fmod((float)cfrao, stepsize) - stepsize+1.0f;      /* ensures consistant stepping */
2396                 colfac= ctime/range;
2397                 BIF_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0f*sqrt(colfac)));
2398                 
2399                 /* only within action range */
2400                 if ((actframe-ctime >= start) && (actframe-ctime <= end)) {
2401                         if (maptime) CFRA= (int)get_action_frame_inv(ob, actframe-ctime);
2402                         else CFRA= (int)floor(actframe-ctime);
2403                         
2404                         if (CFRA != cfrao) {
2405                                 do_all_pose_actions(ob);
2406                                 where_is_pose(ob);
2407                                 draw_pose_channels(base, OB_WIRE);
2408                         }
2409                 }
2410         }
2411         glDisable(GL_BLEND);
2412         if (G.vd->zbuf) glEnable(GL_DEPTH_TEST);
2413
2414         ghost_poses_tag_unselected(ob, 1);              /* unhide unselected bones if need be */
2415         free_pose(posen);
2416         
2417         /* restore */
2418         CFRA= cfrao;
2419         ob->pose= poseo;
2420         arm->flag= flago;
2421         armature_rebuild_pose(ob, ob->data);
2422         ob->flag |= OB_POSEMODE;
2423         ob->ipoflag= ipoflago; 
2424 }
2425
2426 /* ********************************** Armature Drawing - Main ************************* */
2427
2428 /* called from drawobject.c, return 1 if nothing was drawn */
2429 int draw_armature(Base *base, int dt)
2430 {
2431         Object *ob= base->object;
2432         bArmature *arm= ob->data;
2433         int retval= 0;
2434         
2435         if(dt>OB_WIRE && arm->drawtype!=ARM_LINE) {
2436                 /* we use color for solid lighting */
2437                 glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
2438                 glEnable(GL_COLOR_MATERIAL);
2439                 glColor3ub(0,0,0);      // clear spec
2440                 glDisable(GL_COLOR_MATERIAL);
2441                 
2442                 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
2443                 glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW); // only for lighting...
2444         }
2445         
2446         /* arm->flag is being used to detect mode... */
2447         /* editmode? */
2448         if(ob==G.obedit || (G.obedit && ob->data==G.obedit->data)) {
2449                 if(ob==G.obedit) arm->flag |= ARM_EDITMODE;
2450                 draw_ebones(ob, dt);
2451                 arm->flag &= ~ARM_EDITMODE;
2452         }
2453         else{
2454                 /*      Draw Pose */
2455                 if(ob->pose && ob->pose->chanbase.first) {
2456                         /* drawing posemode selection indices or colors only in these cases */
2457                         if(!(base->flag & OB_FROMDUPLI)) {
2458                                 if(G.f & G_PICKSEL) {
2459                                         if(ob->flag & OB_POSEMODE) 
2460                                                 arm->flag |= ARM_POSEMODE;
2461                                 }
2462                                 else if(ob->flag & OB_POSEMODE) {
2463                                         if (arm->ghosttype == ARM_GHOST_RANGE) {
2464                                                 draw_ghost_poses_range(base);
2465                                         }
2466                                         else if (arm->ghosttype == ARM_GHOST_KEYS) {
2467                                                 draw_ghost_poses_keys(base);
2468                                         }
2469                                         else if (arm->ghosttype == ARM_GHOST_CUR) {
2470                                                 if (arm->ghostep)
2471                                                         draw_ghost_poses(base);
2472                                         }
2473                                         
2474                                         if(ob==OBACT) 
2475                                                 arm->flag |= ARM_POSEMODE;
2476                                         else if(G.f & G_WEIGHTPAINT)
2477                                                 arm->flag |= ARM_POSEMODE;
2478                                         
2479                                         draw_pose_paths(ob);
2480                                 }       
2481                         }                       
2482                         draw_pose_channels(base, dt);
2483                         arm->flag &= ~ARM_POSEMODE; 
2484                         
2485                         if(ob->flag & OB_POSEMODE)
2486                                 BIF_ThemeColor(TH_WIRE);        /* restore, for extra draw stuff */
2487                 }
2488                 else retval= 1;
2489         }
2490         /* restore */
2491         glFrontFace(GL_CCW);
2492
2493         return retval;
2494 }
2495
2496 /* *************** END Armature drawing ******************* */
2497