src/ui/canvas.cpp

changeset 66
77c819262b7a
parent 65
87c906545fc3
child 67
612213a168da
equal deleted inserted replaced
65:87c906545fc3 66:77c819262b7a
43 this->worldPosition = glm::round(*this->worldPosition); 43 this->worldPosition = glm::round(*this->worldPosition);
44 // And finally transform it back to grid coordinates by transforming it with the 44 // And finally transform it back to grid coordinates by transforming it with the
45 // grid matrix. 45 // grid matrix.
46 this->worldPosition = this->gridMatrix * glm::vec4{*this->worldPosition, 1}; 46 this->worldPosition = this->gridMatrix * glm::vec4{*this->worldPosition, 1};
47 } 47 }
48 /*
48 if (this->worldPosition.has_value()) 49 if (this->worldPosition.has_value())
49 { 50 {
50 this->newStatusText("Position: (%1, %2, %3)"_q 51 this->newStatusText("Position: (%1, %2, %3)"_q
51 .arg(toDouble(this->worldPosition->x)) 52 .arg(toDouble(this->worldPosition->x))
52 .arg(toDouble(this->worldPosition->y)) 53 .arg(toDouble(this->worldPosition->y))
54 } 55 }
55 else 56 else
56 { 57 {
57 this->newStatusText("Position: <none>"_q); 58 this->newStatusText("Position: <none>"_q);
58 } 59 }
60 */
61 // use a relatively high threshold so that we know when the grid is somewhat perpendicular so we can
62 // automatically change it properly
63 this->newStatusText("Change: %1"_q.arg(isGridPerpendicularToScreen(0.03f) ? "yes" : "no"));
59 PartRenderer::mouseMoveEvent(event); 64 PartRenderer::mouseMoveEvent(event);
60 } 65 }
61 66
62 void Canvas::mousePressEvent(QMouseEvent* event) 67 void Canvas::mousePressEvent(QMouseEvent* event)
63 { 68 {
165 this->gridMatrix * glm::vec4{0, 1, 0, 1}, 170 this->gridMatrix * glm::vec4{0, 1, 0, 1},
166 }; 171 };
167 this->gridPlane = geom::planeFromTriangle(triangle); 172 this->gridPlane = geom::planeFromTriangle(triangle);
168 this->gridProgram->setGridMatrix(this->gridMatrix); 173 this->gridProgram->setGridMatrix(this->gridMatrix);
169 } 174 }
175
176 /**
177 * @brief Calculates if the screen is perpendicular to the current grid
178 * @return yes no
179 */
180 bool Canvas::isGridPerpendicularToScreen(float threshold) const
181 {
182 // Find out where the grid is projected on the screen
183 const QPoint gridOrigin2d = pointFToPoint(this->modelToScreenCoordinates(this->gridPlane.anchor));
184 // Find out which direction the camera is looking at the grid origin in 3d
185 const glm::vec3 cameraDirection = glm::normalize(this->cameraLine(gridOrigin2d).direction);
186 // Compute the dot product. The parameters given are:
187 // - the normal of the grid plane, which is the vector from the grid origin perpendicular to the grid
188 // - the direction of the camera looking at the grid, which is the inverse of the vector from the grid
189 // origin towards the camera
190 // If the dot product between these two vectors is 0, the grid normal is perpendicular to the camera vector
191 // and the grid is perpendicular to the screen.
192 const float dot = glm::dot(glm::normalize(this->gridPlane.normal), glm::normalize(cameraDirection));
193 return std::abs(dot) < threshold;
194 }

mercurial