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