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