OGLplus (0.52.0) a C++ wrapper for OpenGL

torus.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHAPES_TORUS_1107121519_HPP
14 #define OGLPLUS_SHAPES_TORUS_1107121519_HPP
15 
16 #include <oglplus/shapes/draw.hpp>
17 #include <oglplus/face_mode.hpp>
18 
20 
22 #include <oglplus/math/sphere.hpp>
23 
24 namespace oglplus {
25 namespace shapes {
26 
28 class Torus
29  : public DrawingInstructionWriter
30  , public DrawMode
31 {
32 private:
33  GLdouble _radius_out, _radius_in;
34  unsigned _sections, _rings;
35 public:
37  Torus(void)
38  : _radius_out(1.0)
39  , _radius_in(0.5)
40  , _sections(36)
41  , _rings(24)
42  { }
43 
45  Torus(GLdouble rad_out, GLdouble rad_in, unsigned sects, unsigned rings)
46  : _radius_out(rad_out)
47  , _radius_in(rad_in)
48  , _sections(sects)
49  , _rings(rings)
50  { }
51 
54  {
55  return FaceOrientation::CCW;
56  }
57 
59  template <typename T>
60  GLuint Positions(std::vector<T>& dest) const
61  {
62  dest.resize((_rings + 1) * (_sections + 1) * 3);
63  unsigned k = 0;
64  //
65  GLdouble r_step = (math::TwoPi()) / GLdouble(_rings);
66  GLdouble s_step = (math::TwoPi()) / GLdouble(_sections);
67  GLdouble r1 = _radius_in;
68  GLdouble r2 = _radius_out - _radius_in;
69 
70  for(unsigned r=0; r!=(_rings+1); ++r)
71  {
72  GLdouble vx = std::cos(r*r_step);
73  GLdouble vz = -std::sin(r*r_step);
74  for(unsigned s=0; s!=(_sections+1); ++s)
75  {
76  GLdouble vr = std::cos(s*s_step);
77  GLdouble vy = std::sin(s*s_step);
78  dest[k++] = T(vx*(r1 + r2 * (1.0 + vr)));
79  dest[k++] = T(vy*r2);
80  dest[k++] = T(vz*(r1 + r2 * (1.0 + vr)));
81  }
82  }
83  assert(k == dest.size());
84  return 3;
85  }
86 
88  template <typename T>
89  GLuint Normals(std::vector<T>& dest) const
90  {
91  dest.resize((_rings + 1) * (_sections + 1) * 3);
92  unsigned k = 0;
93  //
94  GLdouble r_step = (math::TwoPi()) / GLdouble(_rings);
95  GLdouble s_step = (math::TwoPi()) / GLdouble(_sections);
96 
97  for(unsigned r=0; r!=(_rings+1); ++r)
98  {
99  GLdouble nx = std::cos(r*r_step);
100  GLdouble nz = -std::sin(r*r_step);
101  for(unsigned s=0; s!=(_sections+1); ++s)
102  {
103  GLdouble nr = std::cos(s*s_step);
104  GLdouble ny = std::sin(s*s_step);
105  dest[k++] = T(nx*nr);
106  dest[k++] = T(ny);
107  dest[k++] = T(nz*nr);
108  }
109  }
110  assert(k == dest.size());
111  return 3;
112  }
113 
115  template <typename T>
116  GLuint Tangents(std::vector<T>& dest) const
117  {
118  dest.resize((_rings + 1) * (_sections + 1) * 3);
119  unsigned k = 0;
120  //
121  GLdouble r_step = (math::TwoPi()) / GLdouble(_rings);
122 
123  for(unsigned r=0; r!=(_rings+1); ++r)
124  {
125  GLdouble tx = -std::sin(r*r_step);
126  GLdouble tz = -std::cos(r*r_step);
127  for(unsigned s=0; s!=(_sections+1); ++s)
128  {
129  dest[k++] = T(tx);
130  dest[k++] = T(0);
131  dest[k++] = T(tz);
132  }
133  }
134  assert(k == dest.size());
135  return 3;
136  }
137 
139  template <typename T>
140  GLuint Bitangents(std::vector<T>& dest) const
141  {
142  dest.resize((_rings + 1) * (_sections + 1) * 3);
143  unsigned k = 0;
144  //
145  GLdouble r_step = (math::TwoPi()) / GLdouble(_rings);
146  GLdouble s_step = (math::TwoPi()) / GLdouble(_sections);
147 
148  GLdouble ty = 0.0;
149  for(unsigned r=0; r!=(_rings+1); ++r)
150  {
151  GLdouble tx = -std::sin(r*r_step);
152  GLdouble tz = -std::cos(r*r_step);
153 
154  for(unsigned s=0; s!=(_sections+1); ++s)
155  {
156  GLdouble ny = std::sin(s*s_step);
157  GLdouble nr = std::cos(s*s_step);
158  GLdouble nx = -tz*nr;
159  GLdouble nz = tx*nr;
160 
161  dest[k++] = T(ny*tz-nz*ty);
162  dest[k++] = T(nz*tx-nx*tz);
163  dest[k++] = T(nx*ty-ny*tx);
164  }
165  }
166  assert(k == dest.size());
167  return 3;
168  }
169 
171  template <typename T>
172  GLuint TexCoordinates(std::vector<T>& dest) const
173  {
174  dest.resize((_rings + 1) * (_sections + 1) * 2);
175  unsigned k = 0;
176  //
177  GLdouble r_step = 1.0 / GLdouble(_rings);
178  GLdouble s_step = 1.0 / GLdouble(_sections);
179 
180  for(unsigned r=0; r!=(_rings+1); ++r)
181  {
182  GLdouble u = r*r_step;
183  for(unsigned s=0; s!=(_sections+1); ++s)
184  {
185  GLdouble v = s*s_step;
186  dest[k++] = T(u);
187  dest[k++] = T(v);
188  }
189  }
190  assert(k == dest.size());
191  return 2;
192  }
193 
194 #if OGLPLUS_DOCUMENTATION_ONLY
195 
204  typedef VertexAttribsInfo<Torus> VertexAttribs;
205 #else
206  typedef VertexAttribsInfo<
207  Torus,
208  std::tuple<
209  VertexPositionsTag,
210  VertexNormalsTag,
211  VertexTangentsTag,
212  VertexBitangentsTag,
213  VertexTexCoordinatesTag
214  >
215  > VertexAttribs;
216 #endif
217 
219  template <typename T>
220  void BoundingSphere(oglplus::Sphere<T>& bounding_sphere) const
221  {
222  bounding_sphere = oglplus::Sphere<T>(
223  T(0),
224  T(0),
225  T(0),
226  T(_radius_out)
227  );
228  }
229 
231  typedef std::vector<GLushort> IndexArray;
232 
234  IndexArray Indices(Default = Default()) const;
235 
237  IndexArray Indices(Quads) const;
238 
240  IndexArray Indices(WithAdjacency) const;
241 
243  DrawingInstructions Instructions(Default = Default()) const;
244 
247 
249  DrawingInstructions Instructions(WithAdjacency) const;
250 };
251 
252 } // shapes
253 } // oglplus
254 
255 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
256 #include <oglplus/shapes/torus.ipp>
257 #endif // OGLPLUS_LINK_LIBRARY
258 
259 #endif // include guard
Implementation of shape draw instructions.
GLuint Normals(std::vector< T > &dest) const
Makes vertex normals and returns number of values per vertex.
Definition: torus.hpp:89
Sphere utility class.
void BoundingSphere(oglplus::Sphere< T > &bounding_sphere) const
Queries the bounding sphere coordinates and dimensions.
Definition: torus.hpp:220
GLuint TexCoordinates(std::vector< T > &dest) const
Makes texture coordinates and returns number of values per vertex.
Definition: torus.hpp:172
DrawingInstructions Instructions(Default=Default()) const
Returns the instructions for rendering.
GLuint Positions(std::vector< T > &dest) const
Makes vertex coordinates and returns number of values per vertex.
Definition: torus.hpp:60
FaceOrientation
Face orientation enumeration.
Definition: face_mode.hpp:62
OpenGL face type-related enumeration.
Class providing vertex attributes and instructions for rendering of a Torus.
Definition: torus.hpp:28
FaceOrientation FaceWinding(void) const
Returns the winding direction of faces.
Definition: torus.hpp:53
VertexAttribsInfo< Torus > VertexAttribs
Vertex attribute information for this shape builder.
Definition: torus.hpp:204
IndexArray Indices(Default=Default()) const
Returns element indices that are used with the drawing instructions.
GLuint Bitangents(std::vector< T > &dest) const
Makes vertex bi-tangents and returns number of values per vertex.
Definition: torus.hpp:140
Classes providing additional information about the shape builders.
Torus(GLdouble rad_out, GLdouble rad_in, unsigned sects, unsigned rings)
Creates a torus with unit radius centered at the origin.
Definition: torus.hpp:45
Math constants.
Torus(void)
Creates a torus with unit radius centered at the origin.
Definition: torus.hpp:37
Class encapsulating the instructions for drawing of a shape.
Definition: draw.hpp:219
Class implementing sphere-related functionality.
Definition: sphere.hpp:29
CCW: Counter-clockwise.
std::vector< GLushort > IndexArray
The type of index container returned by Indices()
Definition: torus.hpp:231
GLuint Tangents(std::vector< T > &dest) const
Makes vertex tangents and returns number of values per vertex.
Definition: torus.hpp:116

Copyright © 2010-2014 Matúš Chochlík, University of Žilina, Žilina, Slovakia.
<matus.chochlik -at- fri.uniza.sk>
<chochlik -at -gmail.com>
Documentation generated on Mon Sep 22 2014 by Doxygen (version 1.8.6).