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