shader.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include <QMatrix4x4>
  3. #include <QOpenGLFunctions_3_3_Core>
  4. #include <QString>
  5. #include <QVector2D>
  6. #include <string>
  7. #include <unordered_map>
  8. namespace Render::GL {
  9. class Shader : protected QOpenGLFunctions_3_3_Core {
  10. public:
  11. using UniformHandle = GLint;
  12. static constexpr UniformHandle InvalidUniform = -1;
  13. Shader();
  14. ~Shader() override;
  15. auto load_from_files(const QString &vertex_path,
  16. const QString &fragment_path) -> bool;
  17. auto load_from_source(const QString &vertex_source,
  18. const QString &fragment_source) -> bool;
  19. void use();
  20. void release();
  21. auto uniform_handle(const char *name) -> UniformHandle;
  22. auto optional_uniform_handle(const char *name) -> UniformHandle;
  23. void set_uniform(UniformHandle handle, float value);
  24. void set_uniform(UniformHandle handle, const QVector3D &value);
  25. void set_uniform(UniformHandle handle, const QVector2D &value);
  26. void set_uniform(UniformHandle handle, const QMatrix4x4 &value);
  27. void set_uniform(UniformHandle handle, int value);
  28. void set_uniform(UniformHandle handle, bool value);
  29. void set_uniform(const char *name, float value);
  30. void set_uniform(const char *name, const QVector3D &value);
  31. void set_uniform(const char *name, const QVector2D &value);
  32. void set_uniform(const char *name, const QMatrix4x4 &value);
  33. void set_uniform(const char *name, int value);
  34. void set_uniform(const char *name, bool value);
  35. void set_uniform(const QString &name, float value);
  36. void set_uniform(const QString &name, const QVector3D &value);
  37. void set_uniform(const QString &name, const QVector2D &value);
  38. void set_uniform(const QString &name, const QMatrix4x4 &value);
  39. void set_uniform(const QString &name, int value);
  40. void set_uniform(const QString &name, bool value);
  41. private:
  42. GLuint m_program = 0;
  43. auto compile_shader(const QString &source, GLenum type) -> GLuint;
  44. auto link_program(GLuint vertex_shader, GLuint fragment_shader) -> bool;
  45. std::unordered_map<std::string, UniformHandle> m_uniform_cache;
  46. };
  47. } // namespace Render::GL