Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2015 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OMNI_VISUAL_UTIL_H_
21 #define OMNI_VISUAL_UTIL_H_
22 
23 #include <math.h>
24 #include <QDebug>
25 #include <QPointF>
26 #include <QRectF>
27 #include <QVector2D>
28 #include <QVector3D>
29 #include <QOpenGLFunctions>
30 #include <omni/util.h>
33 
34 class QOpenGLShaderProgram;
35 
36 namespace omni {
37  namespace visual {
38  namespace util {
39  /// Get current time in nano seconds
40  double now();
41 
42 
43  template<typename F>
44  void for_each_circle_point(size_t _numVertices, float _radius, F _f)
45  {
46  for (size_t i = 0; i < _numVertices; ++i)
47  {
48  float _m = 2.0 * M_PI * float(i) / _numVertices;
49  float _cos = cos(_m), _sin = sin(_m);
50  _f(i, QPointF(_cos * _radius, _sin * _radius));
51  }
52  }
53 
54 
55  /// Calculate view rectangle on 2D OpenGL surface
56  QRectF viewRect(int _imageWidth,
57  int _imageHeight,
58  int _canvasWidth,
59  int _canvasHeight,
60  float _border = 0.0);
61 
62  template<typename F>
63  void for_each_arc_point(size_t _numVertices,
64  float _radius,
65  float _beginAngle,
66  float _endAngle,
67  F _f)
68  {
69  float _cos = cos(_beginAngle), _sin = sin(_beginAngle);
70  size_t i = 0;
71 
72  _f(0, QPointF(_cos * _radius, _sin * _radius));
73 
74  for (; i < _numVertices; ++i)
75  {
76  float _m = _beginAngle + float(i) / _numVertices *
77  (_endAngle - _beginAngle);
78  _cos = cos(_m), _sin = sin(_m);
79  _f(i, QPointF(_cos * _radius, _sin * _radius));
80  }
81  _cos = cos(_endAngle), _sin = sin(_endAngle);
82  _f(i, QPointF(_cos * _radius, _sin * _radius));
83  }
84 
85 
86  /// Calculates the aspect ratio from a QSize
87  template<typename SIZE>
88  qreal aspect(SIZE const& _size)
89  {
90  return _size.width() / qreal(_size.height());
91  }
92 
93  /// Reset openGL state to its defaults
94  void resetOpenGLState();
95 
96 
97  /// Convenience function for handling glGetError()
98  void checkOpenGLError();
99 
100  /// Draw into QOpenGLFramebufferObject with given projection and model
101  // view operations
102  template<typename FRAMEBUFFER, typename PROJECTION, typename MODELVIEW>
103  void draw_on_framebuffer(FRAMEBUFFER* _f, PROJECTION _p, MODELVIEW _m)
104  {
105  withCurrentContext([&](QOpenGLFunctions& _) {
106  _f->bind();
107  _.glViewport(0, 0, _f->width(), _f->height());
108 // _.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
109 // GL_STENCIL_BUFFER_BIT);
110 
111  glMatrixMode(GL_TEXTURE);
112  glLoadIdentity();
113 
114  // Projection matrix setup
115  glMatrixMode(GL_PROJECTION);
116  glLoadIdentity();
117  _p(_); // Projection operation
118 
119  // Model view matrix setup
120  glMatrixMode(GL_MODELVIEW);
121  glLoadIdentity();
122 
123  _m(_); // ModelView operation
124 
125  _f->release();
126  });
127  }
128 
129  /// Set viewport for widget
130  template<typename WIDGET>
131  void viewport(WIDGET *_widget)
132  {
133  withCurrentContext([&_widget](QOpenGLFunctions& _)
134  {
135  int d = _widget->devicePixelRatio();
136  _.glViewport(0, 0, _widget->width() * d, _widget->height() * d);
137  });
138  }
139  }
140 
144  using util::viewport;
145  }
146 }
147 
148 
149 #endif /* OMNI_VISUAL_UTIL_H_ */
void draw_on_framebuffer(FRAMEBUFFER *_f, PROJECTION _p, MODELVIEW _m)
Draw into QOpenGLFramebufferObject with given projection and model.
Definition: util.h:103
void resetOpenGLState()
Reset openGL state to its defaults.
Definition: util.cpp:86
void for_each_arc_point(size_t _numVertices, float _radius, float _beginAngle, float _endAngle, F _f)
Definition: util.h:63
void withCurrentContext(ContextFunctor f)
Do OpenGL operations with current context, if it exists.
Definition: ContextSwitch.cpp:45
qreal aspect(SIZE const &_size)
Calculates the aspect ratio from a QSize.
Definition: util.h:88
QRectF viewRect(int _imageWidth, int _imageHeight, int _canvasWidth, int _canvasHeight, float _border)
Calculate view rectangle on 2D OpenGL surface.
Definition: util.cpp:60
void for_each_circle_point(size_t _numVertices, float _radius, F _f)
Definition: util.h:44
double now()
Get current time in nano seconds.
Definition: util.cpp:33
void checkOpenGLError()
Convenience function for handling glGetError()
Definition: util.cpp:39
void viewport(WIDGET *_widget)
Set viewport for widget.
Definition: util.h:131