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