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