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