simplification

Wed, 11 Mar 2020 19:05:34 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 11 Mar 2020 19:05:34 +0200
changeset 80
764381756899
parent 79
5fe2dd4e161a
child 81
62373840e33a

simplification

src/gl/common.h file | annotate | diff | comparison | revisions
src/gl/compiler.cpp file | annotate | diff | comparison | revisions
src/gl/compiler.h file | annotate | diff | comparison | revisions
--- a/src/gl/common.h	Wed Mar 11 17:19:38 2020 +0200
+++ b/src/gl/common.h	Wed Mar 11 19:05:34 2020 +0200
@@ -73,8 +73,7 @@
 		Triangle,
 		Quadrilateral,
 		ConditionalEdge
-	};
-	Type type;
+	} type;
 	glm::vec3 vertices[4];
 	ldraw::Color color;
 	ldraw::id_t id;
@@ -113,6 +112,16 @@
 
 namespace gl
 {
+	constexpr Polygon::Type POLYGON_TYPES[] =
+	{
+		Polygon::Type::EdgeLine,
+		Polygon::Type::Triangle,
+		Polygon::Type::Quadrilateral,
+		Polygon::Type::ConditionalEdge
+	};
+
+	constexpr int NUM_POLYGON_TYPES = countof(POLYGON_TYPES);
+
 	inline Polygon edgeLine(const glm::vec3& v_1, const glm::vec3& v_2, ldraw::Color color, ldraw::id_t id)
 	{
 		return {Polygon::EdgeLine, {v_1, v_2}, color, id};
@@ -159,9 +168,6 @@
 		ConditionalLines
 	};
 
-	constexpr ArrayClass ARRAY_CLASSES[] = {ArrayClass::Lines, ArrayClass::Triangles, ArrayClass::Quads, ArrayClass::ConditionalLines};
-	constexpr int NUM_ARRAY_CLASSES = countof(ARRAY_CLASSES);
-
 	enum class RenderStyle
 	{
 		Normal,
--- a/src/gl/compiler.cpp	Wed Mar 11 17:19:38 2020 +0200
+++ b/src/gl/compiler.cpp	Wed Mar 11 19:05:34 2020 +0200
@@ -171,9 +171,8 @@
 	if (not this->initialized)
 	{
 		this->initializeOpenGLFunctions();
-		for (int i = 0; i < gl::NUM_ARRAY_CLASSES; i += 1)
+		for (auto& object : this->glObjects)
 		{
-			auto& object = this->glObjects[i];
 			object.program = new QOpenGLShaderProgram;
 			gl::buildShaders(object.program, ::vertexShaderSource, ::fragmentShaderSource);
 			object.program->bind();
@@ -186,12 +185,12 @@
 			{
 				object.program->enableAttributeArray(k);
 			}
-			constexpr int stride = sizeof(gl::Vertex);
-			object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(gl::Vertex, position), 3, stride);
-			object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(gl::Vertex, color), 4, stride);
-			object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(gl::Vertex, normal), 3, stride);
-			glVertexAttribIPointer(3, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(gl::Vertex, id)));
-			glVertexAttribIPointer(4, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(gl::Vertex, selected)));
+			constexpr int stride = sizeof(Vertex);
+			object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride);
+			object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 4, stride);
+			object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(Vertex, normal), 3, stride);
+			glVertexAttribIPointer(3, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(Vertex, id)));
+			glVertexAttribIPointer(4, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(Vertex, selected)));
 			object.vertexArray.release();
 			object.buffer.release();
 			object.program->release();
