src/gl/basicshaderprogram.h

changeset 215
34c6e7bc4ee1
parent 118
8e1c9f18ae15
child 216
c7241f504117
--- a/src/gl/basicshaderprogram.h	Sun Jun 12 20:47:04 2022 +0300
+++ b/src/gl/basicshaderprogram.h	Sun Jun 12 23:59:37 2022 +0300
@@ -1,47 +1,36 @@
 #pragma once
 #include "common.h"
 
-/**
- * @brief Base class for basic shader programs
- *
- * A basic program is a collection of a single VAO with a single VBO,
- * a vertex shader and a fragment shader. This program deals with these
- * components, leaving the program-specific details to the subclasses.
- */
-class AbstractBasicShaderProgram : public QObject, protected QOpenGLFunctions
+struct GLAttributeSpec
 {
-	Q_OBJECT
-public:
-	AbstractBasicShaderProgram(QObject* parent = nullptr);
-	~AbstractBasicShaderProgram() = 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 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;
-	/// \returns the vertex data for the VBO
-	virtual const void* vertexData() const = 0;
-	/// \returns the size of a single vertex in bytes
-	virtual int vertexSize() const = 0;
-	/// \returns the amount of vertices in the data
-	virtual int vertexCount() const = 0;
-	/// Called during initialization to set up the VAO. Set up your vertex array attributes here.
-	virtual void setupVertexArrays() = 0;
-	// \returns what kind of elements are drawn (GL_QUADS, GL_TRIANGLES, GL_LINES, etc)
-	virtual GLenum drawMode() const = 0;
-	virtual QOpenGLBuffer::UsagePattern usagePattern() const = 0;
+	GLenum type;
+	int offset;
+	int tuplesize;
+	int stride;
+};
+
+//! @brief A collection of a single VAO with a single VBO,
+//! a vertex shader and a fragment shader.
+class BasicShader final : protected QOpenGLFunctions
+{
 	bool isInitialized = false;
 	QOpenGLBuffer buffer;
 	QOpenGLShader vertexShader;
 	QOpenGLShader fragmentShader;
-	std::optional<gl::ShaderProgram> program{std::nullopt};
+	std::unique_ptr<gl::ShaderProgram> program = nullptr;
 	QOpenGLVertexArrayObject vertexArrayObject;
+	std::size_t vertexCount = 0;
+public:
+	BasicShader();
+	~BasicShader();
+	Q_DISABLE_COPY(BasicShader)
+	void initialize(
+		const char* vertexShaderSource,
+		const char* fragmentShaderSource,
+		QOpenGLBuffer::UsagePattern usagePattern,
+		const std::vector<GLAttributeSpec>& attributeSpecs);
+	void setMvpMatrix(const glm::mat4& newMvpMatrix);
+	void bufferData(const void* data, std::size_t count, std::size_t size);
+	void draw(GLenum drawMode);
+	void teardown();
 };

mercurial