core_func_packing.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/core/func_packing.cpp
  28. /// @date 2011-01-15 / 2011-09-13
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/gtc/type_precision.hpp>
  32. #include <glm/gtc/epsilon.hpp>
  33. #include <glm/vector_relational.hpp>
  34. #include <glm/packing.hpp>
  35. #include <vector>
  36. int test_packUnorm2x16()
  37. {
  38. int Error = 0;
  39. std::vector<glm::vec2> A;
  40. A.push_back(glm::vec2(1.0f, 0.0f));
  41. A.push_back(glm::vec2(0.5f, 0.7f));
  42. A.push_back(glm::vec2(0.1f, 0.2f));
  43. for(std::size_t i = 0; i < A.size(); ++i)
  44. {
  45. glm::vec2 B(A[i]);
  46. glm::uint32 C = glm::packUnorm2x16(B);
  47. glm::vec2 D = glm::unpackUnorm2x16(C);
  48. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 65535.f)) ? 0 : 1;
  49. assert(!Error);
  50. }
  51. return Error;
  52. }
  53. int test_packSnorm2x16()
  54. {
  55. int Error = 0;
  56. std::vector<glm::vec2> A;
  57. A.push_back(glm::vec2( 1.0f, 0.0f));
  58. A.push_back(glm::vec2(-0.5f,-0.7f));
  59. A.push_back(glm::vec2(-0.1f, 0.1f));
  60. for(std::size_t i = 0; i < A.size(); ++i)
  61. {
  62. glm::vec2 B(A[i]);
  63. glm::uint32 C = glm::packSnorm2x16(B);
  64. glm::vec2 D = glm::unpackSnorm2x16(C);
  65. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1;
  66. assert(!Error);
  67. }
  68. return Error;
  69. }
  70. int test_packUnorm4x8()
  71. {
  72. int Error = 0;
  73. std::vector<glm::vec4> A;
  74. A.push_back(glm::vec4(1.0f, 0.7f, 0.3f, 0.0f));
  75. A.push_back(glm::vec4(0.5f, 0.1f, 0.2f, 0.3f));
  76. for(std::size_t i = 0; i < A.size(); ++i)
  77. {
  78. glm::vec4 B(A[i]);
  79. glm::uint32 C = glm::packUnorm4x8(B);
  80. glm::vec4 D = glm::unpackUnorm4x8(C);
  81. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1;
  82. assert(!Error);
  83. }
  84. return Error;
  85. }
  86. int test_packSnorm4x8()
  87. {
  88. int Error = 0;
  89. std::vector<glm::vec4> A;
  90. A.push_back(glm::vec4( 1.0f, 0.0f,-0.5f,-1.0f));
  91. A.push_back(glm::vec4(-0.7f,-0.1f, 0.1f, 0.7f));
  92. for(std::size_t i = 0; i < A.size(); ++i)
  93. {
  94. glm::vec4 B(A[i]);
  95. glm::uint32 C = glm::packSnorm4x8(B);
  96. glm::vec4 D = glm::unpackSnorm4x8(C);
  97. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
  98. assert(!Error);
  99. }
  100. return Error;
  101. }
  102. int test_packHalf2x16()
  103. {
  104. int Error = 0;
  105. /*
  106. std::vector<glm::hvec2> A;
  107. A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 2.0f)));
  108. A.push_back(glm::hvec2(glm::half(-1.0f), glm::half(-2.0f)));
  109. A.push_back(glm::hvec2(glm::half(-1.1f), glm::half( 1.1f)));
  110. */
  111. std::vector<glm::vec2> A;
  112. A.push_back(glm::vec2( 1.0f, 2.0f));
  113. A.push_back(glm::vec2(-1.0f,-2.0f));
  114. A.push_back(glm::vec2(-1.1f, 1.1f));
  115. for(std::size_t i = 0; i < A.size(); ++i)
  116. {
  117. glm::vec2 B(A[i]);
  118. glm::uint C = glm::packHalf2x16(B);
  119. glm::vec2 D = glm::unpackHalf2x16(C);
  120. //Error += B == D ? 0 : 1;
  121. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
  122. assert(!Error);
  123. }
  124. return Error;
  125. }
  126. int test_packDouble2x32()
  127. {
  128. int Error = 0;
  129. std::vector<glm::uvec2> A;
  130. A.push_back(glm::uvec2( 1, 2));
  131. A.push_back(glm::uvec2(-1,-2));
  132. A.push_back(glm::uvec2(-1000, 1100));
  133. for(std::size_t i = 0; i < A.size(); ++i)
  134. {
  135. glm::uvec2 B(A[i]);
  136. double C = glm::packDouble2x32(B);
  137. glm::uvec2 D = glm::unpackDouble2x32(C);
  138. Error += B == D ? 0 : 1;
  139. assert(!Error);
  140. }
  141. return Error;
  142. }
  143. int main()
  144. {
  145. int Error = 0;
  146. Error += test_packSnorm4x8();
  147. Error += test_packUnorm4x8();
  148. Error += test_packSnorm2x16();
  149. Error += test_packUnorm2x16();
  150. Error += test_packHalf2x16();
  151. Error += test_packDouble2x32();
  152. return Error;
  153. }