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?
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:
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.
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:
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:
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With