src/gl/abstractshaderprogram.h

Wed, 17 Feb 2021 16:49:35 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 17 Feb 2021 16:49:35 +0200
changeset 102
9f435f66bd0c
permissions
-rw-r--r--

stuff

#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