diff -r 77c819262b7a -r 612213a168da src/ui/canvas.cpp --- 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