gtx_io.cpp 6.8 KB

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