src/gl/compiler.cpp

changeset 80
764381756899
parent 78
97c3ce5aa498
child 115
ed884a2fb009
--- 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();
 	}
 }
 

mercurial