Unravel Engine C++ Reference
Loading...
Searching...
No Matches
obj_writer.hpp
Go to the documentation of this file.
1#ifndef GENERATOR_OBJWRITER_HPP
2#define GENERATOR_OBJWRITER_HPP
3
4#include <sstream>
5
6#include "mesh_vertex.hpp"
7#include "triangle.hpp"
8
9namespace generator
10{
11
14{
15public:
17
18 template<typename Mesh>
19 void write_mesh(const Mesh& mesh)
20 {
21 int newBase = base_;
22
23 for(const mesh_vertex_t& vertex : mesh.vertices())
24 {
25 ++newBase;
26
27 ss_ << "v " << vertex.position[0] << " " << vertex.position[1] << " " << vertex.position[2] << "\n";
28
29 ss_ << "vn " << vertex.normal[0] << " " << vertex.normal[1] << " " << vertex.normal[2] << "\n";
30
31 ss_ << "vt " << vertex.tex_coord[0] << " " << vertex.tex_coord[1] << "\n";
32 }
33
34 for(const triangle_t& triangle : mesh.triangles())
35 {
36 auto t = triangle.vertices + base_;
37 ss_ << "f " << t[0] << "/" << t[0] << "/" << t[0] << " " << t[1] << "/" << t[1] << "/" << t[1] << " "
38 << t[2] << "/" << t[2] << "/" << t[2] << "\n";
39 }
40
41 base_ = newBase;
42 }
43
44 std::string str()
45 {
46 return ss_.str();
47 }
48
49private:
50 int base_;
51
52 std::stringstream ss_;
53};
54} // namespace generator
55
56#endif
A class for generating obj files for preview and debug purposes.
void write_mesh(const Mesh &mesh)