]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/player_skeleton.qc
4039c905c1f135f3afea1e2e6d1e76d7c65947c5
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / player_skeleton.qc
1 #include "player_skeleton.qh"
2
3 #include "../common/csqcmodel_settings.qh"
4
5 #include "../warpzonelib/anglestransform.qh"
6
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;
14
15 void skeleton_loadinfo(entity e)
16 {
17         int i;
18         if(e.skeleton_info_modelindex == e.modelindex && e.skeleton_info_skin == e.skin)
19                 return;
20         e.bone_upperbody = 0;
21         e.bone_weapon = gettagindex(e, "weapon");
22         if(!e.bone_weapon)
23                 e.bone_weapon = gettagindex(e, "tag_weapon");
24         if(!e.bone_weapon)
25                 e.bone_weapon = gettagindex(e, "bip01 r hand");
26         for(i = 0; i < MAX_AIM_BONES; ++i)
27         {
28                 e.(bone_aim[i]) = 0;
29                 e.(bone_aimweight[i]) = 0;
30         }
31         e.fixbone = 0;
32         if(get_model_parameters(e.model, e.skin))
33         {
34                 if(get_model_parameters_bone_upperbody)
35                         e.bone_upperbody = gettagindex(e, get_model_parameters_bone_upperbody);
36                 if(e.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)
41                 {
42                         if(get_model_parameters_bone_aim[i])
43                                 e.(bone_aim[i]) = gettagindex(e, get_model_parameters_bone_aim[i]);
44                         if(e.bone_aim[i])
45                                 e.(bone_aimweight[i]) = get_model_parameters_bone_aimweight[i];
46                 }
47         }
48         else
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;
54         if(e.skeletonindex)
55         {
56                 skel_delete(e.skeletonindex);
57                 e.skeletonindex = 0;
58         }
59 }
60
61 void skeleton_markbones(entity e)
62 {
63         float s = e.skeletonindex;
64         float n = (e.skeleton_numbones = skel_get_numbones(s));
65         int i;
66         for(i = 1; i <= n; ++i)
67         {
68                 float t = BONETYPE_LOWER;
69                 int p = skel_get_boneparent(s, i);
70                 if(p > 0)
71                         t = e.(skeleton_bonetype[p-1]);
72                 if(i == e.bone_upperbody)
73                         t = BONETYPE_UPPER;
74                 e.(skeleton_bonetype[i-1]) = t;
75         }
76 }
77
78 void skel_set_boneabs(float s, float bone, vector absorg)
79 {
80         vector absang = fixedvectoangles2(v_forward, v_up);
81
82         vector parentorg = skel_get_boneabs(s, skel_get_boneparent(s, bone));
83         vector parentang = fixedvectoangles2(v_forward, v_up);
84
85         vector relang = AnglesTransform_LeftDivide(parentang, absang);
86         vector relorg = AnglesTransform_Apply(AnglesTransform_Invert(parentang), absorg - parentorg);
87
88         fixedmakevectors(relang);
89         skel_set_bone(s, bone, relorg);
90
91         /*
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);
95         */
96 }
97
98 void free_skeleton_from_frames(entity e)
99 {
100         if(e.skeletonindex)
101         {
102                 skel_delete(e.skeletonindex);
103                 e.skeletonindex = 0;
104         }
105 }
106
107 void skeleton_from_frames(entity e, float is_dead)
108 {
109         float m = e.modelindex;
110         if(!e.skeletonindex)
111         {
112                 e.skeletonindex = skel_create(m);
113                 skeleton_markbones(e);
114         }
115         float s = e.skeletonindex;
116         if(!s)
117                 return;
118         float n = e.skeleton_numbones;
119         float savelerpfrac = e.lerpfrac;
120         float savelerpfrac3 = e.lerpfrac3;
121         float savelerpfrac4 = e.lerpfrac4;
122
123         vector fixbone_oldangles = '0 0 0';
124         if(e.fixbone)
125         {
126                 // make all bones BONETYPE_UPPER
127                 e.lerpfrac = 0;
128                 e.lerpfrac3 = savelerpfrac3 * 2;
129                 e.lerpfrac4 = 0;
130                 // build skeleton
131                 skel_build(s, e, m, 0, 1, n);
132                 // get hip bone
133                 skel_get_boneabs(s, e.bone_upperbody);
134                 fixbone_oldangles = fixedvectoangles2(v_forward, v_up);
135         }
136         int bone;
137         for(bone = 0; bone < n; )
138         {
139                 float firstbone = bone;
140                 float bonetype = e.skeleton_bonetype[bone];
141                 for(++bone; (bone < n) && (e.skeleton_bonetype[bone] == bonetype); ++bone)
142                         ;
143                 if(bonetype == BONETYPE_UPPER)
144                 {
145                         // only show frames 1+3 (upper body)
146                         e.lerpfrac = 0;
147                         e.lerpfrac3 = savelerpfrac3 * 2;
148                         e.lerpfrac4 = 0;
149                 }
150                 else
151                 {
152                         // only show frames 2+4 (lower body)
153                         e.lerpfrac = savelerpfrac * 2;
154                         e.lerpfrac3 = 0;
155                         e.lerpfrac4 = savelerpfrac4 * 2;
156                 }
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);
160         }
161         e.lerpfrac = savelerpfrac;
162         e.lerpfrac3 = savelerpfrac3;
163         e.lerpfrac4 = savelerpfrac4;
164
165         if(e.fixbone)
166         {
167                 // FIX IT
168                 vector org = skel_get_boneabs(s, e.bone_upperbody);
169                 fixedmakevectors(fixbone_oldangles);
170                 skel_set_boneabs(s, e.bone_upperbody, org);
171         }
172
173         if(!is_dead)
174         {
175                 if(self == csqcplayer)
176                         self.v_angle_x = input_angles_x;
177                 int i;
178                 for(i = 0; i < MAX_AIM_BONES; ++i)
179                 {
180                         if(e.(bone_aim[i]))
181                         {
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);
188                         }
189                 }
190         }
191 }