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