style cleanup: switch statements, include break statements within braces & indent.
[blender.git] / source / blender / editors / space_view3d / drawarmature.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_view3d/drawarmature.c
29  *  \ingroup spview3d
30  */
31
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37
38 #include "DNA_anim_types.h"
39 #include "DNA_armature_types.h"
40 #include "DNA_constraint_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_view3d_types.h"
44 #include "DNA_object_types.h"
45
46 #include "BLI_blenlib.h"
47 #include "BLI_math.h"
48 #include "BLI_dlrbTree.h"
49 #include "BLI_utildefines.h"
50
51 #include "BKE_animsys.h"
52 #include "BKE_action.h"
53 #include "BKE_armature.h"
54 #include "BKE_global.h"
55 #include "BKE_modifier.h"
56 #include "BKE_nla.h"
57
58
59 #include "BIF_gl.h"
60 #include "BIF_glutil.h"
61
62 #include "ED_armature.h"
63 #include "ED_keyframes_draw.h"
64
65 #include "BLF_api.h"
66
67 #include "UI_resources.h"
68
69 #include "view3d_intern.h"
70
71
72 /* *************** Armature Drawing - Coloring API ***************************** */
73
74 /* global here is reset before drawing each bone */
75 static ThemeWireColor *bcolor = NULL;
76
77 /* values of colCode for set_pchan_glcolor */
78 enum {
79         PCHAN_COLOR_NORMAL  = 0,        /* normal drawing */
80         PCHAN_COLOR_SOLID,              /* specific case where "solid" color is needed */
81         PCHAN_COLOR_CONSTS,             /* "constraint" colors (which may/may-not be suppressed) */
82
83         PCHAN_COLOR_SPHEREBONE_BASE,    /* for the 'stick' of sphere (envelope) bones */
84         PCHAN_COLOR_SPHEREBONE_END,     /* for the ends of sphere (envelope) bones */
85         PCHAN_COLOR_LINEBONE            /* for the middle of line-bones */
86 };
87
88 /* This function sets the color-set for coloring a certain bone */
89 static void set_pchan_colorset(Object *ob, bPoseChannel *pchan)
90 {
91         bPose *pose = (ob) ? ob->pose : NULL;
92         bArmature *arm = (ob) ? ob->data : NULL;
93         bActionGroup *grp = NULL;
94         short color_index = 0;
95         
96         /* sanity check */
97         if (ELEM4(NULL, ob, arm, pose, pchan)) {
98                 bcolor = NULL;
99                 return;
100         }
101         
102         /* only try to set custom color if enabled for armature */
103         if (arm->flag & ARM_COL_CUSTOM) {
104                 /* currently, a bone can only use a custom color set if it's group (if it has one),
105                  * has been set to use one
106                  */
107                 if (pchan->agrp_index) {
108                         grp = (bActionGroup *)BLI_findlink(&pose->agroups, (pchan->agrp_index - 1));
109                         if (grp)
110                                 color_index = grp->customCol;
111                 }
112         }
113         
114         /* bcolor is a pointer to the color set to use. If NULL, then the default
115          * color set (based on the theme colors for 3d-view) is used. 
116          */
117         if (color_index > 0) {
118                 bTheme *btheme = UI_GetTheme();
119                 bcolor = &btheme->tarm[(color_index - 1)];
120         }
121         else if (color_index == -1) {
122                 /* use the group's own custom color set */
123                 bcolor = (grp) ? &grp->cs : NULL;
124         }
125         else 
126                 bcolor = NULL;
127 }
128
129 /* This function is for brightening/darkening a given color (like UI_ThemeColorShade()) */
130 static void cp_shade_color3ub(unsigned char cp[3], const int offset)
131 {
132         int r, g, b;
133         
134         r = offset + (int) cp[0];
135         CLAMP(r, 0, 255);
136         g = offset + (int) cp[1];
137         CLAMP(g, 0, 255);
138         b = offset + (int) cp[2];
139         CLAMP(b, 0, 255);
140         
141         cp[0] = r;
142         cp[1] = g;
143         cp[2] = b;
144 }
145
146 /* This function sets the gl-color for coloring a certain bone (based on bcolor) */
147 static 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                                         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 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 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 unsigned int bone_octahedral_wire_sides[8] = {0, 1, 5, 3, 0, 4, 5, 2};
454 static unsigned int bone_octahedral_wire_square[8] = {1, 2, 3, 4, 1};
455
456 static 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 float bone_octahedral_solid_normals[8][3] = {
470         { 0.70710683f, -0.70710683f,  0.00000000f},
471         {-0.00000000f, -0.70710683f, -0.70710683f},
472         {-0.70710683f, -0.70710683f,  0.00000000f},
473         { 0.00000000f, -0.70710683f,  0.70710683f},
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                         glLoadName(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                 glLoadName(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 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 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                         glLoadName(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                 glLoadName(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                         glLoadName(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                 glEnable(GL_COLOR_MATERIAL);
889                 glEnable(GL_LIGHTING);
890                 
891                 gluQuadricDrawStyle(qobj, GLU_FILL); 
892                 glShadeModel(GL_SMOOTH);
893         }
894         else {
895                 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
896         }
897         
898         /* sphere root color */
899         if (armflag & ARM_EDITMODE) {
900                 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
901                 else UI_ThemeColorShade(TH_BONE_SOLID, -30);
902         }
903         else if (armflag & ARM_POSEMODE)
904                 set_pchan_glColor(PCHAN_COLOR_SPHEREBONE_END, boneflag, constflag);
905         else if (dt == OB_SOLID)
906                 UI_ThemeColorShade(TH_BONE_SOLID, -30);
907         
908         /*      Draw root point if we are not connected */
909         if ((boneflag & BONE_CONNECTED) == 0) {
910                 if (id != -1)
911                         glLoadName(id | BONESEL_ROOT);
912                 gluSphere(qobj, head, 16, 10);
913         }
914         
915         /*      Draw tip point */
916         if (armflag & ARM_EDITMODE) {
917                 if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
918                 else UI_ThemeColorShade(TH_BONE_SOLID, -30);
919         }
920
921         if (id != -1)
922                 glLoadName(id | BONESEL_TIP);
923         
924         glTranslatef(0.0f, 0.0f, length);
925         gluSphere(qobj, tail, 16, 10);
926         glTranslatef(0.0f, 0.0f, -length);
927         
928         /* base */
929         if (armflag & ARM_EDITMODE) {
930                 if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_SELECT);
931                 else UI_ThemeColor(TH_BONE_SOLID);
932         }
933         else if (armflag & ARM_POSEMODE)
934                 set_pchan_glColor(PCHAN_COLOR_SPHEREBONE_BASE, boneflag, constflag);
935         else if (dt == OB_SOLID)
936                 UI_ThemeColor(TH_BONE_SOLID);
937         
938         fac1 = (length - head) / length;
939         fac2 = (length - tail) / length;
940         
941         if (length > (head + tail)) {
942                 if (id != -1)
943                         glLoadName(id | BONESEL_BONE);
944                 
945                 glEnable(GL_POLYGON_OFFSET_FILL);
946                 glPolygonOffset(-1.0f, -1.0f);
947                 
948                 glTranslatef(0.0f, 0.0f, head);
949                 gluCylinder(qobj, fac1 * head + (1.0f - fac1) * tail, fac2 * tail + (1.0f - fac2) * head, length - head - tail, 16, 1);
950                 glTranslatef(0.0f, 0.0f, -head);
951                 
952                 glDisable(GL_POLYGON_OFFSET_FILL);
953                 
954                 /* draw sphere on extrema */
955                 glTranslatef(0.0f, 0.0f, length - tail);
956                 gluSphere(qobj, fac2 * tail + (1.0f - fac2) * head, 16, 10);
957                 glTranslatef(0.0f, 0.0f, -length + tail);
958                 
959                 glTranslatef(0.0f, 0.0f, head);
960                 gluSphere(qobj, fac1 * head + (1.0f - fac1) * tail, 16, 10);
961         }
962         else {
963                 /* 1 sphere in center */
964                 glTranslatef(0.0f, 0.0f, (head + length - tail) / 2.0f);
965                 gluSphere(qobj, fac1 * head + (1.0f - fac1) * tail, 16, 10);
966         }
967         
968         /* restore */
969         if (dt == OB_SOLID) {
970                 glShadeModel(GL_FLAT);
971                 glDisable(GL_LIGHTING);
972                 glDisable(GL_COLOR_MATERIAL);
973         }
974         
975         glPopMatrix();
976         gluDeleteQuadric(qobj);  
977 }
978
979 static GLubyte bm_dot6[] = {0x0, 0x18, 0x3C, 0x7E, 0x7E, 0x3C, 0x18, 0x0};
980 static GLubyte bm_dot8[] = {0x3C, 0x7E, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C};
981
982 static GLubyte bm_dot5[] = {0x0, 0x0, 0x10, 0x38, 0x7c, 0x38, 0x10, 0x0};
983 static GLubyte bm_dot7[] = {0x0, 0x38, 0x7C, 0xFE, 0xFE, 0xFE, 0x7C, 0x38};
984
985
986 static void draw_line_bone(int armflag, int boneflag, short constflag, unsigned int id,
987                            bPoseChannel *pchan, EditBone *ebone)
988 {
989         float length;
990         
991         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
992         
993         if (pchan) 
994                 length = pchan->bone->length;
995         else 
996                 length = ebone->length;
997         
998         glPushMatrix();
999         glScalef(length, length, length);
1000         
1001         /* this chunk not in object mode */
1002         if (armflag & (ARM_EDITMODE | ARM_POSEMODE)) {
1003                 glLineWidth(4.0f);
1004                 if (armflag & ARM_POSEMODE)
1005                         set_pchan_glColor(PCHAN_COLOR_NORMAL, boneflag, constflag);
1006                 else if (armflag & ARM_EDITMODE) {
1007                         UI_ThemeColor(TH_WIRE_EDIT);
1008                 }
1009                 
1010                 /*      Draw root point if we are not connected */
1011                 if ((boneflag & BONE_CONNECTED) == 0) {
1012                         if (G.f & G_PICKSEL) {  /* no bitmap in selection mode, crashes 3d cards... */
1013                                 glLoadName(id | BONESEL_ROOT);
1014                                 glBegin(GL_POINTS);
1015                                 glVertex3f(0.0f, 0.0f, 0.0f);
1016                                 glEnd();
1017                         }
1018                         else {
1019                                 glRasterPos3f(0.0f, 0.0f, 0.0f);
1020                                 glBitmap(8, 8, 4, 4, 0, 0, bm_dot8);
1021                         }
1022                 }
1023                 
1024                 if (id != -1)
1025                         glLoadName((GLuint) id | BONESEL_BONE);
1026                 
1027                 glBegin(GL_LINES);
1028                 glVertex3f(0.0f, 0.0f, 0.0f);
1029                 glVertex3f(0.0f, 1.0f, 0.0f);
1030                 glEnd();
1031                 
1032                 /* tip */
1033                 if (G.f & G_PICKSEL) {
1034                         /* no bitmap in selection mode, crashes 3d cards... */
1035                         glLoadName(id | BONESEL_TIP);
1036                         glBegin(GL_POINTS);
1037                         glVertex3f(0.0f, 1.0f, 0.0f);
1038                         glEnd();
1039                 }
1040                 else {
1041                         glRasterPos3f(0.0f, 1.0f, 0.0f);
1042                         glBitmap(8, 8, 4, 4, 0, 0, bm_dot7);
1043                 }
1044                 
1045                 /* further we send no names */
1046                 if (id != -1)
1047                         glLoadName(id & 0xFFFF);  /* object tag, for bordersel optim */
1048                 
1049                 if (armflag & ARM_POSEMODE)
1050                         set_pchan_glColor(PCHAN_COLOR_LINEBONE, boneflag, constflag);
1051         }
1052         
1053         glLineWidth(2.0);
1054         
1055         /*Draw root point if we are not connected */
1056         if ((boneflag & BONE_CONNECTED) == 0) {
1057                 if ((G.f & G_PICKSEL) == 0) {
1058                         /* no bitmap in selection mode, crashes 3d cards... */
1059                         if (armflag & ARM_EDITMODE) {
1060                                 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
1061                                 else UI_ThemeColor(TH_VERTEX);
1062                         }
1063                         glRasterPos3f(0.0f, 0.0f, 0.0f);
1064                         glBitmap(8, 8, 4, 4, 0, 0, bm_dot6);
1065                 }
1066         }
1067         
1068         if (armflag & ARM_EDITMODE) {
1069                 if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_EDGE_SELECT);
1070                 else UI_ThemeColorShade(TH_BACK, -30);
1071         }
1072         glBegin(GL_LINES);
1073         glVertex3f(0.0f, 0.0f, 0.0f);
1074         glVertex3f(0.0f, 1.0f, 0.0f);
1075         glEnd();
1076         
1077         /* tip */
1078         if ((G.f & G_PICKSEL) == 0) {
1079                 /* no bitmap in selection mode, crashes 3d cards... */
1080                 if (armflag & ARM_EDITMODE) {
1081                         if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
1082                         else UI_ThemeColor(TH_VERTEX);
1083                 }
1084                 glRasterPos3f(0.0f, 1.0f, 0.0f);
1085                 glBitmap(8, 8, 4, 4, 0, 0, bm_dot5);
1086         }
1087         
1088         glLineWidth(1.0);
1089         
1090         glPopMatrix();
1091 }
1092
1093 static void draw_b_bone_boxes(const short dt, bPoseChannel *pchan, float xwidth, float length, float zwidth)
1094 {
1095         int segments = 0;
1096         
1097         if (pchan) 
1098                 segments = pchan->bone->segments;
1099         
1100         if ((segments > 1) && (pchan)) {
1101                 float dlen = length / (float)segments;
1102                 Mat4 *bbone = b_bone_spline_setup(pchan, 0);
1103                 int a;
1104                 
1105                 for (a = 0; a < segments; a++, bbone++) {
1106                         glPushMatrix();
1107                         glMultMatrixf(bbone->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                 glLoadName((GLuint) id | BONESEL_BONE);
1164         }
1165         
1166         /* set up solid drawing */
1167         if (dt > OB_WIRE) {
1168                 glEnable(GL_COLOR_MATERIAL);
1169                 glEnable(GL_LIGHTING);
1170                 
1171                 if (armflag & ARM_POSEMODE)
1172                         set_pchan_glColor(PCHAN_COLOR_SOLID, boneflag, constflag);
1173                 else
1174                         UI_ThemeColor(TH_BONE_SOLID);
1175                 
1176                 draw_b_bone_boxes(OB_SOLID, pchan, xwidth, length, zwidth);
1177                 
1178                 /* disable solid drawing */
1179                 glDisable(GL_COLOR_MATERIAL);
1180                 glDisable(GL_LIGHTING);
1181         }
1182         else {
1183                 /* wire */
1184                 if (armflag & ARM_POSEMODE) {
1185                         if (constflag) {
1186                                 /* set constraint colors */
1187                                 if (set_pchan_glColor(PCHAN_COLOR_CONSTS, boneflag, constflag)) {
1188                                         glEnable(GL_BLEND);
1189                                         
1190                                         draw_b_bone_boxes(OB_SOLID, pchan, xwidth, length, zwidth);
1191                                         
1192                                         glDisable(GL_BLEND);
1193                                 }
1194                                 
1195                                 /* restore colors */
1196                                 set_pchan_glColor(PCHAN_COLOR_NORMAL, boneflag, constflag);
1197                         }
1198                 }
1199                 
1200                 draw_b_bone_boxes(OB_WIRE, pchan, xwidth, length, zwidth);
1201         }
1202 }
1203
1204 static void draw_wire_bone_segments(bPoseChannel *pchan, Mat4 *bbones, float length, int segments)
1205 {
1206         if ((segments > 1) && (pchan)) {
1207                 float dlen = length / (float)segments;
1208                 Mat4 *bbone = bbones;
1209                 int a;
1210                 
1211                 for (a = 0; a < segments; a++, bbone++) {
1212                         glPushMatrix();
1213                         glMultMatrixf(bbone->mat);
1214                         
1215                         glBegin(GL_LINES);
1216                         glVertex3f(0.0f, 0.0f, 0.0f);
1217                         glVertex3f(0.0f, dlen, 0.0f);
1218                         glEnd();  /* GL_LINES */
1219                         
1220                         glPopMatrix();
1221                 }
1222         }
1223         else {
1224                 glPushMatrix();
1225                 
1226                 glBegin(GL_LINES);
1227                 glVertex3f(0.0f, 0.0f, 0.0f);
1228                 glVertex3f(0.0f, length, 0.0f);
1229                 glEnd();
1230                 
1231                 glPopMatrix();
1232         }
1233 }
1234
1235 static void draw_wire_bone(const short dt, int armflag, int boneflag, short constflag, unsigned int id,
1236                            bPoseChannel *pchan, EditBone *ebone)
1237 {
1238         Mat4 *bbones = NULL;
1239         int segments = 0;
1240         float length;
1241         
1242         if (pchan) {
1243                 segments = pchan->bone->segments;
1244                 length = pchan->bone->length;
1245                 
1246                 if (segments > 1)
1247                         bbones = b_bone_spline_setup(pchan, 0);
1248         }
1249         else 
1250                 length = ebone->length;
1251         
1252         /* draw points only if... */
1253         if (armflag & ARM_EDITMODE) {
1254                 /* move to unitspace */
1255                 glPushMatrix();
1256                 glScalef(length, length, length);
1257                 draw_bone_points(dt, armflag, boneflag, id);
1258                 glPopMatrix();
1259                 length *= 0.95f;  /* make vertices visible */
1260         }
1261         
1262         /* this chunk not in object mode */
1263         if (armflag & (ARM_EDITMODE | ARM_POSEMODE)) {
1264                 if (id != -1)
1265                         glLoadName((GLuint) id | BONESEL_BONE);
1266                 
1267                 draw_wire_bone_segments(pchan, bbones, length, segments);
1268                 
1269                 /* further we send no names */
1270                 if (id != -1)
1271                         glLoadName(id & 0xFFFF);    /* object tag, for bordersel optim */
1272         }
1273         
1274         /* colors for modes */
1275         if (armflag & ARM_POSEMODE) {
1276                 set_pchan_glColor(PCHAN_COLOR_NORMAL, boneflag, constflag);
1277         }
1278         else if (armflag & ARM_EDITMODE) {
1279                 set_ebone_glColor(boneflag);
1280         }
1281         
1282         /* draw normal */
1283         draw_wire_bone_segments(pchan, bbones, length, segments);
1284 }
1285
1286 static void draw_bone(const short dt, int armflag, int boneflag, short constflag, unsigned int id, float length)
1287 {
1288         
1289         /* Draw a 3d octahedral bone, we use normalized space based on length,
1290          * for display-lists */
1291         
1292         glScalef(length, length, length);
1293
1294         /* set up solid drawing */
1295         if (dt > OB_WIRE) {
1296                 glEnable(GL_COLOR_MATERIAL);
1297                 glEnable(GL_LIGHTING);
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                 glLoadName((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                 glDisable(GL_COLOR_MATERIAL);
1352                 glDisable(GL_LIGHTING);
1353         }
1354 }
1355
1356 static void draw_custom_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob,
1357                              const short dt, int armflag, int boneflag, unsigned int id, float length)
1358 {
1359         if (ob == NULL) return;
1360         
1361         glScalef(length, length, length);
1362         
1363         /* colors for posemode */
1364         if (armflag & ARM_POSEMODE) {
1365                 set_pchan_glColor(PCHAN_COLOR_NORMAL, boneflag, 0);
1366         }
1367         
1368         if (id != -1) {
1369                 glLoadName((GLuint) id | BONESEL_BONE);
1370         }
1371         
1372         draw_object_instance(scene, v3d, rv3d, ob, dt, armflag & ARM_POSEMODE);
1373 }
1374
1375
1376 static void pchan_draw_IK_root_lines(bPoseChannel *pchan, short only_temp)
1377 {
1378         bConstraint *con;
1379         bPoseChannel *parchan;
1380         
1381         for (con = pchan->constraints.first; con; con = con->next) {
1382                 if (con->enforce == 0.0f)
1383                         continue;
1384                 
1385                 switch (con->type) {
1386                         case CONSTRAINT_TYPE_KINEMATIC:
1387                         {
1388                                 bKinematicConstraint *data = (bKinematicConstraint *)con->data;
1389                                 int segcount = 0;
1390                                 
1391                                 /* if only_temp, only draw if it is a temporary ik-chain */
1392                                 if ((only_temp) && !(data->flag & CONSTRAINT_IK_TEMP))
1393                                         continue;
1394                                 
1395                                 setlinestyle(3);
1396                                 glBegin(GL_LINES);
1397                                 
1398                                 /* exclude tip from chain? */
1399                                 if ((data->flag & CONSTRAINT_IK_TIP) == 0)
1400                                         parchan = pchan->parent;
1401                                 else
1402                                         parchan = pchan;
1403                                 
1404                                 glVertex3fv(parchan->pose_tail);
1405                                 
1406                                 /* Find the chain's root */
1407                                 while (parchan->parent) {
1408                                         segcount++;
1409                                         if (segcount == data->rootbone || segcount > 255) {
1410                                                 break;  /* 255 is weak */
1411                                         }
1412                                         parchan = parchan->parent;
1413                                 }
1414                                 if (parchan)
1415                                         glVertex3fv(parchan->pose_head);
1416                                 
1417                                 glEnd();
1418                                 setlinestyle(0);
1419                                 break;
1420                         }
1421                         case CONSTRAINT_TYPE_SPLINEIK: 
1422                         {
1423                                 bSplineIKConstraint *data = (bSplineIKConstraint *)con->data;
1424                                 int segcount = 0;
1425                                 
1426                                 setlinestyle(3);
1427                                 glBegin(GL_LINES);
1428                                 
1429                                 parchan = pchan;
1430                                 glVertex3fv(parchan->pose_tail);
1431                                 
1432                                 /* Find the chain's root */
1433                                 while (parchan->parent) {
1434                                         segcount++;
1435                                         /* FIXME: revise the breaking conditions */
1436                                         if (segcount == data->chainlen || segcount > 255) break;  /* 255 is weak */
1437                                         parchan = parchan->parent;
1438                                 }
1439                                 if (parchan)  /* XXX revise the breaking conditions to only stop at the tail? */
1440                                         glVertex3fv(parchan->pose_head);
1441
1442                                 glEnd();
1443                                 setlinestyle(0);
1444                                 break;
1445                         }
1446                 }
1447         }
1448 }
1449
1450 static void bgl_sphere_project(float ax, float az)
1451 {
1452         float dir[3], sine, q3;
1453
1454         sine = 1.0f - ax * ax - az * az;
1455         q3 = (sine < 0.0f) ? 0.0f : (float)(2.0 * sqrt(sine));
1456
1457         dir[0] = -az * q3;
1458         dir[1] = 1.0f - 2.0f * sine;
1459         dir[2] = ax * q3;
1460
1461         glVertex3fv(dir);
1462 }
1463
1464 static void draw_dof_ellipse(float ax, float az)
1465 {
1466         static float staticSine[16] = {
1467                 0.0f, 0.104528463268f, 0.207911690818f, 0.309016994375f,
1468                 0.406736643076f, 0.5f, 0.587785252292f, 0.669130606359f,
1469                 0.743144825477f, 0.809016994375f, 0.866025403784f,
1470                 0.913545457643f, 0.951056516295f, 0.978147600734f,
1471                 0.994521895368f, 1.0f
1472         };
1473
1474         int i, j, n = 16;
1475         float x, z, px, pz;
1476
1477         glEnable(GL_BLEND);
1478         glDepthMask(0);
1479
1480         glColor4ub(70, 70, 70, 50);
1481
1482         glBegin(GL_QUADS);
1483         pz = 0.0f;
1484         for (i = 1; i < n; i++) {
1485                 z = staticSine[i];
1486                 
1487                 px = 0.0f;
1488                 for (j = 1; j < n - i + 1; j++) {
1489                         x = staticSine[j];
1490                         
1491                         if (j == n - i) {
1492                                 glEnd();
1493                                 glBegin(GL_TRIANGLES);
1494                                 bgl_sphere_project(ax * px, az * z);
1495                                 bgl_sphere_project(ax * px, az * pz);
1496                                 bgl_sphere_project(ax * x, az * pz);
1497                                 glEnd();
1498                                 glBegin(GL_QUADS);
1499                         }
1500                         else {
1501                                 bgl_sphere_project(ax * x, az * z);
1502                                 bgl_sphere_project(ax * x, az * pz);
1503                                 bgl_sphere_project(ax * px, az * pz);
1504                                 bgl_sphere_project(ax * px, az * z);
1505                         }
1506                         
1507                         px = x;
1508                 }
1509                 pz = z;
1510         }
1511         glEnd();
1512
1513         glDisable(GL_BLEND);
1514         glDepthMask(1);
1515
1516         glColor3ub(0, 0, 0);
1517
1518         glBegin(GL_LINE_STRIP);
1519         for (i = 0; i < n; i++)
1520                 bgl_sphere_project(staticSine[n - i - 1] * ax, staticSine[i] * az);
1521         glEnd();
1522 }
1523
1524 static void draw_pose_dofs(Object *ob)
1525 {
1526         bArmature *arm = ob->data;
1527         bPoseChannel *pchan;
1528         Bone *bone;
1529         
1530         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1531                 bone = pchan->bone;
1532                 
1533                 if ((bone != NULL) && !(bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
1534                         if (bone->flag & BONE_SELECTED) {
1535                                 if (bone->layer & arm->layer) {
1536                                         if (pchan->ikflag & (BONE_IK_XLIMIT | BONE_IK_ZLIMIT)) {
1537                                                 if (ED_pose_channel_in_IK_chain(ob, pchan)) {
1538                                                         float corner[4][3], posetrans[3], mat[4][4];
1539                                                         float phi = 0.0f, theta = 0.0f, scale;
1540                                                         int a, i;
1541                                                         
1542                                                         /* in parent-bone pose, but own restspace */
1543                                                         glPushMatrix();
1544                                                         
1545                                                         copy_v3_v3(posetrans, pchan->pose_mat[3]);
1546                                                         glTranslatef(posetrans[0], posetrans[1], posetrans[2]);
1547                                                         
1548                                                         if (pchan->parent) {
1549                                                                 copy_m4_m4(mat, pchan->parent->pose_mat);
1550                                                                 mat[3][0] = mat[3][1] = mat[3][2] = 0.0f;
1551                                                                 glMultMatrixf(mat);
1552                                                         }
1553                                                         
1554                                                         copy_m4_m3(mat, pchan->bone->bone_mat);
1555                                                         glMultMatrixf(mat);
1556                                                         
1557                                                         scale = bone->length * pchan->size[1];
1558                                                         glScalef(scale, scale, scale);
1559                                                         
1560                                                         if (pchan->ikflag & BONE_IK_XLIMIT) {
1561                                                                 if (pchan->ikflag & BONE_IK_ZLIMIT) {
1562                                                                         float amin[3], amax[3];
1563                                                                         
1564                                                                         for (i = 0; i < 3; i++) {
1565                                                                                 /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1566                                                                                 amin[i] = sinf(pchan->limitmin[i] * 0.5f);
1567                                                                                 amax[i] = sinf(pchan->limitmax[i] * 0.5f);
1568                                                                         }
1569                                                                         
1570                                                                         glScalef(1.0f, -1.0f, 1.0f);
1571                                                                         if ((amin[0] != 0.0f) && (amin[2] != 0.0f))
1572                                                                                 draw_dof_ellipse(amin[0], amin[2]);
1573                                                                         if ((amin[0] != 0.0f) && (amax[2] != 0.0f))
1574                                                                                 draw_dof_ellipse(amin[0], amax[2]);
1575                                                                         if ((amax[0] != 0.0f) && (amin[2] != 0.0f))
1576                                                                                 draw_dof_ellipse(amax[0], amin[2]);
1577                                                                         if ((amax[0] != 0.0f) && (amax[2] != 0.0f))
1578                                                                                 draw_dof_ellipse(amax[0], amax[2]);
1579                                                                         glScalef(1.0f, -1.0f, 1.0f);
1580                                                                 }
1581                                                         }
1582                                                         
1583                                                         /* arcs */
1584                                                         if (pchan->ikflag & BONE_IK_ZLIMIT) {
1585                                                                 /* OpenGL requires rotations in degrees; so we're taking the average angle here */
1586                                                                 theta = RAD2DEGF(0.5f * (pchan->limitmin[2] + pchan->limitmax[2]));
1587                                                                 glRotatef(theta, 0.0f, 0.0f, 1.0f);
1588                                                                 
1589                                                                 glColor3ub(50, 50, 255);  /* blue, Z axis limit */
1590                                                                 glBegin(GL_LINE_STRIP);
1591                                                                 for (a = -16; a <= 16; a++) {
1592                                                                         /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1593                                                                         float fac = ((float)a) / 16.0f * 0.5f;
1594                                                                         
1595                                                                         phi = fac * (pchan->limitmax[2] - pchan->limitmin[2]);
1596                                                                         
1597                                                                         i = (a == -16) ? 0 : 1;
1598                                                                         corner[i][0] = sinf(phi);
1599                                                                         corner[i][1] = cosf(phi);
1600                                                                         corner[i][2] = 0.0f;
1601                                                                         glVertex3fv(corner[i]);
1602                                                                 }
1603                                                                 glEnd();
1604                                                                 
1605                                                                 glRotatef(-theta, 0.0f, 0.0f, 1.0f);
1606                                                         }
1607                                                         
1608                                                         if (pchan->ikflag & BONE_IK_XLIMIT) {
1609                                                                 /* OpenGL requires rotations in degrees; so we're taking the average angle here */
1610                                                                 theta = RAD2DEGF(0.5f * (pchan->limitmin[0] + pchan->limitmax[0]));
1611                                                                 glRotatef(theta, 1.0f, 0.0f, 0.0f);
1612                                                                 
1613                                                                 glColor3ub(255, 50, 50);  /* Red, X axis limit */
1614                                                                 glBegin(GL_LINE_STRIP);
1615                                                                 for (a = -16; a <= 16; a++) {
1616                                                                         /* *0.5f here comes from M_PI/360.0f when rotations were still in degrees */
1617                                                                         float fac = ((float)a) / 16.0f * 0.5f;
1618                                                                         phi = (float)(0.5 * M_PI) + fac * (pchan->limitmax[0] - pchan->limitmin[0]);
1619                                                                         
1620                                                                         i = (a == -16) ? 2 : 3;
1621                                                                         corner[i][0] = 0.0f;
1622                                                                         corner[i][1] = sinf(phi);
1623                                                                         corner[i][2] = cosf(phi);
1624                                                                         glVertex3fv(corner[i]);
1625                                                                 }
1626                                                                 glEnd();
1627                                                                 
1628                                                                 glRotatef(-theta, 1.0f, 0.0f, 0.0f);
1629                                                         }
1630                                                         
1631                                                         /* out of cone, out of bone */
1632                                                         glPopMatrix(); 
1633                                                 }
1634                                         }
1635                                 }
1636                         }
1637                 }
1638         }
1639 }
1640
1641 static void bone_matrix_translate_y(float mat[4][4], float y)
1642 {
1643         float trans[3];
1644
1645         copy_v3_v3(trans, mat[1]);
1646         mul_v3_fl(trans, y);
1647         add_v3_v3(mat[3], trans);
1648 }
1649
1650 /* assumes object is Armature with pose */
1651 static void draw_pose_bones(Scene *scene, View3D *v3d, ARegion *ar, Base *base,
1652                             const short dt, const unsigned char ob_wire_col[4],
1653                             const bool do_const_color, const bool is_outline)
1654 {
1655         RegionView3D *rv3d = ar->regiondata;
1656         Object *ob = base->object;
1657         bArmature *arm = ob->data;
1658         bPoseChannel *pchan;
1659         Bone *bone;
1660         GLfloat tmp;
1661         float smat[4][4], imat[4][4], bmat[4][4];
1662         int index = -1;
1663         short do_dashed = 3;
1664         bool draw_wire = false;
1665         int flag;
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         glEnable(GL_CULL_FACE);
1715         
1716         /* if solid we draw that first, with selection codes, but without names, axes etc */
1717         if (dt > OB_WIRE) {
1718                 if (arm->flag & ARM_POSEMODE) 
1719                         index = base->selcol;
1720                 
1721                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1722                         bone = pchan->bone;
1723                         arm->layer_used |= bone->layer;
1724                         
1725                         /* 1) bone must be visible, 2) for OpenGL select-drawing cannot have unselectable [#27194] */
1726                         if (((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) &&
1727                             ((G.f & G_PICKSEL) == 0 || (bone->flag & BONE_UNSELECTABLE) == 0))
1728                         {
1729                                 if (bone->layer & arm->layer) {
1730                                         const bool use_custom = (pchan->custom) && !(arm->flag & ARM_NO_CUSTOM);
1731                                         glPushMatrix();
1732                                         
1733                                         if (use_custom && pchan->custom_tx) {
1734                                                 glMultMatrixf(pchan->custom_tx->pose_mat);
1735                                         }
1736                                         else {
1737                                                 glMultMatrixf(pchan->pose_mat);
1738                                         }
1739                                         
1740                                         /* catch exception for bone with hidden parent */
1741                                         flag = bone->flag;
1742                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG))) {
1743                                                 flag &= ~BONE_CONNECTED;
1744                                         }
1745                                         
1746                                         /* set temporary flag for drawing bone as active, but only if selected */
1747                                         if (bone == arm->act_bone)
1748                                                 flag |= BONE_DRAW_ACTIVE;
1749                                         
1750                                         if (do_const_color) {
1751                                                 /* keep color */
1752                                         }
1753                                         else {
1754                                                 /* set color-set to use */
1755                                                 set_pchan_colorset(ob, pchan);
1756                                         }
1757                                         
1758                                         if (use_custom) {
1759                                                 /* if drawwire, don't try to draw in solid */
1760                                                 if (pchan->bone->flag & BONE_DRAWWIRE) {
1761                                                         draw_wire = true;
1762                                                 }
1763                                                 else {
1764                                                         draw_custom_bone(scene, v3d, rv3d, pchan->custom,
1765                                                                          OB_SOLID, arm->flag, flag, index, bone->length);
1766                                                 }
1767                                         }
1768                                         else if (arm->drawtype == ARM_LINE) {
1769                                                 /* nothing in solid */
1770                                         }
1771                                         else if (arm->drawtype == ARM_WIRE) {
1772                                                 /* nothing in solid */
1773                                         }
1774                                         else if (arm->drawtype == ARM_ENVELOPE) {
1775                                                 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
1776                                         }
1777                                         else if (arm->drawtype == ARM_B_BONE) {
1778                                                 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
1779                                         }
1780                                         else {
1781                                                 draw_bone(OB_SOLID, arm->flag, flag, 0, index, bone->length);
1782                                         }
1783                                                 
1784                                         glPopMatrix();
1785                                 }
1786                         }
1787                         
1788                         if (index != -1)
1789                                 index += 0x10000;  /* pose bones count in higher 2 bytes only */
1790                 }
1791                 
1792                 /* very very confusing... but in object mode, solid draw, we cannot do glLoadName yet,
1793                  * stick bones and/or wire custom-shapes are drawn in next loop 
1794                  */
1795                 if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE) == 0 && (draw_wire == false)) {
1796                         /* object tag, for bordersel optim */
1797                         glLoadName(index & 0xFFFF);
1798                         index = -1;
1799                 }
1800         }
1801         
1802         /* draw custom bone shapes as wireframes */
1803         if (!(arm->flag & ARM_NO_CUSTOM) &&
1804             (draw_wire || (dt <= OB_WIRE)) )
1805         {
1806                 if (arm->flag & ARM_POSEMODE)
1807                         index = base->selcol;
1808                         
1809                 /* only draw custom bone shapes that need to be drawn as wires */
1810                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1811                         bone = pchan->bone;
1812                         
1813                         /* 1) bone must be visible, 2) for OpenGL select-drawing cannot have unselectable [#27194] */
1814                         if (((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) &&
1815                             ((G.f & G_PICKSEL) == 0 || (bone->flag & BONE_UNSELECTABLE) == 0) )
1816                         {
1817                                 if (bone->layer & arm->layer) {
1818                                         if (pchan->custom) {
1819                                                 if ((dt < OB_SOLID) || (bone->flag & BONE_DRAWWIRE)) {
1820                                                         glPushMatrix();
1821                                                         
1822                                                         if (pchan->custom_tx) {
1823                                                                 glMultMatrixf(pchan->custom_tx->pose_mat);
1824                                                         }
1825                                                         else {
1826                                                                 glMultMatrixf(pchan->pose_mat);
1827                                                         }
1828                                                         
1829                                                         /* prepare colors */
1830                                                         if (do_const_color) {
1831                                                                 /* 13 October 2009, Disabled this to make ghosting show the right colors (Aligorith) */
1832                                                         }
1833                                                         else if (arm->flag & ARM_POSEMODE)
1834                                                                 set_pchan_colorset(ob, pchan);
1835                                                         else {
1836                                                                 glColor3ubv(ob_wire_col);
1837                                                         }
1838                                                                 
1839                                                         /* catch exception for bone with hidden parent */
1840                                                         flag = bone->flag;
1841                                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))
1842                                                                 flag &= ~BONE_CONNECTED;
1843                                                                 
1844                                                         /* set temporary flag for drawing bone as active, but only if selected */
1845                                                         if (bone == arm->act_bone)
1846                                                                 flag |= BONE_DRAW_ACTIVE;
1847                                                         
1848                                                         draw_custom_bone(scene, v3d, rv3d, pchan->custom,
1849                                                                          OB_WIRE, arm->flag, flag, index, bone->length);
1850                                                         
1851                                                         glPopMatrix();
1852                                                 }
1853                                         }
1854                                 }
1855                         }
1856                         
1857                         if (index != -1) 
1858                                 index += 0x10000;  /* pose bones count in higher 2 bytes only */
1859                 }
1860                 /* stick or wire bones have not been drawn yet so don't clear object selection in this case */
1861                 if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE) == 0 && draw_wire) {
1862                         /* object tag, for bordersel optim */
1863                         glLoadName(index & 0xFFFF);
1864                         index = -1;
1865                 }
1866         }
1867         
1868         /* wire draw over solid only in posemode */
1869         if ((dt <= OB_WIRE) || (arm->flag & ARM_POSEMODE) || ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
1870                 /* draw line check first. we do selection indices */
1871                 if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
1872                         if (arm->flag & ARM_POSEMODE) 
1873                                 index = base->selcol;
1874                 }
1875                 /* if solid && posemode, we draw again with polygonoffset */
1876                 else if ((dt > OB_WIRE) && (arm->flag & ARM_POSEMODE)) {
1877                         bglPolygonOffset(rv3d->dist, 1.0);
1878                 }
1879                 else {
1880                         /* and we use selection indices if not done yet */
1881                         if (arm->flag & ARM_POSEMODE) 
1882                                 index = base->selcol;
1883                 }
1884                 
1885                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1886                         bone = pchan->bone;
1887                         arm->layer_used |= bone->layer;
1888                         
1889                         /* 1) bone must be visible, 2) for OpenGL select-drawing cannot have unselectable [#27194] */
1890                         if (((bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) &&
1891                             ((G.f & G_PICKSEL) == 0 || (bone->flag & BONE_UNSELECTABLE) == 0))
1892                         {
1893                                 if (bone->layer & arm->layer) {
1894                                         const short constflag = pchan->constflag;
1895                                         if ((do_dashed & 1) && (pchan->parent)) {
1896                                                 /* Draw a line from our root to the parent's tip 
1897                                                  * - only if V3D_HIDE_HELPLINES is enabled...
1898                                                  */
1899                                                 if ((do_dashed & 2) && ((bone->flag & BONE_CONNECTED) == 0)) {
1900                                                         if (arm->flag & ARM_POSEMODE) {
1901                                                                 glLoadName(index & 0xFFFF);  /* object tag, for bordersel optim */
1902                                                                 UI_ThemeColor(TH_WIRE);
1903                                                         }
1904                                                         setlinestyle(3);
1905                                                         glBegin(GL_LINES);
1906                                                         glVertex3fv(pchan->pose_head);
1907                                                         glVertex3fv(pchan->parent->pose_tail);
1908                                                         glEnd();
1909                                                         setlinestyle(0);
1910                                                 }
1911                                                 
1912                                                 /* Draw a line to IK root bone 
1913                                                  *  - only if temporary chain (i.e. "autoik")
1914                                                  */
1915                                                 if (arm->flag & ARM_POSEMODE) {
1916                                                         if (constflag & PCHAN_HAS_IK) {
1917                                                                 if (bone->flag & BONE_SELECTED) {
1918                                                                         if (constflag & PCHAN_HAS_TARGET) glColor3ub(200, 120, 0);
1919                                                                         else glColor3ub(200, 200, 50);  /* add theme! */
1920
1921                                                                         glLoadName(index & 0xFFFF);
1922                                                                         pchan_draw_IK_root_lines(pchan, !(do_dashed & 2));
1923                                                                 }
1924                                                         }
1925                                                         else if (constflag & PCHAN_HAS_SPLINEIK) {
1926                                                                 if (bone->flag & BONE_SELECTED) {
1927                                                                         glColor3ub(150, 200, 50);  /* add theme! */
1928                                                                         
1929                                                                         glLoadName(index & 0xFFFF);
1930                                                                         pchan_draw_IK_root_lines(pchan, !(do_dashed & 2));
1931                                                                 }
1932                                                         }
1933                                                 }
1934                                         }
1935                                         
1936                                         glPushMatrix();
1937                                         if (arm->drawtype != ARM_ENVELOPE)
1938                                                 glMultMatrixf(pchan->pose_mat);
1939                                         
1940                                         /* catch exception for bone with hidden parent */
1941                                         flag = bone->flag;
1942                                         if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)))
1943                                                 flag &= ~BONE_CONNECTED;
1944                                         
1945                                         /* set temporary flag for drawing bone as active, but only if selected */
1946                                         if (bone == arm->act_bone)
1947                                                 flag |= BONE_DRAW_ACTIVE;
1948                                         
1949                                         /* extra draw service for pose mode */
1950
1951                                         /* set color-set to use */
1952                                         if (do_const_color) {
1953                                                 /* keep color */
1954                                         }
1955                                         else {
1956                                                 set_pchan_colorset(ob, pchan);
1957                                         }
1958                                         
1959                                         if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM)) {
1960                                                 /* custom bone shapes should not be drawn here! */
1961                                         }
1962                                         else if (arm->drawtype == ARM_ENVELOPE) {
1963                                                 if (dt < OB_SOLID)
1964                                                         draw_sphere_bone_wire(smat, imat, arm->flag, flag, constflag, index, pchan, NULL);
1965                                         }
1966                                         else if (arm->drawtype == ARM_LINE)
1967                                                 draw_line_bone(arm->flag, flag, constflag, index, pchan, NULL);
1968                                         else if (arm->drawtype == ARM_WIRE)
1969                                                 draw_wire_bone(dt, arm->flag, flag, constflag, index, pchan, NULL);
1970                                         else if (arm->drawtype == ARM_B_BONE)
1971                                                 draw_b_bone(OB_WIRE, arm->flag, flag, constflag, index, pchan, NULL);
1972                                         else
1973                                                 draw_bone(OB_WIRE, arm->flag, flag, constflag, index, bone->length);
1974                                         
1975                                         glPopMatrix();
1976                                 }
1977                         }
1978                         
1979                         /* pose bones count in higher 2 bytes only */
1980                         if (index != -1) 
1981                                 index += 0x10000;
1982                 }
1983                 /* restore things */
1984                 if (!ELEM(arm->drawtype, ARM_WIRE, ARM_LINE) && (dt > OB_WIRE) && (arm->flag & ARM_POSEMODE))
1985                         bglPolygonOffset(rv3d->dist, 0.0);
1986         }
1987         
1988         /* restore */
1989         glDisable(GL_CULL_FACE);
1990         
1991         /* draw DoFs */
1992         if (arm->flag & ARM_POSEMODE) {
1993                 if (((base->flag & OB_FROMDUPLI) == 0)) {
1994                         draw_pose_dofs(ob);
1995                 }
1996         }
1997
1998         /* finally names and axes */
1999         if ((arm->flag & (ARM_DRAWNAMES | ARM_DRAWAXES)) &&
2000             (is_outline == 0) &&
2001             ((base->flag & OB_FROMDUPLI) == 0))
2002         {
2003                 /* patch for several 3d cards (IBM mostly) that crash on GL_SELECT with text drawing */
2004                 if ((G.f & G_PICKSEL) == 0) {
2005                         float vec[3];
2006
2007                         unsigned char col[4];
2008                         if (do_const_color) {
2009                                 /* so we can draw bone names in current const color */
2010                                 float tcol[4];
2011                                 glGetFloatv(GL_CURRENT_COLOR, tcol);
2012                                 rgb_float_to_uchar(col, tcol);
2013                                 col[3] = 255;
2014                         }
2015                         else {
2016                                 col[0] = ob_wire_col[0];
2017                                 col[1] = ob_wire_col[1];
2018                                 col[2] = ob_wire_col[2];
2019                                 col[3] = 255;
2020                         }
2021                         
2022                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2023                         
2024                         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2025                                 if ((pchan->bone->flag & (BONE_HIDDEN_P | BONE_HIDDEN_PG)) == 0) {
2026                                         if (pchan->bone->layer & arm->layer) {
2027                                                 if (arm->flag & (ARM_EDITMODE | ARM_POSEMODE)) {
2028                                                         bone = pchan->bone;
2029                                                         UI_GetThemeColor3ubv((bone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT, col);
2030                                                 }
2031                                                 else if (dt > OB_WIRE) {
2032                                                         UI_GetThemeColor3ubv(TH_TEXT, col);
2033                                                 }
2034                                                 
2035                                                 /*  Draw names of bone  */
2036                                                 if (arm->flag & ARM_DRAWNAMES) {
2037                                                         mid_v3_v3v3(vec, pchan->pose_head, pchan->pose_tail);
2038                                                         view3d_cached_text_draw_add(vec, pchan->name, 10, 0, col);
2039                                                 }
2040                                                 
2041                                                 /*      Draw additional axes on the bone tail  */
2042                                                 if ((arm->flag & ARM_DRAWAXES) && (arm->flag & ARM_POSEMODE)) {
2043                                                         glPushMatrix();
2044                                                         copy_m4_m4(bmat, pchan->pose_mat);
2045                                                         bone_matrix_translate_y(bmat, pchan->bone->length);
2046                                                         glMultMatrixf(bmat);
2047                                                         
2048                                                         glColor3ubv(col);
2049                                                         drawaxes(pchan->bone->length * 0.25f, OB_ARROWS);
2050                                                         
2051                                                         glPopMatrix();
2052                                                 }
2053                                         }
2054                                 }
2055                         }
2056                         
2057                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2058                 }
2059         }
2060 }
2061
2062 /* in editmode, we don't store the bone matrix... */
2063 static void get_matrix_editbone(EditBone *eBone, float bmat[4][4])
2064 {
2065         float delta[3];
2066         float mat[3][3];
2067         
2068         /* Compose the parent transforms (i.e. their translations) */
2069         sub_v3_v3v3(delta, eBone->tail, eBone->head);
2070         
2071         eBone->length = (float)sqrt(delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2]);
2072         
2073         vec_roll_to_mat3(delta, eBone->roll, mat);
2074         copy_m4_m3(bmat, mat);
2075
2076         add_v3_v3(bmat[3], eBone->head);
2077 }
2078
2079 static void draw_ebones(View3D *v3d, ARegion *ar, Object *ob, const short dt)
2080 {
2081         RegionView3D *rv3d = ar->regiondata;
2082         EditBone *eBone;
2083         bArmature *arm = ob->data;
2084         float smat[4][4], imat[4][4], bmat[4][4];
2085         unsigned int index;
2086         int flag;
2087         
2088         /* being set in code below */
2089         arm->layer_used = 0;
2090
2091         ED_view3d_check_mats_rv3d(rv3d);
2092
2093         /* envelope (deform distance) */
2094         if (arm->drawtype == ARM_ENVELOPE) {
2095                 /* precalc inverse matrix for drawing screen aligned */
2096                 copy_m4_m4(smat, rv3d->viewmatob);
2097                 mul_mat3_m4_fl(smat, 1.0f / len_v3(ob->obmat[0]));
2098                 invert_m4_m4(imat, smat);
2099                 
2100                 /* and draw blended distances */
2101                 glEnable(GL_BLEND);
2102                 //glShadeModel(GL_SMOOTH);
2103                 
2104                 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2105
2106                 for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
2107                         if (eBone->layer & arm->layer) {
2108                                 if ((eBone->flag & (BONE_HIDDEN_A | BONE_NO_DEFORM)) == 0) {
2109                                         if (eBone->flag & (BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL))
2110                                                 draw_sphere_bone_dist(smat, imat, NULL, eBone);
2111                                 }
2112                         }
2113                 }
2114                 
2115                 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2116                 glDisable(GL_BLEND);
2117                 //glShadeModel(GL_FLAT);
2118         }
2119         
2120         /* if solid we draw it first */
2121         if ((dt > OB_WIRE) && (arm->drawtype != ARM_LINE)) {
2122                 for (eBone = arm->edbo->first, index = 0; eBone; eBone = eBone->next, index++) {
2123                         if (eBone->layer & arm->layer) {
2124                                 if ((eBone->flag & BONE_HIDDEN_A) == 0) {
2125                                         glPushMatrix();
2126                                         get_matrix_editbone(eBone, bmat);
2127                                         glMultMatrixf(bmat);
2128                                         
2129                                         /* catch exception for bone with hidden parent */
2130                                         flag = eBone->flag;
2131                                         if ((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent)) {
2132                                                 flag &= ~BONE_CONNECTED;
2133                                         }
2134                                                 
2135                                         /* set temporary flag for drawing bone as active, but only if selected */
2136                                         if (eBone == arm->act_edbone)
2137                                                 flag |= BONE_DRAW_ACTIVE;
2138                                         
2139                                         if (arm->drawtype == ARM_ENVELOPE)
2140                                                 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
2141                                         else if (arm->drawtype == ARM_B_BONE)
2142                                                 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
2143                                         else if (arm->drawtype == ARM_WIRE)
2144                                                 draw_wire_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
2145                                         else {
2146                                                 draw_bone(OB_SOLID, arm->flag, flag, 0, index, eBone->length);
2147                                         }
2148                                         
2149                                         glPopMatrix();
2150                                 }
2151                         }
2152                 }
2153         }
2154         
2155         /* if wire over solid, set offset */
2156         index = -1;
2157         glLoadName(-1);
2158         if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
2159                 if (G.f & G_PICKSEL)
2160                         index = 0;
2161         }
2162         else if (dt > OB_WIRE) 
2163                 bglPolygonOffset(rv3d->dist, 1.0f);
2164         else if (arm->flag & ARM_EDITMODE) 
2165                 index = 0;  /* do selection codes */
2166         
2167         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
2168                 arm->layer_used |= eBone->layer;
2169                 if (eBone->layer & arm->layer) {
2170                         if ((eBone->flag & BONE_HIDDEN_A) == 0) {
2171                                 
2172                                 /* catch exception for bone with hidden parent */
2173                                 flag = eBone->flag;
2174                                 if ((eBone->parent) && !EBONE_VISIBLE(arm, eBone->parent)) {
2175                                         flag &= ~BONE_CONNECTED;
2176                                 }
2177                                         
2178                                 /* set temporary flag for drawing bone as active, but only if selected */
2179                                 if (eBone == arm->act_edbone)
2180                                         flag |= BONE_DRAW_ACTIVE;
2181                                 
2182                                 if (arm->drawtype == ARM_ENVELOPE) {
2183                                         if (dt < OB_SOLID)
2184                                                 draw_sphere_bone_wire(smat, imat, arm->flag, flag, 0, index, NULL, eBone);
2185                                 }
2186                                 else {
2187                                         glPushMatrix();
2188                                         get_matrix_editbone(eBone, bmat);
2189                                         glMultMatrixf(bmat);
2190                                         
2191                                         if (arm->drawtype == ARM_LINE) 
2192                                                 draw_line_bone(arm->flag, flag, 0, index, NULL, eBone);
2193                                         else if (arm->drawtype == ARM_WIRE)
2194                                                 draw_wire_bone(OB_WIRE, arm->flag, flag, 0, index, NULL, eBone);
2195                                         else if (arm->drawtype == ARM_B_BONE)
2196                                                 draw_b_bone(OB_WIRE, arm->flag, flag, 0, index, NULL, eBone);
2197                                         else
2198                                                 draw_bone(OB_WIRE, arm->flag, flag, 0, index, eBone->length);
2199                                         
2200                                         glPopMatrix();
2201                                 }
2202                                 
2203                                 /* offset to parent */
2204                                 if (eBone->parent) {
2205                                         UI_ThemeColor(TH_WIRE_EDIT);
2206                                         glLoadName(-1);  /* -1 here is OK! */
2207                                         setlinestyle(3);
2208                                         
2209                                         glBegin(GL_LINES);
2210                                         glVertex3fv(eBone->parent->tail);
2211                                         glVertex3fv(eBone->head);
2212                                         glEnd();
2213                                         
2214                                         setlinestyle(0);
2215                                 }
2216                         }
2217                 }
2218                 if (index != -1) index++;
2219         }
2220         
2221         /* restore */
2222         if (index != -1) {
2223                 glLoadName(-1);
2224         }
2225
2226         if (ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
2227                 /* pass */
2228         }
2229         else if (dt > OB_WIRE) {
2230                 bglPolygonOffset(rv3d->dist, 0.0f);
2231         }
2232         
2233         /* finally names and axes */
2234         if (arm->flag & (ARM_DRAWNAMES | ARM_DRAWAXES)) {
2235                 /* patch for several 3d cards (IBM mostly) that crash on GL_SELECT with text drawing */
2236                 if ((G.f & G_PICKSEL) == 0) {
2237                         float vec[3];
2238                         unsigned char col[4];
2239                         col[3] = 255;
2240                         
2241                         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2242                         
2243                         for (eBone = arm->edbo->first; eBone; eBone = eBone->next) {
2244                                 if (eBone->layer & arm->layer) {
2245                                         if ((eBone->flag & BONE_HIDDEN_A) == 0) {
2246
2247                                                 UI_GetThemeColor3ubv((eBone->flag & BONE_SELECTED) ? TH_TEXT_HI : TH_TEXT, col);
2248
2249                                                 /*      Draw name */
2250                                                 if (arm->flag & ARM_DRAWNAMES) {
2251                                                         mid_v3_v3v3(vec, eBone->head, eBone->tail);
2252                                                         glRasterPos3fv(vec);
2253                                                         view3d_cached_text_draw_add(vec, eBone->name, 10, 0, col);
2254                                                 }
2255                                                 /*      Draw additional axes */
2256                                                 if (arm->flag & ARM_DRAWAXES) {
2257                                                         glPushMatrix();
2258                                                         get_matrix_editbone(eBone, bmat);
2259                                                         bone_matrix_translate_y(bmat, eBone->length);
2260                                                         glMultMatrixf(bmat);
2261
2262                                                         glColor3ubv(col);
2263                                                         drawaxes(eBone->length * 0.25f, OB_ARROWS);
2264                                                         
2265                                                         glPopMatrix();
2266                                                 }
2267                                                 
2268                                         }
2269                                 }
2270                         }
2271                         
2272                         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2273                 }
2274         }
2275 }
2276
2277 /* ****************************** Armature Visualization ******************************** */
2278
2279 /* ---------- Paths --------- */
2280
2281 /* draw bone paths
2282  *      - in view space 
2283  */
2284 static void draw_pose_paths(Scene *scene, View3D *v3d, ARegion *ar, Object *ob)
2285 {
2286         bAnimVizSettings *avs = &ob->pose->avs;
2287         bArmature *arm = ob->data;
2288         bPoseChannel *pchan;
2289         
2290         /* setup drawing environment for paths */
2291         draw_motion_paths_init(v3d, ar);
2292         
2293         /* draw paths where they exist and they releated bone is visible */
2294         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2295                 if ((pchan->bone->layer & arm->layer) && (pchan->mpath))
2296                         draw_motion_path_instance(scene, ob, pchan, avs, pchan->mpath);
2297         }
2298         
2299         /* cleanup after drawing */
2300         draw_motion_paths_cleanup(v3d);
2301 }
2302
2303
2304 /* ---------- Ghosts --------- */
2305
2306 /* helper function for ghost drawing - sets/removes flags for temporarily 
2307  * hiding unselected bones while drawing ghosts
2308  */
2309 static void ghost_poses_tag_unselected(Object *ob, short unset)
2310 {
2311         bArmature *arm = ob->data;
2312         bPose *pose = ob->pose;
2313         bPoseChannel *pchan;
2314         
2315         /* don't do anything if no hiding any bones */
2316         if ((arm->flag & ARM_GHOST_ONLYSEL) == 0)
2317                 return;
2318                 
2319         /* loop over all pchans, adding/removing tags as appropriate */
2320         for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
2321                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
2322                         if (unset) {
2323                                 /* remove tags from all pchans if cleaning up */
2324                                 pchan->bone->flag &= ~BONE_HIDDEN_PG;
2325                         }
2326                         else {
2327                                 /* set tags on unselected pchans only */
2328                                 if ((pchan->bone->flag & BONE_SELECTED) == 0)
2329                                         pchan->bone->flag |= BONE_HIDDEN_PG;
2330                         }
2331                 }
2332         }
2333 }
2334
2335 /* draw ghosts that occur within a frame range 
2336  *  note: object should be in posemode
2337  */
2338 static void draw_ghost_poses_range(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2339 {
2340         Object *ob = base->object;
2341         AnimData *adt = BKE_animdata_from_id(&ob->id);
2342         bArmature *arm = ob->data;
2343         bPose *posen, *poseo;
2344         float start, end, stepsize, range, colfac;
2345         int cfrao, flago, ipoflago;
2346         
2347         start = (float)arm->ghostsf;
2348         end = (float)arm->ghostef;
2349         if (end <= start)
2350                 return;
2351         
2352         stepsize = (float)(arm->ghostsize);
2353         range = (float)(end - start);
2354         
2355         /* store values */
2356         ob->mode &= ~OB_MODE_POSE;
2357         cfrao = CFRA;
2358         flago = arm->flag;
2359         arm->flag &= ~(ARM_DRAWNAMES | ARM_DRAWAXES);
2360         ipoflago = ob->ipoflag;
2361         ob->ipoflag |= OB_DISABLE_PATH;
2362         
2363         /* copy the pose */
2364         poseo = ob->pose;
2365         BKE_pose_copy_data(&posen, ob->pose, 1);
2366         ob->pose = posen;
2367         BKE_pose_rebuild(ob, ob->data);    /* child pointers for IK */
2368         ghost_poses_tag_unselected(ob, 0);      /* hide unselected bones if need be */
2369         
2370         glEnable(GL_BLEND);
2371         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2372         
2373         /* draw from first frame of range to last */
2374         for (CFRA = (int)start; CFRA < end; CFRA += (int)stepsize) {
2375                 colfac = (end - (float)CFRA) / range;
2376                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128 - (int)(120.0 * sqrt(colfac)));
2377                 
2378                 BKE_animsys_evaluate_animdata(scene, &ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2379                 BKE_pose_where_is(scene, ob);
2380                 draw_pose_bones(scene, v3d, ar, base, OB_WIRE, NULL, true, false);
2381         }
2382         glDisable(GL_BLEND);
2383         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2384         
2385         /* before disposing of temp pose, use it to restore object to a sane state */
2386         BKE_animsys_evaluate_animdata(scene, &ob->id, adt, (float)cfrao, ADT_RECALC_ALL);
2387         
2388         /* clean up temporary pose */
2389         ghost_poses_tag_unselected(ob, 1);      /* unhide unselected bones if need be */
2390         BKE_pose_free(posen);
2391         
2392         /* restore */
2393         CFRA = cfrao;
2394         ob->pose = poseo;
2395         arm->flag = flago;
2396         ob->mode |= OB_MODE_POSE;
2397         ob->ipoflag = ipoflago;
2398 }
2399
2400 /* draw ghosts on keyframes in action within range 
2401  *      - object should be in posemode 
2402  */
2403 static void draw_ghost_poses_keys(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2404 {
2405         Object *ob = base->object;
2406         AnimData *adt = BKE_animdata_from_id(&ob->id);
2407         bAction *act = (adt) ? adt->action : NULL;
2408         bArmature *arm = ob->data;
2409         bPose *posen, *poseo;
2410         DLRBT_Tree keys;
2411         ActKeyColumn *ak, *akn;
2412         float start, end, range, colfac, i;
2413         int cfrao, flago;
2414         
2415         start = (float)arm->ghostsf;
2416         end = (float)arm->ghostef;
2417         if (end <= start)
2418                 return;
2419         
2420         /* get keyframes - then clip to only within range */
2421         BLI_dlrbTree_init(&keys);
2422         action_to_keylist(adt, act, &keys, NULL);
2423         BLI_dlrbTree_linkedlist_sync(&keys);
2424         
2425         range = 0;
2426         for (ak = keys.first; ak; ak = akn) {
2427                 akn = ak->next;
2428                 
2429                 if ((ak->cfra < start) || (ak->cfra > end))
2430                         BLI_freelinkN((ListBase *)&keys, ak);
2431                 else
2432                         range++;
2433         }
2434         if (range == 0) return;
2435         
2436         /* store values */
2437         ob->mode &= ~OB_MODE_POSE;
2438         cfrao = CFRA;
2439         flago = arm->flag;
2440         arm->flag &= ~(ARM_DRAWNAMES | ARM_DRAWAXES);
2441         ob->ipoflag |= OB_DISABLE_PATH;
2442         
2443         /* copy the pose */
2444         poseo = ob->pose;
2445         BKE_pose_copy_data(&posen, ob->pose, 1);
2446         ob->pose = posen;
2447         BKE_pose_rebuild(ob, ob->data);  /* child pointers for IK */
2448         ghost_poses_tag_unselected(ob, 0);    /* hide unselected bones if need be */
2449         
2450         glEnable(GL_BLEND);
2451         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2452         
2453         /* draw from first frame of range to last */
2454         for (ak = keys.first, i = 0; ak; ak = ak->next, i++) {
2455                 colfac = i / range;
2456                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128 - (int)(120.0 * sqrt(colfac)));
2457                 
2458                 CFRA = (int)ak->cfra;
2459                 
2460                 BKE_animsys_evaluate_animdata(scene, &ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2461                 BKE_pose_where_is(scene, ob);
2462                 draw_pose_bones(scene, v3d, ar, base, OB_WIRE, NULL, true, false);
2463         }
2464         glDisable(GL_BLEND);
2465         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2466         
2467         /* before disposing of temp pose, use it to restore object to a sane state */
2468         BKE_animsys_evaluate_animdata(scene, &ob->id, adt, (float)cfrao, ADT_RECALC_ALL);
2469         
2470         /* clean up temporary pose */
2471         ghost_poses_tag_unselected(ob, 1);  /* unhide unselected bones if need be */
2472         BLI_dlrbTree_free(&keys);
2473         BKE_pose_free(posen);
2474         
2475         /* restore */
2476         CFRA = cfrao;
2477         ob->pose = poseo;
2478         arm->flag = flago;
2479         ob->mode |= OB_MODE_POSE;
2480 }
2481
2482 /* draw ghosts around current frame
2483  *  - object is supposed to be armature in posemode
2484  */
2485 static void draw_ghost_poses(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2486 {
2487         Object *ob = base->object;
2488         AnimData *adt = BKE_animdata_from_id(&ob->id);
2489         bArmature *arm = ob->data;
2490         bPose *posen, *poseo;
2491         float cur, start, end, stepsize, range, colfac, actframe, ctime;
2492         int cfrao, flago;
2493         
2494         /* pre conditions, get an action with sufficient frames */
2495         if (ELEM(NULL, adt, adt->action))
2496                 return;
2497
2498         calc_action_range(adt->action, &start, &end, 0);
2499         if (start == end)
2500                 return;
2501
2502         stepsize = (float)(arm->ghostsize);
2503         range = (float)(arm->ghostep) * stepsize + 0.5f;   /* plus half to make the for loop end correct */
2504         
2505         /* store values */
2506         ob->mode &= ~OB_MODE_POSE;
2507         cfrao = CFRA;
2508         actframe = BKE_nla_tweakedit_remap(adt, (float)CFRA, 0);
2509         flago = arm->flag;
2510         arm->flag &= ~(ARM_DRAWNAMES | ARM_DRAWAXES);
2511         
2512         /* copy the pose */
2513         poseo = ob->pose;
2514         BKE_pose_copy_data(&posen, ob->pose, 1);
2515         ob->pose = posen;
2516         BKE_pose_rebuild(ob, ob->data);    /* child pointers for IK */
2517         ghost_poses_tag_unselected(ob, 0);      /* hide unselected bones if need be */
2518         
2519         glEnable(GL_BLEND);
2520         if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2521         
2522         /* draw from darkest blend to lowest */
2523         for (cur = stepsize; cur < range; cur += stepsize) {
2524                 ctime = cur - (float)fmod(cfrao, stepsize);  /* ensures consistent stepping */
2525                 colfac = ctime / range;
2526                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128 - (int)(120.0 * sqrt(colfac)));
2527                 
2528                 /* only within action range */
2529                 if (actframe + ctime >= start && actframe + ctime <= end) {
2530                         CFRA = (int)BKE_nla_tweakedit_remap(adt, actframe + ctime, NLATIME_CONVERT_MAP);
2531                         
2532                         if (CFRA != cfrao) {
2533                                 BKE_animsys_evaluate_animdata(scene, &ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2534                                 BKE_pose_where_is(scene, ob);
2535                                 draw_pose_bones(scene, v3d, ar, base, OB_WIRE, NULL, true, false);
2536                         }
2537                 }
2538                 
2539                 ctime = cur + (float)fmod((float)cfrao, stepsize) - stepsize + 1.0f;   /* ensures consistent stepping */
2540                 colfac = ctime / range;
2541                 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128 - (int)(120.0 * sqrt(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         glDisable(GL_BLEND);
2555         if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2556         
2557         /* before disposing of temp pose, use it to restore object to a sane state */
2558         BKE_animsys_evaluate_animdata(scene, &ob->id, adt, (float)cfrao, ADT_RECALC_ALL);
2559         
2560         /* clean up temporary pose */
2561         ghost_poses_tag_unselected(ob, 1);      /* unhide unselected bones if need be */
2562         BKE_pose_free(posen);
2563         
2564         /* restore */
2565         CFRA = cfrao;
2566         ob->pose = poseo;
2567         arm->flag = flago;
2568         ob->mode |= OB_MODE_POSE;
2569 }
2570
2571 /* ********************************** Armature Drawing - Main ************************* */
2572
2573 /* called from drawobject.c, return 1 if nothing was drawn
2574  * (ob_wire_col == NULL) when drawing ghost */
2575 bool draw_armature(Scene *scene, View3D *v3d, ARegion *ar, Base *base,
2576                    const short dt, const short dflag, const unsigned char ob_wire_col[4],
2577                    const bool is_outline)
2578 {
2579         Object *ob = base->object;
2580         bArmature *arm = ob->data;
2581         bool retval = false;
2582
2583         if (v3d->flag2 & V3D_RENDER_OVERRIDE)
2584                 return true;
2585         
2586         if (dt > OB_WIRE && !ELEM(arm->drawtype, ARM_LINE, ARM_WIRE)) {
2587                 /* we use color for solid lighting */
2588                 const float white[4] = {1.0f, 1.0f, 1.0f, 1.0f};
2589                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
2590                 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
2591                 glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);  /* only for lighting... */
2592         }
2593         
2594         /* arm->flag is being used to detect mode... */
2595         /* editmode? */
2596         if (arm->edbo) {
2597                 arm->flag |= ARM_EDITMODE;
2598                 draw_ebones(v3d, ar, ob, dt);
2599                 arm->flag &= ~ARM_EDITMODE;
2600         }
2601         else {
2602                 /*      Draw Pose */
2603                 if (ob->pose && ob->pose->chanbase.first) {
2604                         /* drawing posemode selection indices or colors only in these cases */
2605                         if (!(base->flag & OB_FROMDUPLI)) {
2606                                 if (G.f & G_PICKSEL) {
2607 #if 0
2608                                         /* nifty but actually confusing to allow bone selection out of posemode */
2609                                         if (OBACT && (OBACT->mode & OB_MODE_WEIGHT_PAINT)) {
2610                                                 if (ob == modifiers_isDeformedByArmature(OBACT))
2611                                                         arm->flag |= ARM_POSEMODE;
2612                                         }
2613                                         else
2614 #endif
2615                                         if (ob->mode & OB_MODE_POSE) {
2616                                                 arm->flag |= ARM_POSEMODE;
2617                                         }
2618                                 }
2619                                 else if (ob->mode & OB_MODE_POSE) {
2620                                         if (arm->ghosttype == ARM_GHOST_RANGE) {
2621                                                 draw_ghost_poses_range(scene, v3d, ar, base);
2622                                         }
2623                                         else if (arm->ghosttype == ARM_GHOST_KEYS) {
2624                                                 draw_ghost_poses_keys(scene, v3d, ar, base);
2625                                         }
2626                                         else if (arm->ghosttype == ARM_GHOST_CUR) {
2627                                                 if (arm->ghostep)
2628                                                         draw_ghost_poses(scene, v3d, ar, base);
2629                                         }
2630                                         if ((dflag & DRAW_SCENESET) == 0) {
2631                                                 if (ob == OBACT)
2632                                                         arm->flag |= ARM_POSEMODE;
2633                                                 else if (OBACT && (OBACT->mode & OB_MODE_WEIGHT_PAINT)) {
2634                                                         if (ob == modifiers_isDeformedByArmature(OBACT))
2635                                                                 arm->flag |= ARM_POSEMODE;
2636                                                 }
2637                                                 draw_pose_paths(scene, v3d, ar, ob);
2638                                         }
2639                                 }
2640                         }
2641                         draw_pose_bones(scene, v3d, ar, base, dt, ob_wire_col, (dflag & DRAW_CONSTCOLOR), is_outline);
2642                         arm->flag &= ~ARM_POSEMODE; 
2643                         
2644                         if (ob->mode & OB_MODE_POSE)
2645                                 UI_ThemeColor(TH_WIRE);  /* restore, for extra draw stuff */
2646                 }
2647                 else {
2648                         retval = true;
2649                 }
2650         }
2651         /* restore */
2652         glFrontFace(GL_CCW);
2653
2654         return retval;
2655 }