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