commit work done on plugging vao to the gl renderer, renders nonsense for now

Wed, 22 Jan 2020 01:17:11 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 22 Jan 2020 01:17:11 +0200
changeset 27
c57fb7a5ffa3
parent 26
3a9e761e4faa
child 28
c92c1daf735f

commit work done on plugging vao to the gl renderer, renders nonsense for now

locale/fi.ts file | annotate | diff | comparison | revisions
src/gl/compiler.cpp file | annotate | diff | comparison | revisions
src/gl/compiler.h file | annotate | diff | comparison | revisions
src/gl/partrenderer.cpp file | annotate | diff | comparison | revisions
src/gl/partrenderer.h file | annotate | diff | comparison | revisions
--- a/locale/fi.ts	Wed Jan 22 00:23:29 2020 +0200
+++ b/locale/fi.ts	Wed Jan 22 01:17:11 2020 +0200
@@ -88,27 +88,27 @@
 <context>
     <name>LibraryManager</name>
     <message>
-        <location filename="../src/libraries.cpp" line="210"/>
+        <location filename="../src/libraries.cpp" line="248"/>
         <source>Official library</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/libraries.cpp" line="212"/>
+        <location filename="../src/libraries.cpp" line="250"/>
         <source>Unofficial library</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/libraries.cpp" line="214"/>
+        <location filename="../src/libraries.cpp" line="252"/>
         <source>Working library</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/libraries.cpp" line="273"/>
+        <location filename="../src/libraries.cpp" line="311"/>
         <source>Path</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/libraries.cpp" line="275"/>
+        <location filename="../src/libraries.cpp" line="313"/>
         <source>Role</source>
         <translation type="unfinished"></translation>
     </message>
@@ -161,27 +161,27 @@
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="44"/>
+        <location filename="../src/mainwindow.cpp" line="62"/>
         <source>Open model</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="46"/>
+        <location filename="../src/mainwindow.cpp" line="64"/>
         <source>LDraw models (*.ldr *.dat)</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="65"/>
+        <location filename="../src/mainwindow.cpp" line="83"/>
         <source>Problem loading references</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="75"/>
+        <location filename="../src/mainwindow.cpp" line="93"/>
         <source>Problem opening file</source>
         <translation type="unfinished"></translation>
     </message>
     <message>
-        <location filename="../src/mainwindow.cpp" line="77"/>
+        <location filename="../src/mainwindow.cpp" line="95"/>
         <source>Could not open %1: %2</source>
         <translation type="unfinished"></translation>
     </message>
@@ -189,7 +189,7 @@
 <context>
     <name>PartRenderer</name>
     <message>
-        <location filename="../src/gl/partrenderer.cpp" line="149"/>
+        <location filename="../src/gl/partrenderer.cpp" line="182"/>
         <source>Rendering error</source>
         <translation type="unfinished"></translation>
     </message>
--- a/src/gl/compiler.cpp	Wed Jan 22 00:23:29 2020 +0200
+++ b/src/gl/compiler.cpp	Wed Jan 22 01:17:11 2020 +0200
@@ -25,6 +25,33 @@
 #include "invert.h"
 #include "ring.h"
 
+static const char* vertexShaderSource = R"(
+#version 330 core
+
+layout(location=0) in vec3 position;
+layout(location=1) in vec4 color;
+out vec4 vColor;
+uniform mat4 CameraTransformation;
+
+void main()
+{
+	vColor = color;
+	gl_Position = projection * modelview * CameraTransformation * vec4(position, 1.0);
+}
+)";
+
+static const char* fragmentShaderSource = R"(
+#version 330 core
+
+in vec4 vColor;
+out vec4 fColor;
+
+void main()
+{
+	fColor = vColor;
+}
+)";
+
 gl::Compiler::Compiler(const ColorTable& colorTable, QObject* parent) :
 	QObject{parent},
 	colorTable{colorTable}
@@ -33,15 +60,6 @@
 
 gl::Compiler::~Compiler()
 {
-	if (this->initialized)
-	{
-		for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
-		{
-			glDeleteProgram(this->glObjects[arrayId].program);
-			glDeleteShader(this->glObjects[arrayId].vertexShader);
-			glDeleteProgram(this->glObjects[arrayId].fragmentShader);
-		}
-	}
 }
 
 void gl::Compiler::initialize()
