src/gl/abstractshaderprogram.h

changeset 102
9f435f66bd0c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/gl/abstractshaderprogram.h	Wed Feb 17 16:49:35 2021 +0200
@@ -0,0 +1,48 @@
+#pragma once
+#include "common.h"
+
+/**
+ * @brief Base class for shader programs
+ */
+class AbstractShaderProgram : public QObject, protected QOpenGLFunctions
+{
+	Q_OBJECT
+public:
+	struct ArraySpecification
+	{
+		GLenum drawMode;
+		std::size_t vertexSize;
+		QOpenGLBuffer::UsagePattern bufferUsagePattern = QOpenGLBuffer::StaticDraw;
+	};
+	struct Array
+	{
+		const ArraySpecification specification;
+		QOpenGLBuffer buffer;
+		QOpenGLVertexArrayObject vertexArrayObject;
+		int count;
+	};
+	AbstractShaderProgram(const QVector<ArraySpecification>& arraySpecifications, QObject* parent = nullptr);
+	~AbstractShaderProgram() = default;
+	void initialize();
+	Q_SLOT void setViewMatrix(const glm::mat4& newViewMatrix);
+	Q_SLOT void setProjectionMatrix(const glm::mat4& newProjectionMatrix);
+	Q_SLOT void setModelMatrix(const glm::mat4& newModelMatrix);
+	void draw();
+	void teardown();
+protected:
+	void setMatrix(const char* name, const glm::mat4& matrix);
+	void upload(Array* array, const void* data, int count);
+	void checkForGLErrors();
+	/// \returns the source code of the vertex shader
+	virtual const char* vertexShaderSource() const = 0;
+	/// \returns the source code of the fragment shader
+	virtual const char* fragmentShaderSource() const = 0;
+	/// Called during initialization to set up the VAO. Set up your vertex array attributes here.
+	virtual void setupVertexArrays() = 0;
+
+	bool isInitialized = false;
+	QOpenGLShader vertexShader;
+	QOpenGLShader fragmentShader;
+	std::optional<gl::ShaderProgram> program{std::nullopt};
+	QVector<Array> arrays;
+};

mercurial