aboutsummaryrefslogtreecommitdiff
path: root/sp/src/game/client/lerp_functions.h
blob: 198cb679af930a30d0da359804b931ed49f4edfa (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#ifndef LERP_FUNCTIONS_H
#define LERP_FUNCTIONS_H
#ifdef _WIN32
#pragma once
#endif


template <class T>
inline T LoopingLerp( float flPercent, T flFrom, T flTo )
{
	T s = flTo * flPercent + flFrom * (1.0f - flPercent);
	return s;
}

template <>
inline float LoopingLerp( float flPercent, float flFrom, float flTo )
{
	if ( fabs( flTo - flFrom ) >= 0.5f )
	{
		if (flFrom < flTo)
			flFrom += 1.0f;
		else
			flTo += 1.0f;
	}

	float s = flTo * flPercent + flFrom * (1.0f - flPercent);

	s = s - (int)(s);
	if (s < 0.0f)
		s = s + 1.0f;

	return s;
}

template <class T>
inline T Lerp_Hermite( float t, const T& p0, const T& p1, const T& p2 )
{
	T d1 = p1 - p0;
	T d2 = p2 - p1;

	T output;
	float tSqr = t*t;
	float tCube = t*tSqr;

	output = p1 * (2*tCube-3*tSqr+1);
	output += p2 * (-2*tCube+3*tSqr);
	output += d1 * (tCube-2*tSqr+t);
	output += d2 * (tCube-tSqr);

	return output;
}


template <class T>
inline T Derivative_Hermite( float t, const T& p0, const T& p1, const T& p2 )
{
	T d1 = p1 - p0;
	T d2 = p2 - p1;

	T output;
	float tSqr = t*t;

	output = p1 * (6*tSqr - 6*t);
	output += p2 * (-6*tSqr + 6*t);
	output += d1 * (3*tSqr - 4*t + 1);
	output += d2 * (3*tSqr - 2*t);

	return output;
}


inline void Lerp_Clamp( int val )
{
}

inline void Lerp_Clamp( float val )
{
}

inline void Lerp_Clamp( const Vector &val )
{
}

inline void Lerp_Clamp( const QAngle &val )
{
}


// If we have a range checked var, then we can clamp to its limits.
template< class T, int minValue, int maxValue, int startValue >
inline void Lerp_Clamp( CRangeCheckedVar<T,minValue,maxValue,startValue> &val )
{
	val.Clamp();
}


template<> 
inline QAngle Lerp_Hermite<QAngle>( float t, const QAngle& p0, const QAngle& p1, const QAngle& p2 )
{
	// Can't do hermite with QAngles, get discontinuities, just do a regular interpolation
	return Lerp( t, p1, p2 );
}

template <class T>
inline T LoopingLerp_Hermite( float t, T p0, T p1, T p2  )
{
	return Lerp_Hermite( t, p0, p1, p2 );
}

template <>
inline float LoopingLerp_Hermite( float t, float p0, float p1, float p2 )
{
	if ( fabs( p1 - p0 ) > 0.5f )
	{
		if ( p0 < p1 )
			p0 += 1.0f;
		else
			p1 += 1.0f;
	}

	if ( fabs( p2 - p1 ) > 0.5f )
	{
		if ( p1 < p2 )
		{
			p1 += 1.0f;

			// see if we need to fix up p0
			// important for vars that are decreasing from p0->p1->p2 where
			// p1 is fixed up relative to p2, eg p0 = 0.2, p1 = 0.1, p2 = 0.9
			if ( abs( p1 - p0 ) > 0.5 )
			{
				if ( p0 < p1 )
					p0 += 1.0f;
				else
					p1 += 1.0f;
			}
		}
		else
		{
			p2 += 1.0f;
		}
	}
		
	float s = Lerp_Hermite( t, p0, p1, p2 );

	s = s - (int)(s);
	if (s < 0.0f)
	{
		s = s + 1.0f;
	}

	return s;
}


// NOTE: C_AnimationLayer has its own versions of these functions in animationlayer.h.


#endif // LERP_FUNCTIONS_H