Xonotic
player_skeleton.qc
Go to the documentation of this file.
1 #include "player_skeleton.qh"
2 
8 
10 
11 classfield(Skeleton) .float skeleton_info_modelindex;
12 classfield(Skeleton) .float skeleton_info_skin;
13 const int BONETYPE_LOWER = 0;
14 const int BONETYPE_UPPER = 1;
15 const int MAX_BONES = 128;
16 classfield(Skeleton) .float skeleton_bonetype[MAX_BONES];
17 classfield(Skeleton) .float skeleton_numbones;
18 
20 {
21  int i;
22  if(e.skeleton_info_modelindex == e.modelindex && e.skeleton_info_skin == e.skin)
23  return;
24  e.bone_upperbody = 0;
25  e.bone_weapon = gettagindex(e, "weapon");
26  if(!e.bone_weapon)
27  e.bone_weapon = gettagindex(e, "tag_weapon");
28  if(!e.bone_weapon)
29  e.bone_weapon = gettagindex(e, "bip01 r hand");
30  MUTATOR_CALLHOOK(Skeleton_CheckBones, e);
31  for(i = 0; i < MAX_AIM_BONES; ++i)
32  {
33  e.(bone_aim[i]) = 0;
34  e.(bone_aimweight[i]) = 0;
35  }
36  e.fixbone = 0;
37  if(get_model_parameters(e.model, e.skin))
38  {
40  e.bone_upperbody = gettagindex(e, get_model_parameters_bone_upperbody);
41  if(e.bone_upperbody)
42  e.fixbone = get_model_parameters_fixbone;
45  MUTATOR_CALLHOOK(Skeleton_CheckModel, e);
46  for(i = 0; i < MAX_AIM_BONES; ++i)
47  {
49  e.(bone_aim[i]) = gettagindex(e, get_model_parameters_bone_aim[i]);
50  if(e.bone_aim[i])
51  e.(bone_aimweight[i]) = get_model_parameters_bone_aimweight[i];
52  }
53  }
54  else
55  LOG_TRACE("No model parameters for ", e.model);
56  //dprint(e.model, " uses ", ftos(e.bone_upperbody), " ", ftos(e.fixbone), "\n");
58  e.skeleton_info_modelindex = e.modelindex;
59  e.skeleton_info_skin = e.skin;
60  if(e.skeletonindex)
61  {
62  skel_delete(e.skeletonindex);
63  e.skeletonindex = 0;
64  }
65 }
66 
68 {
69  float s = e.skeletonindex;
70  float n = (e.skeleton_numbones = skel_get_numbones(s));
71  int i;
72  for(i = 1; i <= n; ++i)
73  {
74  float t = BONETYPE_LOWER;
75  int p = skel_get_boneparent(s, i);
76  if(p > 0)
77  t = e.(skeleton_bonetype[p-1]);
78  if(i == e.bone_upperbody)
79  t = BONETYPE_UPPER;
80  e.(skeleton_bonetype[i-1]) = t;
81  }
82 }
83 
84 void skel_set_boneabs(float s, int bone, vector absorg)
85 {
86  TC(int, bone);
88 
89  vector parentorg = skel_get_boneabs(s, skel_get_boneparent(s, bone));
90  vector parentang = fixedvectoangles2(v_forward, v_up);
91 
92  vector relang = AnglesTransform_LeftDivide(parentang, absang);
93  vector relorg = AnglesTransform_Apply(AnglesTransform_Invert(parentang), absorg - parentorg);
94 
95  fixedmakevectors(relang);
96  skel_set_bone(s, bone, relorg);
97 
98  /*
99  vector neworg = skel_get_boneabs(s, bone);
100  printf("ANG: want: %v, got: %v\n", absang, fixedvectoangles2(v_forward, v_up));
101  printf("ORG: want: %v, got: %v\n", absorg, neworg);
102  */
103 }
104 
106 {
107  if(e.skeletonindex)
108  {
109  skel_delete(e.skeletonindex);
110  e.skeletonindex = 0;
111  }
112 }
113 
114 void skeleton_from_frames(entity e, bool is_dead)
115 {
116  TC(bool, is_dead);
117  float m = e.modelindex;
118  if(!e.skeletonindex)
119  {
120  e.skeletonindex = skel_create(m);
122  }
123  float s = e.skeletonindex;
124  if(!s)
125  return;
126  float n = e.skeleton_numbones;
127  float savelerpfrac = e.lerpfrac;
128  float savelerpfrac3 = e.lerpfrac3;
129  float savelerpfrac4 = e.lerpfrac4;
130 
131  vector fixbone_oldangles = '0 0 0';
132  if(e.fixbone)
133  {
134  // make all bones BONETYPE_UPPER
135  e.lerpfrac = 0;
136  e.lerpfrac3 = savelerpfrac3 * 2;
137  e.lerpfrac4 = 0;
138  // build skeleton
139  skel_build(s, e, m, 0, 1, n);
140  // get hip bone
141  skel_get_boneabs(s, e.bone_upperbody);
142  fixbone_oldangles = fixedvectoangles2(v_forward, v_up);
143  }
144  int bone;
145  for(bone = 0; bone < n; )
146  {
147  float firstbone = bone;
148  float bonetype = e.skeleton_bonetype[bone];
149  for(++bone; (bone < n) && (e.skeleton_bonetype[bone] == bonetype); ++bone)
150  ;
151  if(bonetype == BONETYPE_UPPER)
152  {
153  // only show frames 1+3 (upper body)
154  e.lerpfrac = 0;
155  e.lerpfrac3 = savelerpfrac3 * 2;
156  e.lerpfrac4 = 0;
157  }
158  else
159  {
160  // only show frames 2+4 (lower body)
161  e.lerpfrac = savelerpfrac * 2;
162  e.lerpfrac3 = 0;
163  e.lerpfrac4 = savelerpfrac4 * 2;
164  }
165  //printf("Run: bone %d to %d, type %d\n", firstbone + 1, bone, bonetype);
166  //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);
167  skel_build(s, e, m, 0, firstbone + 1, bone);
168  }
169  e.lerpfrac = savelerpfrac;
170  e.lerpfrac3 = savelerpfrac3;
171  e.lerpfrac4 = savelerpfrac4;
172 
173  if(e.fixbone)
174  {
175  // FIX IT
176  vector org = skel_get_boneabs(s, e.bone_upperbody);
177  fixedmakevectors(fixbone_oldangles);
178  skel_set_boneabs(s, e.bone_upperbody, org);
179  }
180 
181  if(!is_dead)
182  {
183  if(e == csqcplayer)
184  {
185  if(e.move_movetype == MOVETYPE_NONE)
186  {
187  if(!e.v_angle_save_x)
188  e.v_angle_save_x = input_angles.x;
189  e.v_angle_x = e.v_angle_save_x;
190  }
191  else
192  e.v_angle_x = input_angles.x;
193  }
194  int i;
195  for(i = 0; i < MAX_AIM_BONES; ++i)
196  {
197  if(e.(bone_aim[i]))
198  {
199  vector aim = '1 0 0' * bound(-90, e.v_angle_x, 90) * e.(bone_aimweight[i]);
200  vector org = skel_get_boneabs(s, e.(bone_aim[i]));
202  vector ang = AnglesTransform_Multiply(aim, ang_cur);
203  fixedmakevectors(ang);
204  skel_set_boneabs(s, e.(bone_aim[i]), org);
205  }
206  }
207  }
208 }
float MOVETYPE_NONE
Definition: progsdefs.qc:246
void skeleton_markbones(entity e)
string string_null
Definition: nil.qh:9
float get_model_parameters(string m, float sk)
Definition: util.qc:1258
const int MAX_AIM_BONES
Definition: util.qh:148
entity() spawn
vector input_angles
Definition: csprogsdefs.qc:60
#define skel_get_boneparent
Definition: dpextensions.qh:36
float get_model_parameters_fixbone
Definition: util.qh:151
const int BONETYPE_LOWER
void skel_set_boneabs(float s, int bone, vector absorg)
#define gettagindex
Definition: dpextensions.qh:16
const int BONETYPE_UPPER
classfield(Skeleton) .float skeleton_info_modelindex
vector v_up
Definition: csprogsdefs.qc:31
string get_model_parameters_bone_aim[MAX_AIM_BONES]
Definition: util.qh:149
float v_angle_save_x
vector AnglesTransform_Multiply(vector t1, vector t2)
#define skel_get_boneabs
Definition: post.qh:25
#define TC(T, sym)
Definition: _all.inc:82
string get_model_parameters_bone_weapon
Definition: util.qh:147
vector(float skel, float bonenum) _skel_get_boneabs_hidden
string get_model_parameters_bone_upperbody
Definition: util.qh:146
vector AnglesTransform_LeftDivide(vector from_transform, vector to_transform)
vector AnglesTransform_Invert(vector transform)
#define LOG_TRACE(...)
Definition: log.qh:81
#define skel_set_bone
Definition: post.qh:26
const int MAX_BONES
#define aim
Definition: post.qh:22
#define MUTATOR_CALLHOOK(id,...)
Definition: base.qh:140
void free_skeleton_from_frames(entity e)
float get_model_parameters_bone_aimweight[MAX_AIM_BONES]
Definition: util.qh:150
entity csqcplayer
Definition: cl_player.qh:26
vector AnglesTransform_Apply(vector transform, vector v)
void skeleton_from_frames(entity e, bool is_dead)
void skeleton_loadinfo(entity e)
#define fixedmakevectors
#define fixedvectoangles2(a, b)
vector v_forward
Definition: csprogsdefs.qc:31