@@ -49,16 +67,28 @@
 	if (not this->initialized)
 	{
 		this->initializeOpenGLFunctions();
-		for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
+		for (int i = 0; i < gl::NUM_ARRAY_CLASSES; i += 1)
 		{
-			QOpenGLVertexArrayObject& vao = this->vertexArrays[arrayId];
-			vao.create();
-			vao.bind();
-			glGenBuffers(gl::numVboSubclasses, &this->storedVbo[gl::numVboSubclasses * arrayId]);
-			this->buildShaders(arrayId);
-			vao.release();
+			auto& object = this->glObjects[i];
+			object.program = new QOpenGLShaderProgram;
+			object.program->create();
+			object.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource);
+			object.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource);
+			object.program->link();
+			object.program->bind();
+			object.buffer.create();
+			object.buffer.bind();
+			object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
+			object.vertexArray.create();
+			object.vertexArray.bind();
+			object.program->enableAttributeArray(0);
+			object.program->enableAttributeArray(1);
+			object.program->setAttributeBuffer(0, GL_FLOAT, 0, 3);
+			object.program->setAttributeBuffer(1, GL_FLOAT, 3, 4);
+			object.vertexArray.release();
+			object.buffer.release();
+			object.program->release();
 		}
-		//glGenBuffers(countof(this->storedVbo), &this->storedVbo[0]);
 		this->initialized = true;
 	}
 }
@@ -66,26 +96,20 @@
 void gl::Compiler::build(Model* model, DocumentManager* context)
 {
 	this->boundingBox = {};
-	std::vector<GLfloat> vboData[gl::numVbos];
+	std::vector<GLfloat> vboData[gl::NUM_ARRAY_CLASSES];
 	const std::vector<gl::Polygon> polygons = model->getPolygons(context);
 	for (const gl::Polygon& polygon : polygons)
 	{
 		this->buildPolygon(polygon, vboData);
 	}
-	/*
-	for (int index = 0; index < gl::numVbos; index += 1)
-	{
-		this->upload(index, vboData[index]);
-	}*/
 	for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
 	{
-		this->vertexArrays[arrayId].bind();
-		for (int i = 0; i < gl::numVboSubclasses; i += 1)
-		{
-			const int vboIndex = gl::vboIndex({static_cast<gl::ArrayClass>(arrayId), static_cast<gl::VboSubclass>(i)});
-			this->upload(vboIndex, vboData[vboIndex]);
-		}
-		this->vertexArrays[arrayId].release();
+		auto& buffer = this->glObjects[arrayId].buffer;
+		auto& vector = vboData[arrayId];
+		this->storedVboSizes[arrayId] = vector.size();
+		buffer.bind();
+		buffer.allocate(vector.data(), static_cast<int>(vector.size() * sizeof vector[0]));
+		buffer.release();
 	}
 }
 
@@ -119,7 +143,9 @@
 void gl::Compiler::buildPolygon(gl::Polygon polygon, std::vector<GLfloat>* vboData)
 {
 	const gl::ArrayClass vboClass = classifyPolygon(polygon);
-	std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})];
+	//std::vector<GLfloat>& vertexBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::VertexData})];
+	std::vector<GLfloat>& vertexBuffer = vboData[static_cast<int>(vboClass)];
+	/*
 	std::vector<GLfloat>& normalsBuffer = vboData[gl::vboIndex({vboClass, gl::VboSubclass::Normals})];
 	auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
 	reserveMore(normalsBuffer, polygon.numPolygonVertices() * 3_z);
@@ -132,6 +158,7 @@
 		for (const GLfloat coord : {normal.x(), normal.y(), normal.z()})
 			normalsBuffer.push_back(coord);
 	}
+	*/
 	reserveMore(vertexBuffer, polygon.numPolygonVertices() * 7);
 	const QColor color = this->getColorForPolygon(polygon);
 	// Transform vertices so that they're suitable for GL rendering
