Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

glm-math

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::vec3 to hold the vertex and normal data.

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?

like image 760
iKlsR Avatar asked Jul 17 '12 03:07

iKlsR


People also ask

How to overload a vector in GLM VC3?

glm::vec3 doesn't overload operator<< so you can't print the vector itself. What you can do, though, is print the members of the vector: std::cout << " {" << vertices [i].x << " " << vertices [i].y << " " << vertices [i].z << "}"; Even better, if you use that a lot, you can overload operator<< yourself:

How to print all elements of a vector without for loop?

Below is the C++ program to implement the above concept: Printing all elements without for loop by providing element type: All the elements of a vector can be printed using an STL algorithm copy (). All the elements of a vector can be copied to the output stream by providing elements type while calling the copy () algorithm.

How to copy all the elements of a vector in C++?

All the elements of a vector can be copied to the output stream by providing elements type while calling the copy () algorithm. Below is the C++ program to implement the above approach:

How to print the contents of a vector in C++?

Printing in a comma-separated manner: By avoiding overloading of the << operator and by creating a separate function, a custom separator can be provided to print the contents of the vector Below is the C++ program to implement the above approach:


1 Answers

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

Then to print a vector for example:

glm::vec4 test; std::cout<<glm::to_string(test)<<std::endl; 
like image 136
user2103388 Avatar answered Sep 21 '22 01:09

user2103388