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