opengl之鼠标交互事件(六)
opengl之鼠标交互事件(六)
代码如下:
void MainWidget::mousePressEvent(QMouseEvent *e) { // Save mouse press position mousePressPosition = QVector2D(e->localPos()); } void MainWidget::mouseReleaseEvent(QMouseEvent *e) { } void MainWidget::mouseMoveEvent(QMouseEvent *e){ if ((QApplication::keyboardModifiers() == Qt::ShiftModifier) && (e->buttons() & Qt::RightButton)) { // Mouse release position - mouse press position QVector2D diff = QVector2D(e->localPos()) - mousePressPosition; direct = QVector3D(diff.x(), -diff.y(), 0.0).normalized(); //translate = QVector3D(diff.x(), -diff.y(), 0.0) + translate; qreal acc = diff.length() / 10.0; moveSpeed += acc; } else if ((QApplication::keyboardModifiers() == Qt::ControlModifier) && (e->buttons() & Qt::RightButton)) { // Mouse release position - mouse press position QVector2D diff = QVector2D(e->localPos()) - mousePressPosition; // Rotation axis is perpendicular to the mouse position difference // vector QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized(); // Accelerate angular speed relative to the length of the mouse sweep qreal acc = diff.length() / 100.0; // Calculate new rotation axis as weighted sum rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized(); // Increase angular speed angularSpeed += acc; } } void MainWidget::timerEvent(QTimerEvent *) { // Decrease angular speed (friction) angularSpeed *= 0.60; // Stop rotation when speed goes below threshold if (angularSpeed < 0.1) { angularSpeed = 0.0; } else { // Update rotation rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation; // Request an update update(); } moveSpeed *= 0.80; if (moveSpeed < 0.01) { moveSpeed = 0.0; }else{ translate = (moveSpeed * direct) + translate; update(); } } void MainWidget::wheelEvent(QWheelEvent *e) { e->accept(); float wheelDelta_deg = e->delta(); qDebug()<< " wheelDelta_deg :: "<<wheelDelta_deg; if (wheelDelta_deg < 0.f) { zoom = (1.0 + log(-wheelDelta_deg)*0.05) * zoom; } else if(wheelDelta_deg > 0.f) { // projectionfovy = 180.0/3.1415962*(atan(0.1*zoom)+0.5*3.1415962); zoom = (1.0 - log(wheelDelta_deg)*0.05) * zoom; if (zoom < 0.01f) zoom = 0.01f; } update(); }
源代码:
链接:https://pan.baidu.com/s/1zFJKC3Cwz_6fVff56FgStw
提取码:uecq