src/gl/gridprogram.cpp

changeset 70
f21b800b02a4
parent 64
f99d52b1646b
child 72
7c27cda03747
--- a/src/gl/gridprogram.cpp	Fri Mar 06 16:08:53 2020 +0200
+++ b/src/gl/gridprogram.cpp	Fri Mar 06 20:13:10 2020 +0200
@@ -64,68 +64,11 @@
 
 static const glm::vec2 data[] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}};
 
-GridProgram::GridProgram(QObject* parent) :
-	QObject{parent},
-	buffer{QOpenGLBuffer::VertexBuffer},
-	vertexShader{QOpenGLShader::Vertex},
-	fragmentShader{QOpenGLShader::Fragment}
-{
-}
-
-void GridProgram::initialize()
-{
-	if (not isInitialized)
-	{
-		this->initializeOpenGLFunctions();
-		this->isInitialized = true;
-		this->program.emplace(this);
-		gl::buildShaders(&*this->program, ::vertexShaderSource, ::fragmentShaderSource);
-		this->program->bind();
-		this->buffer.create();
-		this->buffer.bind();
-		this->buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
-		this->buffer.allocate(data, countof(data) * sizeof data[0]);
-		this->vertexArrayObject.create();
-		this->vertexArrayObject.bind();
-		this->program->enableAttributeArray(0);
-		this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
-		this->program->setUniformVector("gridColor", this->gridColor);
-		this->vertexArrayObject.release();
-		this->buffer.release();
-		this->program->release();
-		this->checkForGLErrors();
-	}
-}
-
-void GridProgram::setViewMatrix(const glm::mat4& newViewMatrix)
-{
-	this->setMatrix("view", newViewMatrix);
-}
-
-void GridProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix)
-{
-	this->setMatrix("projection", newProjectionMatrix);
-}
-
-void GridProgram::setModelMatrix(const glm::mat4& newModelMatrix)
-{
-	this->setMatrix("model", newModelMatrix);
-}
-
 void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix)
 {
 	this->setMatrix("grid", newGridMatrix);
 }
 
-void GridProgram::setMatrix(const char* name, const glm::mat4& matrix)
-{
-	Q_ASSERT(this->isInitialized);
-	this->program->bind();
-	this->program->setUniformMatrix(name, matrix);
-	this->program->release();
-	this->checkForGLErrors();
-}
-
 void GridProgram::setGridColor(const QColor& newGridColor)
 {
 	const glm::vec4 vec = gl::colorToVector4(newGridColor);
@@ -142,24 +85,39 @@
 	}
 }
 
-void GridProgram::draw()
+const char* GridProgram::vertexShaderSource() const
+{
+	return ::vertexShaderSource;
+}
+
+const char* GridProgram::fragmentShaderSource() const
 {
-	this->program->bind();
-	this->vertexArrayObject.bind();
-	glDrawArrays(GL_QUADS, 0, countof(data));
-	this->vertexArrayObject.release();
-	this->program->release();
-	this->checkForGLErrors();
+	return ::fragmentShaderSource;
+}
+
+const void* GridProgram::vertexData() const
+{
+	return ::data;
 }
 
-void GridProgram::teardown()
+int GridProgram::vertexSize() const
 {
-	this->vertexArrayObject.destroy();
-	this->buffer.destroy();
-	this->program.reset();
+	return sizeof data[0];
+}
+
+int GridProgram::vertexCount() const
+{
+	return countof(data);
 }
 
-void GridProgram::checkForGLErrors()
+void GridProgram::setupVertexArrays()
 {
-	gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent()));
+	this->program->enableAttributeArray(0);
+	this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0);
+	this->program->setUniformVector("gridColor", this->gridColor);
 }
+
+GLenum GridProgram::drawMode() const
+{
+	return GL_QUADS;
+}

mercurial