core_func_packing.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. glm::uint32 Packed = glm::packUnorm4x8(glm::vec4(1.0f, 0.5f, 0.0f, 1.0f));
  74. glm::u8vec4 Vec(255, 128, 0, 255);
  75. glm::uint32 & Ref = *reinterpret_cast<glm::uint32*>(&Vec[0]);
  76. Error += Packed == Ref ? 0 : 1;
  77. std::vector<glm::vec4> A;
  78. A.push_back(glm::vec4(1.0f, 0.7f, 0.3f, 0.0f));
  79. A.push_back(glm::vec4(0.5f, 0.1f, 0.2f, 0.3f));
  80. for(std::size_t i = 0; i < A.size(); ++i)
  81. {
  82. glm::vec4 B(A[i]);
  83. glm::uint32 C = glm::packUnorm4x8(B);
  84. glm::vec4 D = glm::unpackUnorm4x8(C);
  85. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 255.f)) ? 0 : 1;
  86. assert(!Error);
  87. }
  88. return Error;
  89. }
  90. int test_packSnorm4x8()
  91. {
  92. int Error = 0;
  93. std::vector<glm::vec4> A;
  94. A.push_back(glm::vec4( 1.0f, 0.0f,-0.5f,-1.0f));
  95. A.push_back(glm::vec4(-0.7f,-0.1f, 0.1f, 0.7f));
  96. for(std::size_t i = 0; i < A.size(); ++i)
  97. {
  98. glm::vec4 B(A[i]);
  99. glm::uint32 C = glm::packSnorm4x8(B);
  100. glm::vec4 D = glm::unpackSnorm4x8(C);
  101. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
  102. assert(!Error);
  103. }
  104. return Error;
  105. }
  106. int test_packHalf2x16()
  107. {
  108. int Error = 0;
  109. /*
  110. std::vector<glm::hvec2> A;
  111. A.push_back(glm::hvec2(glm::half( 1.0f), glm::half( 2.0f)));
  112. A.push_back(glm::hvec2(glm::half(-1.0f), glm::half(-2.0f)));
  113. A.push_back(glm::hvec2(glm::half(-1.1f), glm::half( 1.1f)));
  114. */
  115. std::vector<glm::vec2> A;
  116. A.push_back(glm::vec2( 1.0f, 2.0f));
  117. A.push_back(glm::vec2(-1.0f,-2.0f));
  118. A.push_back(glm::vec2(-1.1f, 1.1f));
  119. for(std::size_t i = 0; i < A.size(); ++i)
  120. {
  121. glm::vec2 B(A[i]);
  122. glm::uint C = glm::packHalf2x16(B);
  123. glm::vec2 D = glm::unpackHalf2x16(C);
  124. //Error += B == D ? 0 : 1;
  125. Error += glm::all(glm::epsilonEqual(B, D, 1.0f / 127.f)) ? 0 : 1;
  126. assert(!Error);
  127. }
  128. return Error;
  129. }
  130. int test_packDouble2x32()
  131. {
  132. int Error = 0;
  133. std::vector<glm::uvec2> A;
  134. A.push_back(glm::uvec2( 1, 2));
  135. A.push_back(glm::uvec2(-1,-2));
  136. A.push_back(glm::uvec2(-1000, 1100));
  137. for(std::size_t i = 0; i < A.size(); ++i)
  138. {
  139. glm::uvec2 B(A[i]);
  140. double C = glm::packDouble2x32(B);
  141. glm::uvec2 D = glm::unpackDouble2x32(C);
  142. Error += B == D ? 0 : 1;
  143. assert(!Error);
  144. }
  145. return Error;
  146. }
  147. int main()
  148. {
  149. int Error = 0;
  150. Error += test_packSnorm4x8();
  151. Error += test_packUnorm4x8();
  152. Error += test_packSnorm2x16();
  153. Error += test_packUnorm2x16();
  154. Error += test_packHalf2x16();
  155. Error += test_packDouble2x32();
  156. return Error;
  157. }