]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/csqcmodel/cl_model.qc
Merge branch 'master' into divVerent/csqcmodel
[xonotic/xonotic-data.pk3dir.git] / qcsrc / csqcmodel / cl_model.qc
1 /*
2  * Copyright (c) 2011 Rudolf Polzer
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * IN THE SOFTWARE.
21  */
22
23 var float autocvar_cl_lerpanim_maxdelta_framegroups = 0.1;
24 var float autocvar_cl_nolerp = 0;
25
26 .float csqcmodel_lerpfrac;
27 .float csqcmodel_lerpfrac2;
28 .float csqcmodel_lerpfractime;
29 .float csqcmodel_lerpfrac2time;
30 .float csqcmodel_teleported;
31
32 void CSQCModel_InterpolateAnimation_PreNote(float sf)
33 {
34 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
35         if(sf & CSQCMODEL_PROPERTY_FRAME)
36         {
37                 self.frame3 = self.frame;
38                 self.frame3time = self.frame1time;
39         }
40         if(sf & CSQCMODEL_PROPERTY_FRAME2)
41         {
42                 self.frame4 = self.frame2;
43                 self.frame4time = self.frame2time;
44         }
45         if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
46         {
47                 self.csqcmodel_lerpfrac2 = self.csqcmodel_lerpfrac;
48                 self.csqcmodel_lerpfrac2time = self.csqcmodel_lerpfractime;
49                 self.lerpfrac = self.csqcmodel_lerpfrac;
50         }
51 #else
52         if(sf & CSQCMODEL_PROPERTY_FRAME)
53         {
54                 self.frame2 = self.frame;
55                 self.frame2time = self.frame1time;
56         }
57 #endif
58 }
59
60 void CSQCModel_InterpolateAnimation_Note(float sf)
61 {
62 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
63         if(sf & CSQCMODEL_PROPERTY_FRAME)
64         {
65                 self.frame1time = time;
66         }
67         if(sf & CSQCMODEL_PROPERTY_FRAME2)
68         {
69                 self.frame2time = time;
70         }
71         if(sf & CSQCMODEL_PROPERTY_LERPFRAC)
72         {
73                 self.csqcmodel_lerpfrac = self.lerpfrac;
74                 self.csqcmodel_lerpfractime = time;
75         }
76 #else
77         if(sf & CSQCMODEL_PROPERTY_FRAME)
78         {
79                 self.frame1time = time;
80         }
81 #endif
82 }
83
84 void CSQCModel_InterpolateAnimation_Do()
85 {
86 #ifdef CSQCMODEL_HAVE_TWO_FRAMES
87         if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
88         {
89                 self.lerpfrac = self.csqcmodel_lerpfrac;
90                 self.lerpfrac3 = 0;
91                 self.lerpfrac4 = 0;
92         }
93         else
94         {
95                 float l13, l24, llf;
96                 float l24_13;
97
98                 if(self.frame3time == 0) // if frame1/3 were not previously displayed, only frame1 can make sense
99                         l13 = 1;
100                 else
101                         l13 = bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
102
103                 if(self.frame4time == 0) // if frame2/4 were not previously displayed, only frame2 can make sense
104                         l24 = 1;
105                 else
106                         l24 = bound(0, (time - self.frame2time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
107
108                 if(self.csqcmodel_lerpfrac2time == 0) // if there is no old lerpfrac (newly displayed model), only lerpfrac makes sense
109                         llf = 1;
110                 else
111                         llf = bound(0, (time - self.csqcmodel_lerpfractime) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
112
113                 l24_13 = self.csqcmodel_lerpfrac * llf + self.csqcmodel_lerpfrac2 * (1 - llf);
114
115                 self.lerpfrac  = l24 * l24_13;
116                 self.lerpfrac4 = (1 - l24) * l24_13;
117                 self.lerpfrac3 = (1 - l13) * (1 - l24_13);
118
119                 if(l24_13 == 0) // if frames 2/4 are not displayed, clear their frametime
120                 {
121                         self.frame2time = 0;
122                         self.frame4time = 0;
123                 }
124
125                 if(l24_13 == 1) // if frames 1/3 are not displayed, clear their frametime
126                 {
127                         self.frame1time = 0;
128                         self.frame3time = 0;
129                 }
130         }
131 #else
132         if(autocvar_cl_nolerp || (autocvar_cl_lerpanim_maxdelta_framegroups == 0))
133         {
134                 self.lerpfrac = 0;
135         }
136         else
137         {
138                 self.lerpfrac = 1 - bound(0, (time - self.frame1time) / autocvar_cl_lerpanim_maxdelta_framegroups, 1);
139         }
140 #endif
141 }
142
143 void CSQCModel_Draw()
144 {
145         // we don't do this for the local player as that one is already handled
146         // by CSQCPlayer_SetCamera()
147         if(!CSQCPlayer_IsLocalPlayer())
148                 InterpolateOrigin_Do();
149
150         // TODO csqcplayers: run prediction here too
151         CSQCModel_InterpolateAnimation_Do();
152
153         { CSQCMODEL_HOOK_PREDRAW }
154 }
155
156 void CSQCModel_Read()
157 {
158         float sf;
159         sf = ReadShort();
160
161         self.iflags |= IFLAG_ANGLES; // interpolate angles too
162
163         { CSQCMODEL_HOOK_PREUPDATE }
164
165         CSQCPlayer_PreUpdate();
166         InterpolateOrigin_Undo();
167         CSQCModel_InterpolateAnimation_PreNote(sf);
168
169 #define CSQCMODEL_PROPERTY(flag,r,w,f) \
170         if(sf & flag) \
171                 self.f = r();
172 #define CSQCMODEL_PROPERTY_SCALED(flag,r,w,f,s,mi,ma) \
173         if(sf & flag) \
174                 self.f = r() / s;
175         ALLPROPERTIES
176 #undef CSQCMODEL_PROPERTY_SCALED
177 #undef CSQCMODEL_PROPERTY
178
179         if(sf & CSQCMODEL_PROPERTY_MODELINDEX)
180                 setmodelindex(self, self.modelindex); // this retrieves the .model key and sets mins/maxs/absmin/absmax
181
182         if(sf & CSQCMODEL_PROPERTY_TELEPORTED)
183         {
184                 self.iflags |= IFLAG_TELEPORTED;
185                 self.csqcmodel_teleported = 1;
186         }
187         
188         CSQCModel_InterpolateAnimation_Note(sf);
189         InterpolateOrigin_Note();
190         CSQCPlayer_PostUpdate();
191
192         { CSQCMODEL_HOOK_POSTUPDATE }
193
194 #ifdef CSQCMODEL_SUPPORT_GETTAGINFO_BEFORE_DRAW
195         InterpolateOrigin_Do();
196         CSQCModel_InterpolateAnimation_Do();
197 #endif
198
199         // relink
200         setorigin(self, self.origin);
201
202         // draw it
203         self.drawmask = MASK_NORMAL;
204         self.predraw = CSQCModel_Draw;
205 }