grid autorotation

Thu, 05 Mar 2020 15:58:35 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 05 Mar 2020 15:58:35 +0200
changeset 67
612213a168da
parent 66
77c819262b7a
child 68
ddb07bb6840c

grid autorotation

src/ui/canvas.cpp file | annotate | diff | comparison | revisions
src/ui/canvas.h file | annotate | diff | comparison | revisions
--- a/src/ui/canvas.cpp	Mon Mar 02 11:08:13 2020 +0200
+++ b/src/ui/canvas.cpp	Thu Mar 05 15:58:35 2020 +0200
@@ -60,7 +60,30 @@
 	*/
 	// use a relatively high threshold so that we know when the grid is somewhat perpendicular so we can
 	// automatically change it properly
-	this->newStatusText("Change: %1"_q.arg(isGridPerpendicularToScreen(0.03f) ? "yes" : "no"));
+	if (isGridPerpendicularToScreen(0.03f))
+	{
+		const glm::vec3 cameraDirection = this->cameraVector();
+		const glm::vec3 vector_x = glm::normalize(this->gridMatrix * glm::vec4{1, 0, 0, 1});
+		const glm::vec3 vector_y = glm::normalize(this->gridMatrix * glm::vec4{0, 1, 0, 1});
+		const float angle_x = std::abs(glm::dot(vector_x, cameraDirection));
+		const float angle_y = std::abs(glm::dot(vector_y, cameraDirection));
+		if (angle_x < angle_y)
+		{
+			this->newStatusText("rotate by X axis");
+			this->gridMatrix = glm::rotate(this->gridMatrix, float{M_PI} / 2, glm::vec3{1, 0, 0});
+		}
+		else
+		{
+			this->newStatusText("rotate by Y axis");
+			this->gridMatrix = glm::rotate(this->gridMatrix, float{M_PI} / 2, glm::vec3{0, 1, 0});
+		}
+		this->updateGridMatrix();
+		this->update();
+	}
+	else
+	{
+		this->newStatusText("don't rotate");
+	}
 	PartRenderer::mouseMoveEvent(event);
 }
 
@@ -117,12 +140,15 @@
 		}
 	});
 	PartRenderer::initializeGL();
+	/*
 	this->gridMatrix = glm::mat4{
 		{-4, 0, 0, 0},
 		{0, 6.9266, -3.6955, 0},
 		{0, -16.7222, -1.5307, 0},
 		{0, -13.273, -9.255, 1},
 	};
+	*/
+	this->gridMatrix = glm::mat4{1};
 	this->updateGridMatrix();
 }
 
@@ -173,16 +199,21 @@
 	this->gridProgram->setGridMatrix(this->gridMatrix);
 }
 
+glm::vec3 Canvas::cameraVector() const
+{
+	// Find out where the grid is projected on the screen
+	const QPoint gridOrigin2d = pointFToPoint(this->modelToScreenCoordinates(this->gridPlane.anchor));
+	// Find out which direction the camera is looking at the grid origin in 3d
+	return glm::normalize(this->cameraLine(gridOrigin2d).direction);
+}
+
 /**
  * @brief Calculates if the screen is perpendicular to the current grid
  * @return yes no
  */
 bool Canvas::isGridPerpendicularToScreen(float threshold) const
 {
-	// Find out where the grid is projected on the screen
-	const QPoint gridOrigin2d = pointFToPoint(this->modelToScreenCoordinates(this->gridPlane.anchor));
-	// Find out which direction the camera is looking at the grid origin in 3d
-	const glm::vec3 cameraDirection = glm::normalize(this->cameraLine(gridOrigin2d).direction);
+	const glm::vec3 cameraDirection = this->cameraVector();
 	// Compute the dot product. The parameters given are:
 	// - the normal of the grid plane, which is the vector from the grid origin perpendicular to the grid
 	// - the direction of the camera looking at the grid, which is the inverse of the vector from the grid
--- a/src/ui/canvas.h	Mon Mar 02 11:08:13 2020 +0200
+++ b/src/ui/canvas.h	Thu Mar 05 15:58:35 2020 +0200
@@ -24,6 +24,7 @@
 	void selectionChanged(const QSet<ldraw::Id>& newSelection);
 private:
 	void updateGridMatrix();
+	glm::vec3 cameraVector() const;
 	bool isGridPerpendicularToScreen(float threshold) const;
 	std::optional<GridProgram> gridProgram;
 	std::optional<glm::vec3> worldPosition;

mercurial