src/gl/basicshaderprogram.cpp

changeset 377
e1c5e4310f8b
parent 264
76a025db4948
--- a/src/gl/basicshaderprogram.cpp	Tue Apr 11 22:39:18 2023 +0300
+++ b/src/gl/basicshaderprogram.cpp	Tue Apr 11 22:51:12 2023 +0300
@@ -18,94 +18,77 @@
 
 #include "src/gl/basicshaderprogram.h"
 
-BasicShader::BasicShader() :
-	buffer{QOpenGLBuffer::VertexBuffer},
-	vertexShader{QOpenGLShader::Vertex},
-	fragmentShader{QOpenGLShader::Fragment}
-{
-}
-
-BasicShader::~BasicShader()
-{
-	if (this->isInitialized) {
-		this->teardown();
-	}
-}
-
-void BasicShader::initialize(
+void gl::initialize_basic_shader(
+	gl::basic_shader* shader,
 	const char* vertexShaderSource,
 	const char* fragmentShaderSource,
 	QOpenGLBuffer::UsagePattern usagePattern,
 	const std::vector<GLAttributeSpec>& attributeSpecs)
 {
-	if (not this->isInitialized)
-	{
-		this->isInitialized = true;
-		this->program = std::make_unique<gl::ShaderProgram>();
-		gl::buildShaders(this->program.get(), vertexShaderSource, fragmentShaderSource);
-		this->program->bind();
-		this->buffer.create();
-		this->buffer.bind();
-		this->buffer.setUsagePattern(usagePattern);
-		this->vertexArrayObject.create();
-		this->vertexArrayObject.bind();
-		for (std::size_t i = 0; i < attributeSpecs.size(); ++i) {
-			const auto& spec = attributeSpecs[i];
-			const int attr = narrow<int>(signed_cast(i));
-			this->program->enableAttributeArray(attr);
-			this->program->setAttributeBuffer(attr, spec.type, spec.offset, spec.tuplesize, spec.stride);
-		}
-		this->vertexArrayObject.release();
-		this->buffer.release();
-		this->program->release();
-		gl::checkForGLErrors(nullptr);
+	shader->program = std::make_unique<gl::ShaderProgram>();
+	gl::buildShaders(shader->program.get(), vertexShaderSource, fragmentShaderSource);
+	shader->program->bind();
+	shader->buffer.create();
+	shader->buffer.bind();
+	shader->buffer.setUsagePattern(usagePattern);
+	shader->vertexArrayObject.create();
+	shader->vertexArrayObject.bind();
+	for (std::size_t i = 0; i < attributeSpecs.size(); ++i) {
+		const auto& spec = attributeSpecs[i];
+		const int attr = narrow<int>(signed_cast(i));
+		shader->program->enableAttributeArray(attr);
+		shader->program->setAttributeBuffer(attr, spec.type, spec.offset, spec.tuplesize, spec.stride);
 	}
+	shader->vertexArrayObject.release();
+	shader->buffer.release();
+	shader->program->release();
+	gl::checkForGLErrors(nullptr);
 }
 
-void BasicShader::setMvpMatrix(const glm::mat4& newMvpMatrix)
+void gl::set_shader_matrix_uniform(
+	gl::basic_shader* shader,	
+	const char* name,
+	const glm::mat4& value)
 {
-	this->setUniformMatrix("mvp", newMvpMatrix);
-}
-
-void BasicShader::setUniformMatrix(const char* name, const glm::mat4& value)
-{
-	Q_ASSERT(this->isInitialized);
-	this->program->bind();
-	this->program->setUniformMatrix(name, value);
-	this->program->release();
+	Q_ASSERT(shader->program != nullptr);
+	shader->program->bind();
+	shader->program->setUniformMatrix(name, value);
+	shader->program->release();
 	gl::checkForGLErrors(nullptr);
 }
 
-void BasicShader::setUniformVector(const char* name, const glm::vec4& value)
+void gl::set_shader_vector_uniform(
+	gl::basic_shader* shader,
+	const char* name,
+	const glm::vec4& value)
 {
-	Q_ASSERT(this->isInitialized);
-	this->program->bind();
-	this->program->setUniformVector(name, value);
-	this->program->release();
+	Q_ASSERT(shader->program != nullptr);
+	shader->program->bind();
+	shader->program->setUniformVector(name, value);
+	shader->program->release();
 	gl::checkForGLErrors(nullptr);
 }
 
-void BasicShader::bufferData(const void* data, std::size_t count, std::size_t size)
+void gl::buffer_shader_data(
+	gl::basic_shader* shader,
+	const void* data,
+	std::size_t count,
+	std::size_t size)
 {
-	this->buffer.bind();
-	this->buffer.allocate(data, narrow<int>(signed_cast(count * size)));
-	this->buffer.release();
-	this->vertexCount = narrow<int>(signed_cast(count));
+	shader->buffer.bind();
+	shader->buffer.allocate(data, narrow<int>(signed_cast(count * size)));
+	shader->buffer.release();
+	shader->vertexCount = narrow<int>(signed_cast(count));
 }
 
-void BasicShader::draw(GLenum drawMode)
+void gl::draw_shader(
+	gl::basic_shader* shader,
+	GLenum drawMode)
 {
-	this->program->bind();
-	this->vertexArrayObject.bind();
-	glDrawArrays(drawMode, 0, this->vertexCount);
-	this->vertexArrayObject.release();
-	this->program->release();
+	shader->program->bind();
+	shader->vertexArrayObject.bind();
+	glDrawArrays(drawMode, 0, shader->vertexCount);
+	shader->vertexArrayObject.release();
+	shader->program->release();
 	gl::checkForGLErrors(nullptr);
 }
-
-void BasicShader::teardown()
-{
-	this->vertexArrayObject.destroy();
-	this->buffer.destroy();
-	this->program.reset();
-}

mercurial