Unravel Engine C++ Reference
Loading...
Searching...
No Matches
lathe_mesh.hpp
Go to the documentation of this file.
1#ifndef GENERATOR_LATHEMESH_HPP
2#define GENERATOR_LATHEMESH_HPP
3
4#include <memory>
5#include <vector>
6
7#include "axis.hpp"
8#include "mesh_vertex.hpp"
9#include "shape_vertex.hpp"
10#include "triangle.hpp"
11
12namespace generator
13{
14
18template<typename shape_t>
20{
21public:
23 {
24 public:
26 {
27 triangle_t triangle;
28
29 const auto& shapeEdge = shape_edges_.generate();
30
31 int slice = i_ / 2;
32
33 const int delta = mesh_->slices_ + 1;
34
35 if(i_ % 2 == 0)
36 {
37 triangle.vertices[0] = shapeEdge.vertices[0] * delta + slice;
38 triangle.vertices[1] = shapeEdge.vertices[1] * delta + slice;
39 triangle.vertices[2] = shapeEdge.vertices[1] * delta + slice + 1;
40 }
41 else
42 {
43 triangle.vertices[0] = shapeEdge.vertices[0] * delta + slice;
44 triangle.vertices[1] = shapeEdge.vertices[1] * delta + slice + 1;
45 triangle.vertices[2] = shapeEdge.vertices[0] * delta + slice + 1;
46 }
47
48 return triangle;
49 }
50
51 bool done() const noexcept
52 {
53 return shape_edges_.done();
54 }
55
56 void next()
57 {
58 ++i_;
59 if(i_ == 2 * mesh_->slices_)
60 {
61 i_ = 0;
62 shape_edges_.next();
63 }
64 }
65
66 private:
67 const lathe_mesh_t* mesh_;
68
69 typename shape_t::edges_t shape_edges_;
70
71 int i_;
72
73 explicit triangles_t(const lathe_mesh_t& mesh) : mesh_{&mesh}, shape_edges_{mesh.shape_.edges()}, i_{0}
74 {
75 }
76
77 friend class lathe_mesh_t;
78 };
79
81 {
82 public:
84 {
85 mesh_vertex_t vertex;
86
87 const auto shapeVertex = shape_vertices_.generate();
88 const gml::dvec2 normal = shapeVertex.normal();
89
90 double deltaAngle = mesh_->sweep_ / mesh_->slices_;
91 double angle = i_ * deltaAngle + mesh_->start_;
92
93 const gml::dquat q = gml::qrotate(angle, mesh_->axis_);
94
95 vertex.position = gml::transform(q, gml::dvec3{shapeVertex.position[0], shapeVertex.position[1], 0.0});
96
97 vertex.normal = gml::transform(q, gml::dvec3{normal[0], normal[1], 0.0});
98
99 vertex.tex_coord[0] = shapeVertex.tex_coord;
100 vertex.tex_coord[1] = angle / mesh_->sweep_;
101
102 return vertex;
103 }
104
105 bool done() const noexcept
106 {
107 return shape_vertices_.done();
108 }
109
110 void next()
111 {
112 ++i_;
113
114 if(i_ == mesh_->slices_ + 1)
115 {
116 i_ = 0;
117 shape_vertices_.next();
118 }
119 }
120
121 private:
122 const lathe_mesh_t* mesh_;
123
124 typename shape_t::vertices_t shape_vertices_;
125
126 int i_;
127
128 explicit vertices_t(const lathe_mesh_t& mesh) : mesh_{&mesh}, shape_vertices_{mesh.shape_.vertices()}, i_{0}
129 {
130 }
131
132 friend class lathe_mesh_t;
133 };
134
141 lathe_mesh_t(shape_t shape,
142 const gml::dvec2& axis,
143 int slices = 32,
144 double start = 0.0,
145 double sweep = gml::radians(360.0))
146 : axis_{axis, 0.0}
147 , shape_{std::move(shape)}
148 , slices_{slices}
149 , start_{start}
150 , sweep_{sweep}
151 {
152 }
153
154 triangles_t triangles() const noexcept
155 {
156 return triangles_t{*this};
157 }
158
159 vertices_t vertices() const noexcept
160 {
161 return vertices_t{*this};
162 }
163
164private:
165 gml::dvec3 axis_;
166
167 shape_t shape_;
168
169 int slices_;
170
171 double start_;
172
173 double sweep_;
174};
175
176template<typename shape_t>
178 axis_t axis = axis_t::X,
179 int slices = 32,
180 double start = 0.0,
181 double sweep = gml::radians(360.0))
182{
183 return lathe_mesh_t<shape_t>{std::move(shape), axis, slices, start, sweep};
184}
185} // namespace generator
186
187#endif
btVector3 normal
lathe_mesh_t(shape_t shape, const gml::dvec2 &axis, int slices=32, double start=0.0, double sweep=gml::radians(360.0))
vertices_t vertices() const noexcept
triangles_t triangles() const noexcept
gml::dvec2 tex_coord
UV texture coordinates.
gml::dvec3 normal
Unit vector perpendicular to the surface.
gml::ivec3 vertices
Zero based indices of the triangle vertices in counterclockwise order.
Definition triangle.hpp:13
lathe_mesh_t< shape_t > lathe(shape_t shape, axis_t axis=axis_t::X, int slices=32, double start=0.0, double sweep=gml::radians(360.0))
glm::tvec3< T > transform(const glm::tquat< T > &q, const glm::tvec3< T > &v)
Definition math.hpp:114
glm::tquat< T > qrotate(const T &angle, const glm::tvec3< T > &axis)
Definition math.hpp:121