@@ -184,74 +211,21 @@
 
 void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass)
 {
-	this->vertexArrays[static_cast<int>(arrayClass)].bind();
-	glUseProgram(this->glObjects[static_cast<int>(arrayClass)].program);
+	auto& object = this->glObjects[static_cast<int>(arrayClass)];
+	object.vertexArray.bind();
+	object.program->bind();
 }
 
 void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass)
 {
-	this->vertexArrays[static_cast<int>(arrayClass)].release();
+	auto& object = this->glObjects[static_cast<int>(arrayClass)];
+	object.program->release();
+	object.vertexArray.release();
 }
 
-void gl::Compiler::buildShaders(int arrayId)
+std::size_t gl::Compiler::vboSize(const gl::ArrayClass arrayClass) const
 {
-	/*
-	this->glObjects[arrayId].vertexShader = glCreateShader(GL_VERTEX_SHADER);
-	glShaderSource(this->glObjects[arrayId].vertexShader, 1, &vertexShaderSource, nullptr);
-	glCompileShader(this->glObjects[arrayId].vertexShader);
-	this->glObjects[arrayId].fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
-	glShaderSource(this->glObjects[arrayId].fragmentShader, 1, &fragmentShaderSource, nullptr);
-	glCompileShader(this->glObjects[arrayId].fragmentShader);
-	for (auto&& pair : {
-		std::make_pair(this->glObjects[arrayId].vertexShader, tr("vertex shader")),
-		std::make_pair(this->glObjects[arrayId].fragmentShader, tr("fragment shader")),
-	})
-	{
-		GLint status;
-		glGetShaderiv(this->glObjects[arrayId].fragmentShader, GL_COMPILE_STATUS, &status);
-		if (status != GL_TRUE)
-		{
-			char compileLog[512];
-			glGetShaderInfoLog(pair.first, countof(compileLog), nullptr, compileLog);
-			QMessageBox::critical(nullptr, tr("Shader compile error"), tr("Unable to compile the %1. Compile log:\n\n%2").arg(pair.second).arg(compileLog));
-			abort();
-		}
-	}
-	this->glObjects[arrayId].program = glCreateProgram();
-	glAttachShader(this->glObjects[arrayId].program, this->glObjects[arrayId].vertexShader);
-	glAttachShader(this->glObjects[arrayId].program, this->glObjects[arrayId].fragmentShader);
-	glLinkProgram(this->glObjects[arrayId].program);
-	glUseProgram(this->glObjects[arrayId].program);
-	const std::size_t size = gl::FLOATS_PER_VERTEX * sizeof(GLfloat);
-	const GLuint posAttrib = static_cast<GLuint>(glGetAttribLocation(this->glObjects[arrayId].program, "position"));
-	glEnableVertexAttribArray(posAttrib);
-	glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, size, gl::offset(0));
-	const GLuint colAttrib = static_cast<GLuint>(glGetAttribLocation(this->glObjects[arrayId].program, "color"));
-	glEnableVertexAttribArray(colAttrib);
-	glVertexAttribPointer(colAttrib, 4, GL_FLOAT, GL_FALSE, size, gl::offset(3 * sizeof(GLfloat)));
-	*/
-}
-
-///
-/// \brief Uploads data to a vbo
-/// \param vboAddress Which vbo to upload to
-/// \param data Data to upload to vbo
-///
-void gl::Compiler::upload(const int vboIndex, const std::vector<GLfloat>& data)
-{
-	glBindBuffer(GL_ARRAY_BUFFER, this->storedVbo[vboIndex]);
-	glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof data[0]), data.data(), GL_STATIC_DRAW);
-	this->storedVboSizes[vboIndex] = data.size();
-}
-
-GLuint gl::Compiler::vbo(const VboAddress vboAddress) const
-{
-	return this->storedVbo[gl::vboIndex(vboAddress)];
-}
-
-std::size_t gl::Compiler::vboSize(const VboAddress vboAddress) const
-{
-	return this->storedVboSizes[gl::vboIndex(vboAddress)];
+	return this->storedVboSizes[static_cast<int>(arrayClass)];
 }
 
 int gl::vboIndex(const VboAddress vboAddress)
