1 #include "player_skeleton.qh"
3 #include "../common/csqcmodel_settings.qh"
5 #include "../warpzonelib/anglestransform.qh"
7 class(Skeleton) .float skeleton_info_modelindex;
8 class(Skeleton) .float skeleton_info_skin;
9 const int BONETYPE_LOWER = 0;
10 const int BONETYPE_UPPER = 1;
11 const int MAX_BONES = 128;
12 class(Skeleton) .float skeleton_bonetype[MAX_BONES];
13 class(Skeleton) .float skeleton_numbones;
15 void skeleton_loadinfo(entity e)
18 if(e.skeleton_info_modelindex == e.modelindex && e.skeleton_info_skin == e.skin)
21 e.bone_weapon = gettagindex(e, "weapon");
23 e.bone_weapon = gettagindex(e, "tag_weapon");
25 e.bone_weapon = gettagindex(e, "bip01 r hand");
26 for(i = 0; i < MAX_AIM_BONES; ++i)
29 e.(bone_aimweight[i]) = 0;
32 if(get_model_parameters(e.model, e.skin))
34 if(get_model_parameters_bone_upperbody)
35 e.bone_upperbody = gettagindex(e, get_model_parameters_bone_upperbody);
37 e.fixbone = get_model_parameters_fixbone;
38 if(get_model_parameters_bone_weapon)
39 e.bone_weapon = gettagindex(e, get_model_parameters_bone_weapon);
40 for(i = 0; i < MAX_AIM_BONES; ++i)
42 if(get_model_parameters_bone_aim[i])
43 e.(bone_aim[i]) = gettagindex(e, get_model_parameters_bone_aim[i]);
45 e.(bone_aimweight[i]) = get_model_parameters_bone_aimweight[i];
49 dprint("No model parameters for ", e.model, "\n");
50 //dprint(e.model, " uses ", ftos(e.bone_upperbody), " ", ftos(e.fixbone), "\n");
51 get_model_parameters(string_null, 0);
52 e.skeleton_info_modelindex = e.modelindex;
53 e.skeleton_info_skin = e.skin;
56 skel_delete(e.skeletonindex);
61 void skeleton_markbones(entity e)
63 float s = e.skeletonindex;
64 float n = (e.skeleton_numbones = skel_get_numbones(s));
66 for(i = 1; i <= n; ++i)
68 float t = BONETYPE_LOWER;
69 int p = skel_get_boneparent(s, i);
71 t = e.(skeleton_bonetype[p-1]);
72 if(i == e.bone_upperbody)
74 e.(skeleton_bonetype[i-1]) = t;
78 void skel_set_boneabs(float s, float bone, vector absorg)
80 vector absang = fixedvectoangles2(v_forward, v_up);
82 vector parentorg = skel_get_boneabs(s, skel_get_boneparent(s, bone));
83 vector parentang = fixedvectoangles2(v_forward, v_up);
85 vector relang = AnglesTransform_LeftDivide(parentang, absang);
86 vector relorg = AnglesTransform_Apply(AnglesTransform_Invert(parentang), absorg - parentorg);
88 fixedmakevectors(relang);
89 skel_set_bone(s, bone, relorg);
92 vector neworg = skel_get_boneabs(s, bone);
93 printf("ANG: want: %v, got: %v\n", absang, fixedvectoangles2(v_forward, v_up));
94 printf("ORG: want: %v, got: %v\n", absorg, neworg);
98 void free_skeleton_from_frames(entity e)
102 skel_delete(e.skeletonindex);
107 void skeleton_from_frames(entity e, float is_dead)
109 float m = e.modelindex;
112 e.skeletonindex = skel_create(m);
113 skeleton_markbones(e);
115 float s = e.skeletonindex;
118 float n = e.skeleton_numbones;
119 float savelerpfrac = e.lerpfrac;
120 float savelerpfrac3 = e.lerpfrac3;
121 float savelerpfrac4 = e.lerpfrac4;
123 vector fixbone_oldangles = '0 0 0';
126 // make all bones BONETYPE_UPPER
128 e.lerpfrac3 = savelerpfrac3 * 2;
131 skel_build(s, e, m, 0, 1, n);
133 skel_get_boneabs(s, e.bone_upperbody);
134 fixbone_oldangles = fixedvectoangles2(v_forward, v_up);
137 for(bone = 0; bone < n; )
139 float firstbone = bone;
140 float bonetype = e.skeleton_bonetype[bone];
141 for(++bone; (bone < n) && (e.skeleton_bonetype[bone] == bonetype); ++bone)
143 if(bonetype == BONETYPE_UPPER)
145 // only show frames 1+3 (upper body)
147 e.lerpfrac3 = savelerpfrac3 * 2;
152 // only show frames 2+4 (lower body)
153 e.lerpfrac = savelerpfrac * 2;
155 e.lerpfrac4 = savelerpfrac4 * 2;
157 //printf("Run: bone %d to %d, type %d\n", firstbone + 1, bone, bonetype);
158 //printf("frame %d %d %d %d lerpfrac * %d %d %d\n", e.frame, e.frame2, e.frame3, e.frame4, e.lerpfrac, e.lerpfrac3, e.lerpfrac4);
159 skel_build(s, e, m, 0, firstbone + 1, bone);
161 e.lerpfrac = savelerpfrac;
162 e.lerpfrac3 = savelerpfrac3;
163 e.lerpfrac4 = savelerpfrac4;
168 vector org = skel_get_boneabs(s, e.bone_upperbody);
169 fixedmakevectors(fixbone_oldangles);
170 skel_set_boneabs(s, e.bone_upperbody, org);
175 if(self == csqcplayer)
176 self.v_angle_x = input_angles_x;
178 for(i = 0; i < MAX_AIM_BONES; ++i)
182 vector aim = '1 0 0' * bound(-90, self.v_angle.x, 90) * e.(bone_aimweight[i]);
183 vector org = skel_get_boneabs(s, e.(bone_aim[i]));
184 vector ang_cur = fixedvectoangles2(v_forward, v_up);
185 vector ang = AnglesTransform_Multiply(aim, ang_cur);
186 fixedmakevectors(ang);
187 skel_set_boneabs(s, e.(bone_aim[i]), org);