src/gl/partrenderer.cpp

changeset 66
77c819262b7a
parent 61
4585d8d7a7ec
child 70
f21b800b02a4
--- a/src/gl/partrenderer.cpp	Sat Feb 29 23:51:03 2020 +0200
+++ b/src/gl/partrenderer.cpp	Mon Mar 02 11:08:13 2020 +0200
@@ -281,15 +281,13 @@
  * @param plane Plane to raycast against
  * @return world co-ordinates, or no value if the point is behind the camera.
  */
-std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPoint& point, const geom::Plane& plane)
+std::optional<glm::vec3> PartRenderer::screenToModelCoordinates(const QPoint& point, const geom::Plane& plane) const
 {
-	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);
+	const geom::Line line = this->cameraLine(point);
 	std::optional<glm::vec3> result;
 	result = geom::linePlaneIntersection(line, plane, 0.01f);
 	// If the point lies behind the camera, do not return a result.
-	if (result.has_value() and glm::dot(line.direction, *result - p1) < 0)
+	if (result.has_value() and glm::dot(line.direction, *result - line.anchor) < 0)
 	{
 		result.reset();
 	}
@@ -301,7 +299,7 @@
  * @param point Point to unproject
  * @return screen coordinates
  */
-QPointF PartRenderer::modelToScreenCoordinates(const glm::vec3& point)
+QPointF PartRenderer::modelToScreenCoordinates(const glm::vec3& point) const
 {
 	const glm::vec3 projected = glm::project(
 		point,
@@ -311,12 +309,19 @@
 	return toQPointF(glm::vec2{projected.x, this->height() - projected.y});
 }
 
+geom::Line PartRenderer::cameraLine(const QPoint& point) const
+{
+	const glm::vec3 p1 = this->unproject({point.x(), point.y(), 0});
+	const glm::vec3 p2 = this->unproject({point.x(), point.y(), 1});
+	return geom::lineFromPoints(p1, p2);
+}
+
 /**
  * @brief Unprojects the specified window coordinates to model coordinates
  * @param win Window coordinates to project. Z-coordinate indicates depth
  * @return model coordinates
  */
-glm::vec3 PartRenderer::unproject(const glm::vec3& win)
+glm::vec3 PartRenderer::unproject(const glm::vec3& win) const
 {
 	return glm::unProject(
 		glm::vec3{win.x, this->height() - win.y, win.z},

mercurial