-
Notifications
You must be signed in to change notification settings - Fork 599
/
view.cpp
1802 lines (1416 loc) · 43 KB
/
view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// view/refresh setup functions
#include "hud.h"
#include "cl_util.h"
#include "cvardef.h"
#include "usercmd.h"
#include "const.h"
#include "entity_state.h"
#include "cl_entity.h"
#include "ref_params.h"
#include "in_defs.h" // PITCH YAW ROLL
#include "pm_movevars.h"
#include "pm_shared.h"
#include "pm_defs.h"
#include "event_api.h"
#include "pmtrace.h"
#include "bench.h"
#include "screenfade.h"
#include "shake.h"
#include "hltv.h"
#include "Exports.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
#endif
int CL_IsThirdPerson( void );
void CL_CameraOffset( float *ofs );
void CL_DLLEXPORT V_CalcRefdef( struct ref_params_s *pparams );
void PM_ParticleLine( float *start, float *end, int pcolor, float life, float vert);
int PM_GetVisEntInfo( int ent );
extern "C" int PM_GetPhysEntInfo( int ent );
void InterpolateAngles( float * start, float * end, float * output, float frac );
void NormalizeAngles( float * angles );
extern "C" float Distance(const float * v1, const float * v2);
float AngleBetweenVectors( const float * v1, const float * v2 );
float vJumpOrigin[3];
float vJumpAngles[3];
void V_DropPunchAngle ( float frametime, float *ev_punchangle );
void VectorAngles( const float *forward, float *angles );
#include "r_studioint.h"
#include "com_model.h"
#include "kbutton.h"
extern engine_studio_api_t IEngineStudio;
extern kbutton_t in_mlook;
/*
The view is allowed to move slightly from it's true position for bobbing,
but if it exceeds 8 pixels linear distance (spherical, not box), the list of
entities sent from the server may not include everything in the pvs, especially
when crossing a water boudnary.
*/
extern cvar_t *cl_forwardspeed;
extern cvar_t *chase_active;
extern cvar_t *scr_ofsx, *scr_ofsy, *scr_ofsz;
extern cvar_t *cl_vsmoothing;
#define CAM_MODE_RELAX 1
#define CAM_MODE_FOCUS 2
vec3_t v_origin, v_angles, v_cl_angles, v_sim_org, v_lastAngles;
float v_frametime, v_lastDistance;
float v_cameraRelaxAngle = 5.0f;
float v_cameraFocusAngle = 35.0f;
int v_cameraMode = CAM_MODE_FOCUS;
qboolean v_resetCamera = 1;
vec3_t ev_punchangle;
cvar_t *scr_ofsx;
cvar_t *scr_ofsy;
cvar_t *scr_ofsz;
cvar_t *v_centermove;
cvar_t *v_centerspeed;
cvar_t *cl_bobcycle;
cvar_t *cl_bob;
cvar_t *cl_bobup;
cvar_t *cl_waterdist;
cvar_t *cl_chasedist;
// These cvars are not registered (so users can't cheat), so set the ->value field directly
// Register these cvars in V_Init() if needed for easy tweaking
cvar_t v_iyaw_cycle = {"v_iyaw_cycle", "2", 0, 2};
cvar_t v_iroll_cycle = {"v_iroll_cycle", "0.5", 0, 0.5};
cvar_t v_ipitch_cycle = {"v_ipitch_cycle", "1", 0, 1};
cvar_t v_iyaw_level = {"v_iyaw_level", "0.3", 0, 0.3};
cvar_t v_iroll_level = {"v_iroll_level", "0.1", 0, 0.1};
cvar_t v_ipitch_level = {"v_ipitch_level", "0.3", 0, 0.3};
float v_idlescale; // used by TFC for concussion grenade effect
//=============================================================================
/*
void V_NormalizeAngles( float *angles )
{
int i;
// Normalize angles
for ( i = 0; i < 3; i++ )
{
if ( angles[i] > 180.0 )
{
angles[i] -= 360.0;
}
else if ( angles[i] < -180.0 )
{
angles[i] += 360.0;
}
}
}
/*
===================
V_InterpolateAngles
Interpolate Euler angles.
FIXME: Use Quaternions to avoid discontinuities
Frac is 0.0 to 1.0 ( i.e., should probably be clamped, but doesn't have to be )
===================
void V_InterpolateAngles( float *start, float *end, float *output, float frac )
{
int i;
float ang1, ang2;
float d;
V_NormalizeAngles( start );
V_NormalizeAngles( end );
for ( i = 0 ; i < 3 ; i++ )
{
ang1 = start[i];
ang2 = end[i];
d = ang2 - ang1;
if ( d > 180 )
{
d -= 360;
}
else if ( d < -180 )
{
d += 360;
}
output[i] = ang1 + d * frac;
}
V_NormalizeAngles( output );
} */
// Quakeworld bob code, this fixes jitters in the mutliplayer since the clock (pparams->time) isn't quite linear
float V_CalcBob ( struct ref_params_s *pparams )
{
static double bobtime;
static float bob;
float cycle;
static float lasttime;
vec3_t vel;
if ( pparams->onground == -1 ||
pparams->time == lasttime )
{
// just use old value
return bob;
}
lasttime = pparams->time;
bobtime += pparams->frametime;
cycle = bobtime - (int)( bobtime / cl_bobcycle->value ) * cl_bobcycle->value;
cycle /= cl_bobcycle->value;
if ( cycle < cl_bobup->value )
{
cycle = M_PI * cycle / cl_bobup->value;
}
else
{
cycle = M_PI + M_PI * ( cycle - cl_bobup->value )/( 1.0 - cl_bobup->value );
}
// bob is proportional to simulated velocity in the xy plane
// (don't count Z, or jumping messes it up)
VectorCopy( pparams->simvel, vel );
vel[2] = 0;
bob = sqrt( vel[0] * vel[0] + vel[1] * vel[1] ) * cl_bob->value;
bob = bob * 0.3 + bob * 0.7 * sin(cycle);
bob = min( bob, 4 );
bob = max( bob, -7 );
return bob;
}
/*
===============
V_CalcRoll
Used by view and sv_user
===============
*/
float V_CalcRoll (vec3_t angles, vec3_t velocity, float rollangle, float rollspeed )
{
float sign;
float side;
float value;
vec3_t forward, right, up;
AngleVectors ( angles, forward, right, up );
side = DotProduct (velocity, right);
sign = side < 0 ? -1 : 1;
side = fabs( side );
value = rollangle;
if (side < rollspeed)
{
side = side * value / rollspeed;
}
else
{
side = value;
}
return side * sign;
}
typedef struct pitchdrift_s
{
float pitchvel;
int nodrift;
float driftmove;
double laststop;
} pitchdrift_t;
static pitchdrift_t pd;
void V_StartPitchDrift( void )
{
if ( pd.laststop == gEngfuncs.GetClientTime() )
{
return; // something else is keeping it from drifting
}
if ( pd.nodrift || !pd.pitchvel )
{
pd.pitchvel = v_centerspeed->value;
pd.nodrift = 0;
pd.driftmove = 0;
}
}
void V_StopPitchDrift ( void )
{
pd.laststop = gEngfuncs.GetClientTime();
pd.nodrift = 1;
pd.pitchvel = 0;
}
/*
===============
V_DriftPitch
Moves the client pitch angle towards idealpitch sent by the server.
If the user is adjusting pitch manually, either with lookup/lookdown,
mlook and mouse, or klook and keyboard, pitch drifting is constantly stopped.
===============
*/
void V_DriftPitch ( struct ref_params_s *pparams )
{
float delta, move;
if ( gEngfuncs.IsNoClipping() || !pparams->onground || pparams->demoplayback || pparams->spectator )
{
pd.driftmove = 0;
pd.pitchvel = 0;
return;
}
// don't count small mouse motion
if ( pd.nodrift)
{
if ( v_centermove->value > 0 && !(in_mlook.state & 1) )
{
// this is for lazy players. if they stopped, looked around and then continued
// to move the view will be centered automatically if they move more than
// v_centermove units.
if ( fabs( pparams->cmd->forwardmove ) < cl_forwardspeed->value )
pd.driftmove = 0;
else
pd.driftmove += pparams->frametime;
if ( pd.driftmove > v_centermove->value)
{
V_StartPitchDrift ();
}
else
{
return; // player didn't move enough
}
}
return; // don't drift view
}
delta = pparams->idealpitch - pparams->cl_viewangles[PITCH];
if (!delta)
{
pd.pitchvel = 0;
return;
}
move = pparams->frametime * pd.pitchvel;
pd.pitchvel *= (1.0f+(pparams->frametime*0.25f)); // get faster by time
if (delta > 0)
{
if (move > delta)
{
pd.pitchvel = 0;
move = delta;
}
pparams->cl_viewangles[PITCH] += move;
}
else if (delta < 0)
{
if (move > -delta)
{
pd.pitchvel = 0;
move = -delta;
}
pparams->cl_viewangles[PITCH] -= move;
}
}
/*
==============================================================================
VIEW RENDERING
==============================================================================
*/
/*
==================
V_CalcGunAngle
==================
*/
void V_CalcGunAngle ( struct ref_params_s *pparams )
{
cl_entity_t *viewent;
viewent = gEngfuncs.GetViewModel();
if ( !viewent )
return;
viewent->angles[YAW] = pparams->viewangles[YAW] + pparams->crosshairangle[YAW];
viewent->angles[PITCH] = -pparams->viewangles[PITCH] + pparams->crosshairangle[PITCH] * 0.25;
viewent->angles[ROLL] -= v_idlescale * sin(pparams->time*v_iroll_cycle.value) * v_iroll_level.value;
// don't apply all of the v_ipitch to prevent normally unseen parts of viewmodel from coming into view.
viewent->angles[PITCH] -= v_idlescale * sin(pparams->time*v_ipitch_cycle.value) * (v_ipitch_level.value * 0.5);
viewent->angles[YAW] -= v_idlescale * sin(pparams->time*v_iyaw_cycle.value) * v_iyaw_level.value;
VectorCopy( viewent->angles, viewent->curstate.angles );
VectorCopy( viewent->angles, viewent->latched.prevangles );
}
/*
==============
V_AddIdle
Idle swaying
==============
*/
void V_AddIdle ( struct ref_params_s *pparams )
{
pparams->viewangles[ROLL] += v_idlescale * sin(pparams->time*v_iroll_cycle.value) * v_iroll_level.value;
pparams->viewangles[PITCH] += v_idlescale * sin(pparams->time*v_ipitch_cycle.value) * v_ipitch_level.value;
pparams->viewangles[YAW] += v_idlescale * sin(pparams->time*v_iyaw_cycle.value) * v_iyaw_level.value;
}
/*
==============
V_CalcViewRoll
Roll is induced by movement and damage
==============
*/
void V_CalcViewRoll ( struct ref_params_s *pparams )
{
float side;
cl_entity_t *viewentity;
viewentity = gEngfuncs.GetEntityByIndex( pparams->viewentity );
if ( !viewentity )
return;
side = V_CalcRoll ( viewentity->angles, pparams->simvel, pparams->movevars->rollangle, pparams->movevars->rollspeed );
pparams->viewangles[ROLL] += side;
if ( pparams->health <= 0 && ( pparams->viewheight[2] != 0 ) )
{
// only roll the view if the player is dead and the viewheight[2] is nonzero
// this is so deadcam in multiplayer will work.
pparams->viewangles[ROLL] = 80; // dead view angle
return;
}
}
/*
==================
V_CalcIntermissionRefdef
==================
*/
void V_CalcIntermissionRefdef ( struct ref_params_s *pparams )
{
cl_entity_t *ent, *view;
float old;
// ent is the player model ( visible when out of body )
ent = gEngfuncs.GetLocalPlayer();
// view is the weapon model (only visible from inside body )
view = gEngfuncs.GetViewModel();
VectorCopy ( pparams->simorg, pparams->vieworg );
VectorCopy ( pparams->cl_viewangles, pparams->viewangles );
view->model = NULL;
// allways idle in intermission
old = v_idlescale;
v_idlescale = 1;
V_AddIdle ( pparams );
if ( gEngfuncs.IsSpectateOnly() )
{
// in HLTV we must go to 'intermission' position by ourself
VectorCopy( gHUD.m_Spectator.m_cameraOrigin, pparams->vieworg );
VectorCopy( gHUD.m_Spectator.m_cameraAngles, pparams->viewangles );
}
v_idlescale = old;
v_cl_angles = pparams->cl_viewangles;
v_origin = pparams->vieworg;
v_angles = pparams->viewangles;
}
#define ORIGIN_BACKUP 64
#define ORIGIN_MASK ( ORIGIN_BACKUP - 1 )
typedef struct
{
float Origins[ ORIGIN_BACKUP ][3];
float OriginTime[ ORIGIN_BACKUP ];
float Angles[ ORIGIN_BACKUP ][3];
float AngleTime[ ORIGIN_BACKUP ];
int CurrentOrigin;
int CurrentAngle;
} viewinterp_t;
/*
==================
V_CalcRefdef
==================
*/
void V_CalcNormalRefdef ( struct ref_params_s *pparams )
{
cl_entity_t *ent, *view;
int i;
vec3_t angles;
float bob, waterOffset;
static viewinterp_t ViewInterp;
static float oldz = 0;
static float lasttime;
vec3_t camAngles, camForward, camRight, camUp;
cl_entity_t *pwater;
V_DriftPitch ( pparams );
if ( gEngfuncs.IsSpectateOnly() )
{
ent = gEngfuncs.GetEntityByIndex( g_iUser2 );
}
else
{
// ent is the player model ( visible when out of body )
ent = gEngfuncs.GetLocalPlayer();
}
// view is the weapon model (only visible from inside body )
view = gEngfuncs.GetViewModel();
// transform the view offset by the model's matrix to get the offset from
// model origin for the view
bob = V_CalcBob ( pparams );
// refresh position
VectorCopy ( pparams->simorg, pparams->vieworg );
pparams->vieworg[2] += ( bob );
VectorAdd( pparams->vieworg, pparams->viewheight, pparams->vieworg );
VectorCopy ( pparams->cl_viewangles, pparams->viewangles );
gEngfuncs.V_CalcShake();
gEngfuncs.V_ApplyShake( pparams->vieworg, pparams->viewangles, 1.0 );
// never let view origin sit exactly on a node line, because a water plane can
// dissapear when viewed with the eye exactly on it.
// FIXME, we send origin at 1/128 now, change this?
// the server protocol only specifies to 1/16 pixel, so add 1/32 in each axis
pparams->vieworg[0] += 1.0/32;
pparams->vieworg[1] += 1.0/32;
pparams->vieworg[2] += 1.0/32;
// Check for problems around water, move the viewer artificially if necessary
// -- this prevents drawing errors in GL due to waves
waterOffset = 0;
if ( pparams->waterlevel >= 2 )
{
int i, contents, waterDist, waterEntity;
vec3_t point;
waterDist = cl_waterdist->value;
if ( pparams->hardware )
{
waterEntity = gEngfuncs.PM_WaterEntity( pparams->simorg );
if ( waterEntity >= 0 && waterEntity < pparams->max_entities )
{
pwater = gEngfuncs.GetEntityByIndex( waterEntity );
if ( pwater && ( pwater->model != NULL ) )
{
waterDist += ( pwater->curstate.scale * 16 ); // Add in wave height
}
}
}
else
{
waterEntity = 0; // Don't need this in software
}
VectorCopy( pparams->vieworg, point );
// Eyes are above water, make sure we're above the waves
if ( pparams->waterlevel == 2 )
{
point[2] -= waterDist;
for ( i = 0; i < waterDist; i++ )
{
contents = gEngfuncs.PM_PointContents( point, NULL );
if ( contents > CONTENTS_WATER )
break;
point[2] += 1;
}
waterOffset = (point[2] + waterDist) - pparams->vieworg[2];
}
else
{
// eyes are under water. Make sure we're far enough under
point[2] += waterDist;
for ( i = 0; i < waterDist; i++ )
{
contents = gEngfuncs.PM_PointContents( point, NULL );
if ( contents <= CONTENTS_WATER )
break;
point[2] -= 1;
}
waterOffset = (point[2] - waterDist) - pparams->vieworg[2];
}
}
pparams->vieworg[2] += waterOffset;
V_CalcViewRoll ( pparams );
V_AddIdle ( pparams );
// offsets
VectorCopy( pparams->cl_viewangles, angles );
AngleVectors ( angles, pparams->forward, pparams->right, pparams->up );
// don't allow cheats in multiplayer
if ( pparams->maxclients <= 1 )
{
for ( i=0 ; i<3 ; i++ )
{
pparams->vieworg[i] += scr_ofsx->value*pparams->forward[i] + scr_ofsy->value*pparams->right[i] + scr_ofsz->value*pparams->up[i];
}
}
// Treating cam_ofs[2] as the distance
if( CL_IsThirdPerson() )
{
vec3_t ofs;
ofs[0] = ofs[1] = ofs[2] = 0.0;
CL_CameraOffset( (float *)&ofs );
VectorCopy( ofs, camAngles );
camAngles[ ROLL ] = 0;
AngleVectors( camAngles, camForward, camRight, camUp );
for ( i = 0; i < 3; i++ )
{
pparams->vieworg[ i ] += -ofs[2] * camForward[ i ];
}
}
// Give gun our viewangles
VectorCopy ( pparams->cl_viewangles, view->angles );
// set up gun position
V_CalcGunAngle ( pparams );
// Use predicted origin as view origin.
VectorCopy ( pparams->simorg, view->origin );
view->origin[2] += ( waterOffset );
VectorAdd( view->origin, pparams->viewheight, view->origin );
// Let the viewmodel shake at about 10% of the amplitude
gEngfuncs.V_ApplyShake( view->origin, view->angles, 0.9 );
for ( i = 0; i < 3; i++ )
{
view->origin[ i ] += bob * 0.4 * pparams->forward[ i ];
}
view->origin[2] += bob;
// throw in a little tilt.
view->angles[YAW] -= bob * 0.5;
view->angles[ROLL] -= bob * 1;
view->angles[PITCH] -= bob * 0.3;
// pushing the view origin down off of the same X/Z plane as the ent's origin will give the
// gun a very nice 'shifting' effect when the player looks up/down. If there is a problem
// with view model distortion, this may be a cause. (SJB).
view->origin[2] -= 1;
// fudge position around to keep amount of weapon visible
// roughly equal with different FOV
if (pparams->viewsize == 110)
{
view->origin[2] += 1;
}
else if (pparams->viewsize == 100)
{
view->origin[2] += 2;
}
else if (pparams->viewsize == 90)
{
view->origin[2] += 1;
}
else if (pparams->viewsize == 80)
{
view->origin[2] += 0.5;
}
// Add in the punchangle, if any
VectorAdd ( pparams->viewangles, pparams->punchangle, pparams->viewangles );
// Include client side punch, too
VectorAdd ( pparams->viewangles, (float *)&ev_punchangle, pparams->viewangles);
V_DropPunchAngle ( pparams->frametime, (float *)&ev_punchangle );
// smooth out stair step ups
#if 1
if ( !pparams->smoothing && pparams->onground && pparams->simorg[2] - oldz > 0)
{
float steptime;
steptime = pparams->time - lasttime;
if (steptime < 0)
//FIXME I_Error ("steptime < 0");
steptime = 0;
oldz += steptime * 150;
if (oldz > pparams->simorg[2])
oldz = pparams->simorg[2];
if (pparams->simorg[2] - oldz > 18)
oldz = pparams->simorg[2]- 18;
pparams->vieworg[2] += oldz - pparams->simorg[2];
view->origin[2] += oldz - pparams->simorg[2];
}
else
{
oldz = pparams->simorg[2];
}
#endif
{
static float lastorg[3];
vec3_t delta;
VectorSubtract( pparams->simorg, lastorg, delta );
if ( Length( delta ) != 0.0 )
{
VectorCopy( pparams->simorg, ViewInterp.Origins[ ViewInterp.CurrentOrigin & ORIGIN_MASK ] );
ViewInterp.OriginTime[ ViewInterp.CurrentOrigin & ORIGIN_MASK ] = pparams->time;
ViewInterp.CurrentOrigin++;
VectorCopy( pparams->simorg, lastorg );
}
}
// Smooth out whole view in multiplayer when on trains, lifts
if ( cl_vsmoothing && cl_vsmoothing->value &&
( pparams->smoothing && ( pparams->maxclients > 1 ) ) )
{
int foundidx;
int i;
float t;
if ( cl_vsmoothing->value < 0.0 )
{
gEngfuncs.Cvar_SetValue( "cl_vsmoothing", 0.0 );
}
t = pparams->time - cl_vsmoothing->value;
for ( i = 1; i < ORIGIN_MASK; i++ )
{
foundidx = ViewInterp.CurrentOrigin - 1 - i;
if ( ViewInterp.OriginTime[ foundidx & ORIGIN_MASK ] <= t )
break;
}
if ( i < ORIGIN_MASK && ViewInterp.OriginTime[ foundidx & ORIGIN_MASK ] != 0.0 )
{
// Interpolate
vec3_t delta;
double frac;
double dt;
vec3_t neworg;
dt = ViewInterp.OriginTime[ (foundidx + 1) & ORIGIN_MASK ] - ViewInterp.OriginTime[ foundidx & ORIGIN_MASK ];
if ( dt > 0.0 )
{
frac = ( t - ViewInterp.OriginTime[ foundidx & ORIGIN_MASK] ) / dt;
frac = min( 1.0, frac );
VectorSubtract( ViewInterp.Origins[ ( foundidx + 1 ) & ORIGIN_MASK ], ViewInterp.Origins[ foundidx & ORIGIN_MASK ], delta );
VectorMA( ViewInterp.Origins[ foundidx & ORIGIN_MASK ], frac, delta, neworg );
// Dont interpolate large changes
if ( Length( delta ) < 64 )
{
VectorSubtract( neworg, pparams->simorg, delta );
VectorAdd( pparams->simorg, delta, pparams->simorg );
VectorAdd( pparams->vieworg, delta, pparams->vieworg );
VectorAdd( view->origin, delta, view->origin );
}
}
}
}
// Store off v_angles before munging for third person
v_angles = pparams->viewangles;
v_lastAngles = pparams->viewangles;
// v_cl_angles = pparams->cl_viewangles; // keep old user mouse angles !
if ( CL_IsThirdPerson() )
{
VectorCopy( camAngles, pparams->viewangles);
float pitch = camAngles[ 0 ];
// Normalize angles
if ( pitch > 180 )
pitch -= 360.0;
else if ( pitch < -180 )
pitch += 360;
// Player pitch is inverted
pitch /= -3.0;
// Slam local player's pitch value
ent->angles[ 0 ] = pitch;
ent->curstate.angles[ 0 ] = pitch;
ent->prevstate.angles[ 0 ] = pitch;
ent->latched.prevangles[ 0 ] = pitch;
}
// override all previous settings if the viewent isn't the client
if ( pparams->viewentity > pparams->maxclients )
{
cl_entity_t *viewentity;
viewentity = gEngfuncs.GetEntityByIndex( pparams->viewentity );
if ( viewentity )
{
VectorCopy( viewentity->origin, pparams->vieworg );
VectorCopy( viewentity->angles, pparams->viewangles );
// Store off overridden viewangles
v_angles = pparams->viewangles;
}
}
lasttime = pparams->time;
v_origin = pparams->vieworg;
}
void V_SmoothInterpolateAngles( float * startAngle, float * endAngle, float * finalAngle, float degreesPerSec )
{
float absd,frac,d,threshhold;
NormalizeAngles( startAngle );
NormalizeAngles( endAngle );
for ( int i = 0 ; i < 3 ; i++ )
{
d = endAngle[i] - startAngle[i];
if ( d > 180.0f )
{
d -= 360.0f;
}
else if ( d < -180.0f )
{
d += 360.0f;
}
absd = fabs(d);
if ( absd > 0.01f )
{
frac = degreesPerSec * v_frametime;
threshhold= degreesPerSec / 4;
if ( absd < threshhold )
{
float h = absd / threshhold;
h *= h;
frac*= h; // slow down last degrees
}
if ( frac > absd )
{
finalAngle[i] = endAngle[i];
}
else
{
if ( d>0)
finalAngle[i] = startAngle[i] + frac;
else
finalAngle[i] = startAngle[i] - frac;
}
}
else
{
finalAngle[i] = endAngle[i];
}
}
NormalizeAngles( finalAngle );
}
// Get the origin of the Observer based around the target's position and angles
void V_GetChaseOrigin( float * angles, float * origin, float distance, float * returnvec )
{
vec3_t vecEnd;
vec3_t forward;
vec3_t vecStart;
pmtrace_t * trace;
int maxLoops = 8;
int ignoreent = -1; // first, ignore no entity
cl_entity_t * ent = NULL;
// Trace back from the target using the player's view angles
AngleVectors(angles, forward, NULL, NULL);
VectorScale(forward,-1,forward);
VectorCopy( origin, vecStart );
VectorMA(vecStart, distance , forward, vecEnd);
while ( maxLoops > 0)
{
trace = gEngfuncs.PM_TraceLine( vecStart, vecEnd, PM_TRACELINE_PHYSENTSONLY, 2, ignoreent );
// WARNING! trace->ent is is the number in physent list not the normal entity number
if ( trace->ent <= 0)
break; // we hit the world or nothing, stop trace
ent = gEngfuncs.GetEntityByIndex( PM_GetPhysEntInfo( trace->ent ) );
if ( ent == NULL )
break;
// hit non-player solid BSP , stop here
if ( ent->curstate.solid == SOLID_BSP && !ent->player )
break;
// if close enought to end pos, stop, otherwise continue trace
if( Distance(trace->endpos, vecEnd ) < 1.0f )
{
break;
}
else
{
ignoreent = trace->ent; // ignore last hit entity
VectorCopy( trace->endpos, vecStart);
}
maxLoops--;
}
/* if ( ent )
{
gEngfuncs.Con_Printf("Trace loops %i , entity %i, model %s, solid %i\n",(8-maxLoops),ent->curstate.number, ent->model->name , ent->curstate.solid );
} */
VectorMA( trace->endpos, 4, trace->plane.normal, returnvec );
v_lastDistance = Distance(trace->endpos, origin); // real distance without offset
}
/*void V_GetDeathCam(cl_entity_t * ent1, cl_entity_t * ent2, float * angle, float * origin)
{
float newAngle[3]; float newOrigin[3];
float distance = 168.0f;
v_lastDistance+= v_frametime * 96.0f; // move unit per seconds back
if ( v_resetCamera )
v_lastDistance = 64.0f;
if ( distance > v_lastDistance )
distance = v_lastDistance;
VectorCopy(ent1->origin, newOrigin);
if ( ent1->player )
newOrigin[2]+= 17; // head level of living player
// get new angle towards second target
if ( ent2 )
{
VectorSubtract( ent2->origin, ent1->origin, newAngle );
VectorAngles( newAngle, newAngle );
newAngle[0] = -newAngle[0];
}
else
{
// if no second target is given, look down to dead player
newAngle[0] = 90.0f;
newAngle[1] = 0.0f;
newAngle[2] = 0;
}
// and smooth view
V_SmoothInterpolateAngles( v_lastAngles, newAngle, angle, 120.0f );
V_GetChaseOrigin( angle, newOrigin, distance, origin );
VectorCopy(angle, v_lastAngles);
}*/
void V_GetSingleTargetCam(cl_entity_t * ent1, float * angle, float * origin)
{
float newAngle[3]; float newOrigin[3];