1 #pragma once |
1 #pragma once |
2 #include "common.h" |
2 #include "common.h" |
3 |
3 |
4 /** |
4 struct GLAttributeSpec |
5 * @brief Base class for basic shader programs |
|
6 * |
|
7 * A basic program is a collection of a single VAO with a single VBO, |
|
8 * a vertex shader and a fragment shader. This program deals with these |
|
9 * components, leaving the program-specific details to the subclasses. |
|
10 */ |
|
11 class AbstractBasicShaderProgram : public QObject, protected QOpenGLFunctions |
|
12 { |
5 { |
13 Q_OBJECT |
6 GLenum type; |
14 public: |
7 int offset; |
15 AbstractBasicShaderProgram(QObject* parent = nullptr); |
8 int tuplesize; |
16 ~AbstractBasicShaderProgram() = default; |
9 int stride; |
17 void initialize(); |
10 }; |
18 Q_SLOT void setViewMatrix(const glm::mat4& newViewMatrix); |
11 |
19 Q_SLOT void setProjectionMatrix(const glm::mat4& newProjectionMatrix); |
12 //! @brief A collection of a single VAO with a single VBO, |
20 Q_SLOT void setModelMatrix(const glm::mat4& newModelMatrix); |
13 //! a vertex shader and a fragment shader. |
21 void draw(); |
14 class BasicShader final : protected QOpenGLFunctions |
22 void teardown(); |
15 { |
23 protected: |
|
24 void setMatrix(const char* name, const glm::mat4& matrix); |
|
25 void checkForGLErrors(); |
|
26 /// \returns the source code of the vertex shader |
|
27 virtual const char* vertexShaderSource() const = 0; |
|
28 /// \returns the source code of the fragment shader |
|
29 virtual const char* fragmentShaderSource() const = 0; |
|
30 /// \returns the vertex data for the VBO |
|
31 virtual const void* vertexData() const = 0; |
|
32 /// \returns the size of a single vertex in bytes |
|
33 virtual int vertexSize() const = 0; |
|
34 /// \returns the amount of vertices in the data |
|
35 virtual int vertexCount() const = 0; |
|
36 /// Called during initialization to set up the VAO. Set up your vertex array attributes here. |
|
37 virtual void setupVertexArrays() = 0; |
|
38 // \returns what kind of elements are drawn (GL_QUADS, GL_TRIANGLES, GL_LINES, etc) |
|
39 virtual GLenum drawMode() const = 0; |
|
40 virtual QOpenGLBuffer::UsagePattern usagePattern() const = 0; |
|
41 bool isInitialized = false; |
16 bool isInitialized = false; |
42 QOpenGLBuffer buffer; |
17 QOpenGLBuffer buffer; |
43 QOpenGLShader vertexShader; |
18 QOpenGLShader vertexShader; |
44 QOpenGLShader fragmentShader; |
19 QOpenGLShader fragmentShader; |
45 std::optional<gl::ShaderProgram> program{std::nullopt}; |
20 std::unique_ptr<gl::ShaderProgram> program = nullptr; |
46 QOpenGLVertexArrayObject vertexArrayObject; |
21 QOpenGLVertexArrayObject vertexArrayObject; |
|
22 std::size_t vertexCount = 0; |
|
23 public: |
|
24 BasicShader(); |
|
25 ~BasicShader(); |
|
26 Q_DISABLE_COPY(BasicShader) |
|
27 void initialize( |
|
28 const char* vertexShaderSource, |
|
29 const char* fragmentShaderSource, |
|
30 QOpenGLBuffer::UsagePattern usagePattern, |
|
31 const std::vector<GLAttributeSpec>& attributeSpecs); |
|
32 void setMvpMatrix(const glm::mat4& newMvpMatrix); |
|
33 void bufferData(const void* data, std::size_t count, std::size_t size); |
|
34 void draw(GLenum drawMode); |
|
35 void teardown(); |
47 }; |
36 }; |