Wed, 17 Feb 2021 16:49:35 +0200
stuff
| 102 | 1 | #pragma once | 
| 2 | #include "common.h" | |
| 3 | ||
| 4 | /** | |
| 5 | * @brief Base class for shader programs | |
| 6 | */ | |
| 7 | class AbstractShaderProgram : public QObject, protected QOpenGLFunctions | |
| 8 | { | |
| 9 | Q_OBJECT | |
| 10 | public: | |
| 11 | struct ArraySpecification | |
| 12 | { | |
| 13 | GLenum drawMode; | |
| 14 | std::size_t vertexSize; | |
| 15 | QOpenGLBuffer::UsagePattern bufferUsagePattern = QOpenGLBuffer::StaticDraw; | |
| 16 | }; | |
| 17 | struct Array | |
| 18 | { | |
| 19 | const ArraySpecification specification; | |
| 20 | QOpenGLBuffer buffer; | |
| 21 | QOpenGLVertexArrayObject vertexArrayObject; | |
| 22 | int count; | |
| 23 | }; | |
| 24 | AbstractShaderProgram(const QVector<ArraySpecification>& arraySpecifications, QObject* parent = nullptr); | |
| 25 | ~AbstractShaderProgram() = default; | |
| 26 | void initialize(); | |
| 27 | Q_SLOT void setViewMatrix(const glm::mat4& newViewMatrix); | |
| 28 | Q_SLOT void setProjectionMatrix(const glm::mat4& newProjectionMatrix); | |
| 29 | Q_SLOT void setModelMatrix(const glm::mat4& newModelMatrix); | |
| 30 | void draw(); | |
| 31 | void teardown(); | |
| 32 | protected: | |
| 33 | void setMatrix(const char* name, const glm::mat4& matrix); | |
| 34 | void upload(Array* array, const void* data, int count); | |
| 35 | void checkForGLErrors(); | |
| 36 | /// \returns the source code of the vertex shader | |
| 37 | virtual const char* vertexShaderSource() const = 0; | |
| 38 | /// \returns the source code of the fragment shader | |
| 39 | virtual const char* fragmentShaderSource() const = 0; | |
| 40 | /// Called during initialization to set up the VAO. Set up your vertex array attributes here. | |
| 41 | virtual void setupVertexArrays() = 0; | |
| 42 | ||
| 43 | bool isInitialized = false; | |
| 44 | QOpenGLShader vertexShader; | |
| 45 | QOpenGLShader fragmentShader; | |
| 46 | std::optional<gl::ShaderProgram> program{std::nullopt}; | |
| 47 | QVector<Array> arrays; | |
| 48 | }; |