gtx_io.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/gtx/gtx_io.cpp
  28. /// @date 2013-11-22 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/gtc/type_precision.hpp>
  32. #include <glm/gtx/io.hpp>
  33. #include <iostream>
  34. #include <sstream>
  35. #include <typeinfo>
  36. namespace
  37. {
  38. template <typename CTy, typename CTr>
  39. std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, glm::precision const& a)
  40. {
  41. typename std::basic_ostream<CTy,CTr>::sentry const cerberus(os);
  42. if (cerberus)
  43. {
  44. switch (a) {
  45. case glm::highp: os << "hi"; break;
  46. case glm::mediump: os << "md"; break;
  47. case glm::lowp: os << "lo"; break;
  48. }
  49. }
  50. return os;
  51. }
  52. template <typename U, glm::precision P, typename T, typename CTy, typename CTr>
  53. std::basic_string<CTy> type_name(std::basic_ostream<CTy,CTr>& os, T const&)
  54. {
  55. std::basic_ostringstream<CTy,CTr> ostr;
  56. if (typeid(T) == typeid(glm::tquat<U,P>)) { ostr << "quat"; }
  57. else if (typeid(T) == typeid(glm::tvec2<U,P>)) { ostr << "vec2"; }
  58. else if (typeid(T) == typeid(glm::tvec3<U,P>)) { ostr << "vec3"; }
  59. else if (typeid(T) == typeid(glm::tvec4<U,P>)) { ostr << "vec4"; }
  60. else if (typeid(T) == typeid(glm::tmat2x2<U,P>)) { ostr << "mat2x2"; }
  61. else if (typeid(T) == typeid(glm::tmat2x3<U,P>)) { ostr << "mat2x3"; }
  62. else if (typeid(T) == typeid(glm::tmat2x4<U,P>)) { ostr << "mat2x4"; }
  63. else if (typeid(T) == typeid(glm::tmat3x2<U,P>)) { ostr << "mat3x2"; }
  64. else if (typeid(T) == typeid(glm::tmat3x3<U,P>)) { ostr << "mat3x3"; }
  65. else if (typeid(T) == typeid(glm::tmat3x4<U,P>)) { ostr << "mat3x4"; }
  66. else if (typeid(T) == typeid(glm::tmat4x2<U,P>)) { ostr << "mat4x2"; }
  67. else if (typeid(T) == typeid(glm::tmat4x3<U,P>)) { ostr << "mat4x3"; }
  68. else if (typeid(T) == typeid(glm::tmat4x4<U,P>)) { ostr << "mat4x4"; }
  69. else { ostr << "unknown"; }
  70. ostr << '<' << typeid(U).name() << ',' << P << '>';
  71. return ostr.str();
  72. }
  73. } // namespace {
  74. template <typename T, glm::precision P, typename OS>
  75. int test_io_quat(OS& os)
  76. {
  77. os << '\n' << typeid(OS).name() << '\n';
  78. glm::tquat<T,P> const q(1, 0, 0, 0);
  79. {
  80. glm::io::basic_format_saver<typename OS::char_type> const iofs(os);
  81. os << glm::io::precision(2) << glm::io::width(1 + 2 + 1 + 2)
  82. << type_name<T,P>(os, q) << ": " << q << '\n';
  83. }
  84. {
  85. glm::io::basic_format_saver<typename OS::char_type> const iofs(os);
  86. os << glm::io::unformatted
  87. << type_name<T,P>(os, q) << ": " << q << '\n';
  88. }
  89. return 0;
  90. }
  91. template <typename T, glm::precision P, typename OS>
  92. int test_io_vec(OS& os)
  93. {
  94. os << '\n' << typeid(OS).name() << '\n';
  95. glm::tvec2<T,P> const v2(0, 1);
  96. glm::tvec3<T,P> const v3(2, 3, 4);
  97. glm::tvec4<T,P> const v4(5, 6, 7, 8);
  98. os << type_name<T,P>(os, v2) << ": " << v2 << '\n'
  99. << type_name<T,P>(os, v3) << ": " << v3 << '\n'
  100. << type_name<T,P>(os, v4) << ": " << v4 << '\n';
  101. glm::io::basic_format_saver<typename OS::char_type> const iofs(os);
  102. os << glm::io::precision(2) << glm::io::width(1 + 2 + 1 + 2)
  103. << type_name<T,P>(os, v2) << ": " << v2 << '\n'
  104. << type_name<T,P>(os, v3) << ": " << v3 << '\n'
  105. << type_name<T,P>(os, v4) << ": " << v4 << '\n';
  106. return 0;
  107. }
  108. template <typename T, glm::precision P, typename OS>
  109. int test_io_mat(OS& os)
  110. {
  111. os << '\n' << typeid(OS).name() << '\n';
  112. glm::tvec2<T,P> const v2_1( 0, 1);
  113. glm::tvec2<T,P> const v2_2( 2, 3);
  114. glm::tvec2<T,P> const v2_3( 4, 5);
  115. glm::tvec2<T,P> const v2_4( 6, 7);
  116. glm::tvec3<T,P> const v3_1( 8, 9, 10);
  117. glm::tvec3<T,P> const v3_2(11, 12, 13);
  118. glm::tvec3<T,P> const v3_3(14, 15, 16);
  119. glm::tvec3<T,P> const v3_4(17, 18, 19);
  120. glm::tvec4<T,P> const v4_1(20, 21, 22, 23);
  121. glm::tvec4<T,P> const v4_2(24, 25, 26, 27);
  122. glm::tvec4<T,P> const v4_3(28, 29, 30, 31);
  123. glm::tvec4<T,P> const v4_4(32, 33, 34, 35);
  124. #if 0
  125. os << "mat2x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x2<T,P>(v2_1, v2_2) << '\n'
  126. << "mat2x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x3<T,P>(v3_1, v3_2) << '\n'
  127. << "mat2x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x4<T,P>(v4_1, v4_2) << '\n'
  128. << "mat3x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x2<T,P>(v2_1, v2_2, v2_3) << '\n'
  129. << "mat3x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x3<T,P>(v3_1, v3_2, v3_3) << '\n'
  130. << "mat3x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x4<T,P>(v4_1, v4_2, v4_3) << '\n'
  131. << "mat4x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x2<T,P>(v2_1, v2_2, v2_3, v2_4) << '\n'
  132. << "mat4x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x3<T,P>(v3_1, v3_2, v3_3, v3_4) << '\n'
  133. << "mat4x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x4<T,P>(v4_1, v4_2, v4_3, v4_4) << '\n';
  134. #endif
  135. glm::io::basic_format_saver<typename OS::char_type> const iofs(os);
  136. os << glm::io::precision(2) << glm::io::width(1 + 2 + 1 + 2)
  137. << "mat2x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x2<T,P>(v2_1, v2_2) << '\n'
  138. << "mat2x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x3<T,P>(v3_1, v3_2) << '\n'
  139. << "mat2x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x4<T,P>(v4_1, v4_2) << '\n'
  140. << "mat3x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x2<T,P>(v2_1, v2_2, v2_3) << '\n'
  141. << "mat3x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x3<T,P>(v3_1, v3_2, v3_3) << '\n'
  142. << "mat3x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x4<T,P>(v4_1, v4_2, v4_3) << '\n'
  143. << "mat4x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x2<T,P>(v2_1, v2_2, v2_3, v2_4) << '\n'
  144. << "mat4x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x3<T,P>(v3_1, v3_2, v3_3, v3_4) << '\n'
  145. << "mat4x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x4<T,P>(v4_1, v4_2, v4_3, v4_4) << '\n';
  146. os << glm::io::unformatted
  147. << glm::io::order(glm::io::column_major)
  148. << "mat2x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x2<T,P>(v2_1, v2_2) << '\n'
  149. << "mat2x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x3<T,P>(v3_1, v3_2) << '\n'
  150. << "mat2x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat2x4<T,P>(v4_1, v4_2) << '\n'
  151. << "mat3x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x2<T,P>(v2_1, v2_2, v2_3) << '\n'
  152. << "mat3x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x3<T,P>(v3_1, v3_2, v3_3) << '\n'
  153. << "mat3x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat3x4<T,P>(v4_1, v4_2, v4_3) << '\n'
  154. << "mat4x2<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x2<T,P>(v2_1, v2_2, v2_3, v2_4) << '\n'
  155. << "mat4x3<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x3<T,P>(v3_1, v3_2, v3_3, v3_4) << '\n'
  156. << "mat4x4<" << typeid(T).name() << ',' << P << ">: " << glm::tmat4x4<T,P>(v4_1, v4_2, v4_3, v4_4) << '\n';
  157. return 0;
  158. }
  159. int main()
  160. {
  161. int Error(0);
  162. Error += test_io_quat<float, glm::highp>(std::cout);
  163. Error += test_io_quat<float, glm::highp>(std::wcout);
  164. Error += test_io_quat<int, glm::mediump>(std::cout);
  165. Error += test_io_quat<int, glm::mediump>(std::wcout);
  166. Error += test_io_quat<glm::uint, glm::lowp>(std::cout);
  167. Error += test_io_quat<glm::uint, glm::lowp>(std::wcout);
  168. Error += test_io_vec<float, glm::highp>(std::cout);
  169. Error += test_io_vec<float, glm::highp>(std::wcout);
  170. Error += test_io_vec<int, glm::mediump>(std::cout);
  171. Error += test_io_vec<int, glm::mediump>(std::wcout);
  172. Error += test_io_vec<glm::uint, glm::lowp>(std::cout);
  173. Error += test_io_vec<glm::uint, glm::lowp>(std::wcout);
  174. Error += test_io_mat<float, glm::highp>(std::cout);
  175. Error += test_io_mat<float, glm::lowp>(std::wcout);
  176. return Error;
  177. }