--- a/src/gl/compiler.h	Wed Jan 22 00:23:29 2020 +0200
+++ b/src/gl/compiler.h	Wed Jan 22 01:17:11 2020 +0200
@@ -49,9 +49,7 @@
 	~Compiler();
 	void build(Model* model, DocumentManager* context);
 	void buildPolygon(Polygon polygon, std::vector<GLfloat>* vboData);
-	void upload(const int vboIndex, const std::vector<GLfloat>& data);
-	GLuint vbo(const VboAddress vboAddress) const;
-	std::size_t vboSize(const VboAddress vboAddress) const;
+	std::size_t vboSize(gl::ArrayClass arrayClass) const;
 	QColor getColorForPolygon(const gl::Polygon& polygon);
 	Point3D modelCenter() const;
 	double modelDistance() const;
@@ -59,9 +57,20 @@
 	void bindVertexArray(gl::ArrayClass arrayClass);
 	void releaseVertexArray(gl::ArrayClass arrayClass);
 	void buildShaders(int arrayId);
+	template<typename T>
+	void setUniform(const char* uniformName, T&& value)
+	{
+		Q_ASSERT(this->initialized);
+		for (auto& object : this->glObjects)
+		{
+			object.program->bind();
+			const int location = glGetUniformLocation(object.program->programId(), uniformName);
+			Q_ASSERT(location != -1);
+			object.program->setUniformValue(location, std::forward<T>(value));
+			object.program->release();
+		}
+	}
 private:
-	QOpenGLVertexArrayObject vertexArrays[gl::NUM_ARRAY_CLASSES];
-	//GLuint storedVbo[gl::numVbos];
 	bool m_vboChanged[gl::numVbos] = {true};
 	std::size_t storedVboSizes[gl::numVbos] = {0_z};
 	bool initialized = false;
--- a/src/gl/partrenderer.cpp	Wed Jan 22 00:23:29 2020 +0200
+++ b/src/gl/partrenderer.cpp	Wed Jan 22 01:17:11 2020 +0200
@@ -21,34 +21,6 @@
 #include <QMessageBox>
 #include "partrenderer.h"
 
-
-static const char* vertexShaderSource = R"(
-#version 330 core
-
-layout(location=0) in vec3 position;
-layout(location=1) in vec4 color;
-out vec4 vColor;
-uniform mat4 CameraTransformation;
-
-void main()
-{
-	vColor = color;
-	gl_Position = CameraTransformation * vec4(position, 1.0);
-}
-)";
-
-static const char* fragmentShaderSource = R"(
-#version 330 core
-
-in vec4 vColor;
-out vec4 fColor;
-
-void main()
-{
-	fColor = vColor;
-}
-)";
-
 PartRenderer::PartRenderer(Model* model, DocumentManager* documents, const ColorTable& colorTable, QWidget* parent) :
 	QOpenGLWidget{parent},
 	model{model},
@@ -61,7 +33,6 @@
 
 PartRenderer::~PartRenderer()
 {
-	delete this->objects.program;
 }
 
 void PartRenderer::initializeGL()
@@ -73,62 +44,13 @@
 	}
 	glEnableClientState(GL_NORMAL_ARRAY);
 	glEnableClientState(GL_VERTEX_ARRAY);
-	//this->compiler->initialize();
-	//this->compiler->build(this->model, this->documents);
+	this->compiler->initialize();
+	this->compiler->build(this->model, this->documents);
 	this->initializeLighting();
 	this->initialized = true;
 	this->rotation = QQuaternion::fromAxisAndAngle({1, 0, 0}, 30);
 	this->rotation *= QQuaternion::fromAxisAndAngle({0, 1, 0}, 330);
 	glLineWidth(2.0);
