diff -r f7dd937667a5 -r f99d52b1646b src/ui/canvas.cpp --- a/src/ui/canvas.cpp Fri Feb 28 19:24:33 2020 +0200 +++ b/src/ui/canvas.cpp Sat Feb 29 23:43:38 2020 +0200 @@ -22,23 +22,17 @@ this->update(); } -QString vectorToString(const glm::vec3& vec) -{ - return "(%1, %2, %3)"_q - .arg(toDouble(vec.x)) - .arg(toDouble(vec.y)) - .arg(toDouble(vec.z)); -} - void Canvas::mouseMoveEvent(QMouseEvent* event) { const ldraw::Id id = this->pick(event->pos()); this->highlighted = id; this->totalMouseMove += (event->pos() - this->lastMousePosition).manhattanLength(); - this->worldPosition = this->screenToModelCoordinates(event->pos(), geom::XY); + this->worldPosition = this->screenToModelCoordinates(event->pos(), this->gridPlane); if (this->worldPosition.has_value()) { + this->worldPosition = glm::inverse(this->gridMatrix) * glm::vec4{*this->worldPosition, 1}; this->worldPosition = glm::round(*this->worldPosition); + this->worldPosition = this->gridMatrix * glm::vec4{*this->worldPosition, 1}; } if (this->worldPosition.has_value()) { @@ -107,6 +101,13 @@ } }); 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->updateGridMatrix(); } void Canvas::paintGL() @@ -127,4 +128,31 @@ painter.setPen(Qt::white); painter.drawText(pos + QPointF{5, 5}, vectorToString(*this->worldPosition)); } + { + QPainter axisPainter{this}; + axisPainter.setRenderHint(QPainter::Antialiasing); + axisPainter.setPen(Qt::red); + axisPainter.drawLine( + this->modelToScreenCoordinates({10, 0, 0}), + this->modelToScreenCoordinates({-10, 0, 0})); + axisPainter.setPen(Qt::green); + axisPainter.drawLine( + this->modelToScreenCoordinates({0, 10, 0}), + this->modelToScreenCoordinates({0, -10, 0})); + axisPainter.setPen(Qt::blue); + axisPainter.drawLine( + this->modelToScreenCoordinates({0, 0, 10}), + this->modelToScreenCoordinates({0, 0, -10})); + } } + +void Canvas::updateGridMatrix() +{ + const geom::Triangle triangle { + this->gridMatrix * glm::vec4{0, 0, 0, 1}, + this->gridMatrix * glm::vec4{1, 0, 0, 1}, + this->gridMatrix * glm::vec4{0, 1, 0, 1}, + }; + this->gridPlane = geom::planeFromTriangle(triangle); + this->gridProgram->setGridMatrix(this->gridMatrix); +}