diff -r 87c906545fc3 -r 77c819262b7a src/ui/canvas.cpp --- a/src/ui/canvas.cpp Sat Feb 29 23:51:03 2020 +0200 +++ b/src/ui/canvas.cpp Mon Mar 02 11:08:13 2020 +0200 @@ -45,6 +45,7 @@ // grid matrix. this->worldPosition = this->gridMatrix * glm::vec4{*this->worldPosition, 1}; } + /* if (this->worldPosition.has_value()) { this->newStatusText("Position: (%1, %2, %3)"_q @@ -56,6 +57,10 @@ { this->newStatusText("Position: "_q); } + */ + // 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")); PartRenderer::mouseMoveEvent(event); } @@ -167,3 +172,23 @@ this->gridPlane = geom::planeFromTriangle(triangle); this->gridProgram->setGridMatrix(this->gridMatrix); } + +/** + * @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); + // 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 + // origin towards the camera + // If the dot product between these two vectors is 0, the grid normal is perpendicular to the camera vector + // and the grid is perpendicular to the screen. + const float dot = glm::dot(glm::normalize(this->gridPlane.normal), glm::normalize(cameraDirection)); + return std::abs(dot) < threshold; +}