Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

563 lines
17 KiB

5 years ago
  1. #define GLM_ENABLE_EXPERIMENTAL
  2. #include <glm/gtc/type_precision.hpp>
  3. #include <glm/gtx/fast_trigonometry.hpp>
  4. #include <glm/gtx/integer.hpp>
  5. #include <glm/gtx/common.hpp>
  6. #include <glm/gtc/constants.hpp>
  7. #include <glm/gtc/ulp.hpp>
  8. #include <glm/gtc/vec1.hpp>
  9. #include <glm/trigonometric.hpp>
  10. #include <cmath>
  11. #include <ctime>
  12. #include <cstdio>
  13. #include <vector>
  14. namespace fastCos
  15. {
  16. int perf(bool NextFloat)
  17. {
  18. const float begin = -glm::pi<float>();
  19. const float end = glm::pi<float>();
  20. float result = 0.f;
  21. const std::clock_t timestamp1 = std::clock();
  22. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  23. result = glm::fastCos(i);
  24. const std::clock_t timestamp2 = std::clock();
  25. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  26. result = glm::cos(i);
  27. const std::clock_t timestamp3 = std::clock();
  28. const std::clock_t time_fast = timestamp2 - timestamp1;
  29. const std::clock_t time_default = timestamp3 - timestamp2;
  30. std::printf("fastCos Time %d clocks\n", static_cast<int>(time_fast));
  31. std::printf("cos Time %d clocks\n", static_cast<int>(time_default));
  32. return time_fast <= time_default ? 0 : 1;
  33. }
  34. }//namespace fastCos
  35. namespace fastSin
  36. {
  37. /*
  38. float sin(float x) {
  39. float temp;
  40. temp = (x + M_PI) / ((2 * M_PI) - M_PI);
  41. return limited_sin((x + M_PI) - ((2 * M_PI) - M_PI) * temp));
  42. }
  43. */
  44. int perf(bool NextFloat)
  45. {
  46. const float begin = -glm::pi<float>();
  47. const float end = glm::pi<float>();
  48. float result = 0.f;
  49. const std::clock_t timestamp1 = std::clock();
  50. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  51. result = glm::fastSin(i);
  52. const std::clock_t timestamp2 = std::clock();
  53. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  54. result = glm::sin(i);
  55. const std::clock_t timestamp3 = std::clock();
  56. const std::clock_t time_fast = timestamp2 - timestamp1;
  57. const std::clock_t time_default = timestamp3 - timestamp2;
  58. std::printf("fastSin Time %d clocks\n", static_cast<int>(time_fast));
  59. std::printf("sin Time %d clocks\n", static_cast<int>(time_default));
  60. return time_fast <= time_default ? 0 : 1;
  61. }
  62. }//namespace fastSin
  63. namespace fastTan
  64. {
  65. int perf(bool NextFloat)
  66. {
  67. const float begin = -glm::pi<float>();
  68. const float end = glm::pi<float>();
  69. float result = 0.f;
  70. const std::clock_t timestamp1 = std::clock();
  71. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  72. result = glm::fastTan(i);
  73. const std::clock_t timestamp2 = std::clock();
  74. for (float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  75. result = glm::tan(i);
  76. const std::clock_t timestamp3 = std::clock();
  77. const std::clock_t time_fast = timestamp2 - timestamp1;
  78. const std::clock_t time_default = timestamp3 - timestamp2;
  79. std::printf("fastTan Time %d clocks\n", static_cast<int>(time_fast));
  80. std::printf("tan Time %d clocks\n", static_cast<int>(time_default));
  81. return time_fast <= time_default ? 0 : 1;
  82. }
  83. }//namespace fastTan
  84. namespace fastAcos
  85. {
  86. int perf(bool NextFloat)
  87. {
  88. const float begin = -glm::pi<float>();
  89. const float end = glm::pi<float>();
  90. float result = 0.f;
  91. const std::clock_t timestamp1 = std::clock();
  92. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  93. result = glm::fastAcos(i);
  94. const std::clock_t timestamp2 = std::clock();
  95. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  96. result = glm::acos(i);
  97. const std::clock_t timestamp3 = std::clock();
  98. const std::clock_t time_fast = timestamp2 - timestamp1;
  99. const std::clock_t time_default = timestamp3 - timestamp2;
  100. std::printf("fastAcos Time %d clocks\n", static_cast<int>(time_fast));
  101. std::printf("acos Time %d clocks\n", static_cast<int>(time_default));
  102. return time_fast <= time_default ? 0 : 1;
  103. }
  104. }//namespace fastAcos
  105. namespace fastAsin
  106. {
  107. int perf(bool NextFloat)
  108. {
  109. const float begin = -glm::pi<float>();
  110. const float end = glm::pi<float>();
  111. float result = 0.f;
  112. const std::clock_t timestamp1 = std::clock();
  113. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  114. result = glm::fastAsin(i);
  115. const std::clock_t timestamp2 = std::clock();
  116. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  117. result = glm::asin(i);
  118. const std::clock_t timestamp3 = std::clock();
  119. const std::clock_t time_fast = timestamp2 - timestamp1;
  120. const std::clock_t time_default = timestamp3 - timestamp2;
  121. std::printf("fastAsin Time %d clocks\n", static_cast<int>(time_fast));
  122. std::printf("asin Time %d clocks\n", static_cast<int>(time_default));
  123. return time_fast <= time_default ? 0 : 1;
  124. }
  125. }//namespace fastAsin
  126. namespace fastAtan
  127. {
  128. int perf(bool NextFloat)
  129. {
  130. const float begin = -glm::pi<float>();
  131. const float end = glm::pi<float>();
  132. float result = 0.f;
  133. const std::clock_t timestamp1 = std::clock();
  134. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  135. result = glm::fastAtan(i);
  136. const std::clock_t timestamp2 = std::clock();
  137. for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
  138. result = glm::atan(i);
  139. const std::clock_t timestamp3 = std::clock();
  140. const std::clock_t time_fast = timestamp2 - timestamp1;
  141. const std::clock_t time_default = timestamp3 - timestamp2;
  142. std::printf("fastAtan Time %d clocks\n", static_cast<int>(time_fast));
  143. std::printf("atan Time %d clocks\n", static_cast<int>(time_default));
  144. return time_fast <= time_default ? 0 : 1;
  145. }
  146. }//namespace fastAtan
  147. namespace taylorCos
  148. {
  149. using glm::qualifier;
  150. using glm::length_t;
  151. glm::vec4 const AngleShift(0.0f, glm::half_pi<float>(), glm::pi<float>(), glm::three_over_two_pi<float>());
  152. template<length_t L, typename T, qualifier Q>
  153. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> taylorSeriesNewCos(glm::vec<L, T, Q> const& x)
  154. {
  155. glm::vec<L, T, Q> const Powed2(x * x);
  156. glm::vec<L, T, Q> const Powed4(Powed2 * Powed2);
  157. glm::vec<L, T, Q> const Powed6(Powed4 * Powed2);
  158. glm::vec<L, T, Q> const Powed8(Powed4 * Powed4);
  159. return static_cast<T>(1)
  160. - Powed2 * static_cast<T>(0.5)
  161. + Powed4 * static_cast<T>(0.04166666666666666666666666666667)
  162. - Powed6 * static_cast<T>(0.00138888888888888888888888888889)
  163. + Powed8 * static_cast<T>(2.4801587301587301587301587301587e-5);
  164. }
  165. template<length_t L, typename T, qualifier Q>
  166. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> taylorSeriesNewCos6(glm::vec<L, T, Q> const& x)
  167. {
  168. glm::vec<L, T, Q> const Powed2(x * x);
  169. glm::vec<L, T, Q> const Powed4(Powed2 * Powed2);
  170. glm::vec<L, T, Q> const Powed6(Powed4 * Powed2);
  171. return static_cast<T>(1)
  172. - Powed2 * static_cast<T>(0.5)
  173. + Powed4 * static_cast<T>(0.04166666666666666666666666666667)
  174. - Powed6 * static_cast<T>(0.00138888888888888888888888888889);
  175. }
  176. template<glm::length_t L, qualifier Q>
  177. GLM_FUNC_QUALIFIER glm::vec<L, float, Q> fastAbs(glm::vec<L, float, Q> x)
  178. {
  179. int* Pointer = reinterpret_cast<int*>(&x[0]);
  180. Pointer[0] &= 0x7fffffff;
  181. Pointer[1] &= 0x7fffffff;
  182. Pointer[2] &= 0x7fffffff;
  183. Pointer[3] &= 0x7fffffff;
  184. return x;
  185. }
  186. template<glm::length_t L, typename T, qualifier Q>
  187. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> fastCosNew(glm::vec<L, T, Q> const& x)
  188. {
  189. glm::vec<L, T, Q> const Angle0_PI(fastAbs(fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
  190. return taylorSeriesNewCos6(x);
  191. /*
  192. vec<L, bool, Q> const FirstQuarterPi(lessThanEqual(Angle0_PI, vec<L, T, Q>(glm::half_pi<T>())));
  193. vec<L, T, Q> const RevertAngle(mix(vec<L, T, Q>(glm::pi<T>()), vec<L, T, Q>(0), FirstQuarterPi));
  194. vec<L, T, Q> const ReturnSign(mix(vec<L, T, Q>(-1), vec<L, T, Q>(1), FirstQuarterPi));
  195. vec<L, T, Q> const SectionAngle(RevertAngle - Angle0_PI);
  196. return ReturnSign * taylorSeriesNewCos(SectionAngle);
  197. */
  198. }
  199. int perf_fastCosNew(float Begin, float End, std::size_t Samples)
  200. {
  201. std::vector<glm::vec4> Results;
  202. Results.resize(Samples);
  203. float Steps = (End - Begin) / Samples;
  204. std::clock_t const TimeStampBegin = std::clock();
  205. for(std::size_t i = 0; i < Samples; ++i)
  206. Results[i] = fastCosNew(AngleShift + glm::vec4(Begin + Steps * i));
  207. std::clock_t const TimeStampEnd = std::clock();
  208. std::printf("fastCosNew %d clocks\n", static_cast<int>(TimeStampEnd - TimeStampBegin));
  209. int Error = 0;
  210. for(std::size_t i = 0; i < Samples; ++i)
  211. Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
  212. return Error;
  213. }
  214. template<length_t L, typename T, qualifier Q>
  215. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> deterministic_fmod(glm::vec<L, T, Q> const& x, T y)
  216. {
  217. return x - y * trunc(x / y);
  218. }
  219. template<length_t L, typename T, qualifier Q>
  220. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> fastCosDeterminisctic(glm::vec<L, T, Q> const& x)
  221. {
  222. glm::vec<L, T, Q> const Angle0_PI(abs(deterministic_fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
  223. glm::vec<L, bool, Q> const FirstQuarterPi(lessThanEqual(Angle0_PI, glm::vec<L, T, Q>(glm::half_pi<T>())));
  224. glm::vec<L, T, Q> const RevertAngle(mix(glm::vec<L, T, Q>(glm::pi<T>()), glm::vec<L, T, Q>(0), FirstQuarterPi));
  225. glm::vec<L, T, Q> const ReturnSign(mix(glm::vec<L, T, Q>(-1), glm::vec<L, T, Q>(1), FirstQuarterPi));
  226. glm::vec<L, T, Q> const SectionAngle(RevertAngle - Angle0_PI);
  227. return ReturnSign * taylorSeriesNewCos(SectionAngle);
  228. }
  229. int perf_fastCosDeterminisctic(float Begin, float End, std::size_t Samples)
  230. {
  231. std::vector<glm::vec4> Results;
  232. Results.resize(Samples);
  233. float Steps = (End - Begin) / Samples;
  234. std::clock_t const TimeStampBegin = std::clock();
  235. for(std::size_t i = 0; i < Samples; ++i)
  236. Results[i] = taylorCos::fastCosDeterminisctic(AngleShift + glm::vec4(Begin + Steps * i));
  237. std::clock_t const TimeStampEnd = std::clock();
  238. std::printf("fastCosDeterminisctic %d clocks\n", static_cast<int>(TimeStampEnd - TimeStampBegin));
  239. int Error = 0;
  240. for(std::size_t i = 0; i < Samples; ++i)
  241. Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
  242. return Error;
  243. }
  244. template<length_t L, typename T, qualifier Q>
  245. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> taylorSeriesRefCos(glm::vec<L, T, Q> const& x)
  246. {
  247. return static_cast<T>(1)
  248. - (x * x) / glm::factorial(static_cast<T>(2))
  249. + (x * x * x * x) / glm::factorial(static_cast<T>(4))
  250. - (x * x * x * x * x * x) / glm::factorial(static_cast<T>(6))
  251. + (x * x * x * x * x * x * x * x) / glm::factorial(static_cast<T>(8));
  252. }
  253. template<length_t L, typename T, qualifier Q>
  254. GLM_FUNC_QUALIFIER glm::vec<L, T, Q> fastRefCos(glm::vec<L, T, Q> const& x)
  255. {
  256. glm::vec<L, T, Q> const Angle0_PI(glm::abs(fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
  257. // return taylorSeriesRefCos(Angle0_PI);
  258. glm::vec<L, bool, Q> const FirstQuarterPi(lessThanEqual(Angle0_PI, glm::vec<L, T, Q>(glm::half_pi<T>())));
  259. glm::vec<L, T, Q> const RevertAngle(mix(glm::vec<L, T, Q>(glm::pi<T>()), glm::vec<L, T, Q>(0), FirstQuarterPi));
  260. glm::vec<L, T, Q> const ReturnSign(mix(glm::vec<L, T, Q>(-1), glm::vec<L, T, Q>(1), FirstQuarterPi));
  261. glm::vec<L, T, Q> const SectionAngle(RevertAngle - Angle0_PI);
  262. return ReturnSign * taylorSeriesRefCos(SectionAngle);
  263. }
  264. int perf_fastCosRef(float Begin, float End, std::size_t Samples)
  265. {
  266. std::vector<glm::vec4> Results;
  267. Results.resize(Samples);
  268. float Steps = (End - Begin) / Samples;
  269. std::clock_t const TimeStampBegin = std::clock();
  270. for(std::size_t i = 0; i < Samples; ++i)
  271. Results[i] = taylorCos::fastRefCos(AngleShift + glm::vec4(Begin + Steps * i));
  272. std::clock_t const TimeStampEnd = std::clock();
  273. std::printf("fastCosRef %d clocks\n", static_cast<int>(TimeStampEnd - TimeStampBegin));
  274. int Error = 0;
  275. for(std::size_t i = 0; i < Samples; ++i)
  276. Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
  277. return Error;
  278. }
  279. int perf_fastCosOld(float Begin, float End, std::size_t Samples)
  280. {
  281. std::vector<glm::vec4> Results;
  282. Results.resize(Samples);
  283. float Steps = (End - Begin) / Samples;
  284. std::clock_t const TimeStampBegin = std::clock();
  285. for(std::size_t i = 0; i < Samples; ++i)
  286. Results[i] = glm::fastCos(AngleShift + glm::vec4(Begin + Steps * i));
  287. std::clock_t const TimeStampEnd = std::clock();
  288. std::printf("fastCosOld %d clocks\n", static_cast<int>(TimeStampEnd - TimeStampBegin));
  289. int Error = 0;
  290. for(std::size_t i = 0; i < Samples; ++i)
  291. Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
  292. return Error;
  293. }
  294. int perf_cos(float Begin, float End, std::size_t Samples)
  295. {
  296. std::vector<glm::vec4> Results;
  297. Results.resize(Samples);
  298. float Steps = (End - Begin) / Samples;
  299. std::clock_t const TimeStampBegin = std::clock();
  300. for(std::size_t i = 0; i < Samples; ++i)
  301. Results[i] = glm::cos(AngleShift + glm::vec4(Begin + Steps * i));
  302. std::clock_t const TimeStampEnd = std::clock();
  303. std::printf("cos %d clocks\n", static_cast<int>(TimeStampEnd - TimeStampBegin));
  304. int Error = 0;
  305. for(std::size_t i = 0; i < Samples; ++i)
  306. Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
  307. return Error;
  308. }
  309. int perf(std::size_t const Samples)
  310. {
  311. int Error = 0;
  312. float const Begin = -glm::pi<float>();
  313. float const End = glm::pi<float>();
  314. Error += perf_cos(Begin, End, Samples);
  315. Error += perf_fastCosOld(Begin, End, Samples);
  316. Error += perf_fastCosRef(Begin, End, Samples);
  317. //Error += perf_fastCosNew(Begin, End, Samples);
  318. Error += perf_fastCosDeterminisctic(Begin, End, Samples);
  319. return Error;
  320. }
  321. int test()
  322. {
  323. int Error = 0;
  324. //for(float Angle = -4.0f * glm::pi<float>(); Angle < 4.0f * glm::pi<float>(); Angle += 0.1f)
  325. //for(float Angle = -720.0f; Angle < 720.0f; Angle += 0.1f)
  326. for(float Angle = 0.0f; Angle < 180.0f; Angle += 0.1f)
  327. {
  328. float const modAngle = std::fmod(glm::abs(Angle), 360.f);
  329. assert(modAngle >= 0.0f && modAngle <= 360.f);
  330. float const radAngle = glm::radians(modAngle);
  331. float const Cos0 = std::cos(radAngle);
  332. float const Cos1 = taylorCos::fastRefCos(glm::fvec1(radAngle)).x;
  333. Error += glm::abs(Cos1 - Cos0) < 0.1f ? 0 : 1;
  334. //float const Cos2 = taylorCos::fastCosNew(glm::fvec1(radAngle)).x;
  335. //Error += glm::abs(Cos2 - Cos0) < 0.1f ? 0 : 1;
  336. assert(!Error);
  337. }
  338. return Error;
  339. }
  340. }//namespace taylorCos
  341. namespace taylor2
  342. {
  343. glm::vec4 const AngleShift(0.0f, glm::pi<float>() * 0.5f, glm::pi<float>() * 1.0f, glm::pi<float>() * 1.5f);
  344. float taylorCosA(float x)
  345. {
  346. return 1.f
  347. - (x * x) * (1.f / 2.f)
  348. + (x * x * x * x) * (1.f / 24.f)
  349. - (x * x * x * x * x * x) * (1.f / 720.f)
  350. + (x * x * x * x * x * x * x * x) * (1.f / 40320.f);
  351. }
  352. float taylorCosB(float x)
  353. {
  354. return 1.f
  355. - (x * x) * (1.f / 2.f)
  356. + (x * x * x * x) * (1.f / 24.f)
  357. - (x * x * x * x * x * x) * (1.f / 720.f)
  358. + (x * x * x * x * x * x * x * x) * (1.f / 40320.f);
  359. }
  360. float taylorCosC(float x)
  361. {
  362. return 1.f
  363. - (x * x) * (1.f / 2.f)
  364. + ((x * x) * (x * x)) * (1.f / 24.f)
  365. - (((x * x) * (x * x)) * (x * x)) * (1.f / 720.f)
  366. + (((x * x) * (x * x)) * ((x * x) * (x * x))) * (1.f / 40320.f);
  367. }
  368. int perf_taylorCosA(float Begin, float End, std::size_t Samples)
  369. {
  370. std::vector<float> Results;
  371. Results.resize(Samples);
  372. float Steps = (End - Begin) / Samples;
  373. std::clock_t const TimeStampBegin = std::clock();
  374. for(std::size_t i = 0; i < Samples; ++i)
  375. Results[i] = taylorCosA(AngleShift.x + Begin + Steps * i);
  376. std::clock_t const TimeStampEnd = std::clock();
  377. std::printf("taylorCosA %d clocks\n", static_cast<int>(TimeStampEnd - TimeStampBegin));
  378. int Error = 0;
  379. for(std::size_t i = 0; i < Samples; ++i)
  380. Error += Results[i] >= -1.0f && Results[i] <= 1.0f ? 0 : 1;
  381. return Error;
  382. }
  383. int perf_taylorCosB(float Begin, float End, std::size_t Samples)
  384. {
  385. std::vector<float> Results;
  386. Results.resize(Samples);
  387. float Steps = (End - Begin) / Samples;
  388. std::clock_t const TimeStampBegin = std::clock();
  389. for(std::size_t i = 0; i < Samples; ++i)
  390. Results[i] = taylorCosB(AngleShift.x + Begin + Steps * i);
  391. std::clock_t const TimeStampEnd = std::clock();
  392. std::printf("taylorCosB %d clocks\n", static_cast<int>(TimeStampEnd - TimeStampBegin));
  393. int Error = 0;
  394. for(std::size_t i = 0; i < Samples; ++i)
  395. Error += Results[i] >= -1.0f && Results[i] <= 1.0f ? 0 : 1;
  396. return Error;
  397. }
  398. int perf_taylorCosC(float Begin, float End, std::size_t Samples)
  399. {
  400. std::vector<float> Results;
  401. Results.resize(Samples);
  402. float Steps = (End - Begin) / Samples;
  403. std::clock_t const TimeStampBegin = std::clock();
  404. for(std::size_t i = 0; i < Samples; ++i)
  405. Results[i] = taylorCosC(AngleShift.x + Begin + Steps * i);
  406. std::clock_t const TimeStampEnd = std::clock();
  407. std::printf("taylorCosC %d clocks\n", static_cast<int>(TimeStampEnd - TimeStampBegin));
  408. int Error = 0;
  409. for(std::size_t i = 0; i < Samples; ++i)
  410. Error += Results[i] >= -1.0f && Results[i] <= 1.0f ? 0 : 1;
  411. return Error;
  412. }
  413. int perf(std::size_t Samples)
  414. {
  415. int Error = 0;
  416. float const Begin = -glm::pi<float>();
  417. float const End = glm::pi<float>();
  418. Error += perf_taylorCosA(Begin, End, Samples);
  419. Error += perf_taylorCosB(Begin, End, Samples);
  420. Error += perf_taylorCosC(Begin, End, Samples);
  421. return Error;
  422. }
  423. }//namespace taylor2
  424. int main()
  425. {
  426. int Error(0);
  427. Error += ::taylor2::perf(1000);
  428. Error += ::taylorCos::test();
  429. Error += ::taylorCos::perf(1000);
  430. # ifdef NDEBUG
  431. ::fastCos::perf(false);
  432. ::fastSin::perf(false);
  433. ::fastTan::perf(false);
  434. ::fastAcos::perf(false);
  435. ::fastAsin::perf(false);
  436. ::fastAtan::perf(false);
  437. # endif//NDEBUG
  438. return Error;
  439. }