wykobi_graphics_opengl.hpp
Go to the documentation of this file.
1 /*
2 (***********************************************************************)
3 (* *)
4 (* Wykobi Computational Geometry Library *)
5 (* Release Version 0.0.5 *)
6 (* http://www.wykobi.com *)
7 (* Copyright (c) 2005-2017 Arash Partow, All Rights Reserved. *)
8 (* *)
9 (* The Wykobi computational geometry library and its components are *)
10 (* supplied under the terms of the open source MIT License. *)
11 (* The contents of the Wykobi computational geometry library and its *)
12 (* components may not be copied or disclosed except in accordance with *)
13 (* the terms of the MIT License. *)
14 (* *)
15 (* URL: https://opensource.org/licenses/MIT *)
16 (* *)
17 (***********************************************************************)
18 */
19 
20 #ifndef INCLUDE_WYKOBI_GRAPHICS_OPENGL
21 #define INCLUDE_WYKOBI_GRAPHICS_OPENGL
22 
23 #include <GL/gl.h>
24 #include <GL/glu.h>
25 
26 #include <cmath>
27 #include <iostream>
28 #include <iterator>
29 #include <string>
30 #include <vector>
31 
32 #include "wykobi.hpp"
33 #include "wykobi_utilities.hpp"
34 
35 namespace wykobi {
37 
38 const GLfloat basic_color[14][3] = {
39  {0.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0},
40  {255.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0},
41  {0.0 / 255.0, 255.0 / 255.0, 0.0 / 255.0},
42  {0.0 / 255.0, 0.0 / 255.0, 255.0 / 255.0},
43  {255.0 / 255.0, 0.0 / 255.0, 255.0 / 255.0},
44  {0.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0},
45  {0.0 / 255.0, 0.0 / 255.0, 255.0 / 255.0},
46  {255.0 / 255.0, 255.0 / 255.0, 0.0 / 255.0},
47  {0.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0},
48  {255.0 / 255.0, 192.0 / 255.0, 64.0 / 255.0},
49  {255.0 / 255.0, 0.0 / 255.0, 255.0 / 255.0},
50  {255.0 / 255.0, 0.0 / 255.0, 0.0 / 255.0},
51  {255.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0},
52  {255.0 / 255.0, 255.0 / 255.0, 0.0 / 255.0}};
53 
54 template <typename T>
56  public:
57  wykobi_graphics_opengl(const unsigned int& w, const unsigned int& h,
58  DrawingMode dm)
59  : _width(w),
60  _height(h),
61  drawing_mode(dm),
62  red(1.0),
63  green(1.0),
64  blue(1.0) {}
65 
67 
68  inline void set_color(const unsigned int& _red, const unsigned int& _green,
69  const unsigned int& _blue) {
70  red = (1.0 * _red) / 256.0;
71  green = (1.0 * _green) / 256.0;
72  blue = (1.0 * _blue) / 256.0;
73 
74  color[0] = red;
75  color[1] = green;
76  color[2] = blue;
77 
78  glColor3fv(color);
79  }
80 
81  inline void set_color(const unsigned int& color_index) {
82  color[0] = basic_color[0][color_index % 14];
83  color[1] = basic_color[1][color_index % 14];
84  color[2] = basic_color[2][color_index % 14];
85 
86  red = color[0];
87  green = color[1];
88  blue = color[2];
89 
90  glColor3fv(color);
91  }
92 
93  inline void draw_text(const T& x, const T& y, const std::string& text) {}
94 
95  inline void draw_pixel(const T& x, const T& y) const {
96  glBegin(GL_POINTS);
97  glVertex2d(double(x), double(y));
98  glEnd();
99  }
100 
101  inline void draw_pixel(const T& x, const T& y, const T& z) const {
102  glBegin(GL_POINTS);
103  glVertex3d(double(x), double(y), double(z));
104  glEnd();
105  }
106 
107  inline void draw_segment(const T& x1, const T& y1, const T& x2,
108  const T& y2) const {
109  glBegin(GL_LINES);
110  glVertex2d(double(x1), double(y1));
111  glVertex2d(double(x2), double(y2));
112  glEnd();
113  }
114 
115  inline void draw_segment(const T& x1, const T& y1, const T& z1, const T& x2,
116  const T& y2, const T& z2) const {
117  glBegin(GL_LINES);
118  glVertex3d(x1, y1, z1);
119  glVertex3d(x2, y2, z2);
120  glEnd();
121  }
122 
123  inline void draw_triangle(const T& x1, const T& y1, const T& x2, const T& y2,
124  const T& x3, const T& y3) const {
125  switch (drawing_mode) {
126  case eSolid:
127  glBegin(GL_TRIANGLES);
128  break;
129  case eOutLine:
130  glBegin(GL_LINE_LOOP);
131  break;
132  case eNoDraw:
133  return;
134  }
135  glVertex2d(double(x1), double(y1));
136  glVertex2d(double(x2), double(y2));
137  glVertex2d(double(x3), double(y3));
138  glEnd();
139  }
140 
141  inline void draw_triangle(const T& x1, const T& y1, const T& z1, const T& x2,
142  const T& y2, const T& z2, const T& x3, const T& y3,
143  const T& z3) const {
144  switch (drawing_mode) {
145  case eSolid:
146  glBegin(GL_TRIANGLES);
147  break;
148  case eOutLine:
149  glBegin(GL_LINE_LOOP);
150  break;
151  case eNoDraw:
152  return;
153  }
154  glVertex3d(double(x1), double(y1), double(z1));
155  glVertex3d(double(x2), double(y2), double(z2));
156  glVertex3d(double(x3), double(y3), double(z3));
157  glEnd();
158  }
159 
160  inline void draw_quadix(const T& x1, const T& y1, const T& x2, const T& y2,
161  const T& x3, const T& y3, const T& x4,
162  const T& y4) const {
163  switch (drawing_mode) {
164  case eSolid:
165  glBegin(GL_QUADS);
166  break;
167  case eOutLine:
168  glBegin(GL_LINE_LOOP);
169  break;
170  case eNoDraw:
171  return;
172  }
173  glVertex2d(double(x1), double(y1));
174  glVertex2d(double(x2), double(y2));
175  glVertex2d(double(x3), double(y3));
176  glVertex2d(double(x4), double(y4));
177  glEnd();
178  }
179 
180  inline void draw_quadix(const T& x1, const T& y1, const T& z1, const T& x2,
181  const T& y2, const T& z2, const T& x3, const T& y3,
182  const T& z3, const T& x4, const T& y4,
183  const T& z4) const {
184  switch (drawing_mode) {
185  case eSolid:
186  glBegin(GL_QUADS);
187  break;
188  case eOutLine:
189  glBegin(GL_LINE_LOOP);
190  break;
191  case eNoDraw:
192  return;
193  }
194  glVertex3d(double(x1), double(y1), double(z1));
195  glVertex3d(double(x2), double(y2), double(z2));
196  glVertex3d(double(x3), double(y3), double(z3));
197  glVertex3d(double(x4), double(y4), double(z4));
198  glEnd();
199  }
200 
201  inline void draw_rectangle(const T& x1, const T& y1, const T& x2,
202  const T& y2) const {
203  draw_quadix(x1, y1, x2, y1, x2, y2, x1, y2);
204  }
205 
206  inline void draw_circle(const T& x, const T& y, const T& radius) const {
207  wykobi::polygon<T, 2> _polygon = make_polygon(make_circle(x, y, radius));
208  glColor3fv(color);
209  switch (drawing_mode) {
210  case eSolid:
211  glBegin(GL_TRIANGLE_FAN);
212  break;
213  case eOutLine:
214  glBegin(GL_LINE_LOOP);
215  break;
216  case eNoDraw:
217  return;
218  }
219  if (drawing_mode == eSolid) {
220  glVertex2d(double(_polygon[0].x), double(_polygon[0].y));
221  }
222  for (std::size_t i = (drawing_mode == eSolid) ? 1 : 0; i < _polygon.size();
223  ++i) {
224  glVertex2d(double(_polygon[i].x), double(_polygon[i].y));
225  }
226  glEnd();
227  }
228 
229  inline void draw_sphere(const T& x, const T& y, const T& radius) const {}
230 
231  inline void draw_pixel(const point2d<T>& point) const {
232  draw_pixel(point.x, point.y);
233  }
234 
235  inline void draw_pixel(const point3d<T>& point) const {
236  draw_pixel(point.x, point.y, point.z);
237  }
238 
239  inline void draw_segment(const point2d<T>& point1,
240  const point2d<T>& point2) const {
241  draw_segment(point1.x, point1.y, point2.x, point2.y);
242  }
243 
244  inline void draw_triangle(const point2d<T>& point1, const point2d<T>& point2,
245  const point2d<T>& point3) const {
246  draw_triangle(point1.x, point1.y, point2.x, point2.y, point3.x, point3.y);
247  }
248 
249  inline void draw_triangle(const point3d<T>& point1, const point3d<T>& point2,
250  const point3d<T>& point3) const {
251  draw_triangle(point1.x, point1.y, point1.z, point2.x, point2.y, point2.z,
252  point3.x, point3.y, point3.z);
253  }
254 
255  inline void draw_quadix(const point2d<T>& point1, const point2d<T>& point2,
256  const point2d<T>& point3,
257  const point2d<T>& point4) const {
258  draw_quadix(point1.x, point1.y, point2.x, point2.y, point3.x, point3.y,
259  point4.x, point4.y);
260  }
261 
262  inline void draw_quadix(const point3d<T>& point1, const point3d<T>& point2,
263  const point3d<T>& point3,
264  const point3d<T>& point4) const {
265  draw_quadix(point1.x, point1.y, point1.z, point2.x, point2.y, point2.z,
266  point3.x, point3.y, point3.z, point4.x, point4.y, point4.z);
267  }
268 
269  inline void draw_rectangle(const point2d<T>& point1,
270  const point2d<T>& point2) const {
271  draw_rectangle(point1.x, point1.y, point2.x, point2.y);
272  }
273 
274  inline void draw_circle(const point2d<T>& point, const T& radius) const {
275  draw_circle(point.x, point.y, radius);
276  }
277 
278  inline void draw_polyline(std::vector<point2d<T> > point_list) {
279  for (std::size_t i = 0; i < point_list.size() - 1; ++i) {
280  draw_segment(point_list[i], point_list[i + 1]);
281  }
282  }
283 
284  inline void draw_polyline(std::vector<point3d<T> > point_list) {
285  for (std::size_t i = 0; i < point_list.size() - 1; ++i) {
286  draw_segment(point_list[i], point_list[i + 1]);
287  }
288  }
289 
290  inline void clear() { glClear(GL_COLOR_BUFFER_BIT); }
291 
292  inline void draw(const point2d<T>& point) { draw_pixel(point); }
293  inline void draw(const segment<T, 2>& segment) {
294  draw_segment(segment[0], segment[1]);
295  }
296  inline void draw(const triangle<T, 2>& triangle) {
298  }
299  inline void draw(const rectangle<T>& rectangle) {
301  }
302  inline void draw(const quadix<T, 2>& quadix) {
303  draw_quadix(quadix[0], quadix[1], quadix[2], quadix[3]);
304  }
305  inline void draw(const circle<T>& circle) {
307  }
308 
309  inline void draw(const point3d<T>& point) { draw_pixel(point); }
310  inline void draw(const segment<T, 3>& segment) {
311  draw_segment(segment[0], segment[1]);
312  }
313  inline void draw(const triangle<T, 3>& triangle) {
315  }
316  inline void draw(const quadix<T, 3>& quadix) {
317  draw_quadix(quadix[0], quadix[1], quadix[2], quadix[3]);
318  }
319  inline void draw(const sphere<T>& sphere) {
321  }
322 
323  inline void draw(const polygon<T, 2>& polygon, const bool convex = false) {
324  if (polygon.size() < 3) return;
325  if (!convex) {
326  std::size_t j = polygon.size() - 1;
327  for (std::size_t i = 0; i < polygon.size(); ++i) {
328  draw_segment(polygon[i], polygon[j]);
329  j = i;
330  }
331  } else {
332  switch (drawing_mode) {
333  case eSolid:
334  glBegin(GL_POLYGON);
335  break;
336  case eOutLine:
337  glBegin(GL_LINE_LOOP);
338  break;
339  case eNoDraw:
340  return;
341  }
342  for (std::size_t i = 0; i < polygon.size(); ++i) {
343  glVertex3d(polygon[i].x1, polygon[i].y1, polygon[i].z1);
344  }
345  glEnd();
346  }
347  }
348 
349  inline void draw(const polygon<T, 3>& polygon) {
350  if (polygon.size() < 3) return;
351  std::size_t j = polygon.size() - 1;
352  for (std::size_t i = 0; i < polygon.size(); ++i) {
353  draw_segment(polygon[i], polygon[j]);
354  j = i;
355  }
356  }
357 
358  inline void draw(const cubic_bezier<T, 2>& bezier,
359  const std::size_t& point_count) {
360  std::vector<point2d<T> > point_list;
361  wykobi::generate_bezier(bezier, point_count, point_list);
362  draw_polyline(point_list);
363  }
364 
365  inline void draw(const cubic_bezier<T, 3>& bezier,
366  const std::size_t& point_count) {
367  std::vector<point2d<T> > point_list;
368  wykobi::generate_bezier(bezier, point_count, point_list);
369  draw_polyline(point_list);
370  }
371 
372  inline void draw(const quadratic_bezier<T, 2>& bezier,
373  const std::size_t& point_count) {
374  std::vector<point2d<T> > point_list;
375  wykobi::generate_bezier(bezier, point_count, point_list);
376  draw_polyline(point_list);
377  }
378 
379  inline void draw(const quadratic_bezier<T, 3>& bezier,
380  const std::size_t& point_count) {
381  std::vector<point2d<T> > point_list;
382  wykobi::generate_bezier(bezier, point_count, point_list);
383  draw_polyline(point_list);
384  }
385 
386  private:
387  unsigned int _width;
388  unsigned int _height;
389  float red;
390  float green;
391  float blue;
392  GLfloat color[3];
393  DrawingMode drawing_mode;
394 };
395 
396 } // namespace wykobi
397 
398 #endif
Definition: wykobi.hpp:426
T y
Definition: wykobi.hpp:428
T x
Definition: wykobi.hpp:428
T radius
Definition: wykobi.hpp:428
Definition: wykobi.hpp:493
T x
Definition: wykobi.hpp:104
T y
Definition: wykobi.hpp:104
T z
Definition: wykobi.hpp:135
T x
Definition: wykobi.hpp:135
T y
Definition: wykobi.hpp:135
Definition: wykobi.hpp:383
std::size_t size() const
Definition: wykobi.hpp:412
Definition: wykobi.hpp:359
Definition: wykobi.hpp:468
Definition: wykobi.hpp:335
Definition: wykobi.hpp:263
Definition: wykobi.hpp:433
T y
Definition: wykobi.hpp:435
T radius
Definition: wykobi.hpp:435
T x
Definition: wykobi.hpp:435
Definition: wykobi.hpp:311
Definition: wykobi_graphics_opengl.hpp:55
void draw_rectangle(const point2d< T > &point1, const point2d< T > &point2) const
Definition: wykobi_graphics_opengl.hpp:269
void draw(const quadratic_bezier< T, 3 > &bezier, const std::size_t &point_count)
Definition: wykobi_graphics_opengl.hpp:379
void clear()
Definition: wykobi_graphics_opengl.hpp:290
void draw_segment(const T &x1, const T &y1, const T &z1, const T &x2, const T &y2, const T &z2) const
Definition: wykobi_graphics_opengl.hpp:115
void draw_quadix(const T &x1, const T &y1, const T &z1, const T &x2, const T &y2, const T &z2, const T &x3, const T &y3, const T &z3, const T &x4, const T &y4, const T &z4) const
Definition: wykobi_graphics_opengl.hpp:180
void draw_triangle(const point2d< T > &point1, const point2d< T > &point2, const point2d< T > &point3) const
Definition: wykobi_graphics_opengl.hpp:244
void draw(const point2d< T > &point)
Definition: wykobi_graphics_opengl.hpp:292
void draw(const polygon< T, 2 > &polygon, const bool convex=false)
Definition: wykobi_graphics_opengl.hpp:323
void draw_quadix(const point3d< T > &point1, const point3d< T > &point2, const point3d< T > &point3, const point3d< T > &point4) const
Definition: wykobi_graphics_opengl.hpp:262
void draw_pixel(const T &x, const T &y, const T &z) const
Definition: wykobi_graphics_opengl.hpp:101
void draw(const rectangle< T > &rectangle)
Definition: wykobi_graphics_opengl.hpp:299
void draw_pixel(const T &x, const T &y) const
Definition: wykobi_graphics_opengl.hpp:95
void draw_triangle(const point3d< T > &point1, const point3d< T > &point2, const point3d< T > &point3) const
Definition: wykobi_graphics_opengl.hpp:249
void draw(const sphere< T > &sphere)
Definition: wykobi_graphics_opengl.hpp:319
void draw(const circle< T > &circle)
Definition: wykobi_graphics_opengl.hpp:305
void draw(const triangle< T, 3 > &triangle)
Definition: wykobi_graphics_opengl.hpp:313
void draw_polyline(std::vector< point3d< T > > point_list)
Definition: wykobi_graphics_opengl.hpp:284
void draw(const cubic_bezier< T, 2 > &bezier, const std::size_t &point_count)
Definition: wykobi_graphics_opengl.hpp:358
void draw_polyline(std::vector< point2d< T > > point_list)
Definition: wykobi_graphics_opengl.hpp:278
void set_color(const unsigned int &_red, const unsigned int &_green, const unsigned int &_blue)
Definition: wykobi_graphics_opengl.hpp:68
wykobi_graphics_opengl(const unsigned int &w, const unsigned int &h, DrawingMode dm)
Definition: wykobi_graphics_opengl.hpp:57
void draw_quadix(const T &x1, const T &y1, const T &x2, const T &y2, const T &x3, const T &y3, const T &x4, const T &y4) const
Definition: wykobi_graphics_opengl.hpp:160
void draw_sphere(const T &x, const T &y, const T &radius) const
Definition: wykobi_graphics_opengl.hpp:229
void set_color(const unsigned int &color_index)
Definition: wykobi_graphics_opengl.hpp:81
void draw(const segment< T, 2 > &segment)
Definition: wykobi_graphics_opengl.hpp:293
void draw(const cubic_bezier< T, 3 > &bezier, const std::size_t &point_count)
Definition: wykobi_graphics_opengl.hpp:365
void draw_pixel(const point2d< T > &point) const
Definition: wykobi_graphics_opengl.hpp:231
void draw(const point3d< T > &point)
Definition: wykobi_graphics_opengl.hpp:309
void draw_rectangle(const T &x1, const T &y1, const T &x2, const T &y2) const
Definition: wykobi_graphics_opengl.hpp:201
void draw(const segment< T, 3 > &segment)
Definition: wykobi_graphics_opengl.hpp:310
void draw_segment(const T &x1, const T &y1, const T &x2, const T &y2) const
Definition: wykobi_graphics_opengl.hpp:107
void draw_pixel(const point3d< T > &point) const
Definition: wykobi_graphics_opengl.hpp:235
void draw_text(const T &x, const T &y, const std::string &text)
Definition: wykobi_graphics_opengl.hpp:93
void draw_circle(const T &x, const T &y, const T &radius) const
Definition: wykobi_graphics_opengl.hpp:206
void draw(const quadix< T, 3 > &quadix)
Definition: wykobi_graphics_opengl.hpp:316
void draw(const polygon< T, 3 > &polygon)
Definition: wykobi_graphics_opengl.hpp:349
void draw(const triangle< T, 2 > &triangle)
Definition: wykobi_graphics_opengl.hpp:296
void draw_quadix(const point2d< T > &point1, const point2d< T > &point2, const point2d< T > &point3, const point2d< T > &point4) const
Definition: wykobi_graphics_opengl.hpp:255
void draw(const quadix< T, 2 > &quadix)
Definition: wykobi_graphics_opengl.hpp:302
void draw_circle(const point2d< T > &point, const T &radius) const
Definition: wykobi_graphics_opengl.hpp:274
void draw(const quadratic_bezier< T, 2 > &bezier, const std::size_t &point_count)
Definition: wykobi_graphics_opengl.hpp:372
~wykobi_graphics_opengl()
Definition: wykobi_graphics_opengl.hpp:66
void draw_triangle(const T &x1, const T &y1, const T &z1, const T &x2, const T &y2, const T &z2, const T &x3, const T &y3, const T &z3) const
Definition: wykobi_graphics_opengl.hpp:141
void draw_triangle(const T &x1, const T &y1, const T &x2, const T &y2, const T &x3, const T &y3) const
Definition: wykobi_graphics_opengl.hpp:123
void draw_segment(const point2d< T > &point1, const point2d< T > &point2) const
Definition: wykobi_graphics_opengl.hpp:239
Definition: wykobi.hpp:32
polygon< T, D > make_polygon(const InputIterator begin, const InputIterator end)
circle< T > make_circle(const T &x, const T &y, const T &radius)
void generate_bezier(const quadratic_bezier< T, 2 > &bezier, OutputIterator out, const std::size_t &point_count=1000)
const GLfloat basic_color[14][3]
Definition: wykobi_graphics_opengl.hpp:38
DrawingMode
Definition: wykobi_graphics_opengl.hpp:36
@ eNoDraw
Definition: wykobi_graphics_opengl.hpp:36
@ eSolid
Definition: wykobi_graphics_opengl.hpp:36
@ eOutLine
Definition: wykobi_graphics_opengl.hpp:36