glm/test/gtx/gtx_io.cpp

187 lines
7.1 KiB
C++
Raw Permalink Normal View History

2016-11-12 13:19:22 +00:00
#define GLM_ENABLE_EXPERIMENTAL
2018-07-11 22:36:59 +00:00
#include <glm/glm.hpp>
#if GLM_LANG & GLM_LANG_CXXMS_FLAG
2013-11-22 23:48:58 +00:00
#include <glm/gtc/type_precision.hpp>
#include <glm/gtx/io.hpp>
#include <iostream>
2013-12-18 10:34:06 +00:00
#include <sstream>
2013-11-22 23:48:58 +00:00
#include <typeinfo>
2014-11-14 23:56:14 +00:00
namespace
{
template<typename CTy, typename CTr>
2017-08-15 19:23:47 +00:00
std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, glm::qualifier const& a)
2014-11-14 23:56:14 +00:00
{
typename std::basic_ostream<CTy,CTr>::sentry const cerberus(os);
if (cerberus)
{
switch (a) {
case glm::highp: os << "uhi"; break;
case glm::mediump: os << "umd"; break;
case glm::lowp: os << "ulo"; break;
2018-07-31 21:05:48 +00:00
# if GLM_CONFIG_ALIGNED_GENTYPES == GLM_ENABLE
2016-07-17 01:43:56 +00:00
case glm::aligned_highp: os << "ahi"; break;
case glm::aligned_mediump: os << "amd"; break;
case glm::aligned_lowp: os << "alo"; break;
# endif
2014-11-14 23:56:14 +00:00
}
}
return os;
}
2017-08-15 19:23:47 +00:00
template<typename U, glm::qualifier P, typename T, typename CTy, typename CTr>
2017-08-08 09:30:21 +00:00
std::basic_string<CTy> type_name(std::basic_ostream<CTy,CTr>&, T const&)
2014-11-14 23:56:14 +00:00
{
std::basic_ostringstream<CTy,CTr> ostr;
2018-08-07 12:46:56 +00:00
if (typeid(T) == typeid(glm::qua<U,P>)) { ostr << "quat"; }
else if (typeid(T) == typeid(glm::vec<2, U,P>)) { ostr << "vec2"; }
else if (typeid(T) == typeid(glm::vec<3, U,P>)) { ostr << "vec3"; }
else if (typeid(T) == typeid(glm::vec<4, U,P>)) { ostr << "vec4"; }
else if (typeid(T) == typeid(glm::mat<2, 2, U,P>)) { ostr << "mat2x2"; }
else if (typeid(T) == typeid(glm::mat<2, 3, U,P>)) { ostr << "mat2x3"; }
else if (typeid(T) == typeid(glm::mat<2, 4, U,P>)) { ostr << "mat2x4"; }
else if (typeid(T) == typeid(glm::mat<3, 2, U,P>)) { ostr << "mat3x2"; }
else if (typeid(T) == typeid(glm::mat<3, 3, U,P>)) { ostr << "mat3x3"; }
else if (typeid(T) == typeid(glm::mat<3, 4, U,P>)) { ostr << "mat3x4"; }
else if (typeid(T) == typeid(glm::mat<4, 2, U,P>)) { ostr << "mat4x2"; }
else if (typeid(T) == typeid(glm::mat<4, 3, U,P>)) { ostr << "mat4x3"; }
else if (typeid(T) == typeid(glm::mat<4, 4, U,P>)) { ostr << "mat4x4"; }
2014-11-14 23:56:14 +00:00
else { ostr << "unknown"; }
ostr << '<' << typeid(U).name() << ',' << P << '>';
return ostr.str();
}
2013-11-22 23:48:58 +00:00
} // namespace {
2017-08-15 19:23:47 +00:00
template<typename T, glm::qualifier P, typename OS>
int test_io_quat(OS& os)
{
2014-11-14 23:56:14 +00:00
os << '\n' << typeid(OS).name() << '\n';
2018-08-07 12:46:56 +00:00
glm::qua<T, P> const q(1, 0, 0, 0);
2014-11-14 23:56:14 +00:00
{
glm::io::basic_format_saver<typename OS::char_type> const iofs(os);
os << glm::io::precision(2) << glm::io::width(1 + 2 + 1 + 2)
2018-08-07 12:46:56 +00:00
<< type_name<T, P>(os, q) << ": " << q << '\n';
2014-11-14 23:56:14 +00:00
}
{
glm::io::basic_format_saver<typename OS::char_type> const iofs(os);
2014-11-14 23:56:14 +00:00
os << glm::io::unformatted
2018-08-07 12:46:56 +00:00
<< type_name<T, P>(os, q) << ": " << q << '\n';
2014-11-14 23:56:14 +00:00
}
return 0;
}
2017-08-15 19:23:47 +00:00
template<typename T, glm::qualifier P, typename OS>
2013-11-22 23:48:58 +00:00
int test_io_vec(OS& os)
{
2014-11-14 23:56:14 +00:00
os << '\n' << typeid(OS).name() << '\n';
2013-11-22 23:48:58 +00:00
glm::vec<2, T,P> const v2(0, 1);
glm::vec<3, T,P> const v3(2, 3, 4);
glm::vec<4, T,P> const v4(5, 6, 7, 8);
2013-11-22 23:48:58 +00:00
2014-11-14 23:56:14 +00:00
os << type_name<T,P>(os, v2) << ": " << v2 << '\n'
<< type_name<T,P>(os, v3) << ": " << v3 << '\n'
<< type_name<T,P>(os, v4) << ": " << v4 << '\n';
glm::io::basic_format_saver<typename OS::char_type> const iofs(os);
os << glm::io::precision(2) << glm::io::width(1 + 2 + 1 + 2)
<< type_name<T,P>(os, v2) << ": " << v2 << '\n'
<< type_name<T,P>(os, v3) << ": " << v3 << '\n'
<< type_name<T,P>(os, v4) << ": " << v4 << '\n';
2013-11-22 23:48:58 +00:00
2014-11-14 23:56:14 +00:00
return 0;
2013-11-22 23:48:58 +00:00
}
2017-08-15 19:23:47 +00:00
template<typename T, glm::qualifier P, typename OS>
2015-10-29 17:56:08 +00:00
int test_io_mat(OS& os, glm::io::order_type otype)
2013-11-22 23:48:58 +00:00
{
2014-11-14 23:56:14 +00:00
os << '\n' << typeid(OS).name() << '\n';
glm::vec<2, T,P> const v2_1( 0, 1);
glm::vec<2, T,P> const v2_2( 2, 3);
glm::vec<2, T,P> const v2_3( 4, 5);
glm::vec<2, T,P> const v2_4( 6, 7);
glm::vec<3, T,P> const v3_1( 8, 9, 10);
glm::vec<3, T,P> const v3_2(11, 12, 13);
glm::vec<3, T,P> const v3_3(14, 15, 16);
glm::vec<3, T,P> const v3_4(17, 18, 19);
glm::vec<4, T,P> const v4_1(20, 21, 22, 23);
glm::vec<4, T,P> const v4_2(24, 25, 26, 27);
glm::vec<4, T,P> const v4_3(28, 29, 30, 31);
glm::vec<4, T,P> const v4_4(32, 33, 34, 35);
2013-11-22 23:48:58 +00:00
2014-11-14 23:56:14 +00:00
glm::io::basic_format_saver<typename OS::char_type> const iofs(os);
os << glm::io::precision(2) << glm::io::width(1 + 2 + 1 + 2)
2015-10-29 17:56:08 +00:00
<< glm::io::order(otype)
<< "mat2x2<" << typeid(T).name() << ',' << P << ">: " << glm::mat<2, 2, T,P>(v2_1, v2_2) << '\n'
<< "mat2x3<" << typeid(T).name() << ',' << P << ">: " << glm::mat<2, 3, T,P>(v3_1, v3_2) << '\n'
<< "mat2x4<" << typeid(T).name() << ',' << P << ">: " << glm::mat<2, 4, T,P>(v4_1, v4_2) << '\n'
<< "mat3x2<" << typeid(T).name() << ',' << P << ">: " << glm::mat<3, 2, T,P>(v2_1, v2_2, v2_3) << '\n'
<< "mat3x3<" << typeid(T).name() << ',' << P << ">: " << glm::mat<3, 3, T,P>(v3_1, v3_2, v3_3) << '\n'
<< "mat3x4<" << typeid(T).name() << ',' << P << ">: " << glm::mat<3, 4, T,P>(v4_1, v4_2, v4_3) << '\n'
<< "mat4x2<" << typeid(T).name() << ',' << P << ">: " << glm::mat<4, 2, T,P>(v2_1, v2_2, v2_3, v2_4) << '\n'
<< "mat4x3<" << typeid(T).name() << ',' << P << ">: " << glm::mat<4, 3, T,P>(v3_1, v3_2, v3_3, v3_4) << '\n'
<< "mat4x4<" << typeid(T).name() << ',' << P << ">: " << glm::mat<4, 4, T,P>(v4_1, v4_2, v4_3, v4_4) << '\n';
2014-11-14 23:56:14 +00:00
os << glm::io::unformatted
2015-10-29 17:56:08 +00:00
<< glm::io::order(otype)
<< "mat2x2<" << typeid(T).name() << ',' << P << ">: " << glm::mat<2, 2, T,P>(v2_1, v2_2) << '\n'
<< "mat2x3<" << typeid(T).name() << ',' << P << ">: " << glm::mat<2, 3, T,P>(v3_1, v3_2) << '\n'
<< "mat2x4<" << typeid(T).name() << ',' << P << ">: " << glm::mat<2, 4, T,P>(v4_1, v4_2) << '\n'
<< "mat3x2<" << typeid(T).name() << ',' << P << ">: " << glm::mat<3, 2, T,P>(v2_1, v2_2, v2_3) << '\n'
<< "mat3x3<" << typeid(T).name() << ',' << P << ">: " << glm::mat<3, 3, T,P>(v3_1, v3_2, v3_3) << '\n'
<< "mat3x4<" << typeid(T).name() << ',' << P << ">: " << glm::mat<3, 4, T,P>(v4_1, v4_2, v4_3) << '\n'
<< "mat4x2<" << typeid(T).name() << ',' << P << ">: " << glm::mat<4, 2, T,P>(v2_1, v2_2, v2_3, v2_4) << '\n'
<< "mat4x3<" << typeid(T).name() << ',' << P << ">: " << glm::mat<4, 3, T,P>(v3_1, v3_2, v3_3, v3_4) << '\n'
<< "mat4x4<" << typeid(T).name() << ',' << P << ">: " << glm::mat<4, 4, T,P>(v4_1, v4_2, v4_3, v4_4) << '\n';
2013-11-22 23:48:58 +00:00
2014-11-14 23:56:14 +00:00
return 0;
2013-11-22 23:48:58 +00:00
}
int main()
{
int Error(0);
2013-12-16 22:59:20 +00:00
Error += test_io_quat<float, glm::highp>(std::cout);
Error += test_io_quat<float, glm::highp>(std::wcout);
Error += test_io_quat<int, glm::mediump>(std::cout);
Error += test_io_quat<int, glm::mediump>(std::wcout);
Error += test_io_quat<glm::uint, glm::lowp>(std::cout);
Error += test_io_quat<glm::uint, glm::lowp>(std::wcout);
2013-12-16 22:59:20 +00:00
Error += test_io_vec<float, glm::highp>(std::cout);
Error += test_io_vec<float, glm::highp>(std::wcout);
Error += test_io_vec<int, glm::mediump>(std::cout);
Error += test_io_vec<int, glm::mediump>(std::wcout);
Error += test_io_vec<glm::uint, glm::lowp>(std::cout);
Error += test_io_vec<glm::uint, glm::lowp>(std::wcout);
2015-10-29 17:56:08 +00:00
Error += test_io_mat<float, glm::highp>(std::cout, glm::io::column_major);
Error += test_io_mat<float, glm::lowp>(std::wcout, glm::io::column_major);
Error += test_io_mat<float, glm::highp>(std::cout, glm::io::row_major);
Error += test_io_mat<float, glm::lowp>(std::wcout, glm::io::row_major);
2013-12-16 22:59:20 +00:00
2013-11-22 23:48:58 +00:00
return Error;
}
2018-07-11 22:36:59 +00:00
#else
int main()
{
return 0;
}
2018-07-12 19:18:44 +00:00
#endif// GLM_LANG & GLM_LANG_CXXMS_FLAG