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