13 #ifndef OGLPLUS_SHAPES_REVOLVE_1107121519_HPP
14 #define OGLPLUS_SHAPES_REVOLVE_1107121519_HPP
31 template <
typename Type>
33 :
public DrawingInstructionWriter
37 const std::vector<Type> _sections, _section_factors;
38 const unsigned _rings;
40 const std::vector<Vector<Type, 3>> _positions_0, _positions_1;
41 const std::vector<Vector<Type, 3>> _normals_0, _normals_1;
42 const std::vector<Vector<Type, 3>> _tex_coords_0, _tex_coords_1;
52 if(factor < Type(0)) factor = Type(0);
53 if(factor > Type(1)) factor = Type(1);
54 return a * (Type(1) - factor) + b * factor;
62 _section_factors[section]
71 _section_factors[section]
80 _section_factors[section]
84 static std::vector<Type> _make_default_sections(
unsigned sections)
86 std::vector<Type> result(sections + 1);
87 const Type s_step = Type(1) / Type(sections);
89 for(
auto i=result.begin(), e=result.end(); i!=e; ++i, s+=s_step)
94 static std::vector<Vector<Type, 3>> _calculate_normals(
101 assert(pos.size() == nml.size());
104 std::vector<Vector<Type, 3>> result(pos.size());
106 const unsigned n = result.size()-1;
107 const Vec3f tgnt(0.0, 0.0, -1.0);
109 result[0] = Normalized(Cross(tgnt, pos[1] - pos[0]));
110 for(
unsigned i=1; i!=n; ++i)
111 result[i] = Normalized(Cross(tgnt, pos[i+1]-pos[i-1]));
112 result[n] = Normalized(Cross(tgnt, pos[n] - pos[n-1]));
119 assert(_sections.size() > 2);
120 assert(_sections.size() == _section_factors.size());
122 assert(_positions_0.size() == _rings);
123 assert(_positions_1.size() == _rings);
124 assert(_normals_0.size() == _rings);
125 assert(_normals_1.size() == _rings);
126 assert(_tex_coords_0.size() == _rings);
127 assert(_tex_coords_1.size() == _rings);
130 void _calc_radius(
void)
134 for(
unsigned i=0; i!=_rings; ++i)
136 l = Length(_positions_0[i]);
137 if(_radius < l) _radius = l;
138 l = Length(_positions_1[i]);
139 if(_radius < l) _radius = l;
143 using DrawMode::Default;
151 ): _sections(_make_default_sections(sections))
152 , _section_factors(_sections.size(), Type(0))
153 , _rings(positions.size())
154 , _positions_0(positions)
155 , _positions_1(_positions_0)
156 , _normals_0(_calculate_normals(_positions_0, normals))
157 , _normals_1(_normals_0)
158 , _tex_coords_0(tex_coords)
159 , _tex_coords_1(_tex_coords_0)
167 const std::vector<Type>& section_factors,
174 ): _sections(_make_default_sections(section_factors.size()-1))
175 , _section_factors(section_factors)
176 , _rings(positions_0.size())
177 , _positions_0(positions_0)
178 , _positions_1(positions_1)
179 , _normals_0(_calculate_normals(_positions_0, normals_0))
180 , _normals_1(_calculate_normals(_positions_1, normals_1))
181 , _tex_coords_0(tex_coords_0)
182 , _tex_coords_1(tex_coords_1)
195 template <
typename T>
198 dest.resize(_rings * _sections.size() * 3);
201 for(
unsigned si=0, sn=_sections.size(); si!=sn; ++si)
206 for(
unsigned r=0; r!=_rings; ++r)
211 dest[k++] =
T(out.x());
212 dest[k++] =
T(out.
y());
213 dest[k++] =
T(out.
z());
216 assert(k == dest.size());
221 template <
typename T>
224 dest.resize(_rings * _sections.size() * 3);
227 for(
unsigned si=0, sn=_sections.size(); si!=sn; ++si)
232 for(
unsigned r=0; r!=_rings; ++r)
237 dest[k++] =
T(out.x());
238 dest[k++] =
T(out.
y());
239 dest[k++] =
T(out.
z());
242 assert(k == dest.size());
247 template <
typename T>
250 dest.resize(_rings * _sections.size() * 3);
255 for(
unsigned si=0, sn=_sections.size(); si!=sn; ++si)
259 const auto out = mat * in;
261 for(
unsigned r=0; r!=_rings; ++r)
263 dest[k++] =
T(out.x());
264 dest[k++] =
T(out.y());
265 dest[k++] =
T(out.z());
268 assert(k == dest.size());
273 template <
typename T>
276 dest.resize(_rings * _sections.size() * 3);
281 for(
unsigned si=0, sn=_sections.size(); si!=sn; ++si)
286 for(
unsigned r=0; r!=_rings; ++r)
290 const auto out = mat * in;
292 dest[k++] =
T(out.x());
293 dest[k++] =
T(out.y());
294 dest[k++] =
T(out.z());
297 assert(k == dest.size());
302 template <
typename T>
305 dest.resize(_rings * _sections.size() * 3);
310 for(
unsigned si=0, sn=_sections.size(); si!=sn; ++si)
312 const T u_mult = _sections[si];
314 for(
unsigned r=0; r!=_rings; ++r)
316 auto tc = _get_tex_coord(r, si);
317 dest[k++] =
T(tc.x()*u_mult);
318 dest[k++] =
T(tc.y());
319 dest[k++] =
T(tc.z());
322 assert(k == dest.size());
326 #if OGLPLUS_DOCUMENTATION_ONLY
338 typedef VertexAttribsInfo<
345 VertexTexCoordinatesTag
351 template <
typename T>
369 const unsigned sn = _sections.size() - 1;
370 const unsigned n = sn * (2 * _rings + 1);
376 for(
unsigned s=0; s!=sn; ++s)
378 for(
unsigned r=0; r!=_rings; ++r)
380 indices[k++] = offs + r + _rings;
381 indices[k++] = offs + r;
387 assert(k == indices.size());
396 auto instructions = this->MakeInstructions();
397 const unsigned sn = _sections.size() - 1;
398 const unsigned n = sn * (2 * _rings + 1);
401 operation.
method = DrawOperation::Method::DrawElements;
403 operation.first = GLuint(0);
404 operation.count = GLuint(n);
405 operation.restart_index = GLuint(n);
408 this->AddInstruction(instructions, operation);
417 #endif // include guard
Implementation of shape draw instructions.
IndexArray Indices(Default=Default()) const
Returns element indices that are used with the drawing instructions.
Definition: revolve.hpp:367
GLuint TexCoordinates(std::vector< T > &dest) const
Makes texture coordinates and returns number of values per vertex.
Definition: revolve.hpp:303
GLuint Bitangents(std::vector< T > &dest) const
Makes vertex bi-tangents and returns number of values per vertex.
Definition: revolve.hpp:274
RevolveY(const std::vector< Type > §ion_factors, const std::vector< Vector< Type, 3 >> &positions_0, const std::vector< Vector< Type, 3 >> &positions_1, const std::vector< Vector< Type, 3 >> &normals_0, const std::vector< Vector< Type, 3 >> &normals_1, const std::vector< Vector< Type, 3 >> &tex_coords_0, const std::vector< Vector< Type, 3 >> &tex_coords_1)
Creates a shape by revolving curve approximation around the y-axis.
Definition: revolve.hpp:166
GLuint Normals(std::vector< T > &dest) const
Makes vertex normals and returns number of values per vertex.
Definition: revolve.hpp:222
std::vector< GLuint > IndexArray
The type of index container returned by Indices()
Definition: revolve.hpp:364
FaceOrientation
Face orientation enumeration.
Definition: face_mode.hpp:62
OpenGL face type-related enumeration.
T z(void) const
Returns the 2-nd component.
Definition: vector.hpp:237
VertexAttribsInfo< RevolveY > VertexAttribs
Vertex attribute information for this shape builder.
Definition: revolve.hpp:336
GLuint Positions(std::vector< T > &dest) const
Makes vertex coordinates and returns number of values per vertex.
Definition: revolve.hpp:196
DrawingInstructions Instructions(Default=Default()) const
Returns the instructions for rendering.
Definition: revolve.hpp:394
T y(void) const
Returns the 1-st component.
Definition: vector.hpp:231
FaceOrientation FaceWinding(void) const
Returns the winding direction of faces.
Definition: revolve.hpp:189
void BoundingSphere(oglplus::Sphere< T > &bounding_sphere) const
Queries the bounding sphere coordinates and dimensions.
Definition: revolve.hpp:352
Class providing attributes and instructions for rendering of a revolved shape.
Definition: revolve.hpp:32
GLuint Tangents(std::vector< T > &dest) const
Makes vertex tangents and returns number of values per vertex.
Definition: revolve.hpp:248
Structure containing information about how to draw a part of a shape.
Definition: draw.hpp:100
Method method
The method to be used to draw.
Definition: draw.hpp:106
Classes providing additional information about the shape builders.
Class encapsulating the instructions for drawing of a shape.
Definition: draw.hpp:219
Class implementing sphere-related functionality.
Definition: sphere.hpp:29
static ModelMatrix RotationY(Angle< T > angle)
Constructs a Y-axis rotation matrix.
Definition: matrix.hpp:925
Basic template for vector types.
Definition: fwd.hpp:43
RevolveY(unsigned sections, const std::vector< Vector< Type, 3 >> &positions, const std::vector< Vector< Type, 3 >> &normals, const std::vector< Vector< Type, 3 >> &tex_coords)
Creates a shape by revolving curve approximation around the y-axis.
Definition: revolve.hpp:146
Angle< AngleValueType > FullCircles(AngleValueType value)
Creates a new angle from a value in "full circles" (i.e. 360 degrees)
Definition: angle.hpp:480