core_func_packing.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2014 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. /*
  40. std::vector<glm::hvec2> A;
  41. A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 0.0f)));
  42. A.push_back(glm::hvec2(glm::half( 0.5f), glm::half( 0.7f)));
  43. A.push_back(glm::hvec2(glm::half( 0.1f), glm::half( 0.2f)));
  44. */
  45. std::vector<glm::vec2> A;
  46. A.push_back(glm::vec2(1.0f, 0.0f));
  47. A.push_back(glm::vec2(0.5f, 0.7f));
  48. A.push_back(glm::vec2(0.1f, 0.2f));
  49. for(std::size_t i = 0; i < A.size(); ++i)
  50. {
  51. glm::vec2 B(A[i]);
  52. glm::uint32 C = glm::packUnorm2x16(B);
  53. glm::vec2 D = glm::unpackUnorm2x16(C);
  54. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 65535.f)) ? 0 : 1;
  55. assert(!Error);
  56. }
  57. return Error;
  58. }
  59. int test_packSnorm2x16()
  60. {
  61. int Error = 0;
  62. std::vector<glm::vec2> A;
  63. A.push_back(glm::vec2( 1.0f, 0.0f));
  64. A.push_back(glm::vec2(-0.5f,-0.7f));
  65. A.push_back(glm::vec2(-0.1f, 0.1f));
  66. for(std::size_t i = 0; i < A.size(); ++i)
  67. {
  68. glm::vec2 B(A[i]);
  69. glm::uint32 C = glm::packSnorm2x16(B);
  70. glm::vec2 D = glm::unpackSnorm2x16(C);
  71. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 32767.0f * 2.0f)) ? 0 : 1;
  72. assert(!Error);
  73. }
  74. return Error;
  75. }
  76. int test_packUnorm4x8()
  77. {
  78. int Error = 0;
  79. std::vector<glm::vec4> A;
  80. A.push_back(glm::vec4(1.0f, 0.7f, 0.3f, 0.0f));
  81. A.push_back(glm::vec4(0.5f, 0.1f, 0.2f, 0.3f));
  82. for(std::size_t i = 0; i < A.size(); ++i)
  83. {
  84. glm::vec4 B(A[i]);
  85. glm::uint32 C = glm::packUnorm4x8(B);
  86. glm::vec4 D = glm::unpackUnorm4x8(C);
  87. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1;
  88. assert(!Error);
  89. }
  90. return Error;
  91. }
  92. int test_packSnorm4x8()
  93. {
  94. int Error = 0;
  95. std::vector<glm::vec4> A;
  96. A.push_back(glm::vec4( 1.0f, 0.0f,-0.5f,-1.0f));
  97. A.push_back(glm::vec4(-0.7f,-0.1f, 0.1f, 0.7f));
  98. for(std::size_t i = 0; i < A.size(); ++i)
  99. {
  100. glm::vec4 B(A[i]);
  101. glm::uint32 C = glm::packSnorm4x8(B);
  102. glm::vec4 D = glm::unpackSnorm4x8(C);
  103. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
  104. assert(!Error);
  105. }
  106. return Error;
  107. }
  108. int test_packHalf2x16()
  109. {
  110. int Error = 0;
  111. /*
  112. std::vector<glm::hvec2> A;
  113. A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 2.0f)));
  114. A.push_back(glm::hvec2(glm::half(-1.0f), glm::half(-2.0f)));
  115. A.push_back(glm::hvec2(glm::half(-1.1f), glm::half( 1.1f)));
  116. */
  117. std::vector<glm::vec2> A;
  118. A.push_back(glm::vec2( 1.0f, 2.0f));
  119. A.push_back(glm::vec2(-1.0f,-2.0f));
  120. A.push_back(glm::vec2(-1.1f, 1.1f));
  121. for(std::size_t i = 0; i < A.size(); ++i)
  122. {
  123. glm::vec2 B(A[i]);
  124. glm::uint C = glm::packHalf2x16(B);
  125. glm::vec2 D = glm::unpackHalf2x16(C);
  126. //Error += B == D ? 0 : 1;
  127. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
  128. assert(!Error);
  129. }
  130. return Error;
  131. }
  132. int test_packDouble2x32()
  133. {
  134. int Error = 0;
  135. std::vector<glm::uvec2> A;
  136. A.push_back(glm::uvec2( 1, 2));
  137. A.push_back(glm::uvec2(-1,-2));
  138. A.push_back(glm::uvec2(-1000, 1100));
  139. for(std::size_t i = 0; i < A.size(); ++i)
  140. {
  141. glm::uvec2 B(A[i]);
  142. double C = glm::packDouble2x32(B);
  143. glm::uvec2 D = glm::unpackDouble2x32(C);
  144. Error += B == D ? 0 : 1;
  145. assert(!Error);
  146. }
  147. return Error;
  148. }
  149. int main()
  150. {
  151. int Error = 0;
  152. Error += test_packSnorm4x8();
  153. Error += test_packUnorm4x8();
  154. Error += test_packSnorm2x16();
  155. Error += test_packUnorm2x16();
  156. Error += test_packHalf2x16();
  157. Error += test_packDouble2x32();
  158. return Error;
  159. }