如何打印通过引用传递的glm::vec3类型的矢量值
How do I print vector values of type glm::vec3 that have been passed by reference?
我有一个小的obj加载程序,它接受两个参数并将它们传递回输入变量。。然而,这是我第一次这样做,我不知道现在如何打印所述值。这是我测试加载程序是否工作的主要功能。我有两个类型为glm::vec3
的向量来保存顶点和法线数据。
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
int main() {
bool test = loadOBJ("cube.obj", vertices, normals);
for (int i = 0; i < vertices.size(); i++) {
std::cout << vertices[i] << std::endl; // problem line
}
return 0;
}
上面评论的一行就是产生无用信息的原因。如果我这样处理并运行程序,我会收到一堆错误(太未格式化且太长,无法粘贴到此处),如果我添加引用运算符,我会得到这样的输出:
0x711ea0
0x711eac
0x711eb8
0x711ec4 // etc
知道我做错了什么吗?
glm对此有一个扩展。添加#include "glm/ext.hpp"
或"glm/gtx/string_cast.hpp"
然后打印矢量,例如:
glm::vec4 test;
std::cout<<glm::to_string(test)<<std::endl;
我认为最优雅的解决方案可能是将已经发布的两个答案结合起来,并添加模板,这样您就不必为所有向量/矩阵类型重新实现运算符(不过,这将函数定义限制为头文件)。
#include <glm/gtx/string_cast.hpp>
template<typename genType>
std::ostream& operator<<(std::ostream& out, const genType& g)
{
return out << glm::to_string(g);
}
glm::vec3
不会重载operator<<
,因此无法打印矢量本身。不过,你可以做的是打印矢量的成员:
std::cout << "{"
<< vertices[i].x << " " << vertices[i].y << " " << vertices[i].z
<< "}";
更好的是,如果你经常使用它,你可以自己过载operator<<
:
std::ostream &operator<< (std::ostream &out, const glm::vec3 &vec) {
out << "{"
<< vec.x << " " << vec.y << " "<< vec.z
<< "}";
return out;
}
然后打印,只需使用:
std::cout << vertices[i];
GLM在<glm/gtx/io.hpp>
中具有operator<<()
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtx/io.hpp>
int main()
{
glm::vec3 v(1.0f, 2.0f, 3.0f);
std::cout << v << std::endl;
}
输出为:
[ 1.000, 2.000, 3.000]
要获得正确的过载分辨率,可以执行以下操作:
// Writes a generic GLM vector type to a stream.
template <int D, typename T, glm::qualifier P>
std::ostream &operator<<(std::ostream &os, glm::vec<D, T, P> v) {
return os << glm::to_string(v);
}
详细阐述JAB的答案,我使用以下内容来避免'operator <<' is ambiguous
错误:
#include <glm/glm.hpp>
#include <glm/gtx/string_cast.hpp>
/**
* can_cout<T> tells whether there already exists an override of operator<< that
* supports T.
*/
template <typename, typename = void>
struct can_cout : public std::false_type {};
template <typename T>
struct can_cout<T, std::void_t<decltype(operator<<(std::declval<std::ostream>(), std::declval<T>()))> > : public std::true_type {};
/**
* Implement operator<< iff GlmType is handled by glm::to_string but there is
* not already an override of operator<< that hanldes this type.
*/
template<
typename GlmType,
typename = std::enable_if_t<!can_cout<GlmType>::value>,
typename = decltype(glm::to_string(std::declval<GlmType>()))
>
std::ostream& operator<<(std::ostream& out, const GlmType& g)
{
return out << glm::to_string(g);
}
相关文章:
- 带有"up-"和"center-vec3"参数的glm::rotate()的有效替代方案?
- 'Incomplete type'在VS中使用glm::vec3和ReSharper时的警告
- 如何为数据类型 vector<glm::vec3> (opengl 数据类型) 编写 std::minmax 函数的自定义比较器
- 如何将GLM :: VEC3(1.f,0.f,0.f)设置为枚举值
- 检查是否已设置 glm::vec3
- 如何使用boost ::几何:: rtree with glm :: vec3作为自定义点类型
- 如何在C++中填写 glm::vec3
- 绘制一个向量<glm::vec3> opengl c++
- glm::vec3 和 glm::mat4 是如何初始化的?
- 将glm::vec3与boost多精度浮点相乘
- 使用glm将vec3与mat4相乘
- 如何打印通过引用传递的glm::vec3类型的矢量值
- OpenGL using std::vector with glm::vec3
- 在 Angelscript 中为 glm::vec3 的基于 vic3 的值类型注册赋值运算符
- 使用boost::spirit::qi(error_invalid_expression)解析glm::vec3的3个浮
- 什么是glm的vec1,vec2,vec3,我应该如何/何时使用它们?
- 当我将glm::vec3*转换为double*时会发生什么?
- 使用glm创建基于三个vec3的模型矩阵
- 由于某种原因,我的C++ glm::vec3 数组似乎重置为空
- 将一个glm::vec3常量引用赋值给另一个glm::vec3时奇怪的崩溃