2 * Copyright (c) 2011 Rudolf PolzerCSQCModel_InterpolateAnimation_2To4_PreNote
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 #include "../dpdefs/csprogsdefs.qh"
24 #include "../client/defs.qh"
25 #include "../common/util.qh"
26 #include "../common/animdecide.qh"
27 #include "interpolate.qh"
28 #include "../common/csqcmodel_settings.qh"
30 #include "cl_model.qh"
31 #include "cl_player.qh"
36 float autocvar_cl_lerpanim_maxdelta_framegroups = 0.1;
37 float autocvar_cl_nolerp = 0;
39 .float csqcmodel_lerpfrac;
40 .float csqcmodel_lerpfrac2;
41 .float csqcmodel_lerpfractime;
42 .float csqcmodel_lerpfrac2time;
44 void CSQCModel_InterpolateAnimation_2To4_PreNote(int sf)
46 if(sf & CSQCMODEL_PROPERTY_FRAME)
48 self.frame3 = self.frame;
49 self.frame3time = self.frame1time;
51 if(sf & CSQCMODEL_PROPERTY_FRAME2)
53 self.frame4 = self.frame2;
54 self.frame4time = self.frame2time;
56 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
58 self.csqcmodel_lerpfrac2 = self.csqcmodel_lerpfrac;
59 self.csqcmodel_lerpfrac2time = self.csqcmodel_lerpfractime;
60 self.lerpfrac = self.csqcmodel_lerpfrac;
63 void CSQCModel_InterpolateAnimation_1To2_PreNote(int sf)
65 if(sf & CSQCMODEL_PROPERTY_FRAME)
67 self.frame2 = self.frame;
68 self.frame2time = self.frame1time;
71 void CSQCModel_InterpolateAnimation_PreNote(int sf)
73 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
74 CSQCModel_InterpolateAnimation_2To4_PreNote(sf);
76 CSQCModel_InterpolateAnimation_1To2_PreNote(sf);
80 void CSQCModel_InterpolateAnimation_2To4_Note(int sf, float set_times)
82 if(sf & CSQCMODEL_PROPERTY_FRAME)
85 self.frame1time = time;
87 if(sf & CSQCMODEL_PROPERTY_FRAME2)
90 self.frame2time = time;
92 if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
94 self.csqcmodel_lerpfrac = self.lerpfrac;
96 self.csqcmodel_lerpfractime = time;
99 void CSQCModel_InterpolateAnimation_1To2_Note(int sf, float set_times)
101 if(sf & CSQCMODEL_PROPERTY_FRAME)
104 self.frame1time = time;
107 void CSQCModel_InterpolateAnimation_Note(int sf)
109 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
110 CSQCModel_InterpolateAnimation_2To4_Note(sf, true);
112 CSQCModel_InterpolateAnimation_1To2_Note(sf, true);
116 void CSQCModel_InterpolateAnimation_2To4_Do()
118 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
120 self.lerpfrac = self.csqcmodel_lerpfrac;
129 if(self.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
132 l13 = bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
134 if(self.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
137 l24 = bound(0, (time - self.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
139 if(self.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
142 llf = bound(0, (time - self.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
144 l24_13 = self.csqcmodel_lerpfrac * llf + self.csqcmodel_lerpfrac2 * (1 - llf);
146 self.lerpfrac = l24 * l24_13;
147 self.lerpfrac4 = (1 - l24) * l24_13;
148 self.lerpfrac3 = (1 - l13) * (1 - l24_13);
150 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
156 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
163 void CSQCModel_InterpolateAnimation_1To2_Do()
165 if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
171 if(self.frame2time == 0) // if frame2 was not previously displayed, only frame1 can make sense
174 self.lerpfrac = 1 - bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
177 void CSQCModel_InterpolateAnimation_Do()
179 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
180 CSQCModel_InterpolateAnimation_2To4_Do();
182 CSQCModel_InterpolateAnimation_1To2_Do();
186 void CSQCModel_Draw()
188 // some nice flags for CSQCMODEL_IF and the hooks
189 bool isplayer = (self.entnum >= 1 && self.entnum <= maxclients);
190 noref bool islocalplayer = (self.entnum == player_localnum + 1);
191 noref bool isnolocalplayer = (isplayer && (self.entnum != player_localnum + 1));
193 // we don't do this for the local player as that one is already handled
194 // by CSQCPlayer_SetCamera()
195 if(!CSQCPlayer_IsLocalPlayer())
196 InterpolateOrigin_Do();
198 CSQCModel_InterpolateAnimation_Do();
200 { CSQCMODEL_HOOK_PREDRAW }
202 // inherit draw flags easily
204 while(root.tag_entity)
205 root = root.tag_entity;
208 self.renderflags &= ~(RF_EXTERNALMODEL | RF_VIEWMODEL);
209 self.renderflags |= (root.renderflags & (RF_EXTERNALMODEL | RF_VIEWMODEL));
212 // we're drawn, now teleporting is over
213 self.csqcmodel_teleported = 0;
216 void CSQCModel_Read(bool isnew)
218 int sf = ReadInt24_t();
220 // some nice flags for CSQCMODEL_IF and the hooks
221 bool isplayer = (self.entnum >= 1 && self.entnum <= maxclients);
222 bool islocalplayer = (self.entnum == player_localnum + 1);
223 noref bool isnolocalplayer = (isplayer && (self.entnum != player_localnum + 1));
225 self.classname = "csqcmodel";
226 self.iflags |= IFLAG_ORIGIN; // interpolate origin too
227 self.iflags |= IFLAG_ANGLES; // interpolate angles too
228 self.iflags |= IFLAG_VELOCITY | IFLAG_AUTOVELOCITY; // let's calculate velocity automatically
230 { CSQCMODEL_HOOK_PREUPDATE }
232 CSQCPlayer_PreUpdate();
233 InterpolateOrigin_Undo();
234 CSQCModel_InterpolateAnimation_PreNote(sf);
236 #define CSQCMODEL_IF(cond) if(cond) {
237 #define CSQCMODEL_ENDIF }
238 #define CSQCMODEL_PROPERTY(flag,t,r,w,f) \
241 #define CSQCMODEL_PROPERTY_SCALED(flag,t,r,w,f,s,mi,ma) \
243 self.f = (r() + mi) / s;
245 #undef CSQCMODEL_PROPERTY_SCALED
246 #undef CSQCMODEL_PROPERTY
247 #undef CSQCMODEL_ENDIF
250 if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
252 vector pmin = self.mins, pmax = self.maxs;
253 setmodelindex(self, self.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
254 setsize(self, pmin, pmax);
257 if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
259 self.iflags |= IFLAG_TELEPORTED;
260 self.csqcmodel_teleported = 1;
263 CSQCModel_InterpolateAnimation_Note(sf);
264 InterpolateOrigin_Note();
265 CSQCPlayer_PostUpdate();
267 { CSQCMODEL_HOOK_POSTUPDATE }
269 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
270 InterpolateOrigin_Do();
271 CSQCModel_InterpolateAnimation_Do();
275 setorigin(self, self.origin);
277 // set obvious render flags
278 if(self.entnum == player_localentnum)
279 self.renderflags |= RF_EXTERNALMODEL;
281 self.renderflags &= ~RF_EXTERNALMODEL;
284 self.drawmask = MASK_NORMAL;
285 self.predraw = CSQCModel_Draw;
288 entity CSQCModel_server2csqc(float pl)
290 return findfloat(world, entnum, pl); // FIXME optimize this using an array