OGLplus (0.52.0) a C++ wrapper for OpenGL

plane.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #ifndef OGLPLUS_SHAPES_PLANE_1107121519_HPP
14 #define OGLPLUS_SHAPES_PLANE_1107121519_HPP
15 
16 #include <oglplus/shapes/draw.hpp>
17 #include <oglplus/math/vector.hpp>
18 #include <oglplus/face_mode.hpp>
19 
21 
22 #include <oglplus/math/sphere.hpp>
23 
24 #include <cmath>
25 #include <cassert>
26 
27 namespace oglplus {
28 namespace shapes {
29 
31 class Plane
32  : public DrawingInstructionWriter
33  , public DrawMode
34 {
35 private:
36  Vec3f _point;
37  Vec3f _u, _v;
38  unsigned _udiv, _vdiv;
39 
40  unsigned _vertex_count(void) const
41  {
42  return (_udiv+1)*(_vdiv+1);
43  }
44 public:
46  Plane(void)
47  : _point(0.0f, 0.0f, 0.0f)
48  , _u(1.0f, 0.0f, 0.0f)
49  , _v(0.0f, 0.0f,-1.0f)
50  , _udiv(2)
51  , _vdiv(2)
52  { }
53 
55  Plane(unsigned udiv, unsigned vdiv)
56  : _point(0.0f, 0.0f, 0.0f)
57  , _u(1.0f, 0.0f, 0.0f)
58  , _v(0.0f, 0.0f,-1.0f)
59  , _udiv(udiv)
60  , _vdiv(vdiv)
61  {
62  assert(udiv > 1);
63  assert(vdiv > 1);
64  }
65 
67  Plane(const Vec3f u, const Vec3f v)
68  : _point(0.0f, 0.0f, 0.0f)
69  , _u(u)
70  , _v(v)
71  , _udiv(2)
72  , _vdiv(2)
73  {
74  assert(Length(_u) > 0.0f);
75  assert(Length(_v) > 0.0f);
76  }
77 
80  const Vec3f p,
81  const Vec3f u,
82  const Vec3f v,
83  unsigned udiv,
84  unsigned vdiv
85  ): _point(p)
86  , _u(u)
87  , _v(v)
88  , _udiv(udiv)
89  , _vdiv(vdiv)
90  {
91  assert(Length(_u) > 0.0f);
92  assert(Length(_v) > 0.0f);
93  assert(_udiv != 0);
94  assert(_vdiv != 0);
95  }
96 
97  const Vec3f& Point(void) const
98  {
99  return _point;
100  }
101 
102  const Vec3f& U(void) const
103  {
104  return _u;
105  }
106 
107  const Vec3f& V(void) const
108  {
109  return _v;
110  }
111 
112  inline Vec3f Normal(void) const
113  {
114  return Normalized(Cross(_u, _v));
115  }
116 
117  inline Vec3f Tangential(void) const
118  {
119  return Normalized(_u);
120  }
121 
122  inline Vec3f Bitangential(void) const
123  {
124  return Normalized(_v);
125  }
126 
127  Vec4f Equation(void) const
128  {
129  return Vec4f(Normal(), -Dot(Normal(), _point));
130  }
131 
134  {
135  return FaceOrientation::CW;
136  }
137 
139  template <typename T>
140  GLuint Normals(std::vector<T>& dest) const
141  {
142  unsigned k = 0, n = _vertex_count();
143  dest.resize(n * 3);
144  Vec3f normal = Normal();
145 
146  for(unsigned i=0; i!=n; ++i)
147  {
148  dest[k++] = T(normal.x());
149  dest[k++] = T(normal.y());
150  dest[k++] = T(normal.z());
151  }
152  //
153  assert(k == dest.size());
154  // 3 values per vertex
155  return 3;
156  }
157 
159  template <typename T>
160  GLuint Tangents(std::vector<T>& dest) const
161  {
162  unsigned k = 0, n = _vertex_count();
163  dest.resize(n * 3);
164  Vec3f tangent(Normalized(_u));
165 
166  for(unsigned i=0; i!=n; ++i)
167  {
168  dest[k++] = T(tangent.x());
169  dest[k++] = T(tangent.y());
170  dest[k++] = T(tangent.z());
171  }
172  //
173  assert(k == dest.size());
174  // 3 values per vertex
175  return 3;
176  }
177 
179  template <typename T>
180  GLuint Bitangents(std::vector<T>& dest) const
181  {
182  unsigned k = 0, n = _vertex_count();
183  dest.resize(n * 3);
184  Vec3f bitangent(Normalized(_v));
185 
186  for(unsigned i=0; i!=n; ++i)
187  {
188  dest[k++] = T(bitangent.x());
189  dest[k++] = T(bitangent.y());
190  dest[k++] = T(bitangent.z());
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  unsigned k = 0, n = _vertex_count();
203  dest.resize(n * 3);
204 
205 
206  Vec3f pos(_point - _u - _v);
207  Vec3f ustep(_u * (2.0 / _udiv));
208  Vec3f vstep(_v * (2.0 / _vdiv));
209 
210  unsigned leap = _udiv+1;
211 
212  for(unsigned j=0; j!=(_vdiv+1); ++j)
213  {
214  Vec3f tmp = pos;
215  for(unsigned i=0; i!=leap; ++i)
216  {
217  dest[k++] = T(tmp.x());
218  dest[k++] = T(tmp.y());
219  dest[k++] = T(tmp.z());
220  tmp += ustep;
221  }
222  pos += vstep;
223  }
224  assert(k == dest.size());
225  return 3;
226  }
227 
229  template <typename T>
230  GLuint TexCoordinates(std::vector<T>& dest) const
231  {
232  unsigned k = 0, n = _vertex_count();
233  dest.resize(n * 2);
234 
235 
236  T uc, vc = T(0);
237  T ustep = T(1) / _udiv;
238  T vstep = T(1) / _vdiv;
239 
240  unsigned leap = _udiv+1;
241 
242 
243  for(unsigned j=0; j!=(_vdiv+1); ++j)
244  {
245  uc = T(0);
246  for(unsigned i=0; i!=leap; ++i)
247  {
248  dest[k++] = T(uc);
249  dest[k++] = T(vc);
250  uc += ustep;
251  }
252  vc += vstep;
253  }
254  assert(k == dest.size());
255  // 2 values per vertex
256  return 2;
257  }
258 
259 #if OGLPLUS_DOCUMENTATION_ONLY
260 
269  typedef VertexAttribsInfo<Plane> VertexAttribs;
270 #else
271  typedef VertexAttribsInfo<
272  Plane,
273  std::tuple<
274  VertexPositionsTag,
275  VertexNormalsTag,
276  VertexTangentsTag,
277  VertexBitangentsTag,
278  VertexTexCoordinatesTag
279  >
280  > VertexAttribs;
281 #endif
282 
284  template <typename T>
285  void BoundingSphere(oglplus::Sphere<T>& bounding_sphere) const
286  {
287  bounding_sphere = oglplus::Sphere<T>(
288  T(_point.x()),
289  T(_point.y()),
290  T(_point.z()),
291  T(Length(_u + _v))
292  );
293  }
294 
296  typedef std::vector<GLuint> IndexArray;
297 
299  IndexArray Indices(Default = Default()) const;
300 
302  DrawingInstructions Instructions(Default = Default()) const;
303 
304 #if OGLPLUS_DOCUMENTATION_ONLY || GL_VERSION_4_0
305 
309  IndexArray Indices(Patches) const;
310 
312 
316 #endif
317 
319  IndexArray Indices(Edges) const;
320 
322  DrawingInstructions Instructions(Edges) const;
323 };
324 
325 } // shapes
326 } // oglplus
327 
328 #if !OGLPLUS_LINK_LIBRARY || defined(OGLPLUS_IMPLEMENTING_LIBRARY)
329 #include <oglplus/shapes/plane.ipp>
330 #endif // OGLPLUS_LINK_LIBRARY
331 
332 #endif // include guard
Implementation of shape draw instructions.
void BoundingSphere(oglplus::Sphere< T > &bounding_sphere) const
Queries the bounding sphere coordinates and dimensions.
Definition: plane.hpp:285
Vector< GLfloat, 4 > Vec4f
4D float vector
Definition: vector.hpp:85
VertexAttribsInfo< Plane > VertexAttribs
Vertex attribute information for this shape builder.
Definition: plane.hpp:269
std::vector< GLuint > IndexArray
The type of index container returned by Indices()
Definition: plane.hpp:296
GLuint Positions(std::vector< T > &dest) const
Makes vertex coordinates and returns number of values per vertex.
Definition: plane.hpp:200
Vector< GLfloat, 3 > Vec3f
3D float vector
Definition: vector.hpp:79
Plane(const Vec3f p, const Vec3f u, const Vec3f v, unsigned udiv, unsigned vdiv)
Creates a plane going through p specified by u and v.
Definition: plane.hpp:79
Sphere utility class.
Class providing vertex attributes and instructions for rendering of a Plane.
Definition: plane.hpp:31
Plane(const Vec3f u, const Vec3f v)
Creates a plane going through origin specified by u and v.
Definition: plane.hpp:67
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
FaceOrientation FaceWinding(void) const
Returns the winding direction of faces.
Definition: plane.hpp:133
GLuint Normals(std::vector< T > &dest) const
Makes vertex normals and returns number of values per vertex.
Definition: plane.hpp:140
T y(void) const
Returns the 1-st component.
Definition: vector.hpp:231
GLuint TexCoordinates(std::vector< T > &dest) const
Makes texture-coorinates and returns number of values per vertex.
Definition: plane.hpp:230
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: plane.hpp:180
Plane(unsigned udiv, unsigned vdiv)
Creates a default plane with udiv and vdiv divisions.
Definition: plane.hpp:55
Classes providing additional information about the shape builders.
GLuint Tangents(std::vector< T > &dest) const
Makes vertex tangents and returns number of values per vertex.
Definition: plane.hpp:160
Class encapsulating the instructions for drawing of a shape.
Definition: draw.hpp:219
Class implementing sphere-related functionality.
Definition: sphere.hpp:29
Plane(void)
Creates a default plane.
Definition: plane.hpp:46
A vector class.
DrawingInstructions Instructions(Default=Default()) const
Returns the instructions for rendering.

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