CalcMySky  v0.3.1
util.hpp
1 #ifndef INCLUDE_ONCE_C49956E1_F7B6_4759_8745_711BBDFE6FE7
2 #define INCLUDE_ONCE_C49956E1_F7B6_4759_8745_711BBDFE6FE7
3 
4 #include <string>
5 #include <iostream>
6 #include <string_view>
7 #include <QVector4D>
8 #include <QOpenGLFunctions_3_3_Core>
9 #include <glm/glm.hpp>
10 #include "data.hpp"
11 #include "../common/util.hpp"
12 
13 extern QOpenGLFunctions_3_3_Core gl;
14 
15 inline QVector4D QVec(glm::vec4 v) { return QVector4D(v.x, v.y, v.z, v.w); }
16 inline QString toString(int x) { return QString::number(x); }
17 inline QString toString(double x) { return QString::number(x, 'g', 17); }
18 inline QString toString(float x) { return QString::number(x, 'g', 9); }
19 inline QString toString(glm::vec2 v) { return QString("vec2(%1,%2)").arg(double(v.x), 0,'g',9)
20  .arg(double(v.y), 0,'g',9); }
21 inline QString toString(glm::vec3 v) { return QString("vec3(%1,%2,%3)").arg(double(v.x), 0,'g',9)
22  .arg(double(v.y), 0,'g',9)
23  .arg(double(v.z), 0,'g',9); }
24 inline QString toString(glm::vec4 v) { return QString("vec4(%1,%2,%3,%4)").arg(double(v.x), 0,'g',9)
25  .arg(double(v.y), 0,'g',9)
26  .arg(double(v.z), 0,'g',9)
27  .arg(double(v.w), 0,'g',9); }
28 inline QString toString(glm::mat4 const& m) { return QString("mat4(%1,%2,%3,%4, %5,%6,%7,%8, %9,%10,%11,%12, %13,%14,%15,%16)")
29  .arg(double(m[0][0]),0,'g',9).arg(double(m[0][1]),0,'g',9).arg(double(m[0][2]),0,'g',9).arg(double(m[0][3]),0,'g',9)
30  .arg(double(m[1][0]),0,'g',9).arg(double(m[1][1]),0,'g',9).arg(double(m[1][2]),0,'g',9).arg(double(m[1][3]),0,'g',9)
31  .arg(double(m[2][0]),0,'g',9).arg(double(m[2][1]),0,'g',9).arg(double(m[2][2]),0,'g',9).arg(double(m[2][3]),0,'g',9)
32  .arg(double(m[3][0]),0,'g',9).arg(double(m[3][1]),0,'g',9).arg(double(m[3][2]),0,'g',9).arg(double(m[3][3]),0,'g',9); }
33 inline QMatrix4x4 toQMatrix(glm::mat4 const& m) { return QMatrix4x4(&m[0][0]).transposed(); }
34 void setupDebugPrintCallback(QOpenGLContext& context, bool needFullDebugOutput);
35 void setupTexture(TextureId id, GLsizei width, GLsizei height);
36 void setupTexture(TextureId id, GLsizei width, GLsizei height, GLsizei depth);
37 void setupTexture(GLuint tex, GLsizei width, GLsizei height, GLsizei depth);
38 inline void setUniformTexture(QOpenGLShaderProgram& program, GLenum target, GLuint texture, GLint sampler, const char* uniformName)
39 {
40  gl.glActiveTexture(GL_TEXTURE0+sampler);
41  gl.glBindTexture(target, texture);
42  program.setUniformValue(uniformName,sampler);
43 }
44 inline void setUniformTexture(QOpenGLShaderProgram& program, GLenum target, TextureId id, GLint sampler, const char* uniformName)
45 { setUniformTexture(program, target, textures[id], sampler, uniformName); }
46 
47 inline void setDrawBuffers(std::vector<GLenum> const& bufs)
48 {
49  gl.glDrawBuffers(GLsizei(bufs.size()), bufs.data());
50 }
51 
52 void renderQuad();
53 inline void checkFramebufferStatus(const char*const fboDescription) { return checkFramebufferStatus(gl, fboDescription); }
54 void qtMessageHandler(const QtMsgType type, QMessageLogContext const&, QString const& message);
55 DEFINE_EXPLICIT_BOOL(ReturnTextureData);
56 std::vector<glm::vec4> saveTexture(GLenum target, GLuint texture, std::string_view name, std::string_view path,
57  std::vector<int> const& sizes, ReturnTextureData=ReturnTextureData{false});
58 void createDirs(std::string const& path);
59 
61 {
62  inline static unsigned outputIndent=0;
63  friend std::string indentOutput();
64 public:
65  OutputIndentIncrease() { ++outputIndent; }
66  ~OutputIndentIncrease() { --outputIndent; }
67 };
68 inline std::string indentOutput()
69 {
70  return std::string(OutputIndentIncrease::outputIndent, ' ');
71 }
72 
73 #define OPENGL_DEBUG_CHECK_ERROR(errorMessage) \
74 do { \
75  if(opts.openglDebug) \
76  { \
77  if(const auto err=gl.glGetError(); err!=GL_NO_ERROR) \
78  { \
79  std::cerr << errorMessage << ":" << openglErrorString(err) << "\n"; \
80  throw MustQuit{}; \
81  } \
82  } \
83 } while(0)
84 
85 #endif
Definition: util.hpp:60