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