@@ -203,13 +202,13 @@
 void gl::Compiler::build(Model* model, DocumentManager* context, const gl::RenderPreferences& preferences)
 {
 	this->boundingBox = {};
-	std::vector<gl::Vertex> vboData[gl::NUM_ARRAY_CLASSES];
+	std::vector<Vertex> vboData[gl::NUM_POLYGON_TYPES];
 	const std::vector<gl::Polygon> polygons = model->getPolygons(context);
 	for (const gl::Polygon& polygon : polygons)
 	{
 		this->buildPolygon(polygon, vboData, preferences);
 	}
-	for (int arrayId = 0; arrayId < gl::NUM_ARRAY_CLASSES; arrayId += 1)
+	for (int arrayId = 0; arrayId < gl::NUM_POLYGON_TYPES; arrayId += 1)
 	{
 		auto& buffer = this->glObjects[arrayId].buffer;
 		auto& vector = vboData[arrayId];
@@ -244,11 +243,11 @@
 
 void gl::Compiler::buildPolygon(
 	gl::Polygon polygon,
-	std::vector<gl::Vertex>* vboData,
+	std::vector<Vertex>* vboData,
 	const gl::RenderPreferences& preferences)
 {
 	const gl::ArrayClass vboClass = classifyPolygon(polygon);
-	std::vector<gl::Vertex>& vertexBuffer = vboData[static_cast<int>(vboClass)];
+	std::vector<Vertex>& vertexBuffer = vboData[static_cast<int>(vboClass)];
 	auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices());
 	reserveMore(vertexBuffer, polygon.numPolygonVertices());
 	const QColor color = this->getColorForPolygon(polygon, preferences);
@@ -258,7 +257,7 @@
 		const glm::vec3& v2 = vertexRing[i];
 		const glm::vec3& v3 = vertexRing[i + 1];
 		this->boundingBox.consider(polygon.vertices[i]);
-		gl::Vertex& vertex = vertexBuffer.emplace_back();
+		Vertex& vertex = vertexBuffer.emplace_back();
 		vertex.position = polygon.vertices[i];
 		vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2));
 		vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()};
@@ -313,16 +312,17 @@
 
 void gl::Compiler::setSelectedObjects(const QSet<ldraw::id_t> ids)
 {
-	for (int i = 0; i < gl::NUM_ARRAY_CLASSES; i += 1)
+	for (auto& object : this->glObjects)
 	{
-		auto& vector = this->glObjects[i].cachedData;
-		for (gl::Vertex& vertex : vector)
+		std::vector<Vertex>& vector = object.cachedData;
+		for (Vertex& vertex : vector)
 		{
 			vertex.selected = (ids.contains({vertex.id})) ? 1 : 0;
 		}
 		const GLsizeiptr size = static_cast<int>(vector.size() * sizeof vector[0]);
-		this->glObjects[i].buffer.bind();
+		object.buffer.bind();
 		glBufferSubData(GL_ARRAY_BUFFER, 0, size, vector.data());
+		object.buffer.release();
 	}
 }
 
--- a/src/gl/compiler.h	Wed Mar 11 17:19:38 2020 +0200
+++ b/src/gl/compiler.h	Wed Mar 11 19:05:34 2020 +0200
@@ -34,15 +34,6 @@
 namespace gl
 {
 	class Compiler;
-
-	struct Vertex
-	{
-		glm::vec3 position;
-		glm::vec4 color;
-		glm::vec3 normal;
-		glm::int32 id;
-		glm::int32 selected = 0;
-	};
 }
 
 class gl::Compiler : public QObject, protected QOpenGLExtraFunctions
@@ -52,7 +43,6 @@
 	Compiler(const ldraw::ColorTable& colorTable, QObject* parent);
 	~Compiler();
 	void build(Model* model, DocumentManager* context, const RenderPreferences& preferences);
-	void buildPolygon(Polygon polygon, std::vector<Vertex>* vboData, const gl::RenderPreferences& preferences);
 	std::size_t vertexCount(gl::ArrayClass arrayClass) const;
 	QColor getColorForPolygon(const gl::Polygon& polygon, const RenderPreferences& preferences);
 	glm::vec3 modelCenter() const;
@@ -86,7 +76,16 @@
 		this->setUniform(uniformName, *array);
 	}
 private:
-	std::size_t storedVertexCounts[gl::NUM_ARRAY_CLASSES] = {0_z};
+	struct Vertex
+	{
+		glm::vec3 position;
+		glm::vec4 color;
+		glm::vec3 normal;
+		glm::int32 id;
+		glm::int32 selected = 0;
+	};
+	void buildPolygon(Polygon polygon, std::vector<Vertex>* vboData, const gl::RenderPreferences& preferences);
+	std::size_t storedVertexCounts[gl::NUM_POLYGON_TYPES] = {0};
 	bool initialized = false;
 	BoundingBox boundingBox;
 	const ldraw::ColorTable& colorTable;
@@ -97,8 +96,8 @@
 		QOpenGLShaderProgram* pickSceneProgram = nullptr;
 		QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer};
 		QOpenGLVertexArrayObject vertexArray;
-		std::vector<gl::Vertex> cachedData;
-	} glObjects[gl::NUM_ARRAY_CLASSES];
+		std::vector<Vertex> cachedData;
+	} glObjects[gl::NUM_POLYGON_TYPES];
 };
 
 #define CHECK_GL_ERROR() { checkGLError(__FILE__, __LINE__); }

mercurial