src/gl/basicshaderprogram.h

Tue, 27 Jul 2021 12:44:54 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 27 Jul 2021 12:44:54 +0300
changeset 116
aad3e897bc32
parent 75
204dc77e5654
child 102
9f435f66bd0c
child 118
8e1c9f18ae15
permissions
-rw-r--r--

refactor

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

mercurial