RayZaler 0.1
The free opto-mechanical simulation framework
GLHelpers.h
1//
2// Copyright (c) 2024 Gonzalo José Carracedo Carballal
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Lesser General Public License as
6// published by the Free Software Foundation, either version 3 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Lesser General Public License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public
15// License along with this program. If not, see
16// <http://www.gnu.org/licenses/>
17//
18
19#ifndef _GLHELPERS_H
20#define _GLHELPERS_H
21
22#include <GL/glu.h>
23#include <vector>
24#include <Vector.h>
25#include <ReferenceFrame.h>
26
27namespace RZ {
28 struct Vec3;
29
31 GLfloat m_params[4];
32
33 inline GLfloat *
34 get(GLfloat x, GLfloat y, GLfloat z, GLfloat t = 1.)
35 {
36 m_params[0] = x;
37 m_params[1] = y;
38 m_params[2] = z;
39 m_params[3] = t;
40
41 return m_params;
42 }
43 };
44
45 class GLShader {
46 int m_id = -1;
47 bool m_initialized = false;
48 bool checkBuildErrors(unsigned int shader, std::string const &type);
49
50public:
51 GLShader(const char * , const char *);
52 ~GLShader();
53
54 inline int
55 program() const
56 {
57 return m_id;
58 }
59
60 void use();
61 void leave();
62
63 void set(std::string const &, bool) const;
64 void set(std::string const &, int) const;
65 void set(std::string const &, unsigned int) const;
66 void set(std::string const &, Vec3 const &) const;
67 void set(std::string const &, Real) const;
68};
69
70 void GLCube(GLfloat, bool wireFrame = false);
71
72 struct GLPrimitive {
73 virtual void display() = 0;
74 };
75
76 class GLCone : public GLPrimitive {
77 GLUquadric *m_quadric = nullptr;
78 GLdouble m_base;
79 GLdouble m_height;
80 GLint m_slices;
81 GLint m_stacks;
82
83 public:
84 GLCone();
85 ~GLCone();
86
87 void setBase(GLdouble);
88 void setHeight(GLdouble);
89 void setSlices(GLint);
90 void setStacks(GLint);
91 virtual void display() override;
92 };
93
94 class GLAbstractCap : public GLPrimitive {
95 public:
96 virtual void requestRecalc();
97 virtual const std::vector<GLfloat> *edge() const = 0;
98 };
99
100 class GLDisc : public GLAbstractCap {
101 GLUquadric *m_quadric = nullptr;
102 bool m_dirty = true;
103 std::vector<GLfloat> m_vertices;
104 std::vector<GLfloat> m_normals;
105 std::vector<GLfloat> m_texCoords;
106 std::vector<GLfloat> m_edge;
107 std::vector<GLint> m_indices;
108
109 GLint m_slices = 32;
110 GLdouble m_width = 1;
111 GLdouble m_height = 1;
112 bool m_invertNormals = false;
113
114 void recalculate();
115
116 public:
117 GLdouble
118 width() const
119 {
120 return m_width;
121 }
122
123 GLdouble
124 height() const
125 {
126 return m_height;
127 }
128
129 virtual void requestRecalc() override;
130 void setInverted(bool);
131 void setRadius(GLdouble);
132 void setWidth(GLdouble);
133 void setHeight(GLdouble);
134 void setSlices(GLint);
135
136 const std::vector<GLfloat> *edge() const override;
137 virtual void display() override;
138
139 GLDisc();
140 ~GLDisc();
141 };
142
143 class GLRing : public GLPrimitive {
144 GLUquadric *m_quadric = nullptr;
145 bool m_dirty = true;
146 std::vector<GLfloat> m_vertices;
147 std::vector<GLfloat> m_normals;
148 std::vector<GLfloat> m_texCoords;
149 std::vector<GLint> m_indices;
150
151 GLint m_slices = 32;
152 GLdouble m_innerRadius = .5;
153 GLdouble m_outerRadius = 1.;
154
155 void recalculate();
156
157 public:
158 GLdouble
159 innerRadius() const
160 {
161 return m_innerRadius;
162 }
163
164 GLdouble
165 outerRadius() const
166 {
167 return m_outerRadius;
168 }
169
170 void setInnerRadius(GLdouble);
171 void setOuterRadius(GLdouble);
172 void setSlices(GLint);
173
174 virtual void display() override;
175
176 GLRing();
177 ~GLRing();
178 };
179
181 GLUquadric *m_quadric = nullptr;
182 bool m_dirty = true;
183
184 GLDisc m_topDiscCap, m_bottomDiscCap;
185 GLAbstractCap *m_topCap = nullptr;
186 GLAbstractCap *m_bottomCap = nullptr;
187
188 std::vector<GLfloat> m_strip;
189 std::vector<GLfloat> m_normals;
190
191 bool m_drawTop = false;
192 bool m_drawBase = false;
193 bool m_invertNormals = false;
194 GLdouble m_height = 1.;
195 GLdouble m_radius = .25;
196 GLint m_slices = 64;
197
198 void recalculateCaps();
199
200 public:
201 GLdouble
202 height() const
203 {
204 return m_height;
205 }
206
207 GLdouble
208 radius() const
209 {
210 return m_radius;
211 }
212
213 void setCaps(GLAbstractCap *top, GLAbstractCap *bottom);
214 void setHeight(GLdouble);
215 void setRadius(GLdouble);
216 void setSlices(GLint);
217 void setVisibleCaps(bool, bool);
218 void setInvertNormals(bool);
219 virtual void display() override;
220
223 };
224
225 class GLTube : public GLPrimitive {
226 GLUquadric *m_outerQuadric = nullptr;
227 GLUquadric *m_innerQuadric = nullptr;
228 bool m_dirty = true;
229
230 GLRing m_topCap, m_bottomCap;
231
232 bool m_drawTop = false;
233 bool m_drawBase = false;
234 GLdouble m_height = 1.;
235 GLdouble m_innerRadius = .125;
236 GLdouble m_outerRadius = .25;
237 GLint m_slices = 32;
238
239 void recalculateCaps();
240
241 public:
242 GLdouble
243 height() const
244 {
245 return m_height;
246 }
247
248 GLdouble
249 innerRadius() const
250 {
251 return m_innerRadius;
252 }
253
254 GLdouble
255 outerRadius() const
256 {
257 return m_outerRadius;
258 }
259
260 void setHeight(GLdouble);
261 void setInnerRadius(GLdouble);
262 void setOuterRadius(GLdouble);
263 void setSlices(GLint);
264 void setVisibleCaps(bool, bool);
265
266 virtual void display() override;
267
268 GLTube();
269 ~GLTube();
270 };
271
273 GLUquadric *m_quadric = nullptr;
274 bool m_dirty = true;
275 std::vector<GLfloat> m_vertices;
276 std::vector<GLfloat> m_normals;
277 std::vector<GLfloat> m_texCoords;
278 std::vector<GLint> m_indices;
279 std::vector<GLfloat> m_edge;
280
281 GLdouble m_rCurv = 1;
282 GLdouble m_radius = .25;
283 GLdouble m_x0 = 0;
284 GLdouble m_y0 = 0;
285 GLint m_sectors = 32;
286 GLint m_stacks = 8;
287 bool m_invertNormals = false;
288
289 void recalculate();
290
291 public:
292 GLdouble
293 curvatureRadius() const
294 {
295 return m_rCurv;
296 }
297
298 GLdouble
299 radius() const
300 {
301 return m_radius;
302 }
303
304 void setCenterOffset(GLdouble, GLdouble);
305 void setCurvatureRadius(GLdouble);
306 void setRadius(GLdouble);
307 void setSectors(GLint);
308 void setStacks(GLint);
309 void setInvertNormals(bool);
310
311 virtual void display() override;
312 virtual const std::vector<GLfloat> *edge() const override;
313 virtual void requestRecalc() override;
314
317 };
318
320 GLUquadric *m_quadric = nullptr;
321 bool m_dirty = true;
322 std::vector<GLfloat> m_vertices;
323 std::vector<GLfloat> m_normals;
324 std::vector<GLfloat> m_texCoords;
325 std::vector<GLint> m_indices;
326 std::vector<GLfloat> m_edge;
327
328 GLdouble m_flength = 2;
329 GLdouble m_radius = .25;
330 GLdouble m_x0 = 0;
331 GLdouble m_y0 = 0;
332 GLint m_sectors = 64;
333 GLint m_stacks = 8;
334 bool m_invertNormals = false;
335
336 void recalculate();
337
338 public:
339 GLdouble
340 fnum() const
341 {
342 return m_flength;
343 }
344
345 GLdouble
346 radius() const
347 {
348 return m_radius;
349 }
350
351 void setCenterOffset(GLdouble, GLdouble);
352 void setFocalLength(GLdouble);
353 void setRadius(GLdouble);
354 void setSectors(GLint);
355 void setStacks(GLint);
356 void setInvertNormals(bool);
357
358 virtual const std::vector<GLfloat> *edge() const override;
359 virtual void requestRecalc() override;
360 virtual void display() override;
361
364 };
365
366 class GLConicCap : public GLAbstractCap {
367 GLUquadric *m_quadric = nullptr;
368 bool m_dirty = true;
369 std::vector<GLfloat> m_vertices;
370 std::vector<GLfloat> m_normals;
371 std::vector<GLfloat> m_texCoords;
372 std::vector<GLint> m_indices;
373 std::vector<GLfloat> m_edge;
374
375 GLdouble m_rCurv = 1;
376 GLdouble m_K = 0;
377 bool m_convex = false;
378 GLdouble m_radius = .25;
379 GLdouble m_x0 = 0;
380 GLdouble m_y0 = 0;
381 GLdouble m_rHole = 0;
382 GLint m_sectors = 64;
383 GLint m_stacks = 8;
384 bool m_invertNormals = false;
385
386 void recalculate();
387
388 public:
389 GLdouble
390 fnum() const
391 {
392 return m_rCurv / 2;
393 }
394
395 GLdouble
396 radius() const
397 {
398 return m_radius;
399 }
400
401 void setCenterOffset(GLdouble, GLdouble);
402 void setConicConstant(GLdouble);
403 void setCurvatureRadius(GLdouble);
404 void setConvex(bool);
405
406 void setRadius(GLdouble);
407 void setHoleRadius(GLdouble);
408 void setSectors(GLint);
409 void setStacks(GLint);
410 void setInvertNormals(bool);
411
412 virtual const std::vector<GLfloat> *edge() const override;
413 virtual void requestRecalc() override;
414 virtual void display() override;
415
416 GLConicCap();
417 ~GLConicCap();
418 };
419
420 class GLPinHole : public GLPrimitive {
421 GLUquadric *m_quadric = nullptr;
422 bool m_dirty = true;
423 std::vector<GLfloat> m_vertices;
424 std::vector<GLfloat> m_normals;
425
426 GLint m_slices = 32;
427 GLdouble m_radius = .25;
428 GLdouble m_width = 1;
429 GLdouble m_height = 1;
430
431 void recalculate();
432
433 public:
434 GLdouble
435 radius() const
436 {
437 return m_radius;
438 }
439
440 GLdouble
441 width() const
442 {
443 return m_width;
444 }
445
446 GLdouble
447 height() const
448 {
449 return m_height;
450 }
451
452 void setRadius(GLdouble);
453 void setWidth(GLdouble);
454 void setHeight(GLdouble);
455 void setSlices(GLint);
456
457 virtual void display() override;
458
459 GLPinHole();
460 ~GLPinHole();
461 };
462
463 class GLRectangle : public GLPrimitive {
464 bool m_dirty = true;
465 std::vector<GLfloat> m_vertices;
466
467 GLdouble m_width = 1;
468 GLdouble m_height = 1;
469
470 void recalculate();
471
472 public:
473 GLdouble
474 width() const
475 {
476 return m_width;
477 }
478
479 GLdouble
480 height() const
481 {
482 return m_height;
483 }
484
485 void setWidth(GLdouble);
486 void setHeight(GLdouble);
487
488 virtual void display() override;
489
490 GLRectangle();
491 ~GLRectangle();
492 };
493
494
496 GLCappedCylinder m_axisCylinder;
497 GLCone m_axisArrow;
498
499 GLfloat m_height = 1e-1;
500 GLfloat m_radius = 5e-3;
501 GLfloat m_arrowHeight = 1e-1 / 3;
502 GLfloat m_arrowBase = 1e-2;
503
504
505 public:
506 void setHeight(GLfloat);
507 void setRadius(GLfloat);
508 void setArrowHeight(GLfloat);
509 void setArrowBase(GLfloat);
510
511 GLfloat
512 height() const
513 {
514 return m_height;
515 }
516
517 GLfloat
518 radius() const
519 {
520 return m_radius;
521 }
522
523 GLfloat
524 arrowHeight() const
525 {
526 return m_arrowHeight;
527 }
528
529 GLfloat
530 arrowBase() const
531 {
532 return m_arrowBase;
533 }
534
535 virtual void display() override;
536
539 };
540
541 class GLArrow : public GLPrimitive {
542 Vec3 m_direction;
543 GLfloat m_thickness = 2;
544
545 public:
546 void setThickness(GLfloat thickness);
547 void setDirection(Vec3 const &vec);
548
549 virtual void display() override;
550
551 GLArrow();
552 ~GLArrow();
553 };
554
555 class GLText : public GLPrimitive {
556 unsigned int m_texId;
557 std::vector<uint32_t> m_texture;
558 std::string m_text;
559 std::string m_face;
560 GLfloat m_vertices[6][2];
561 unsigned int m_texWidth, m_texHeight;
562 unsigned int m_fontSize = 48;
563
564 GLfloat m_scale = 1e-3;
565 GLfloat m_color[4] = {1, 1, 1, 1};
566
567 unsigned int m_size = 48;
568
569 bool m_haveTexture = false;
570 bool m_texLoaded = false;
571 bool m_needsReload = false;
572
573 void composeTexture();
574 void reloadTexture();
575
576 public:
577 ~GLText();
578
579 void setSize(unsigned);
580 void setScale(GLfloat);
581 void setColor(GLfloat, GLfloat, GLfloat, GLfloat = 1);
582 void setText(std::string const &);
583 void setFace(std::string const &);
584
585 virtual void display() override;
586 };
587
588 class GLGrid : public GLPrimitive {
589 std::vector<GLfloat> m_vertices;
590 std::vector<GLfloat> m_hlVertices;
591 GLfloat m_gridColor[4] = {1, 1, 1, 1};
592 GLfloat m_hlColor[4] = {1, 0, 0, 1};
593
594 unsigned m_stepsX = 10;
595 unsigned m_stepsY = 10;
596 Real m_step = 0.1;
597 GLfloat m_thickness = 1;
598 GLfloat m_x = 0;
599 GLfloat m_y = 0;
600
601 void recalculateHighlight();
602 void recalculate();
603
604 public:
605 inline GLfloat
606 width() const
607 {
608 return m_stepsX * m_step;
609 }
610
611 inline GLfloat
612 height() const
613 {
614 return m_stepsY * m_step;
615 }
616
617 inline GLfloat
618 step() const
619 {
620 return m_step;
621 }
622
623 virtual void display() override;
624
625 void setGridColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1);
626 void setHighlightColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1);
627
628 void setStepsX(unsigned);
629 void setStepsY(unsigned);
630 void setStep(Real);
631 void setThickness(GLfloat);
632
633 void highlight(GLfloat x, GLfloat y);
634
635 GLGrid();
636 ~GLGrid();
637 };
638};
639
640#endif // _GLHELPERS_H
Definition: GLHelpers.h:94
Definition: GLHelpers.h:541
Definition: GLHelpers.h:180
Definition: GLHelpers.h:76
Definition: GLHelpers.h:366
Definition: GLHelpers.h:100
Definition: GLHelpers.h:588
Definition: GLHelpers.h:319
Definition: GLHelpers.h:420
Definition: GLHelpers.h:463
Definition: GLHelpers.h:495
Definition: GLHelpers.h:143
Definition: GLHelpers.h:45
Definition: GLHelpers.h:272
Definition: GLHelpers.h:555
Definition: GLHelpers.h:225
Definition: GLHelpers.h:72
Definition: GLHelpers.h:30
Definition: Vector.h:66