-	this->objects.program = new QOpenGLShaderProgram;
-	this->objects.program->create();
-	this->checkForGLErrors();
-	this->objects.program->addShaderFromSourceCode(QOpenGLShader::Vertex, ::vertexShaderSource);
-	this->checkForGLErrors();
-	this->objects.program->addShaderFromSourceCode(QOpenGLShader::Fragment, ::fragmentShaderSource);
-	this->checkForGLErrors();
-	this->objects.program->link();
-	this->checkForGLErrors();
-	this->objects.program->bind();
-	this->checkForGLErrors();
-	this->objects.buffer.create();
-	this->checkForGLErrors();
-	this->objects.buffer.bind();
-	this->checkForGLErrors();
-	this->objects.buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
-	this->checkForGLErrors();
-	/*
-	GLfloat data[] = {
-		20.0f, 20.0f, 6.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-		30.0f, 20.0f, 6.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-		30.0f, 30.0f, 6.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-	};
-	*/
-	GLfloat data[] = {
-		0.00f, 0.75f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-		-0.75f, -0.75f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-		0.75f, -0.75f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f
-	};
-	this->objects.buffer.allocate(data, sizeof data);
-	this->checkForGLErrors();
-	this->objects.vertexArray.create();
-	this->checkForGLErrors();
-	this->objects.vertexArray.bind();
-	this->checkForGLErrors();
-	this->objects.program->enableAttributeArray(0);
-	this->checkForGLErrors();
-	this->objects.program->enableAttributeArray(1);
-	this->checkForGLErrors();
-	this->objects.program->setAttributeBuffer(0, GL_FLOAT, 0, 3);
-	this->checkForGLErrors();
-	this->objects.program->setAttributeBuffer(1, GL_FLOAT, 3, 4);
-	this->checkForGLErrors();
-	this->objects.vertexArray.release();
-	this->checkForGLErrors();
-	this->objects.buffer.release();
-	this->checkForGLErrors();
-	this->objects.program->release();
-	this->checkForGLErrors();
 }
 
 /*
@@ -227,38 +149,21 @@
 		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 		break;
 	}
-	this->objects.program->bind();
-	this->checkForGLErrors();
-	const int cameraTransformationUniform = glGetUniformLocation(this->objects.program->programId(), "CameraTransformation");
-	this->checkForGLErrors();
-	this->objects.program->setUniformValue(cameraTransformationUniform, rotationMatrix);
-	this->checkForGLErrors();
-	this->objects.vertexArray.bind();
-	this->checkForGLErrors();
-	glDrawArrays(GL_TRIANGLES, 0, 3);
-	this->checkForGLErrors();
-	this->objects.vertexArray.release();
-	this->checkForGLErrors();
-	this->objects.program->release();
-	this->checkForGLErrors();
-#if 0
+	this->compiler->setUniform("CameraTransformation", rotationMatrix);
 	// Lines need to be rendered last so that anti-aliasing does not interfere with polygon rendering.
 	renderVao(gl::ArrayClass::Triangles);
 	renderVao(gl::ArrayClass::Quads);
 	renderVao(gl::ArrayClass::Lines);
-#endif
 	glDisable(GL_POLYGON_OFFSET_FILL);
 }
 
-void PartRenderer::renderVao(const gl::ArrayClass /*arrayClass*/)
+void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
 {
-	/*
 	this->compiler->bindVertexArray(arrayClass);
-	const std::size_t vertexCount = this->compiler->vboSize({arrayClass, gl::VboSubclass::VertexData}) / gl::FLOATS_PER_VERTEX;
+	const std::size_t vertexCount = this->compiler->vboSize(arrayClass) / gl::FLOATS_PER_VERTEX;
 	glDrawArrays(getGlTypeForArrayClass(arrayClass), 0, static_cast<GLsizei>(vertexCount));
 	this->compiler->releaseVertexArray(arrayClass);
 	this->checkForGLErrors();
-	*/
 }
 
 void PartRenderer::checkForGLErrors()
--- a/src/gl/partrenderer.h	Wed Jan 22 00:23:29 2020 +0200
+++ b/src/gl/partrenderer.h	Wed Jan 22 01:17:11 2020 +0200
@@ -33,12 +33,6 @@
 	QQuaternion rotation;
 	gl::Compiler* compiler;
 	gl::RenderStyle renderStyle = gl::RenderStyle::Normal;
-	struct
-	{
-		QOpenGLShaderProgram* program = nullptr;
-		QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer};
-		QOpenGLVertexArrayObject vertexArray;
-	} objects;
 	bool initialized = false;
 	void initializeLighting();
 	void renderVao(const gl::ArrayClass arrayClass);

mercurial