OGLplus (0.52.0) a C++ wrapper for OpenGL

sphere.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHAPES_SPHERE_1107121519_HPP
14 #define OGLPLUS_SHAPES_SPHERE_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 Sphere
29  : public DrawingInstructionWriter
30  , public DrawMode
31 {
32 private:
33  double _radius;
34  unsigned _sections, _rings;
35 public:
37  Sphere(void)
38  : _radius(1.0)
39  , _sections(18)
40  , _rings(12)
41  { }
42 
44 
47  Sphere(double radius, unsigned sections, unsigned rings)
48  : _radius(radius)
49  , _sections(sections)
50  , _rings(rings)
51  {
52  assert(_radius > 0);
53  assert(_sections > 0);
54  assert(_rings > 0);
55  }
56 
58  double Radius(void) const
59  {
60  return _radius;
61  }
62 
64 
67  void Radius(double radius)
68  {
69  _radius = radius;
70  assert(_radius > 0);
71  }
72 
74  unsigned Sections(void) const
75  {
76  return _sections;
77  }
78 
80 
83  void Sections(unsigned sections)
84  {
85  _sections = sections;
86  assert(_sections > 0);
87  }
88 
90  unsigned Rings(void) const
91  {
92  return _rings;
93  }
94 
96 
99  void Rings(unsigned rings)
100  {
101  _rings = rings;
102  assert(_rings > 0);
103  }
104 
107  {
108  return FaceOrientation::CCW;
109  }
110 
112  template <typename T>
113  GLuint Normals(std::vector<T>& dest) const
114  {
115  dest.resize(((_rings + 2) * (_sections + 1)) * 3);
116  unsigned k = 0;
117  //
118  double r_step = (1.0 * math::Pi()) / double(_rings + 1);
119  double s_step = (2.0 * math::Pi()) / double(_sections);
120 
121  for(unsigned r=0; r!=(_rings+2);++r)
122  {
123  double r_lat = std::cos(r*r_step);
124  double r_rad = std::sin(r*r_step);
125  // the sections
126  for(unsigned s=0; s!=(_sections+1);++s)
127  {
128  dest[k++] = T(r_rad * std::cos(s*s_step));
129  dest[k++] = T(r_lat);
130  dest[k++] = T(r_rad * -std::sin(s*s_step));
131  }
132  }
133  //
134  assert(k == dest.size());
135  // 3 values per vertex
136  return 3;
137  }
138 
140  template <typename T>
141  GLuint Tangents(std::vector<T>& dest) const
142  {
143  dest.resize(((_rings + 2) * (_sections + 1)) * 3);
144  unsigned k = 0;
145  //
146  double s_step = (2.0 * math::Pi()) / double(_sections);
147 
148  for(unsigned r=0; r!=(_rings+2);++r)
149  {
150  for(unsigned s=0; s!=(_sections+1);++s)
151  {
152  dest[k++] = T(-std::sin(s*s_step));
153  dest[k++] = T(0);
154  dest[k++] = T(-std::cos(s*s_step));
155  }
156  }
157  //
158  assert(k == dest.size());
159  // 3 values per vertex
160  return 3;
161  }
162 
164  template <typename T>
165  GLuint Bitangents(std::vector<T>& dest) const
166  {
167  dest.resize(((_rings + 2) * (_sections + 1)) * 3);
168  unsigned k = 0;
169  //
170  double r_step = (1.0 * math::Pi()) / double(_rings + 1);
171  double s_step = (2.0 * math::Pi()) / double(_sections);
172 
173  double ty = 0.0;
174  for(unsigned r=0; r!=(_rings+2);++r)
175  {
176  double r_lat = std::cos(r*r_step);
177  double r_rad = std::sin(r*r_step);
178  double ny = r_lat;
179  // the sections
180  for(unsigned s=0; s!=(_sections+1);++s)
181  {
182  double tx = -std::sin(s*s_step);
183  double tz = -std::cos(s*s_step);
184  double nx = -r_rad * tz;
185  double nz = r_rad * tx;
186 
187  dest[k++] = T(ny*tz-nz*ty);
188  dest[k++] = T(nz*tx-nx*tz);
189  dest[k++] = T(nx*ty-ny*tx);
190  }
191  }
192  //
193  assert(k == dest.size());
194  // 3 values per vertex
195  return 3;
196  }
197 
199  template <typename T>
200  GLuint Positions(std::vector<T>& dest) const
201  {
202  GLuint n = Normals(dest);
203  if(_radius != 1.0)
204  for(auto i=dest.begin(),e=dest.end(); i!= e; ++i)
205  *i *= _radius;
206  return n;
207  }
208 
210  template <typename T>
211  GLuint TexCoordinates(std::vector<T>& dest) const
212  {
213  dest.resize(((_rings + 2) * (_sections + 1)) * 2);
214  unsigned k = 0;
215  //
216  double r_step = 1.0 / double(_rings + 1);
217  double s_step = 1.0 / double(_sections);
218  for(unsigned r=0; r!=(_rings+2);++r)
219  {
220  double r_lat = 1.0 - r*r_step;
221  // the sections
222  for(unsigned s=0; s!=(_sections+1);++s)
223  {
224  dest[k++] = T(s * s_step);
225  dest[k++] = T(r_lat);
226  }
227  }
228  assert(k == dest.size());
229  // 2 values per vertex
230  return 2;
231  }
232 
233 #if OGLPLUS_DOCUMENTATION_ONLY
234 
243  typedef VertexAttribsInfo<Sphere> VertexAttribs;
244 #else
245  typedef VertexAttribsInfo<
246  Sphere,
247  std::tuple<
248  VertexPositionsTag,
249  VertexNormalsTag,
250  VertexTangentsTag,
251  VertexBitangentsTag,
252  VertexTexCoordinatesTag
253  >
254  > VertexAttribs;
255 #endif
256 
258  template <typename T>
259  void BoundingSphere(oglplus::Sphere<T>& bounding_sphere) const
260  {
261  bounding_sphere = oglplus::Sphere<T>(
262  T(0),
263  T(0),
264  T(0),
265  T(_radius)
266  );
267  }
268 
270  typedef std::vector<GLushort> IndexArray;
271 
273  IndexArray Indices(Default = Default()) const;
274 
276  DrawingInstructions Instructions(Default = Default()) const;
277 };
278 
279 } // shapes
280 } // oglplus
281 
282 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
283 #include <oglplus/shapes/sphere.ipp>
284 #endif // OGLPLUS_LINK_LIBRARY
285 
286 #endif // include guard
Implementation of shape draw instructions.
GLuint Positions(std::vector< T > &dest) const
Makes vertex coordinates and returns number of values per vertex.
Definition: sphere.hpp:200
unsigned Sections(void) const
Returns the number of sections of the sphere.
Definition: sphere.hpp:74
Sphere utility class.
GLuint Bitangents(std::vector< T > &dest) const
Makes vertex bi-tangents and returns number of values per vertex.
Definition: sphere.hpp:165
void BoundingSphere(oglplus::Sphere< T > &bounding_sphere) const
Queries the bounding sphere coordinates and dimensions.
Definition: sphere.hpp:259
IndexArray Indices(Default=Default()) const
Returns element indices that are used with the drawing instructions.
void Radius(double radius)
Sets the radius of the sphere.
Definition: sphere.hpp:67
Sphere(double radius, unsigned sections, unsigned rings)
Creates a sphere with specified radius centered at the origin.
Definition: sphere.hpp:47
std::vector< GLushort > IndexArray
The type of index container returned by Indices()
Definition: sphere.hpp:270
FaceOrientation
Face orientation enumeration.
Definition: face_mode.hpp:62
OpenGL face type-related enumeration.
GLuint Tangents(std::vector< T > &dest) const
Makes vertex tangents and returns number of values per vertex.
Definition: sphere.hpp:141
Sphere(void)
Creates a sphere with unit radius centered at the origin.
Definition: sphere.hpp:37
void Rings(unsigned rings)
Sets the number of rings of the sphere.
Definition: sphere.hpp:99
GLuint Normals(std::vector< T > &dest) const
Makes vertex normals and returns number of values per vertex.
Definition: sphere.hpp:113
double Radius(void) const
Returns the radius of the sphere.
Definition: sphere.hpp:58
VertexAttribsInfo< Sphere > VertexAttribs
Vertex attribute information for this shape builder.
Definition: sphere.hpp:243
Classes providing additional information about the shape builders.
Math constants.
void Sections(unsigned sections)
Sets the number of sections of the sphere.
Definition: sphere.hpp:83
Class encapsulating the instructions for drawing of a shape.
Definition: draw.hpp:219
DrawingInstructions Instructions(Default=Default()) const
Returns the instructions for rendering.
Class implementing sphere-related functionality.
Definition: sphere.hpp:29
GLuint TexCoordinates(std::vector< T > &dest) const
Makes texture-coorinates and returns number of values per vertex.
Definition: sphere.hpp:211
FaceOrientation FaceWinding(void) const
Returns the winding direction of faces.
Definition: sphere.hpp:106
CCW: Counter-clockwise.
unsigned Rings(void) const
Returns the number of rings of the sphere.
Definition: sphere.hpp:90
Class providing vertex attributes and instructions for rendering of a Sphere.
Definition: sphere.hpp:28

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).