src/gl/partrenderer.cpp

changeset 61
4585d8d7a7ec
parent 60
0f221121849b
child 66
77c819262b7a
--- a/src/gl/partrenderer.cpp	Thu Feb 27 14:38:58 2020 +0200
+++ b/src/gl/partrenderer.cpp	Thu Feb 27 22:46:39 2020 +0200
@@ -34,8 +34,7 @@
 	model{model},
 	documents{documents},
 	colorTable{colorTable},
-	compiler{new gl::Compiler{this->colorTable, this}},
-	gridProgram{this}
+	compiler{new gl::Compiler{this->colorTable, this}}
 {
 	this->setMouseTracking(true);
 }
@@ -46,7 +45,11 @@
 
 static QVector3D vec3FromQColor(const QColor& color)
 {
-	return {(float)color.redF(), (float)color.greenF(), (float)color.blueF()};
+	return {
+		toFloat(color.redF()),
+		toFloat(color.greenF()),
+		toFloat(color.blueF()),
+	};
 }
 
 void PartRenderer::initializeGL()
@@ -56,8 +59,6 @@
 	{
 		abort();
 	}
-	this->gridProgram.emplace(this);
-	this->gridProgram->initialize();
 	this->compiler->initialize();
 	this->compiler->build(this->model, this->documents, this->renderPreferences);
 	this->initialized = true;
@@ -79,7 +80,7 @@
 		0.1f,
 		10000.f);
 	this->compiler->setUniformMatrix("projectionMatrix", this->projectionMatrix);
-	this->gridProgram->setProjectionMatrix(this->projectionMatrix);
+	emit projectionMatrixChanged();
 }
 
 static GLenum getGlTypeForArrayClass(const gl::ArrayClass vboClass)
@@ -173,10 +174,6 @@
 		this->renderAllArrays();
 		break;
 	}
-	glEnable(GL_BLEND);
-	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-	this->gridProgram->draw();
-	glDisable(GL_BLEND);
 	glDisable(GL_POLYGON_OFFSET_FILL);
 }
 
@@ -195,24 +192,19 @@
 	const double z  = 2 * std::exp(this->zoom) * (1 + this->compiler->modelDistance());
 	this->viewMatrix = glm::lookAt(glm::vec3{0, 0, z}, {0, 0, 0}, {0, -1, 0});
 	this->compiler->setUniformMatrix("viewMatrix", this->viewMatrix);
-	this->gridProgram->setViewMatrix(this->viewMatrix);
+	emit this->viewMatrixChanged();
 }
 
 void PartRenderer::updateModelMatrix()
 {
-	const glm::mat4 modelMatrix = glm::mat4_cast(this->modelQuaternion);
+	this->modelMatrix = glm::mat4_cast(this->modelQuaternion);
 	this->compiler->setUniformMatrix("modelMatrix", modelMatrix);
-	this->gridProgram->setModelMatrix(modelMatrix);
+	emit this->modelMatrixChanged();
 	this->update();
 }
 
 void PartRenderer::setupBackgroundColor()
 {
-	if (this->gridProgram.has_value())
-	{
-		const bool isDark = luma(this->renderPreferences.backgroundColor) < 0.25;
-		this->gridProgram->setGridColor(isDark ? Qt::white : Qt::black);
-	}
 }
 
 void PartRenderer::renderVao(const gl::ArrayClass arrayClass)
@@ -255,7 +247,7 @@
 void PartRenderer::mouseMoveEvent(QMouseEvent* event)
 {
 	const bool left = event->buttons() & Qt::LeftButton;
-	const QPointF move = pointToPointF(event->pos()) - this->lastMousePosition;
+	const QPoint move = event->pos() - this->lastMousePosition;
 	if (left and not move.isNull())
 	{
 		// q_x is the rotation of the brick along the vertical y-axis, because turning the
@@ -269,7 +261,7 @@
 		this->modelQuaternion = q_x * q_y * this->modelQuaternion;
 		this->updateModelMatrix();
 	}
-	this->lastMousePosition = pointToPointF(event->pos());
+	this->lastMousePosition = event->pos();
 }
 
 void PartRenderer::wheelEvent(QWheelEvent* event)
@@ -281,11 +273,6 @@
 	this->update();
 }
 
-glm::vec3 PartRenderer::viewport(const glm::vec3& point)
-{
-	return viewport(point, this->width(), this->height());
-}
-
 /**
  * @brief Converts the specified on the screen into the 3D world. The point is unprojected twice into 3D and the
  * intersection of the resulting line with the specified plane is returned. If the intersection point lies behind
@@ -296,9 +283,9 @@
  */
 std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPoint& point, const geom::Plane& plane)
 {
-	auto p1 = this->unproject({point.x(), point.y(), 0});
-	auto p2 = this->unproject({point.x(), point.y(), 1});
-	geom::Line line = geom::lineFromPoints(p1, p2);
+	const glm::vec3 p1 = this->unproject({point.x(), point.y(), 0});
+	const glm::vec3 p2 = this->unproject({point.x(), point.y(), 1});
+	const geom::Line line = geom::lineFromPoints(p1, p2);
 	std::optional<glm::vec3> result;
 	result = geom::linePlaneIntersection(line, plane, 0.01f);
 	// If the point lies behind the camera, do not return a result.
@@ -376,14 +363,6 @@
 		this->compiler->build(this->model, this->documents, this->renderPreferences);
 		this->setupBackgroundColor();
 	}
+	emit this->renderPreferencesChanged();
 	this->update();
 }
-
-glm::vec3 PartRenderer::viewport(const glm::vec3& point, float width, float height)
-{
-	return {
-		width * 0.5 * (point.x + 1),
-		height * 0.5 * (-point.y + 1),
-		0
-	};
-}

mercurial