OGLplus (0.52.0) a C++ wrapper for OpenGL

grid.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHAPES_GRID_1107121519_HPP
14 #define OGLPLUS_SHAPES_GRID_1107121519_HPP
15 
16 #include <oglplus/shapes/draw.hpp>
17 #include <oglplus/math/vector.hpp>
18 #include <oglplus/face_mode.hpp>
19 
21 #include <oglplus/math/sphere.hpp>
22 
23 #include <cmath>
24 #include <cassert>
25 
26 namespace oglplus {
27 namespace shapes {
28 
30 class Grid
31  : public DrawingInstructionWriter
32  , public DrawMode
33 {
34 private:
35  Vec3f _point;
36  Vec3f _u, _v;
37  unsigned _udiv, _vdiv;
38 
39  unsigned _vertex_count(void) const
40  {
41  return (_udiv+1)*2+(_vdiv-1)*2;
42  }
43 public:
45  Grid(void)
46  : _point(0.0f, 0.0f, 0.0f)
47  , _u(1.0f, 0.0f, 0.0f)
48  , _v(0.0f, 0.0f,-1.0f)
49  , _udiv(2)
50  , _vdiv(2)
51  { }
52 
54  Grid(const Vec3f u, const Vec3f v)
55  : _point(0.0f, 0.0f, 0.0f)
56  , _u(u)
57  , _v(v)
58  , _udiv(2)
59  , _vdiv(2)
60  {
61  assert(Length(_u) > 0.0f);
62  assert(Length(_v) > 0.0f);
63  }
64 
67  const Vec3f p,
68  const Vec3f u,
69  const Vec3f v,
70  unsigned udiv,
71  unsigned vdiv
72  ): _point(p)
73  , _u(u)
74  , _v(v)
75  , _udiv(udiv)
76  , _vdiv(vdiv)
77  {
78  assert(Length(_u) > 0.0f);
79  assert(Length(_v) > 0.0f);
80  assert(_udiv != 0);
81  assert(_vdiv != 0);
82  }
83 
84  const Vec3f& Point(void) const
85  {
86  return _point;
87  }
88 
89  const Vec3f& U(void) const
90  {
91  return _u;
92  }
93 
94  const Vec3f& V(void) const
95  {
96  return _v;
97  }
98 
99  inline Vec3f Normal(void) const
100  {
101  return Normalized(Cross(_u, _v));
102  }
103 
104  inline Vec3f Tangential(void) const
105  {
106  return Normalized(_u);
107  }
108 
109  inline Vec3f Bitangential(void) const
110  {
111  return Normalized(_v);
112  }
113 
114  Vec4f PlaneEquation(void) const
115  {
116  return Vec4f(Normal(), -Dot(Normal(), _point));
117  }
118 
121  {
122  return FaceOrientation::CW;
123  }
124 
126  template <typename T>
127  GLuint Positions(std::vector<T>& dest) const
128  {
129  unsigned k = 0, n = _vertex_count();
130  dest.resize(n * 3);
131 
132  const Vec3f pos(_point - _u - _v);
133  const Vec3f ustep(_u * (2.0 / _udiv));
134  const Vec3f vstep(_v * (2.0 / _vdiv));
135 
136  for(unsigned i=0; i<(_udiv+1); ++i)
137  {
138  Vec3f tmp = pos+ustep*i;
139  dest[k++] = T(tmp.x());
140  dest[k++] = T(tmp.y());
141  dest[k++] = T(tmp.z());
142 
143  tmp += 2.0*_v;
144  dest[k++] = T(tmp.x());
145  dest[k++] = T(tmp.y());
146  dest[k++] = T(tmp.z());
147  }
148 
149  for(unsigned j=1; j<(_vdiv); ++j)
150  {
151  Vec3f tmp = pos+vstep*j;
152  dest[k++] = T(tmp.x());
153  dest[k++] = T(tmp.y());
154  dest[k++] = T(tmp.z());
155 
156  tmp += 2.0*_u;
157  dest[k++] = T(tmp.x());
158  dest[k++] = T(tmp.y());
159  dest[k++] = T(tmp.z());
160  }
161  assert(k == dest.size());
162  return 3;
163  }
164 
166  template <typename T>
167  GLuint TexCoordinates(std::vector<T>& dest) const
168  {
169  unsigned k = 0, n = _vertex_count();
170  dest.resize(n * 2);
171 
172 
173  T ustep = T(1) / _udiv;
174  T vstep = T(1) / _vdiv;
175 
176 
177  for(unsigned i=0; i<(_udiv+1); ++i)
178  {
179  dest[k++] = T(ustep*i);
180  dest[k++] = T(0);
181 
182  dest[k++] = T(ustep*i);
183  dest[k++] = T(1);
184  }
185 
186  for(unsigned j=1; j<(_vdiv); ++j)
187  {
188  dest[k++] = T(0);
189  dest[k++] = T(vstep*j);
190 
191  dest[k++] = T(1);
192  dest[k++] = T(vstep*j);
193  }
194  assert(k == dest.size());
195  // 2 values per vertex
196  return 2;
197  }
198 
199 #if OGLPLUS_DOCUMENTATION_ONLY
200 
206  typedef VertexAttribsInfo<Grid> VertexAttribs;
207 #else
208  typedef VertexAttribsInfo<
209  Grid,
210  std::tuple<
211  VertexPositionsTag,
212  VertexTexCoordinatesTag
213  >
214  > VertexAttribs;
215 #endif
216 
218  template <typename T>
219  void BoundingSphere(oglplus::Sphere<T>& bounding_sphere) const
220  {
221  bounding_sphere = oglplus::Sphere<T>(
222  T(_point.x()),
223  T(_point.y()),
224  T(_point.z()),
225  T(Length(_u + _v))
226  );
227  }
228 
230  typedef std::vector<GLuint> IndexArray;
231 
233  IndexArray Indices(Default = Default()) const;
234 
236  DrawingInstructions Instructions(Default = Default()) const;
237 };
238 
239 } // shapes
240 } // oglplus
241 
242 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
243 #include <oglplus/shapes/grid.ipp>
244 #endif // OGLPLUS_LINK_LIBRARY
245 
246 #endif // include guard
Implementation of shape draw instructions.
Vector< GLfloat, 4 > Vec4f
4D float vector
Definition: vector.hpp:85
std::vector< GLuint > IndexArray
The type of index container returned by Indices()
Definition: grid.hpp:230
IndexArray Indices(Default=Default()) const
Returns element indices that are used with the drawing instructions.
FaceOrientation FaceWinding(void) const
Returns the winding direction of faces (unused)
Definition: grid.hpp:120
GLuint Positions(std::vector< T > &dest) const
Makes vertex coordinates and returns number of values per vertex.
Definition: grid.hpp:127
Grid(const Vec3f u, const Vec3f v)
Creates a grid with going through origin specified by u and v.
Definition: grid.hpp:54
Vector< GLfloat, 3 > Vec3f
3D float vector
Definition: vector.hpp:79
Sphere utility class.
GLuint TexCoordinates(std::vector< T > &dest) const
Makes texture-coorinates and returns number of values per vertex.
Definition: grid.hpp:167
DrawingInstructions Instructions(Default=Default()) const
Returns the instructions for rendering.
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
void BoundingSphere(oglplus::Sphere< T > &bounding_sphere) const
Queries the bounding sphere coordinates and dimensions.
Definition: grid.hpp:219
T y(void) const
Returns the 1-st component.
Definition: vector.hpp:231
VertexAttribsInfo< Grid > VertexAttribs
Vertex attribute information for this shape builder.
Definition: grid.hpp:206
Grid(const Vec3f p, const Vec3f u, const Vec3f v, unsigned udiv, unsigned vdiv)
Creates a grid with going through p specified by u and v.
Definition: grid.hpp:66
Classes providing additional information about the shape builders.
Class encapsulating the instructions for drawing of a shape.
Definition: draw.hpp:219
Grid(void)
Creates a default grid.
Definition: grid.hpp:45
Class implementing sphere-related functionality.
Definition: sphere.hpp:29
Class providing vertex attributes and instructions for rendering of a grid.
Definition: grid.hpp:30
A vector class.

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