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