CalcMySky  v0.3.1
util.hpp
1 #ifndef INCLUDE_ONCE_E28E88C6_7992_4205_828C_8E04CC339B83
2 #define INCLUDE_ONCE_E28E88C6_7992_4205_828C_8E04CC339B83
3 
4 #include <iostream>
5 #include <glm/glm.hpp>
6 #include <QString>
7 #include <QVector3D>
8 #include <QVector4D>
9 #include <QGenericMatrix>
10 #include <QOpenGLFunctions_3_3_Core>
11 #include "../ShowMySky/api/ShowMySky/Exception.hpp"
12 
13 #define DEFINE_EXPLICIT_BOOL(Type) \
14 struct Type \
15 { \
16  bool on=true; \
17  explicit Type()=default; \
18  explicit Type(bool on) : on(on) {} \
19  operator bool() const { return on; } \
20 }
21 
22 template<typename T> auto sqr(T const& x) { return x*x; }
23 inline QVector3D toQVector(glm::dvec3 const& v) { return QVector3D(v.x, v.y, v.z); }
24 inline QVector3D toQVector(glm::vec3 const& v) { return QVector3D(v.x, v.y, v.z); }
25 inline QVector4D toQVector(glm::dvec4 const& v) { return QVector4D(v.x, v.y, v.z, v.w); }
26 inline QVector4D toQVector(glm::vec4 const& v) { return QVector4D(v.x, v.y, v.z, v.w); }
27 inline QMatrix3x3 toQMatrix(glm::mat3 const& m) { return QMatrix3x3(&transpose(m)[0][0]); }
28 
29 struct MustQuit{ int exitCode=1; };
30 
32 {
33  QString message;
34 public:
35  InitializationError(QString const& message) : message(message) {}
36  QString errorType() const override { return QObject::tr("Initialization error"); }
37  QString what() const override { return message; }
38 };
39 
41 {
42  QString message;
43 public:
44  OpenGLError(QString const& message) : message(message) {}
45  QString errorType() const override { return QObject::tr("OpenGL error"); }
46  QString what() const override { return message; }
47 };
48 
50 {
51  QString message;
52 public:
53  DataLoadError(QString const& message) : message(message) {}
54  QString errorType() const override { return QObject::tr("Error loading data"); }
55  QString what() const override { return message; }
56 };
57 
59 {
60  QString message;
61 public:
62  BadCommandLine(QString const& message) : message(message) {}
63  QString errorType() const override { return QObject::tr("Bad command line"); }
64  QString what() const override { return message; }
65 };
66 
68 {
69  QString message;
70  QString filename;
71  int lineNumber;
72 public:
73  ParsingError(QString const& filename, int lineNumber, QString const& message)
74  : message(message), filename(filename), lineNumber(lineNumber) {}
75  QString errorType() const override { return QObject::tr("Parsing error"); }
76  QString what() const override { return QString("%1:%2: %3").arg(filename).arg(lineNumber).arg(message); }
77 };
78 
79 inline std::ostream& operator<<(std::ostream& os, QString const& s)
80 {
81  os << s.toStdString();
82  return os;
83 }
84 
85 void checkFramebufferStatus(QOpenGLFunctions_3_3_Core& gl, const char* fboDescription);
86 
87 // Function useful only for debugging
88 void dumpActiveUniforms(QOpenGLFunctions_3_3_Core& gl, GLuint program);
89 
90 std::string openglErrorString(GLenum error);
91 
93 {
94 #ifdef Q_OS_WIN
95  UINT oldConsoleCP;
96 public:
97  UTF8Console()
98  : oldConsoleCP(GetConsoleOutputCP())
99  {
100  SetConsoleOutputCP(65001); // UTF-8 console
101  }
102  ~UTF8Console()
103  {
104  restore();
105  }
106  void restore()
107  {
108  SetConsoleOutputCP(oldConsoleCP);
109  }
110 #else
111  void restore() {}
112 #endif
113 };
114 
115 // XXX: keep in sync with the same function in texture-coordinates.frag
116 inline float unitRangeToTexCoord(const float u, const int texSize)
117 {
118  return (0.5+(texSize-1)*u)/texSize;
119 }
120 
121 // XXX: keep in sync with the same function in texture-coordinates.frag
122 inline float texCoordToUnitRange(const float texCoord, const float texSize)
123 {
124  return (texSize*texCoord-0.5)/(texSize-1);
125 }
126 
127 // XXX: keep in sync with the same function in common-functions.frag
128 template<typename Number>
129 Number clampCosine(const Number x)
130 {
131  return std::clamp(x, Number(-1), Number(1));
132 }
133 
134 glm::mat4 radianceToLuminance(unsigned texIndex, std::vector<glm::vec4> const& allWavelengths);
135 
136 // Rounds each float to \p precision bits.
137 void roundTexData(GLfloat* data, size_t size, int precision);
138 
139 inline int roundDownToClosestPowerOfTwo(const int x)
140 {
141  if(x==0) return 1;
142  int shift=0;
143  for(auto v=x;v;v>>=1)
144  ++shift;
145  return 1<<(shift-1);
146 }
147 
148 #endif
Definition: util.hpp:58
Definition: util.hpp:40
Definition: util.hpp:31
An error that ShowMySky classes may throw.
Definition: Exception.hpp:25
QString errorType() const override
A string suitable for use as a title of a message box.
Definition: util.hpp:36
QString what() const override
A description of the error.
Definition: util.hpp:37
QString errorType() const override
A string suitable for use as a title of a message box.
Definition: util.hpp:75
QString errorType() const override
A string suitable for use as a title of a message box.
Definition: util.hpp:63
Definition: util.hpp:29
QString errorType() const override
A string suitable for use as a title of a message box.
Definition: util.hpp:45
QString what() const override
A description of the error.
Definition: util.hpp:46
Definition: util.hpp:49
QString what() const override
A description of the error.
Definition: util.hpp:64
QString what() const override
A description of the error.
Definition: util.hpp:55
Definition: util.hpp:92
Definition: util.hpp:67
QString what() const override
A description of the error.
Definition: util.hpp:76
QString errorType() const override
A string suitable for use as a title of a message box.
Definition: util.hpp:54