4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
38 #include "MEM_guardedalloc.h"
40 #include "DNA_anim_types.h"
41 #include "DNA_action_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_constraint_types.h"
45 #include "DNA_nla_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_space_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_userdef_types.h"
53 #include "BLI_blenlib.h"
54 #include "BLI_arithb.h"
55 #include "BLI_dlrbTree.h"
57 #include "BKE_animsys.h"
58 #include "BKE_action.h"
59 #include "BKE_armature.h"
60 #include "BKE_constraint.h"
61 #include "BKE_context.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_global.h"
66 #include "BKE_modifier.h"
68 #include "BKE_object.h"
69 #include "BKE_utildefines.h"
72 #include "BIF_glutil.h"
74 #include "ED_armature.h"
75 #include "ED_keyframes_draw.h"
81 #include "UI_resources.h"
83 #include "view3d_intern.h"
86 /* *************** Armature Drawing - Coloring API ***************************** */
88 /* global here is reset before drawing each bone */
89 static ThemeWireColor *bcolor= NULL;
91 /* values of colCode for set_pchan_glcolor */
93 PCHAN_COLOR_NORMAL = 0, /* normal drawing */
94 PCHAN_COLOR_SOLID, /* specific case where "solid" color is needed */
95 PCHAN_COLOR_CONSTS, /* "constraint" colors (which may/may-not be suppressed) */
97 PCHAN_COLOR_SPHEREBONE_BASE, /* for the 'stick' of sphere (envelope) bones */
98 PCHAN_COLOR_SPHEREBONE_END, /* for the ends of sphere (envelope) bones */
99 PCHAN_COLOR_LINEBONE /* for the middle of line-bones */
102 /* This function sets the color-set for coloring a certain bone */
103 static void set_pchan_colorset (Object *ob, bPoseChannel *pchan)
105 bPose *pose= (ob) ? ob->pose : NULL;
106 bArmature *arm= (ob) ? ob->data : NULL;
107 bActionGroup *grp= NULL;
108 short color_index= 0;
111 if (ELEM4(NULL, ob, arm, pose, pchan)) {
116 /* only try to set custom color if enabled for armature */
117 if (arm->flag & ARM_COL_CUSTOM) {
118 /* currently, a bone can only use a custom color set if it's group (if it has one),
119 * has been set to use one
121 if (pchan->agrp_index) {
122 grp= (bActionGroup *)BLI_findlink(&pose->agroups, (pchan->agrp_index - 1));
124 color_index= grp->customCol;
128 /* bcolor is a pointer to the color set to use. If NULL, then the default
129 * color set (based on the theme colors for 3d-view) is used.
131 if (color_index > 0) {
132 bTheme *btheme= U.themes.first;
133 bcolor= &btheme->tarm[(color_index - 1)];
135 else if (color_index == -1) {
136 /* use the group's own custom color set */
137 bcolor= (grp)? &grp->cs : NULL;
143 /* This function is for brightening/darkening a given color (like UI_ThemeColorShade()) */
144 static void cp_shade_color3ub (char cp[], int offset)
148 r= offset + (int) cp[0];
150 g= offset + (int) cp[1];
152 b= offset + (int) cp[2];
160 /* This function sets the gl-color for coloring a certain bone (based on bcolor) */
161 static short set_pchan_glColor (short colCode, int armflag, int boneflag, int constflag)
164 case PCHAN_COLOR_NORMAL:
169 if (boneflag & BONE_ACTIVE) {
170 VECCOPY(cp, bcolor->active);
172 else if (boneflag & BONE_SELECTED) {
173 VECCOPY(cp, bcolor->select);
176 /* a bit darker than solid */
177 VECCOPY(cp, bcolor->solid);
178 cp_shade_color3ub(cp, -50);
181 glColor3ub(cp[0], cp[1], cp[2]);
184 if (boneflag & BONE_ACTIVE) UI_ThemeColorShade(TH_BONE_POSE, 40);
185 else if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_BONE_POSE);
186 else UI_ThemeColor(TH_WIRE);
193 case PCHAN_COLOR_SOLID:
196 char *cp= bcolor->solid;
197 glColor3ub(cp[0], cp[1], cp[2]);
200 UI_ThemeColor(TH_BONE_SOLID);
206 case PCHAN_COLOR_CONSTS:
208 if ( (bcolor == NULL) || (bcolor->flag & TH_WIRECOLOR_CONSTCOLS) ) {
209 if (constflag & PCHAN_HAS_STRIDE) glColor4ub(0, 0, 200, 80);
210 else if (constflag & PCHAN_HAS_TARGET) glColor4ub(255, 150, 0, 80);
211 else if (constflag & PCHAN_HAS_IK) glColor4ub(255, 255, 0, 80);
212 else if (constflag & PCHAN_HAS_CONST) glColor4ub(0, 255, 120, 80);
213 else if (constflag) UI_ThemeColor4(TH_BONE_POSE); // PCHAN_HAS_ACTION
222 case PCHAN_COLOR_SPHEREBONE_BASE:
227 if (boneflag & BONE_ACTIVE) {
228 VECCOPY(cp, bcolor->active);
230 else if (boneflag & BONE_SELECTED) {
231 VECCOPY(cp, bcolor->select);
234 VECCOPY(cp, bcolor->solid);
237 glColor3ub(cp[0], cp[1], cp[2]);
240 if (boneflag & BONE_ACTIVE) UI_ThemeColorShade(TH_BONE_POSE, 40);
241 else if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_BONE_POSE);
242 else UI_ThemeColor(TH_BONE_SOLID);
248 case PCHAN_COLOR_SPHEREBONE_END:
253 if (boneflag & BONE_ACTIVE) {
254 VECCOPY(cp, bcolor->active);
255 cp_shade_color3ub(cp, 10);
257 else if (boneflag & BONE_SELECTED) {
258 VECCOPY(cp, bcolor->select);
259 cp_shade_color3ub(cp, -30);
262 VECCOPY(cp, bcolor->solid);
263 cp_shade_color3ub(cp, -30);
266 glColor3ub(cp[0], cp[1], cp[2]);
269 if (boneflag & BONE_ACTIVE) UI_ThemeColorShade(TH_BONE_POSE, 10);
270 else if (boneflag & BONE_SELECTED) UI_ThemeColorShade(TH_BONE_POSE, -30);
271 else UI_ThemeColorShade(TH_BONE_SOLID, -30);
276 case PCHAN_COLOR_LINEBONE:
278 /* inner part in background color or constraint */
279 if ( (constflag) && ((bcolor==NULL) || (bcolor->flag & TH_WIRECOLOR_CONSTCOLS)) ) {
280 if (constflag & PCHAN_HAS_STRIDE) glColor3ub(0, 0, 200);
281 else if (constflag & PCHAN_HAS_TARGET) glColor3ub(255, 150, 0);
282 else if (constflag & PCHAN_HAS_IK) glColor3ub(255, 255, 0);
283 else if (constflag & PCHAN_HAS_CONST) glColor3ub(0, 255, 120);
284 else if (constflag) UI_ThemeColor(TH_BONE_POSE); /* PCHAN_HAS_ACTION */
288 char *cp= bcolor->solid;
289 glColor4ub(cp[0], cp[1], cp[2], 204);
292 UI_ThemeColorShade(TH_BACK, -30);
304 /* *************** Armature drawing, helper calls for parts ******************* */
306 /* half the cube, in Y */
307 static float cube[8][3] = {
318 static void drawsolidcube_size(float xsize, float ysize, float zsize)
320 static GLuint displist=0;
323 glScalef(xsize, ysize, zsize);
325 n[0]=0; n[1]=0; n[2]=0;
328 displist= glGenLists(1);
329 glNewList(displist, GL_COMPILE_AND_EXECUTE);
334 glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
338 glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[1]);
342 glVertex3fv(cube[4]); glVertex3fv(cube[7]); glVertex3fv(cube[6]); glVertex3fv(cube[5]);
346 glVertex3fv(cube[7]); glVertex3fv(cube[3]); glVertex3fv(cube[2]); glVertex3fv(cube[6]);
350 glVertex3fv(cube[1]); glVertex3fv(cube[5]); glVertex3fv(cube[6]); glVertex3fv(cube[2]);
354 glVertex3fv(cube[7]); glVertex3fv(cube[4]); glVertex3fv(cube[0]); glVertex3fv(cube[3]);
359 else glCallList(displist);
363 static void drawcube_size(float xsize, float ysize, float zsize)
365 static GLuint displist=0;
367 glScalef(xsize, ysize, zsize);
370 displist= glGenLists(1);
371 glNewList(displist, GL_COMPILE_AND_EXECUTE);
373 glBegin(GL_LINE_STRIP);
374 glVertex3fv(cube[0]); glVertex3fv(cube[1]);glVertex3fv(cube[2]); glVertex3fv(cube[3]);
375 glVertex3fv(cube[0]); glVertex3fv(cube[4]);glVertex3fv(cube[5]); glVertex3fv(cube[6]);
376 glVertex3fv(cube[7]); glVertex3fv(cube[4]);
380 glVertex3fv(cube[1]); glVertex3fv(cube[5]);
381 glVertex3fv(cube[2]); glVertex3fv(cube[6]);
382 glVertex3fv(cube[3]); glVertex3fv(cube[7]);
387 else glCallList(displist);
392 static void draw_bonevert(void)
394 static GLuint displist=0;
399 displist= glGenLists(1);
400 glNewList(displist, GL_COMPILE_AND_EXECUTE);
404 qobj = gluNewQuadric();
405 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE);
406 gluDisk(qobj, 0.0, 0.05, 16, 1);
408 glRotatef(90, 0, 1, 0);
409 gluDisk(qobj, 0.0, 0.05, 16, 1);
411 glRotatef(90, 1, 0, 0);
412 gluDisk(qobj, 0.0, 0.05, 16, 1);
414 gluDeleteQuadric(qobj);
420 glCallList(displist);
423 static void draw_bonevert_solid(void)
425 static GLuint displist=0;
430 displist= glGenLists(1);
431 glNewList(displist, GL_COMPILE_AND_EXECUTE);
433 qobj = gluNewQuadric();
434 gluQuadricDrawStyle(qobj, GLU_FILL);
435 glShadeModel(GL_SMOOTH);
436 gluSphere(qobj, 0.05, 8, 5);
437 glShadeModel(GL_FLAT);
438 gluDeleteQuadric(qobj);
443 glCallList(displist);
446 static void draw_bone_octahedral()
448 static GLuint displist=0;
453 displist= glGenLists(1);
454 glNewList(displist, GL_COMPILE_AND_EXECUTE);
456 vec[0][0]= vec[0][1]= vec[0][2]= 0.0f;
457 vec[5][0]= vec[5][2]= 0.0f; vec[5][1]= 1.0f;
459 vec[1][0]= 0.1f; vec[1][2]= 0.1f; vec[1][1]= 0.1f;
460 vec[2][0]= 0.1f; vec[2][2]= -0.1f; vec[2][1]= 0.1f;
461 vec[3][0]= -0.1f; vec[3][2]= -0.1f; vec[3][1]= 0.1f;
462 vec[4][0]= -0.1f; vec[4][2]= 0.1f; vec[4][1]= 0.1f;
464 /* Section 1, sides */
465 glBegin(GL_LINE_LOOP);
476 /* Section 1, square */
477 glBegin(GL_LINE_LOOP);
487 glCallList(displist);
490 static void draw_bone_solid_octahedral(void)
492 static GLuint displist=0;
495 float vec[6][3], nor[3];
497 displist= glGenLists(1);
498 glNewList(displist, GL_COMPILE_AND_EXECUTE);
500 vec[0][0]= vec[0][1]= vec[0][2]= 0.0f;
501 vec[5][0]= vec[5][2]= 0.0f; vec[5][1]= 1.0f;
503 vec[1][0]= 0.1f; vec[1][2]= 0.1f; vec[1][1]= 0.1f;
504 vec[2][0]= 0.1f; vec[2][2]= -0.1f; vec[2][1]= 0.1f;
505 vec[3][0]= -0.1f; vec[3][2]= -0.1f; vec[3][1]= 0.1f;
506 vec[4][0]= -0.1f; vec[4][2]= 0.1f; vec[4][1]= 0.1f;
509 glBegin(GL_TRIANGLES);
511 CalcNormFloat(vec[2], vec[1], vec[0], nor);
513 glVertex3fv(vec[2]); glVertex3fv(vec[1]); glVertex3fv(vec[0]);
515 CalcNormFloat(vec[3], vec[2], vec[0], nor);
517 glVertex3fv(vec[3]); glVertex3fv(vec[2]); glVertex3fv(vec[0]);
519 CalcNormFloat(vec[4], vec[3], vec[0], nor);
521 glVertex3fv(vec[4]); glVertex3fv(vec[3]); glVertex3fv(vec[0]);
523 CalcNormFloat(vec[1], vec[4], vec[0], nor);
525 glVertex3fv(vec[1]); glVertex3fv(vec[4]); glVertex3fv(vec[0]);
528 CalcNormFloat(vec[5], vec[1], vec[2], nor);
530 glVertex3fv(vec[5]); glVertex3fv(vec[1]); glVertex3fv(vec[2]);
532 CalcNormFloat(vec[5], vec[2], vec[3], nor);
534 glVertex3fv(vec[5]); glVertex3fv(vec[2]); glVertex3fv(vec[3]);
536 CalcNormFloat(vec[5], vec[3], vec[4], nor);
538 glVertex3fv(vec[5]); glVertex3fv(vec[3]); glVertex3fv(vec[4]);
540 CalcNormFloat(vec[5], vec[4], vec[1], nor);
542 glVertex3fv(vec[5]); glVertex3fv(vec[4]); glVertex3fv(vec[1]);
549 glCallList(displist);
552 /* *************** Armature drawing, bones ******************* */
555 static void draw_bone_points(int dt, int armflag, unsigned int boneflag, int id)
557 /* Draw root point if we are not connected */
558 if ((boneflag & BONE_CONNECTED)==0) {
560 glLoadName(id | BONESEL_ROOT);
563 if (armflag & ARM_EDITMODE) {
564 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
565 else UI_ThemeColor(TH_VERTEX);
569 if (armflag & ARM_POSEMODE)
570 set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, 0);
572 UI_ThemeColor(TH_BONE_SOLID);
576 draw_bonevert_solid();
583 glLoadName(id | BONESEL_TIP);
586 if (armflag & ARM_EDITMODE) {
587 if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
588 else UI_ThemeColor(TH_VERTEX);
592 if (armflag & ARM_POSEMODE)
593 set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, 0);
595 UI_ThemeColor(TH_BONE_SOLID);
598 glTranslatef(0.0f, 1.0f, 0.0f);
600 draw_bonevert_solid();
603 glTranslatef(0.0f, -1.0f, 0.0f);
607 /* 16 values of sin function (still same result!) */
608 static float si[16] = {
610 0.20129852f, 0.39435585f,
611 0.57126821f, 0.72479278f,
612 0.84864425f, 0.93775213f,
613 0.98846832f, 0.99871650f,
614 0.96807711f, 0.89780453f,
615 0.79077573f, 0.65137248f,
616 0.48530196f, 0.29936312f,
619 /* 16 values of cos function (still same result!) */
620 static float co[16] ={
622 0.97952994f, 0.91895781f,
623 0.82076344f, 0.68896691f,
624 0.52896401f, 0.34730525f,
625 0.15142777f, -0.05064916f,
626 -0.25065253f, -0.44039415f,
627 -0.61210598f, -0.75875812f,
628 -0.87434661f, -0.95413925f,
634 /* smat, imat = mat & imat to draw screenaligned */
635 static void draw_sphere_bone_dist(float smat[][4], float imat[][4], int boneflag, bPoseChannel *pchan, EditBone *ebone)
637 float head, tail, length, dist;
638 float *headvec, *tailvec, dirvec[3];
640 /* figure out the sizes of spheres */
642 /* this routine doesn't call get_matrix_editbone() that calculates it */
643 ebone->length = VecLenf(ebone->head, ebone->tail);
645 length= ebone->length;
646 tail= ebone->rad_tail;
648 if (ebone->parent && (ebone->flag & BONE_CONNECTED))
649 head= ebone->parent->rad_tail;
651 head= ebone->rad_head;
652 headvec= ebone->head;
653 tailvec= ebone->tail;
656 length= pchan->bone->length;
657 tail= pchan->bone->rad_tail;
658 dist= pchan->bone->dist;
659 if (pchan->parent && (pchan->bone->flag & BONE_CONNECTED))
660 head= pchan->parent->bone->rad_tail;
662 head= pchan->bone->rad_head;
663 headvec= pchan->pose_head;
664 tailvec= pchan->pose_tail;
667 /* ***** draw it ***** */
669 /* move vector to viewspace */
670 VecSubf(dirvec, tailvec, headvec);
671 Mat4Mul3Vecfl(smat, dirvec);
674 /* move vector back */
675 Mat4Mul3Vecfl(imat, dirvec);
677 if (0.0f != Normalize(dirvec)) {
678 float norvec[3], vec1[3], vec2[3], vec[3];
681 //VecMulf(dirvec, head);
682 Crossf(norvec, dirvec, imat[2]);
684 glBegin(GL_QUAD_STRIP);
686 for (a=0; a<16; a++) {
687 vec[0]= - *(si+a) * dirvec[0] + *(co+a) * norvec[0];
688 vec[1]= - *(si+a) * dirvec[1] + *(co+a) * norvec[1];
689 vec[2]= - *(si+a) * dirvec[2] + *(co+a) * norvec[2];
691 vec1[0]= headvec[0] + head*vec[0];
692 vec1[1]= headvec[1] + head*vec[1];
693 vec1[2]= headvec[2] + head*vec[2];
694 vec2[0]= headvec[0] + (head+dist)*vec[0];
695 vec2[1]= headvec[1] + (head+dist)*vec[1];
696 vec2[2]= headvec[2] + (head+dist)*vec[2];
698 glColor4ub(255, 255, 255, 50);
700 //glColor4ub(255, 255, 255, 0);
704 for (a=15; a>=0; a--) {
705 vec[0]= *(si+a) * dirvec[0] + *(co+a) * norvec[0];
706 vec[1]= *(si+a) * dirvec[1] + *(co+a) * norvec[1];
707 vec[2]= *(si+a) * dirvec[2] + *(co+a) * norvec[2];
709 vec1[0]= tailvec[0] + tail*vec[0];
710 vec1[1]= tailvec[1] + tail*vec[1];
711 vec1[2]= tailvec[2] + tail*vec[2];
712 vec2[0]= tailvec[0] + (tail+dist)*vec[0];
713 vec2[1]= tailvec[1] + (tail+dist)*vec[1];
714 vec2[2]= tailvec[2] + (tail+dist)*vec[2];
716 //glColor4ub(255, 255, 255, 50);
718 //glColor4ub(255, 255, 255, 0);
721 /* make it cyclic... */
723 vec[0]= - *(si) * dirvec[0] + *(co) * norvec[0];
724 vec[1]= - *(si) * dirvec[1] + *(co) * norvec[1];
725 vec[2]= - *(si) * dirvec[2] + *(co) * norvec[2];
727 vec1[0]= headvec[0] + head*vec[0];
728 vec1[1]= headvec[1] + head*vec[1];
729 vec1[2]= headvec[2] + head*vec[2];
730 vec2[0]= headvec[0] + (head+dist)*vec[0];
731 vec2[1]= headvec[1] + (head+dist)*vec[1];
732 vec2[2]= headvec[2] + (head+dist)*vec[2];
734 //glColor4ub(255, 255, 255, 50);
736 //glColor4ub(255, 255, 255, 0);
744 /* smat, imat = mat & imat to draw screenaligned */
745 static void draw_sphere_bone_wire(float smat[][4], float imat[][4], int armflag, int boneflag, int constflag, unsigned int id, bPoseChannel *pchan, EditBone *ebone)
747 float head, tail, length;
748 float *headvec, *tailvec, dirvec[3];
750 /* figure out the sizes of spheres */
752 /* this routine doesn't call get_matrix_editbone() that calculates it */
753 ebone->length = VecLenf(ebone->head, ebone->tail);
755 length= ebone->length;
756 tail= ebone->rad_tail;
757 if (ebone->parent && (boneflag & BONE_CONNECTED))
758 head= ebone->parent->rad_tail;
760 head= ebone->rad_head;
761 headvec= ebone->head;
762 tailvec= ebone->tail;
765 length= pchan->bone->length;
766 tail= pchan->bone->rad_tail;
767 if ((pchan->parent) && (boneflag & BONE_CONNECTED))
768 head= pchan->parent->bone->rad_tail;
770 head= pchan->bone->rad_head;
771 headvec= pchan->pose_head;
772 tailvec= pchan->pose_tail;
775 /* sphere root color */
776 if (armflag & ARM_EDITMODE) {
777 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
778 else UI_ThemeColor(TH_VERTEX);
780 else if (armflag & ARM_POSEMODE)
781 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
783 /* Draw root point if we are not connected */
784 if ((boneflag & BONE_CONNECTED)==0) {
786 glLoadName(id | BONESEL_ROOT);
788 drawcircball(GL_LINE_LOOP, headvec, head, imat);
792 if (armflag & ARM_EDITMODE) {
793 if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
794 else UI_ThemeColor(TH_VERTEX);
798 glLoadName(id | BONESEL_TIP);
800 drawcircball(GL_LINE_LOOP, tailvec, tail, imat);
803 if (armflag & ARM_EDITMODE) {
804 if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_SELECT);
805 else UI_ThemeColor(TH_WIRE);
808 VecSubf(dirvec, tailvec, headvec);
810 /* move vector to viewspace */
811 Mat4Mul3Vecfl(smat, dirvec);
814 /* move vector back */
815 Mat4Mul3Vecfl(imat, dirvec);
817 if (0.0f != Normalize(dirvec)) {
818 float norvech[3], norvect[3], vec[3];
820 VECCOPY(vec, dirvec);
822 VecMulf(dirvec, head);
823 Crossf(norvech, dirvec, imat[2]);
826 Crossf(norvect, vec, imat[2]);
829 glLoadName(id | BONESEL_BONE);
832 vec[0]= headvec[0] + norvech[0];
833 vec[1]= headvec[1] + norvech[1];
834 vec[2]= headvec[2] + norvech[2];
836 vec[0]= tailvec[0] + norvect[0];
837 vec[1]= tailvec[1] + norvect[1];
838 vec[2]= tailvec[2] + norvect[2];
840 vec[0]= headvec[0] - norvech[0];
841 vec[1]= headvec[1] - norvech[1];
842 vec[2]= headvec[2] - norvech[2];
844 vec[0]= tailvec[0] - norvect[0];
845 vec[1]= tailvec[1] - norvect[1];
846 vec[2]= tailvec[2] - norvect[2];
853 /* does wire only for outline selecting */
854 static void draw_sphere_bone(int dt, int armflag, int boneflag, int constflag, unsigned int id, bPoseChannel *pchan, EditBone *ebone)
857 float head, tail, length;
861 qobj = gluNewQuadric();
863 /* figure out the sizes of spheres */
865 length= ebone->length;
866 tail= ebone->rad_tail;
867 if (ebone->parent && (boneflag & BONE_CONNECTED))
868 head= ebone->parent->rad_tail;
870 head= ebone->rad_head;
873 length= pchan->bone->length;
874 tail= pchan->bone->rad_tail;
875 if (pchan->parent && (boneflag & BONE_CONNECTED))
876 head= pchan->parent->bone->rad_tail;
878 head= pchan->bone->rad_head;
881 /* move to z-axis space */
882 glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
885 /* set up solid drawing */
886 glEnable(GL_COLOR_MATERIAL);
887 glEnable(GL_LIGHTING);
889 gluQuadricDrawStyle(qobj, GLU_FILL);
890 glShadeModel(GL_SMOOTH);
893 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE);
896 /* sphere root color */
897 if (armflag & ARM_EDITMODE) {
898 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
899 else UI_ThemeColorShade(TH_BONE_SOLID, -30);
901 else if (armflag & ARM_POSEMODE)
902 set_pchan_glColor(PCHAN_COLOR_SPHEREBONE_END, armflag, boneflag, constflag);
903 else if (dt==OB_SOLID)
904 UI_ThemeColorShade(TH_BONE_SOLID, -30);
906 /* Draw root point if we are not connected */
907 if ((boneflag & BONE_CONNECTED)==0) {
909 glLoadName(id | BONESEL_ROOT);
910 gluSphere(qobj, head, 16, 10);
914 if (armflag & ARM_EDITMODE) {
915 if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
916 else UI_ThemeColorShade(TH_BONE_SOLID, -30);
920 glLoadName(id | BONESEL_TIP);
922 glTranslatef(0.0f, 0.0f, length);
923 gluSphere(qobj, tail, 16, 10);
924 glTranslatef(0.0f, 0.0f, -length);
927 if (armflag & ARM_EDITMODE) {
928 if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_SELECT);
929 else UI_ThemeColor(TH_BONE_SOLID);
931 else if (armflag & ARM_POSEMODE)
932 set_pchan_glColor(PCHAN_COLOR_SPHEREBONE_BASE, armflag, boneflag, constflag);
933 else if (dt == OB_SOLID)
934 UI_ThemeColor(TH_BONE_SOLID);
936 fac1= (length-head)/length;
937 fac2= (length-tail)/length;
939 if (length > (head+tail)) {
941 glLoadName (id | BONESEL_BONE);
943 glEnable(GL_POLYGON_OFFSET_FILL);
944 glPolygonOffset(-1.0f, -1.0f);
946 glTranslatef(0.0f, 0.0f, head);
947 gluCylinder(qobj, fac1*head + (1.0f-fac1)*tail, fac2*tail + (1.0f-fac2)*head, length-head-tail, 16, 1);
948 glTranslatef(0.0f, 0.0f, -head);
950 glDisable(GL_POLYGON_OFFSET_FILL);
952 /* draw sphere on extrema */
953 glTranslatef(0.0f, 0.0f, length-tail);
954 gluSphere(qobj, fac2*tail + (1.0f-fac2)*head, 16, 10);
955 glTranslatef(0.0f, 0.0f, -length+tail);
957 glTranslatef(0.0f, 0.0f, head);
958 gluSphere(qobj, fac1*head + (1.0f-fac1)*tail, 16, 10);
961 /* 1 sphere in center */
962 glTranslatef(0.0f, 0.0f, (head + length-tail)/2.0f);
963 gluSphere(qobj, fac1*head + (1.0f-fac1)*tail, 16, 10);
968 glShadeModel(GL_FLAT);
969 glDisable(GL_LIGHTING);
970 glDisable(GL_COLOR_MATERIAL);
974 gluDeleteQuadric(qobj);
977 static GLubyte bm_dot6[]= {0x0, 0x18, 0x3C, 0x7E, 0x7E, 0x3C, 0x18, 0x0};
978 static GLubyte bm_dot8[]= {0x3C, 0x7E, 0xFF, 0xFF, 0xFF, 0xFF, 0x7E, 0x3C};
980 static GLubyte bm_dot5[]= {0x0, 0x0, 0x10, 0x38, 0x7c, 0x38, 0x10, 0x0};
981 static GLubyte bm_dot7[]= {0x0, 0x38, 0x7C, 0xFE, 0xFE, 0xFE, 0x7C, 0x38};
984 static void draw_line_bone(int armflag, int boneflag, int constflag, unsigned int id, bPoseChannel *pchan, EditBone *ebone)
988 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
991 length= pchan->bone->length;
993 length= ebone->length;
996 glScalef(length, length, length);
998 /* this chunk not in object mode */
999 if (armflag & (ARM_EDITMODE|ARM_POSEMODE)) {
1001 if (armflag & ARM_POSEMODE)
1002 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1003 else if (armflag & ARM_EDITMODE) {
1004 UI_ThemeColor(TH_WIRE);
1007 /* Draw root point if we are not connected */
1008 if ((boneflag & BONE_CONNECTED)==0) {
1009 if (G.f & G_PICKSEL) { // no bitmap in selection mode, crashes 3d cards...
1010 glLoadName (id | BONESEL_ROOT);
1012 glVertex3f(0.0f, 0.0f, 0.0f);
1016 glRasterPos3f(0.0f, 0.0f, 0.0f);
1017 glBitmap(8, 8, 4, 4, 0, 0, bm_dot8);
1022 glLoadName((GLuint) id|BONESEL_BONE);
1025 glVertex3f(0.0f, 0.0f, 0.0f);
1026 glVertex3f(0.0f, 1.0f, 0.0f);
1030 if (G.f & G_PICKSEL) {
1031 /* no bitmap in selection mode, crashes 3d cards... */
1032 glLoadName(id | BONESEL_TIP);
1034 glVertex3f(0.0f, 1.0f, 0.0f);
1038 glRasterPos3f(0.0f, 1.0f, 0.0f);
1039 glBitmap(8, 8, 4, 4, 0, 0, bm_dot7);
1042 /* further we send no names */
1044 glLoadName(id & 0xFFFF); /* object tag, for bordersel optim */
1046 if (armflag & ARM_POSEMODE)
1047 set_pchan_glColor(PCHAN_COLOR_LINEBONE, armflag, boneflag, constflag);
1052 /*Draw root point if we are not connected */
1053 if ((boneflag & BONE_CONNECTED)==0) {
1054 if ((G.f & G_PICKSEL)==0) {
1055 /* no bitmap in selection mode, crashes 3d cards... */
1056 if (armflag & ARM_EDITMODE) {
1057 if (boneflag & BONE_ROOTSEL) UI_ThemeColor(TH_VERTEX_SELECT);
1058 else UI_ThemeColor(TH_VERTEX);
1060 glRasterPos3f(0.0f, 0.0f, 0.0f);
1061 glBitmap(8, 8, 4, 4, 0, 0, bm_dot6);
1065 if (armflag & ARM_EDITMODE) {
1066 if (boneflag & BONE_SELECTED) UI_ThemeColor(TH_EDGE_SELECT);
1067 else UI_ThemeColorShade(TH_BACK, -30);
1070 glVertex3f(0.0f, 0.0f, 0.0f);
1071 glVertex3f(0.0f, 1.0f, 0.0f);
1075 if ((G.f & G_PICKSEL)==0) {
1076 /* no bitmap in selection mode, crashes 3d cards... */
1077 if (armflag & ARM_EDITMODE) {
1078 if (boneflag & BONE_TIPSEL) UI_ThemeColor(TH_VERTEX_SELECT);
1079 else UI_ThemeColor(TH_VERTEX);
1081 glRasterPos3f(0.0f, 1.0f, 0.0f);
1082 glBitmap(8, 8, 4, 4, 0, 0, bm_dot5);
1090 static void draw_b_bone_boxes(int dt, bPoseChannel *pchan, float xwidth, float length, float zwidth)
1095 segments= pchan->bone->segments;
1097 if ((segments > 1) && (pchan)) {
1098 float dlen= length/(float)segments;
1099 Mat4 *bbone= b_bone_spline_setup(pchan, 0);
1102 for (a=0; a<segments; a++, bbone++) {
1104 glMultMatrixf(bbone->mat);
1105 if (dt==OB_SOLID) drawsolidcube_size(xwidth, dlen, zwidth);
1106 else drawcube_size(xwidth, dlen, zwidth);
1112 if (dt==OB_SOLID) drawsolidcube_size(xwidth, length, zwidth);
1113 else drawcube_size(xwidth, length, zwidth);
1118 static void draw_b_bone(int dt, int armflag, int boneflag, int constflag, unsigned int id, bPoseChannel *pchan, EditBone *ebone)
1120 float xwidth, length, zwidth;
1123 xwidth= pchan->bone->xwidth;
1124 length= pchan->bone->length;
1125 zwidth= pchan->bone->zwidth;
1128 xwidth= ebone->xwidth;
1129 length= ebone->length;
1130 zwidth= ebone->zwidth;
1133 /* draw points only if... */
1134 if (armflag & ARM_EDITMODE) {
1135 /* move to unitspace */
1137 glScalef(length, length, length);
1138 draw_bone_points(dt, armflag, boneflag, id);
1140 length*= 0.95f; // make vertices visible
1143 /* colors for modes */
1144 if (armflag & ARM_POSEMODE) {
1146 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1148 set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, constflag);
1150 else if (armflag & ARM_EDITMODE) {
1152 if (boneflag & BONE_ACTIVE) UI_ThemeColor(TH_EDGE_SELECT);
1153 else if (boneflag & BONE_SELECTED) UI_ThemeColorShade(TH_EDGE_SELECT, -20);
1154 else UI_ThemeColor(TH_WIRE);
1157 UI_ThemeColor(TH_BONE_SOLID);
1161 glLoadName ((GLuint) id|BONESEL_BONE);
1164 /* set up solid drawing */
1166 glEnable(GL_COLOR_MATERIAL);
1167 glEnable(GL_LIGHTING);
1169 if (armflag & ARM_POSEMODE)
1170 set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, constflag);
1172 UI_ThemeColor(TH_BONE_SOLID);
1174 draw_b_bone_boxes(OB_SOLID, pchan, xwidth, length, zwidth);
1176 /* disable solid drawing */
1177 glDisable(GL_COLOR_MATERIAL);
1178 glDisable(GL_LIGHTING);
1182 if (armflag & ARM_POSEMODE) {
1184 /* set constraint colors */
1185 if (set_pchan_glColor(PCHAN_COLOR_CONSTS, armflag, boneflag, constflag)) {
1188 draw_b_bone_boxes(OB_SOLID, pchan, xwidth, length, zwidth);
1190 glDisable(GL_BLEND);
1193 /* restore colors */
1194 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1198 draw_b_bone_boxes(OB_WIRE, pchan, xwidth, length, zwidth);
1202 static void draw_bone(int dt, int armflag, int boneflag, int constflag, unsigned int id, float length)
1205 /* Draw a 3d octahedral bone, we use normalized space based on length,
1206 for glDisplayLists */
1208 glScalef(length, length, length);
1210 /* set up solid drawing */
1212 glEnable(GL_COLOR_MATERIAL);
1213 glEnable(GL_LIGHTING);
1214 UI_ThemeColor(TH_BONE_SOLID);
1217 /* colors for posemode */
1218 if (armflag & ARM_POSEMODE) {
1220 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1222 set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, constflag);
1226 draw_bone_points(dt, armflag, boneflag, id);
1228 /* now draw the bone itself */
1230 glLoadName((GLuint) id|BONESEL_BONE);
1234 if (dt <= OB_WIRE) {
1236 if (armflag & ARM_EDITMODE) {
1237 if (boneflag & BONE_ACTIVE) UI_ThemeColor(TH_EDGE_SELECT);
1238 else if (boneflag & BONE_SELECTED) UI_ThemeColorShade(TH_EDGE_SELECT, -20);
1239 else UI_ThemeColor(TH_WIRE);
1241 else if (armflag & ARM_POSEMODE) {
1243 /* draw constraint colors */
1244 if (set_pchan_glColor(PCHAN_COLOR_CONSTS, armflag, boneflag, constflag)) {
1247 draw_bone_solid_octahedral();
1249 glDisable(GL_BLEND);
1252 /* restore colors */
1253 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, constflag);
1256 draw_bone_octahedral();
1260 if (armflag & ARM_POSEMODE)
1261 set_pchan_glColor(PCHAN_COLOR_SOLID, armflag, boneflag, constflag);
1263 UI_ThemeColor(TH_BONE_SOLID);
1264 draw_bone_solid_octahedral();
1267 /* disable solid drawing */
1269 glDisable(GL_COLOR_MATERIAL);
1270 glDisable(GL_LIGHTING);
1274 static void draw_custom_bone(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob, int dt, int armflag, int boneflag, unsigned int id, float length)
1276 if(ob==NULL) return;
1278 glScalef(length, length, length);
1280 /* colors for posemode */
1281 if (armflag & ARM_POSEMODE) {
1282 set_pchan_glColor(PCHAN_COLOR_NORMAL, armflag, boneflag, 0);
1286 glLoadName((GLuint) id|BONESEL_BONE);
1289 draw_object_instance(scene, v3d, rv3d, ob, dt, armflag & ARM_POSEMODE);
1293 static void pchan_draw_IK_root_lines(bPoseChannel *pchan, short only_temp)
1296 bPoseChannel *parchan;
1298 for (con= pchan->constraints.first; con; con= con->next) {
1299 if (con->type == CONSTRAINT_TYPE_KINEMATIC && (con->enforce!=0.0)) {
1300 bKinematicConstraint *data = (bKinematicConstraint*)con->data;
1303 /* if only_temp, only draw if it is a temporary ik-chain */
1304 if ((only_temp) && !(data->flag & CONSTRAINT_IK_TEMP))
1310 /* exclude tip from chain? */
1311 if ((data->flag & CONSTRAINT_IK_TIP)==0)
1312 parchan= pchan->parent;
1316 glVertex3fv(parchan->pose_tail);
1318 /* Find the chain's root */
1319 while (parchan->parent) {
1321 if(segcount==data->rootbone || segcount>255) break; // 255 is weak
1322 parchan= parchan->parent;
1325 glVertex3fv(parchan->pose_head);
1333 static void bgl_sphere_project(float ax, float az)
1335 float dir[3], sine, q3;
1337 sine= 1.0f - ax*ax - az*az;
1338 q3= (sine < 0.0f)? 0.0f: (float)(2.0*sqrt(sine));
1341 dir[1]= 1.0f - 2.0f*sine;
1347 static void draw_dof_ellipse(float ax, float az)
1349 static float staticSine[16] = {
1350 0.0f, 0.104528463268f, 0.207911690818f, 0.309016994375f,
1351 0.406736643076f, 0.5f, 0.587785252292f, 0.669130606359f,
1352 0.743144825477f, 0.809016994375f, 0.866025403784f,
1353 0.913545457643f, 0.951056516295f, 0.978147600734f,
1354 0.994521895368f, 1.0f
1363 glColor4ub(70, 70, 70, 50);
1367 for(i=1; i<n; i++) {
1371 for(j=1; j<n-i+1; j++) {
1376 glBegin(GL_TRIANGLES);
1377 bgl_sphere_project(ax*px, az*z);
1378 bgl_sphere_project(ax*px, az*pz);
1379 bgl_sphere_project(ax*x, az*pz);
1384 bgl_sphere_project(ax*x, az*z);
1385 bgl_sphere_project(ax*x, az*pz);
1386 bgl_sphere_project(ax*px, az*pz);
1387 bgl_sphere_project(ax*px, az*z);
1396 glDisable(GL_BLEND);
1399 glColor3ub(0, 0, 0);
1401 glBegin(GL_LINE_STRIP);
1403 bgl_sphere_project(staticSine[n-i-1]*ax, staticSine[i]*az);
1407 static void draw_pose_dofs(Object *ob)
1409 bArmature *arm= ob->data;
1410 bPoseChannel *pchan;
1413 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1416 if ( (bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))) {
1417 if (bone->flag & BONE_SELECTED) {
1418 if (bone->layer & arm->layer) {
1419 if (pchan->ikflag & (BONE_IK_XLIMIT|BONE_IK_ZLIMIT)) {
1420 if (ED_pose_channel_in_IK_chain(ob, pchan)) {
1421 float corner[4][3], posetrans[3], mat[4][4];
1422 float phi=0.0f, theta=0.0f, scale;
1425 /* in parent-bone pose, but own restspace */
1428 VECCOPY(posetrans, pchan->pose_mat[3]);
1429 glTranslatef(posetrans[0], posetrans[1], posetrans[2]);
1431 if (pchan->parent) {
1432 Mat4CpyMat4(mat, pchan->parent->pose_mat);
1433 mat[3][0]= mat[3][1]= mat[3][2]= 0.0f;
1437 Mat4CpyMat3(mat, pchan->bone->bone_mat);
1440 scale= bone->length*pchan->size[1];
1441 glScalef(scale, scale, scale);
1443 if (pchan->ikflag & BONE_IK_XLIMIT) {
1444 if (pchan->ikflag & BONE_IK_ZLIMIT) {
1445 float amin[3], amax[3];
1447 for (i=0; i<3; i++) {
1448 amin[i]= (float)sin(pchan->limitmin[i]*M_PI/360.0);
1449 amax[i]= (float)sin(pchan->limitmax[i]*M_PI/360.0);
1452 glScalef(1.0f, -1.0f, 1.0f);
1453 if ((amin[0] != 0.0f) && (amin[2] != 0.0f))
1454 draw_dof_ellipse(amin[0], amin[2]);
1455 if ((amin[0] != 0.0f) && (amax[2] != 0.0f))
1456 draw_dof_ellipse(amin[0], amax[2]);
1457 if ((amax[0] != 0.0f) && (amin[2] != 0.0f))
1458 draw_dof_ellipse(amax[0], amin[2]);
1459 if ((amax[0] != 0.0f) && (amax[2] != 0.0f))
1460 draw_dof_ellipse(amax[0], amax[2]);
1461 glScalef(1.0f, -1.0f, 1.0f);
1466 if (pchan->ikflag & BONE_IK_ZLIMIT) {
1467 theta= 0.5f*(pchan->limitmin[2]+pchan->limitmax[2]);
1468 glRotatef(theta, 0.0f, 0.0f, 1.0f);
1470 glColor3ub(50, 50, 255); // blue, Z axis limit
1471 glBegin(GL_LINE_STRIP);
1472 for (a=-16; a<=16; a++) {
1473 float fac= ((float)a)/16.0f;
1475 phi= fac * (float)(M_PI/360.0f) * (pchan->limitmax[2] - pchan->limitmin[2]);
1477 i= (a == -16) ? 0 : 1;
1478 corner[i][0]= (float)sin(phi);
1479 corner[i][1]= (float)cos(phi);
1481 glVertex3fv(corner[i]);
1485 glRotatef(-theta, 0.0f, 0.0f, 1.0f);
1488 if (pchan->ikflag & BONE_IK_XLIMIT) {
1489 theta= 0.5f * (pchan->limitmin[0] + pchan->limitmax[0]);
1490 glRotatef(theta, 1.0f, 0.0f, 0.0f);
1492 glColor3ub(255, 50, 50); // Red, X axis limit
1493 glBegin(GL_LINE_STRIP);
1494 for (a=-16; a<=16; a++) {
1495 float fac= ((float)a)/16.0f;
1496 phi= (float)(0.5*M_PI) + fac * (float)(M_PI/360.0f) * (pchan->limitmax[0] - pchan->limitmin[0]);
1498 i= (a == -16) ? 2 : 3;
1500 corner[i][1]= (float)sin(phi);
1501 corner[i][2]= (float)cos(phi);
1502 glVertex3fv(corner[i]);
1506 glRotatef(-theta, 1.0f, 0.0f, 0.0f);
1509 /* out of cone, out of bone */
1519 static void bone_matrix_translate_y(float mat[][4], float y)
1523 VECCOPY(trans, mat[1]);
1525 VecAddf(mat[3], mat[3], trans);
1528 /* assumes object is Armature with pose */
1529 static void draw_pose_channels(Scene *scene, View3D *v3d, ARegion *ar, Base *base, int dt)
1531 RegionView3D *rv3d= ar->regiondata;
1532 Object *ob= base->object;
1533 bArmature *arm= ob->data;
1534 bPoseChannel *pchan;
1537 float smat[4][4], imat[4][4], bmat[4][4];
1539 short do_dashed= 3, draw_wire= 0;
1540 short flag, constflag;
1542 /* hacky... prevent outline select from drawing dashed helplines */
1543 glGetFloatv(GL_LINE_WIDTH, &tmp);
1544 if (tmp > 1.1) do_dashed &= ~1;
1545 if (v3d->flag & V3D_HIDE_HELPLINES) do_dashed &= ~2;
1547 /* precalc inverse matrix for drawing screen aligned */
1548 if (arm->drawtype==ARM_ENVELOPE) {
1549 /* precalc inverse matrix for drawing screen aligned */
1551 Mat4MulFloat3(smat[0], 1.0f/VecLength(ob->obmat[0]));
1552 Mat4Invert(imat, smat);
1554 /* and draw blended distances */
1555 if (arm->flag & ARM_POSEMODE) {
1557 //glShadeModel(GL_SMOOTH);
1559 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1561 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1563 if (bone && !(bone->flag & (BONE_HIDDEN_P|BONE_NO_DEFORM|BONE_HIDDEN_PG))) {
1564 if (bone->flag & (BONE_SELECTED)) {
1565 if (bone->layer & arm->layer)
1566 draw_sphere_bone_dist(smat, imat, bone->flag, pchan, NULL);
1571 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
1572 glDisable(GL_BLEND);
1573 //glShadeModel(GL_FLAT);
1577 /* little speedup, also make sure transparent only draws once */
1578 glCullFace(GL_BACK);
1579 glEnable(GL_CULL_FACE);
1581 /* if solid we draw that first, with selection codes, but without names, axes etc */
1583 if (arm->flag & ARM_POSEMODE)
1584 index= base->selcol;
1586 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1589 if ( (bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)) ) {
1590 if (bone->layer & arm->layer) {
1592 glMultMatrixf(pchan->pose_mat);
1594 /* catch exception for bone with hidden parent */
1596 if ( (bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)) )
1597 flag &= ~BONE_CONNECTED;
1599 /* set color-set to use */
1600 set_pchan_colorset(ob, pchan);
1602 if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM)) {
1603 /* if drawwire, don't try to draw in solid */
1604 if (pchan->bone->flag & BONE_DRAWWIRE)
1607 draw_custom_bone(scene, v3d, rv3d, pchan->custom, OB_SOLID, arm->flag, flag, index, bone->length);
1609 else if (arm->drawtype==ARM_LINE)
1610 ; /* nothing in solid */
1611 else if (arm->drawtype==ARM_ENVELOPE)
1612 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
1613 else if (arm->drawtype==ARM_B_BONE)
1614 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, pchan, NULL);
1616 draw_bone(OB_SOLID, arm->flag, flag, 0, index, bone->length);
1623 index+= 0x10000; // pose bones count in higher 2 bytes only
1626 /* very very confusing... but in object mode, solid draw, we cannot do glLoadName yet,
1627 * stick bones and/or wire custom-shapes are drawn in next loop
1629 if ((arm->drawtype != ARM_LINE) && (draw_wire == 0)) {
1630 /* object tag, for bordersel optim */
1631 glLoadName(index & 0xFFFF);
1636 /* draw custom bone shapes as wireframes */
1637 if ( !(arm->flag & ARM_NO_CUSTOM) &&
1638 ((draw_wire) || (dt <= OB_WIRE)) )
1640 if (arm->flag & ARM_POSEMODE)
1641 index= base->selcol;
1643 /* only draw custom bone shapes that need to be drawn as wires */
1644 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1647 if ((bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))) {
1648 if (bone->layer & arm->layer) {
1649 if (pchan->custom) {
1650 if ((dt < OB_SOLID) || (bone->flag & BONE_DRAWWIRE)) {
1652 glMultMatrixf(pchan->pose_mat);
1654 /* prepare colors */
1655 if (arm->flag & ARM_POSEMODE)
1656 set_pchan_colorset(ob, pchan);
1657 #if 0 // XXX - 13 October 2009, Disabled this to make ghosting show the right colors (Aligorith)
1659 if ((scene->basact)==base) {
1660 if (base->flag & (SELECT+BA_WAS_SEL)) UI_ThemeColor(TH_ACTIVE);
1661 else UI_ThemeColor(TH_WIRE);
1664 if (base->flag & (SELECT+BA_WAS_SEL)) UI_ThemeColor(TH_SELECT);
1665 else UI_ThemeColor(TH_WIRE);
1670 /* catch exception for bone with hidden parent */
1672 if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)))
1673 flag &= ~BONE_CONNECTED;
1675 draw_custom_bone(scene, v3d, rv3d, pchan->custom, OB_WIRE, arm->flag, flag, index, bone->length);
1684 index+= 0x10000; // pose bones count in higher 2 bytes only
1688 /* object tag, for bordersel optim */
1689 glLoadName(index & 0xFFFF);
1694 /* wire draw over solid only in posemode */
1695 if ((dt <= OB_WIRE) || (arm->flag & ARM_POSEMODE) || (arm->drawtype==ARM_LINE)) {
1696 /* draw line check first. we do selection indices */
1697 if (arm->drawtype==ARM_LINE) {
1698 if (arm->flag & ARM_POSEMODE)
1699 index= base->selcol;
1701 /* if solid && posemode, we draw again with polygonoffset */
1702 else if ((dt > OB_WIRE) && (arm->flag & ARM_POSEMODE)) {
1703 bglPolygonOffset(rv3d->dist, 1.0);
1706 /* and we use selection indices if not done yet */
1707 if (arm->flag & ARM_POSEMODE)
1708 index= base->selcol;
1711 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1714 if ((bone) && !(bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))) {
1715 if (bone->layer & arm->layer) {
1716 if ((do_dashed & 1) && (bone->parent)) {
1717 /* Draw a line from our root to the parent's tip
1718 * - only if V3D_HIDE_HELPLINES is enabled...
1720 if ( (do_dashed & 2) && ((bone->flag & BONE_CONNECTED)==0) ) {
1721 if (arm->flag & ARM_POSEMODE) {
1722 glLoadName(index & 0xFFFF); // object tag, for bordersel optim
1723 UI_ThemeColor(TH_WIRE);
1727 glVertex3fv(pchan->pose_head);
1728 glVertex3fv(pchan->parent->pose_tail);
1733 /* Draw a line to IK root bone
1734 * - only if temporary chain (i.e. "autoik")
1736 if (arm->flag & ARM_POSEMODE) {
1737 if (pchan->constflag & PCHAN_HAS_IK) {
1738 if (bone->flag & BONE_SELECTED) {
1739 if (pchan->constflag & PCHAN_HAS_TARGET) glColor3ub(200, 120, 0);
1740 else glColor3ub(200, 200, 50); // add theme!
1742 glLoadName(index & 0xFFFF);
1743 pchan_draw_IK_root_lines(pchan, !(do_dashed & 2));
1750 if (arm->drawtype != ARM_ENVELOPE)
1751 glMultMatrixf(pchan->pose_mat);
1753 /* catch exception for bone with hidden parent */
1755 if ((bone->parent) && (bone->parent->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG)))
1756 flag &= ~BONE_CONNECTED;
1758 /* extra draw service for pose mode */
1759 constflag= pchan->constflag;
1760 if (pchan->flag & (POSE_ROT|POSE_LOC|POSE_SIZE))
1761 constflag |= PCHAN_HAS_ACTION;
1762 if (pchan->flag & POSE_STRIDE)
1763 constflag |= PCHAN_HAS_STRIDE;
1765 /* set color-set to use */
1766 set_pchan_colorset(ob, pchan);
1768 if ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM))
1769 ; // custom bone shapes should not be drawn here!
1770 else if (arm->drawtype==ARM_ENVELOPE) {
1772 draw_sphere_bone_wire(smat, imat, arm->flag, flag, constflag, index, pchan, NULL);
1774 else if (arm->drawtype==ARM_LINE)
1775 draw_line_bone(arm->flag, flag, constflag, index, pchan, NULL);
1776 else if (arm->drawtype==ARM_B_BONE)
1777 draw_b_bone(OB_WIRE, arm->flag, flag, constflag, index, pchan, NULL);
1779 draw_bone(OB_WIRE, arm->flag, flag, constflag, index, bone->length);
1785 /* pose bones count in higher 2 bytes only */
1789 /* restore things */
1790 if ((arm->drawtype!=ARM_LINE)&& (dt>OB_WIRE) && (arm->flag & ARM_POSEMODE))
1791 bglPolygonOffset(rv3d->dist, 0.0);
1795 glDisable(GL_CULL_FACE);
1798 if (arm->flag & ARM_POSEMODE)
1801 /* finally names and axes */
1802 if (arm->flag & (ARM_DRAWNAMES|ARM_DRAWAXES)) {
1803 /* patch for several 3d cards (IBM mostly) that crash on glSelect with text drawing */
1804 if ((G.f & G_PICKSEL) == 0) {
1807 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1809 for (pchan=ob->pose->chanbase.first; pchan; pchan=pchan->next) {
1810 if ((pchan->bone->flag & (BONE_HIDDEN_P|BONE_HIDDEN_PG))==0) {
1811 if (pchan->bone->layer & arm->layer) {
1812 if (arm->flag & (ARM_EDITMODE|ARM_POSEMODE)) {
1815 if (bone->flag & BONE_SELECTED) UI_ThemeColor(TH_TEXT_HI);
1816 else UI_ThemeColor(TH_TEXT);
1818 else if (dt > OB_WIRE)
1819 UI_ThemeColor(TH_TEXT);
1821 /* Draw names of bone */
1822 if (arm->flag & ARM_DRAWNAMES) {
1823 VecMidf(vec, pchan->pose_head, pchan->pose_tail);
1824 view3d_cached_text_draw_add(vec[0], vec[1], vec[2], pchan->name, 10);
1827 /* Draw additional axes on the bone tail */
1828 if ( (arm->flag & ARM_DRAWAXES) && (arm->flag & ARM_POSEMODE) ) {
1830 Mat4CpyMat4(bmat, pchan->pose_mat);
1831 bone_matrix_translate_y(bmat, pchan->bone->length);
1832 glMultMatrixf(bmat);
1834 /* do cached text draw immediate to include transform */
1835 view3d_cached_text_draw_begin();
1836 drawaxes(pchan->bone->length*0.25f, 0, OB_ARROWS);
1837 view3d_cached_text_draw_end(v3d, ar, 1, bmat);
1845 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
1850 /* in editmode, we don't store the bone matrix... */
1851 static void get_matrix_editbone(EditBone *eBone, float bmat[][4])
1856 /* Compose the parent transforms (i.e. their translations) */
1857 VecSubf(delta, eBone->tail, eBone->head);
1859 eBone->length = (float)sqrt(delta[0]*delta[0] + delta[1]*delta[1] +delta[2]*delta[2]);
1861 vec_roll_to_mat3(delta, eBone->roll, mat);
1862 Mat4CpyMat3(bmat, mat);
1864 VecAddf(bmat[3], bmat[3], eBone->head);
1867 static void draw_ebones(View3D *v3d, ARegion *ar, Object *ob, int dt)
1869 RegionView3D *rv3d= ar->regiondata;
1871 bArmature *arm= ob->data;
1872 float smat[4][4], imat[4][4], bmat[4][4];
1876 /* envelope (deform distance) */
1877 if(arm->drawtype==ARM_ENVELOPE) {
1878 /* precalc inverse matrix for drawing screen aligned */
1880 Mat4MulFloat3(smat[0], 1.0f/VecLength(ob->obmat[0]));
1881 Mat4Invert(imat, smat);
1883 /* and draw blended distances */
1885 //glShadeModel(GL_SMOOTH);
1887 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
1889 for (eBone=arm->edbo->first, index=0; eBone; eBone=eBone->next, index++) {
1890 if (eBone->layer & arm->layer) {
1891 if ((eBone->flag & (BONE_HIDDEN_A|BONE_NO_DEFORM))==0) {
1892 if (eBone->flag & (BONE_SELECTED|BONE_TIPSEL|BONE_ROOTSEL))
1893 draw_sphere_bone_dist(smat, imat, eBone->flag, NULL, eBone);
1898 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
1899 glDisable(GL_BLEND);
1900 //glShadeModel(GL_FLAT);
1903 /* if solid we draw it first */
1904 if ((dt > OB_WIRE) && (arm->drawtype!=ARM_LINE)) {
1906 for (eBone=arm->edbo->first, index=0; eBone; eBone=eBone->next, index++) {
1907 if (eBone->layer & arm->layer) {
1908 if ((eBone->flag & BONE_HIDDEN_A)==0) {
1910 get_matrix_editbone(eBone, bmat);
1911 glMultMatrixf(bmat);
1913 /* catch exception for bone with hidden parent */
1915 if ( (eBone->parent) && ((eBone->parent->flag & BONE_HIDDEN_A) || (eBone->parent->layer & arm->layer)==0) )
1916 flag &= ~BONE_CONNECTED;
1918 if (arm->drawtype==ARM_ENVELOPE)
1919 draw_sphere_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
1920 else if(arm->drawtype==ARM_B_BONE)
1921 draw_b_bone(OB_SOLID, arm->flag, flag, 0, index, NULL, eBone);
1923 draw_bone(OB_SOLID, arm->flag, flag, 0, index, eBone->length);
1932 /* if wire over solid, set offset */
1935 if (arm->drawtype==ARM_LINE) {
1939 else if (dt > OB_WIRE)
1940 bglPolygonOffset(rv3d->dist, 1.0f);
1941 else if (arm->flag & ARM_EDITMODE)
1942 index= 0; /* do selection codes */
1944 for (eBone=arm->edbo->first; eBone; eBone=eBone->next) {
1945 if (eBone->layer & arm->layer) {
1946 if ((eBone->flag & BONE_HIDDEN_A)==0) {
1948 /* catch exception for bone with hidden parent */
1950 if ( (eBone->parent) && ((eBone->parent->flag & BONE_HIDDEN_A) || (eBone->parent->layer & arm->layer)==0) )
1951 flag &= ~BONE_CONNECTED;
1953 if (arm->drawtype == ARM_ENVELOPE) {
1955 draw_sphere_bone_wire(smat, imat, arm->flag, flag, 0, index, NULL, eBone);
1959 get_matrix_editbone(eBone, bmat);
1960 glMultMatrixf(bmat);
1962 if (arm->drawtype == ARM_LINE)
1963 draw_line_bone(arm->flag, flag, 0, index, NULL, eBone);
1964 else if (arm->drawtype == ARM_B_BONE)
1965 draw_b_bone(OB_WIRE, arm->flag, flag, 0, index, NULL, eBone);
1967 draw_bone(OB_WIRE, arm->flag, flag, 0, index, eBone->length);
1972 /* offset to parent */
1973 if (eBone->parent) {
1974 UI_ThemeColor(TH_WIRE);
1975 glLoadName (-1); // -1 here is OK!
1979 glVertex3fv(eBone->parent->tail);
1980 glVertex3fv(eBone->head);
1987 if(index!=-1) index++;
1991 if (arm->drawtype==ARM_LINE);
1992 else if (dt>OB_WIRE) bglPolygonOffset(rv3d->dist, 0.0f);
1994 /* finally names and axes */
1995 if (arm->flag & (ARM_DRAWNAMES|ARM_DRAWAXES)) {
1996 // patch for several 3d cards (IBM mostly) that crash on glSelect with text drawing
1997 if ((G.f & G_PICKSEL) == 0) {
2000 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2002 for (eBone=arm->edbo->first, index=0; eBone; eBone=eBone->next, index++) {
2003 if(eBone->layer & arm->layer) {
2004 if ((eBone->flag & BONE_HIDDEN_A)==0) {
2006 if (eBone->flag & BONE_SELECTED) UI_ThemeColor(TH_TEXT_HI);
2007 else UI_ThemeColor(TH_TEXT);
2010 if (arm->flag & ARM_DRAWNAMES) {
2011 VecMidf(vec, eBone->head, eBone->tail);
2012 glRasterPos3fv(vec);
2013 view3d_cached_text_draw_add(vec[0], vec[1], vec[2], eBone->name, 10);
2015 /* Draw additional axes */
2016 if (arm->flag & ARM_DRAWAXES) {
2018 get_matrix_editbone(eBone, bmat);
2019 bone_matrix_translate_y(bmat, eBone->length);
2020 glMultMatrixf(bmat);
2022 /* do cached text draw immediate to include transform */
2023 view3d_cached_text_draw_begin();
2024 drawaxes(eBone->length*0.25f, 0, OB_ARROWS);
2025 view3d_cached_text_draw_end(v3d, ar, 1, bmat);
2034 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2039 /* ****************************** Armature Visualisation ******************************** */
2041 /* ---------- Paths --------- */
2046 static void draw_pose_paths(Scene *scene, View3D *v3d, ARegion *ar, Object *ob)
2048 RegionView3D *rv3d= ar->regiondata;
2049 AnimData *adt= BKE_animdata_from_id(&ob->id);
2050 bArmature *arm= ob->data;
2051 bPoseChannel *pchan;
2054 float *fp, *fp_start;
2056 int sfra, efra, len;
2058 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2061 glLoadMatrixf(rv3d->viewmat);
2063 /* version patch here - cannot access frame info from file reading */
2064 if (arm->pathsize == 0) arm->pathsize= 1;
2065 stepsize = arm->pathsize;
2067 for (pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2068 if (pchan->bone->layer & arm->layer) {
2070 /* version patch here - cannot access frame info from file reading */
2071 if ((pchan->pathsf == 0) || (pchan->pathef == 0)) {
2072 pchan->pathsf= SFRA;
2073 pchan->pathef= EFRA;
2076 /* get frame ranges */
2077 if (arm->pathflag & ARM_PATH_ACFRA) {
2080 /* With "Around Current", we only choose frames from around
2081 * the current frame to draw. However, this range is still
2082 * restricted by the limits of the original path.
2084 sfra= CFRA - arm->pathbc;
2085 efra= CFRA + arm->pathac;
2086 if (sfra < pchan->pathsf) sfra= pchan->pathsf;
2087 if (efra > pchan->pathef) efra= pchan->pathef;
2091 sind= sfra - pchan->pathsf;
2092 fp_start= (pchan->path + (3*sind));
2095 sfra= pchan->pathsf;
2096 efra = sfra + pchan->pathlen;
2097 len = pchan->pathlen;
2098 fp_start = pchan->path;
2101 /* draw curve-line of path */
2102 glShadeModel(GL_SMOOTH);
2104 glBegin(GL_LINE_STRIP);
2105 for (a=0, fp=fp_start; a<len; a++, fp+=3) {
2106 float intensity; /* how faint */
2109 * - more intense for active/selected bones, less intense for unselected bones
2110 * - black for before current frame, green for current frame, blue for after current frame
2111 * - intensity decreases as distance from current frame increases
2113 #define SET_INTENSITY(A, B, C, min, max) (((1.0f - ((C - B) / (C - A))) * (max-min)) + min)
2114 if ((a+sfra) < CFRA) {
2115 /* black - before cfra */
2116 if (pchan->bone->flag & BONE_SELECTED) {
2118 intensity = SET_INTENSITY(sfra, a, CFRA, 0.25f, 0.75f);
2122 intensity = SET_INTENSITY(sfra, a, CFRA, 0.68f, 0.92f);
2124 UI_ThemeColorBlend(TH_WIRE, TH_BACK, intensity);
2126 else if ((a+sfra) > CFRA) {
2127 /* blue - after cfra */
2128 if (pchan->bone->flag & BONE_SELECTED) {
2130 intensity = SET_INTENSITY(CFRA, a, efra, 0.25f, 0.75f);
2134 intensity = SET_INTENSITY(CFRA, a, efra, 0.68f, 0.92f);
2136 UI_ThemeColorBlend(TH_BONE_POSE, TH_BACK, intensity);
2139 /* green - on cfra */
2140 if (pchan->bone->flag & BONE_SELECTED) {
2146 UI_ThemeColorBlendShade(TH_CFRAME, TH_BACK, intensity, 10);
2149 /* draw a vertex with this color */
2154 glShadeModel(GL_FLAT);
2158 /* draw little black point at each frame
2159 * NOTE: this is not really visible/noticable
2162 for (a=0, fp=fp_start; a<len; a++, fp+=3)
2166 /* Draw little white dots at each framestep value */
2167 UI_ThemeColor(TH_TEXT_HI);
2169 for (a=0, fp=fp_start; a<len; a+=stepsize, fp+=(stepsize*3))
2173 /* Draw frame numbers at each framestep value */
2174 if (arm->pathflag & ARM_PATH_FNUMS) {
2175 for (a=0, fp=fp_start; a<len; a+=stepsize, fp+=(stepsize*3)) {
2178 /* only draw framenum if several consecutive highlighted points don't occur on same point */
2180 sprintf(str, "%d", (a+sfra));
2181 view3d_cached_text_draw_add(fp[0], fp[1], fp[2], str, 0);
2183 else if ((a > stepsize) && (a < len-stepsize)) {
2184 if ((VecEqual(fp, fp-(stepsize*3))==0) || (VecEqual(fp, fp+(stepsize*3))==0)) {
2185 sprintf(str, "%d", (a+sfra));
2186 view3d_cached_text_draw_add(fp[0], fp[1], fp[2], str, 0);
2192 /* Keyframes - dots and numbers */
2193 if (arm->pathflag & ARM_PATH_KFRAS) {
2194 /* build list of all keyframes in active action for pchan */
2195 BLI_dlrbTree_init(&keys);
2198 bActionGroup *agrp= action_groups_find_named(adt->action, pchan->name);
2200 agroup_to_keylist(adt, agrp, &keys, NULL);
2201 BLI_dlrbTree_linkedlist_sync(&keys);
2205 /* Draw slightly-larger yellow dots at each keyframe */
2206 UI_ThemeColor(TH_VERTEX_SELECT);
2210 for (a=0, fp=fp_start; a<len; a++, fp+=3) {
2211 for (ak= keys.first; ak; ak= ak->next) {
2212 if (ak->cfra == (a+sfra))
2220 /* Draw frame numbers of keyframes */
2221 if ((arm->pathflag & ARM_PATH_FNUMS) || (arm->pathflag & ARM_PATH_KFNOS)) {
2222 for(a=0, fp=fp_start; a<len; a++, fp+=3) {
2223 for (ak= keys.first; ak; ak= ak->next) {
2224 if (ak->cfra == (a+sfra)) {
2227 sprintf(str, "%d", (a+sfra));
2228 view3d_cached_text_draw_add(fp[0], fp[1], fp[2], str, 0);
2234 BLI_dlrbTree_free(&keys);
2240 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2245 /* ---------- Ghosts --------- */
2247 /* helper function for ghost drawing - sets/removes flags for temporarily
2248 * hiding unselected bones while drawing ghosts
2250 static void ghost_poses_tag_unselected(Object *ob, short unset)
2252 bArmature *arm= ob->data;
2253 bPose *pose= ob->pose;
2254 bPoseChannel *pchan;
2256 /* don't do anything if no hiding any bones */
2257 if ((arm->flag & ARM_GHOST_ONLYSEL)==0)
2260 /* loop over all pchans, adding/removing tags as appropriate */
2261 for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
2262 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
2264 /* remove tags from all pchans if cleaning up */
2265 pchan->bone->flag &= ~BONE_HIDDEN_PG;
2268 /* set tags on unselected pchans only */
2269 if ((pchan->bone->flag & BONE_SELECTED)==0)
2270 pchan->bone->flag |= BONE_HIDDEN_PG;
2276 /* draw ghosts that occur within a frame range
2277 * note: object should be in posemode
2279 static void draw_ghost_poses_range(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2281 Object *ob= base->object;
2282 AnimData *adt= BKE_animdata_from_id(&ob->id);
2283 bArmature *arm= ob->data;
2284 bPose *posen, *poseo;
2285 float start, end, stepsize, range, colfac;
2286 int cfrao, flago, ipoflago;
2288 start = (float)arm->ghostsf;
2289 end = (float)arm->ghostef;
2293 stepsize= (float)(arm->ghostsize);
2294 range= (float)(end - start);
2297 ob->mode &= ~OB_MODE_POSE;
2300 arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2301 ipoflago= ob->ipoflag;
2302 ob->ipoflag |= OB_DISABLE_PATH;
2306 copy_pose(&posen, ob->pose, 1);
2308 armature_rebuild_pose(ob, ob->data); /* child pointers for IK */
2309 ghost_poses_tag_unselected(ob, 0); /* hide unselected bones if need be */
2312 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2314 /* draw from first frame of range to last */
2315 for (CFRA= (int)start; CFRA < end; CFRA += (int)stepsize) {
2316 colfac = (end - (float)CFRA) / range;
2317 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
2319 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2320 where_is_pose(scene, ob);
2321 draw_pose_channels(scene, v3d, ar, base, OB_WIRE);
2323 glDisable(GL_BLEND);
2324 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2326 ghost_poses_tag_unselected(ob, 1); /* unhide unselected bones if need be */
2333 armature_rebuild_pose(ob, ob->data);
2334 ob->mode |= OB_MODE_POSE;
2335 ob->ipoflag= ipoflago;
2338 /* draw ghosts on keyframes in action within range
2339 * - object should be in posemode
2341 static void draw_ghost_poses_keys(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2343 Object *ob= base->object;
2344 AnimData *adt= BKE_animdata_from_id(&ob->id);
2345 bAction *act= (adt) ? adt->action : NULL;
2346 bArmature *arm= ob->data;
2347 bPose *posen, *poseo;
2349 ActKeyColumn *ak, *akn;
2350 float start, end, range, colfac, i;
2353 start = (float)arm->ghostsf;
2354 end = (float)arm->ghostef;
2358 /* get keyframes - then clip to only within range */
2359 BLI_dlrbTree_init(&keys);
2360 action_to_keylist(adt, act, &keys, NULL);
2361 BLI_dlrbTree_linkedlist_sync(&keys);
2364 for (ak= keys.first; ak; ak= akn) {
2367 if ((ak->cfra < start) || (ak->cfra > end))
2368 BLI_freelinkN((ListBase *)&keys, ak);
2372 if (range == 0) return;
2375 ob->mode &= ~OB_MODE_POSE;
2378 arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2379 ob->ipoflag |= OB_DISABLE_PATH;
2383 copy_pose(&posen, ob->pose, 1);
2385 armature_rebuild_pose(ob, ob->data); /* child pointers for IK */
2386 ghost_poses_tag_unselected(ob, 0); /* hide unselected bones if need be */
2389 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2391 /* draw from first frame of range to last */
2392 for (ak=keys.first, i=0; ak; ak=ak->next, i++) {
2394 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
2396 CFRA= (int)ak->cfra;
2398 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2399 where_is_pose(scene, ob);
2400 draw_pose_channels(scene, v3d, ar, base, OB_WIRE);
2402 glDisable(GL_BLEND);
2403 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2405 ghost_poses_tag_unselected(ob, 1); /* unhide unselected bones if need be */
2406 BLI_dlrbTree_free(&keys);
2413 armature_rebuild_pose(ob, ob->data);
2414 ob->mode |= OB_MODE_POSE;
2417 /* draw ghosts around current frame
2418 * - object is supposed to be armature in posemode
2420 static void draw_ghost_poses(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
2422 Object *ob= base->object;
2423 AnimData *adt= BKE_animdata_from_id(&ob->id);
2424 bArmature *arm= ob->data;
2425 bPose *posen, *poseo;
2426 float cur, start, end, stepsize, range, colfac, actframe, ctime;
2429 /* pre conditions, get an action with sufficient frames */
2430 if ELEM(NULL, adt, adt->action)
2433 calc_action_range(adt->action, &start, &end, 0);
2437 stepsize= (float)(arm->ghostsize);
2438 range= (float)(arm->ghostep)*stepsize + 0.5f; /* plus half to make the for loop end correct */
2441 ob->mode &= ~OB_MODE_POSE;
2443 actframe= BKE_nla_tweakedit_remap(adt, (float)CFRA, 0);
2445 arm->flag &= ~(ARM_DRAWNAMES|ARM_DRAWAXES);
2449 copy_pose(&posen, ob->pose, 1);
2451 armature_rebuild_pose(ob, ob->data); /* child pointers for IK */
2452 ghost_poses_tag_unselected(ob, 0); /* hide unselected bones if need be */
2455 if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
2457 /* draw from darkest blend to lowest */
2458 for(cur= stepsize; cur<range; cur+=stepsize) {
2459 ctime= cur - (float)fmod(cfrao, stepsize); /* ensures consistant stepping */
2460 colfac= ctime/range;
2461 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
2463 /* only within action range */
2464 if (actframe+ctime >= start && actframe+ctime <= end) {
2465 CFRA= (int)BKE_nla_tweakedit_remap(adt, actframe+ctime, NLATIME_CONVERT_MAP);
2467 if (CFRA != cfrao) {
2468 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2469 where_is_pose(scene, ob);
2470 draw_pose_channels(scene, v3d, ar, base, OB_WIRE);
2474 ctime= cur + (float)fmod((float)cfrao, stepsize) - stepsize+1.0f; /* ensures consistant stepping */
2475 colfac= ctime/range;
2476 UI_ThemeColorShadeAlpha(TH_WIRE, 0, -128-(int)(120.0*sqrt(colfac)));
2478 /* only within action range */
2479 if ((actframe-ctime >= start) && (actframe-ctime <= end)) {
2480 CFRA= (int)BKE_nla_tweakedit_remap(adt, actframe-ctime, NLATIME_CONVERT_MAP);
2482 if (CFRA != cfrao) {
2483 BKE_animsys_evaluate_animdata(&ob->id, adt, (float)CFRA, ADT_RECALC_ALL);
2484 where_is_pose(scene, ob);
2485 draw_pose_channels(scene, v3d, ar, base, OB_WIRE);
2489 glDisable(GL_BLEND);
2490 if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
2492 ghost_poses_tag_unselected(ob, 1); /* unhide unselected bones if need be */
2499 armature_rebuild_pose(ob, ob->data);
2500 ob->mode |= OB_MODE_POSE;
2503 /* ********************************** Armature Drawing - Main ************************* */
2505 /* called from drawobject.c, return 1 if nothing was drawn */
2506 int draw_armature(Scene *scene, View3D *v3d, ARegion *ar, Base *base, int dt, int flag)
2508 Object *ob= base->object;
2509 bArmature *arm= ob->data;
2512 if(G.f & G_RENDER_SHADOW)
2515 if(dt>OB_WIRE && arm->drawtype!=ARM_LINE) {
2516 /* we use color for solid lighting */
2517 glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
2518 glEnable(GL_COLOR_MATERIAL);
2519 glColor3ub(0,0,0); // clear spec
2520 glDisable(GL_COLOR_MATERIAL);
2522 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
2523 glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW); // only for lighting...
2526 /* arm->flag is being used to detect mode... */
2529 arm->flag |= ARM_EDITMODE;
2530 draw_ebones(v3d, ar, ob, dt);
2531 arm->flag &= ~ARM_EDITMODE;
2535 if(ob->pose && ob->pose->chanbase.first) {
2536 /* drawing posemode selection indices or colors only in these cases */
2537 if(!(base->flag & OB_FROMDUPLI)) {
2538 if(G.f & G_PICKSEL) {
2539 if(OBACT && (OBACT->mode & OB_MODE_WEIGHT_PAINT)) {
2540 if(ob==modifiers_isDeformedByArmature(OBACT))
2541 arm->flag |= ARM_POSEMODE;
2543 else if(ob->mode & OB_MODE_POSE)
2544 arm->flag |= ARM_POSEMODE;
2546 else if(ob->mode & OB_MODE_POSE) {
2547 if (arm->ghosttype == ARM_GHOST_RANGE) {
2548 draw_ghost_poses_range(scene, v3d, ar, base);
2550 else if (arm->ghosttype == ARM_GHOST_KEYS) {
2551 draw_ghost_poses_keys(scene, v3d, ar, base);
2553 else if (arm->ghosttype == ARM_GHOST_CUR) {
2555 draw_ghost_poses(scene, v3d, ar, base);
2557 if ((flag & DRAW_SCENESET)==0) {
2559 arm->flag |= ARM_POSEMODE;
2560 else if(OBACT && (OBACT->mode & OB_MODE_WEIGHT_PAINT)) {
2561 if(ob==modifiers_isDeformedByArmature(OBACT))
2562 arm->flag |= ARM_POSEMODE;
2564 draw_pose_paths(scene, v3d, ar, ob);
2568 draw_pose_channels(scene, v3d, ar, base, dt);
2569 arm->flag &= ~ARM_POSEMODE;
2571 if(ob->mode & OB_MODE_POSE)
2572 UI_ThemeColor(TH_WIRE); /* restore, for extra draw stuff */
2577 glFrontFace(GL_CCW);
2582 /* *************** END Armature drawing ******************* */