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