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