KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_primitive.h
Go to the documentation of this file.
1 /* $Id: gr_primitive.h 18859 2015-04-15 03:25:16Z ernesto $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 #ifndef KJB_CPP_PRIMITIVE_H
20 #define KJB_CPP_PRIMITIVE_H
21 
22 #ifdef KJB_HAVE_OPENGL
23 
29 #include <gr_cpp/gr_renderable.h>
30 #include <m_cpp/m_vector.h>
32 
33 namespace kjb
34 {
35 namespace opengl
36 {
37 
41 class Quadric :
43 {
44 public:
45  Quadric(unsigned int slices = DEFAULT_SLICES, unsigned int rings = DEFAULT_RINGS);
46 
47  Quadric(const Quadric& src);
48 
49  Quadric& operator=(const Quadric& other);
50 
51  void swap(Quadric&);
52 
53  virtual ~Quadric();
54 
55  void wire_render() const;
56 
57  void solid_render() const
58  {
59  if(_mesh_dirty) _update_mesh();
60 
61  _generic_render_in_opengl(_solid_list_id);
62  }
63 protected:
64 
65  void _init_quadric();
66  void _generic_render_in_opengl(unsigned int list_id) const;
67 
68  static void _quad_error_callback(unsigned int error_code);
69 
70  void _update_mesh() const;
71 
72  virtual void _render() const = 0;
73 protected:
74  // we only store one glu quad object for all quadrics
75  static GLUquadricObj* _quad;
76  static unsigned int _num_instances;
77 
78  mutable unsigned int _solid_list_id;
79  mutable unsigned int _wire_list_id;
80 
81  mutable bool _mesh_dirty;
82 
83  static const unsigned int DEFAULT_SLICES = 30;
84  static const unsigned int DEFAULT_RINGS = 10;
85 
86  unsigned int _slices;
87  unsigned int _rings;
88 };
89 
93 class Cylinder : public Quadric
94 {
95 public:
96  Cylinder(
97  float base_radius = 1,
98  float top_radius = 1,
99  float height = 1,
100  unsigned int slices = DEFAULT_SLICES,
101 
102  unsigned int rings = DEFAULT_RINGS) :
103  Quadric(slices, rings),
104  _base_r(base_radius),
105  _top_r(top_radius),
106  _height(height)
107  {
108  _update_mesh();
109  }
110 
111  Cylinder(const Cylinder& src) :
112  Quadric(src),
113  _base_r(src._base_r),
114  _top_r(src._top_r),
115  _height(src._height)
116  {
117  _update_mesh();
118  }
119 
120  Cylinder& operator=(const Cylinder& other);
121 
122  void swap(Cylinder&);
123 
124  virtual ~Cylinder(){};
125 
126  void set_base_radius(float r) {_base_r = r; _mesh_dirty = true;}
127  void set_top_radius(float r) {_top_r = r; _mesh_dirty = true;}
128  void set_height(float h) {_height = h; _mesh_dirty = true;}
129 private:
130  float _base_r;
131  float _top_r;
132  float _height;
133 
134  virtual void _render() const;
135 };
136 
142 class Static_cylinder :
144 {
145 public:
146  Static_cylinder(
147  float base_radius = 1,
148  float top_radius = 1,
149  float height = 1,
150  unsigned int slices = DEFAULT_SLICES,
151 
152  unsigned int rings = DEFAULT_RINGS);
153 
154  void wire_render() const;
155 
156  void solid_render() const;
157 
158 private:
159  unsigned int wire_index_;
160  unsigned int solid_index_;
161 
162  static const unsigned int DEFAULT_SLICES = 30;
163  static const unsigned int DEFAULT_RINGS = 10;
164 };
165 
169 class Sphere : public Quadric
170 {
171 public:
172  Sphere(float radius = 1, unsigned int slices = DEFAULT_SLICES, unsigned int rings = DEFAULT_RINGS):
173  Quadric(slices, rings),
174  _r(radius)
175  {
176  _update_mesh();
177  }
178 
179 
180  Sphere(const Sphere& src):
181  Quadric(src),
182  _r(src._r)
183  {
184  _update_mesh();
185  }
186 
187  Sphere& operator=(const Sphere& other);
188 
189  void swap(Sphere&);
190 
191  virtual ~Sphere(){};
192 
193  void set_radius(float r)
194  {
195  _r = r; _mesh_dirty = true;
196  }
197 private:
198 
199  float _r;
200 
201  virtual void _render() const;
202 };
203 
204 
205 class Disk : public Quadric
206 {
207 public:
208  Disk(float outer_radius = 1, unsigned int slices = DEFAULT_SLICES, unsigned int rings = DEFAULT_RINGS):
209  Quadric(slices, rings),
210  _inner_r(0.0),
211  _outer_r(outer_radius)
212  {
213  _update_mesh();
214  }
215 
216  Disk(float inner_radius, float outer_radius, unsigned int slices = DEFAULT_SLICES, unsigned int rings = DEFAULT_RINGS):
217  Quadric(slices, rings),
218  _inner_r(inner_radius),
219  _outer_r(outer_radius)
220  {
221  _update_mesh();
222  }
223 
224 
225  Disk(const Disk& src):
226  Quadric(src),
227  _inner_r(src._inner_r),
228  _outer_r(src._outer_r)
229  {
230  _update_mesh();
231  }
232 
233  Disk& operator=(const Disk& other);
234 
235  void swap(Disk&);
236 
237  virtual ~Disk(){};
238 
239  void set_inner_radius(float r)
240  {
241  _inner_r = r; _mesh_dirty = true;
242  }
243 
244  void set_outer_radius(float r)
245  {
246  _outer_r = r; _mesh_dirty = true;
247  }
248 private:
249 
250  float _inner_r;
251  float _outer_r;
252 
253  virtual void _render() const;
254 };
255 
259 class Arrow3d : public kjb::Generic_renderable
260 {
261 public:
262  Arrow3d() :
263  _pos(3, 0.0),
264  _length(0.01),
265  _shaft_length(0.0),
266  _width(1.0),
267  _dir(3, 0.0),
268  _shaft(),
269  _head(),
270  _shaft_cap(),
271  _head_cap()
272  {
273  _dir[2] = 1.0;
274  }
275 
276  Arrow3d(const kjb::Vector& position, const kjb::Vector& direction, float width = 1.0) :
277  _pos(position),
278  _length(direction.magnitude()),
279  _shaft_length(0.0),
280  _width(width),
281  _dir(direction / _length),
282  _shaft(),
283  _head(),
284  _shaft_cap(),
285  _head_cap()
286  {
287  double head_length = std::min(_length, 2 * _width);
288  double head_width = head_length;
289 
290 
291  _shaft_length = std::max(0.01, _length - head_length);
292  _shaft = Cylinder(_width/2, _width/2, _shaft_length, 10);
293  _head = Cylinder(head_width/2, 0.0, head_length, 10);
294 
295  _shaft_cap = Disk(_width/2);
296  _head_cap = Disk(head_width/2);
297  }
298 
299  void set_position(const kjb::Vector& position)
300  {
301  assert(position.size() == 3);
302  _pos = position;
303  }
304 
305  void set_direction(const kjb::Vector& direction)
306  {
307  assert(direction.size() == 3);
308  _dir = direction;
309  }
310 
311  void render() const
312  {
313  _generic_render(Renderer());
314  }
315 
316  void solid_render() const
317  {
318  _generic_render(Solid_renderer());
319  }
320 
321  void wire_render() const
322  {
323  _generic_render(Wire_renderer());
324  }
325 
326  void wire_occlude_render() const
327  {
328  _generic_render(Wire_occlude_renderer());
329  }
330 
331 private:
332  void _generic_render(const Generic_renderer& renderer) const;
333 
334  kjb::Vector _pos;
335  float _length;
336  float _shaft_length;
337  float _width;
338  kjb::Vector _dir;
339 
340  Cylinder _shaft;
341  Cylinder _head;
342 
343  Disk _shaft_cap;
344  Disk _head_cap;
345 };
346 
347 class Teapot : public kjb::Generic_renderable
348 {
349 public:
350  void solid_render() const;
351 };
352 
354 class Ellipse : public Generic_renderable
355 {
356 public:
358  Ellipse(double r1, double r2, size_t slices = DEFAULT_SLICES);
359 
361  void render() const;
362 
364  void solid_render() const;
365 
367  void wire_render() const;
368 
369 private:
371  void render_points() const;
372 
373  double m_rad1;
374  double m_rad2;
375  size_t m_slices;
376 
377  static const size_t DEFAULT_SLICES = 32;
378 };
379 
380 } // namespace opengl
381 } // namespace kjb
382 #endif
383 #endif
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
void swap(Perspective_camera &cam1, Perspective_camera &cam2)
Swap two cameras.
Definition: perspective_camera.h:599
height
Definition: APPgetLargeConnectedEdges.m:33
r
Definition: APPgetLargeConnectedEdges.m:127
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
Abstract class to render this object with GL.
void render(const Cuboid &c)
Definition: psi_weighted_box.cpp:56
Int_matrix::Value_type min(const Int_matrix &mat)
Return the minimum value in this matrix.
Definition: l_int_matrix.h:1385
Definition: gr_renderable.h:233
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
int GLUquadricObj
Definition: gr_opengl_headers.h:63