hx3d  1
2D/3D Simple Game Framework
interpolation.inl.hpp
1 /*
2  Interpolation functions.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/color.hpp"
22 
24 
25 namespace hx3d {
26 namespace math {
27 
28 static float outBounce(float t) {
29  return
30  (t < 0.3636) ? 7.5625 * t * t:
31  (t < 0.7272) ? 7.5625 * (t - 0.5454) * (t - 0.5454) + 0.75:
32  (t < 0.9090) ? 7.5625 * (t - 0.8181) * (t - 0.8181) + 0.9375:
33  7.5625 * (t - 0.9545) * (t - 0.9545) + 0.984375;
34 }
35 
36 static float inExpo(float t) {
37  return std::pow(2, 10 * (t-1));
38 }
39 
40 static float inCirc(float t) {
41  return -(std::sqrt(1 - t*t) - 1);
42 }
43 
44 static float inBack(float t) {
45  return t*t * (3*t - 2);
46 }
47 
48 static float inElastic(float t) {
49  return (t == 0 || t == 1) ? t :
50  -std::pow(2, 8 * (t-1)) * std::sin(((t-1) * 80 - 7.5) * math::PI / 15);
51 }
52 
53 template <class T>
54 T interpolate(T a, T b, float t, Interpolation type) {
55  switch (type) {
56  break;
57  case Interpolation::InQuad:
58  t = t * t;
59  break;
60  case Interpolation::OutQuad:
61  t = t * (2-t);
62  break;
63  case Interpolation::InOutQuad:
64  t = (t < 0.5f) ? 2 * t * t : -1 + (4-2*t)*t;
65  break;
66 
67  case Interpolation::InCubic:
68  t = t * t * t;
69  break;
70  case Interpolation::OutCubic:
71  t = (t-1) * (t-1) * (t-1) + 1;
72  break;
73  case Interpolation::InOutCubic:
74  t = (t < 0.5f) ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1;
75  break;
76 
77  case Interpolation::InQuart:
78  t = t * t * t * t;
79  break;
80  case Interpolation::OutQuart:
81  t = 1 - (t-1) * (t-1) * (t-1) * (t-1);
82  break;
83  case Interpolation::InOutQuart:
84  t = (t < 0.5f) ? 8*t*t*t*t : 1 - 8*(t-1)*(t-1)*(t-1)*(t-1);
85  break;
86 
87  case Interpolation::InQuint:
88  t = t*t*t*t*t*t;
89  break;
90  case Interpolation::OutQuint:
91  t = 1 + (t-1) * (t-1) * (t-1) * (t-1) * (t-1);
92  break;
93  case Interpolation::InOutQuint:
94  t = (t < 0.5f) ? 16*t*t*t*t*t : 1 + 16*(t-1)*(t-1)*(t-1)*(t-1)*(t-1);
95  break;
96 
97  case Interpolation::InSine:
98  t = -1 * std::cos(t * (math::PI / 2)) + 1;
99  break;
100  case Interpolation::OutSine:
101  t = std::sin(t * (math::PI / 2));
102  break;
103  case Interpolation::InOutSine:
104  t = (std::cos(t * math::PI) - 1) / -2;
105  break;
106 
107  case Interpolation::InExpo:
108  t = inExpo(t);
109  break;
110  case Interpolation::OutExpo:
111  t = 1 - inExpo(1 - t);
112  break;
113  case Interpolation::InOutExpo:
114  t = (t < 0.5f) ?
115  inExpo(t * 2) / 2 :
116  1 - inExpo(t * -2 + 2) / 2;
117  break;
118 
119  case Interpolation::InCirc:
120  t = inCirc(t);
121  break;
122  case Interpolation::OutCirc:
123  t = 1 - inCirc(1 - t);
124  break;
125  case Interpolation::InOutCirc:
126  t = (t < 0.5f) ?
127  inCirc(t * 2) / 2 :
128  1 - inCirc(t * -2 + 2) / 2;
129  break;
130 
131  case Interpolation::InBack:
132  t = inBack(t);
133  break;
134  case Interpolation::OutBack:
135  t = 1 - inBack(1 - t);
136  break;
137  case Interpolation::InOutBack:
138  t = (t < 0.5f) ?
139  inBack(t * 2) / 2 :
140  1 - inBack(t * -2 + 2) / 2;
141  break;
142 
143  case Interpolation::InBounce:
144  t = 1 - outBounce(1 - t);
145  break;
146  case Interpolation::OutBounce:
147  t = outBounce(t);
148  break;
149  case Interpolation::InOutBounce:
150  t =
151  (t < 0.5f) ?
152  (1 - outBounce(1 - t*2)) * 0.5:
153  outBounce(t * 2 - 1) * 0.5 + 1 * 0.5;
154  break;
155 
156  case Interpolation::InElastic:
157  t = inElastic(t);
158  break;
159  case Interpolation::OutElastic:
160  t = 1 - inElastic(1 - t);
161  break;
162  case Interpolation::InOutElastic:
163  t = (t < 0.5f) ?
164  inElastic(t * 2) / 2 :
165  1 - inElastic(t * -2 + 2) / 2;
166  break;
167 
168  case Interpolation::Linear:
169  default:
170  break;
171  }
172 
173  return a + t*(b - a);
174 }
175 
176 template <>
177 Color interpolate(Color a, Color b, float t, Interpolation type) {
178  Color ca = Color::rgbToHsv(a);
179  Color cb = Color::rgbToHsv(b);
180  Color final;
181 
182  final.r = math::interpolate(ca.r, cb.r, t, type);
183  final.g = math::interpolate(ca.g, cb.g, t, type);
184  final.b = math::interpolate(ca.b, cb.b, t, type);
185 
186  return Color::hsvToRgb(final);
187 }
188 
189 } /* math */
190 } /* hx3d */
unsigned char g
Green component.
Definition: color.hpp:98
Four [0..255] components defined color.
Definition: color.hpp:32
T interpolate(T a, T b, float t, Interpolation type)
Interpolate between two values.
hx3d framework namespace
Definition: audio.hpp:26
Interpolation
Interpolate using functions.
const double PI
PI.
Definition: constants.hpp:38
unsigned char b
Blue component.
Definition: color.hpp:100
unsigned char r
Red component.
Definition: color.hpp:96