C++ 如何打印通过引用传递的 glm::vec3 类型的向量值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11515469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:12:45  来源:igfitidea点击:

How do I print vector values of type glm::vec3 that have been passed by reference?

c++glm-math

提问by iKlsR

I have a small obj loader and it takes two parameters and passes them back to the input variables.. however this is my first time doing this and i'm not sure how to print said values now. Here is my main function to test if the loader is working. I have two vectors of type glm::vec3to hold the vertex and normal data.

我有一个小的 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;   
}

The line commented above is what is generating useless info. If I leave it like that and run the program I get a bunch of errors spewed at me (too unformatted and long to paste here) and if I add the reference operator I get output like this:

上面注释的行是生成无用信息的内容。如果我像这样离开并运行程序,我会收到一堆错误(太未格式化且太长而无法粘贴在这里),如果我添加引用运算符,我会得到如下输出:

0x711ea0
0x711eac
0x711eb8
0x711ec4    // etc

Any idea what I am doing wrong?

知道我做错了什么吗?

回答by user2103388

glm has an extension for this. Add #include "glm/ext.hpp"or "glm/gtx/string_cast.hpp"

glm 对此有一个扩展。添加#include "glm/ext.hpp""glm/gtx/string_cast.hpp"

Then to print a vector for example:

然后打印一个向量,例如:

glm::vec4 test;
std::cout<<glm::to_string(test)<<std::endl;

回答by JAB

I think the most elegant solution might be a combination of the two answers already posted, with the addition of templating so you don't have to reimplement the operator for all vector/matrix types (this restricts the function definition to header files, though).

我认为最优雅的解决方案可能是已经发布的两个答案的组合,并添加了模板,因此您不必为所有向量/矩阵类型重新实现运算符(尽管这将函数定义限制为头文件) .

#include <glm/gtx/string_cast.hpp>

template<typename genType>
std::ostream& operator<<(std::ostream& out, const genType& g)
{
    return out << glm::to_string(g);
}

回答by chris

glm::vec3doesn't overload operator<<so you can't print the vector itself. What you can do, though, is print the members of the vector:

glm::vec3不会超载,operator<<因此您无法打印矢量本身。但是,您可以做的是打印向量的成员:

std::cout << "{" 
          << vertices[i].x << " " << vertices[i].y << " " << vertices[i].z 
          << "}";

Even better, if you use that a lot, you can overload operator<<yourself:

更好的是,如果你经常使用它,你可以operator<<让自己超负荷:

std::ostream &operator<< (std::ostream &out, const glm::vec3 &vec) {
    out << "{" 
        << vec.x << " " << vec.y << " "<< vec.z 
        << "}";

    return out;
}

Then to print, just use:

然后打印,只需使用:

std::cout << vertices[i];

回答by Matthew Gingell

To get the overload resolution right, you can do something like:

要获得正确的重载解析,您可以执行以下操作:

// 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);
}