用于旋转网格的 OpenGL ArcBall

OpenGL ArcBall for rotating mesh

我正在使用旧版 OpenGL 绘制网格。我现在正在尝试实现一个 arcball class 来用鼠标旋转对象。但是,当我移动鼠标时,对象要么不旋转,要么旋转角度太大。

这是点击鼠标时调用的方法:

void ArcBall::startRotation(int xPos, int yPos) {
    int x = xPos - context->getWidth() / 2;
    int y = context->getHeight() / 2 - yPos;
    startVector = ArcBall::mapCoordinates(x, y).normalized();
    endVector = startVector;
    rotating = true;
}

这个方法的目的是简单地将鼠标坐标映射到屏幕中心,并将它们映射到边界球体,从而得到一个起始向量

这是鼠标移动时调用的方法:

void ArcBall::updateRotation(int xPos, int yPos) {
    int x = xPos - context->getWidth() / 2;
    int y = context->getHeight() / 2 - yPos;
    endVector = mapCoordinates(x, y).normalized();
    rotationAxis = QVector3D::crossProduct(endVector, startVector).normalized();
    angle  = (float)qRadiansToDegrees(acos(QVector3D::dotProduct(startVector, endVector)));
    rotation.rotate(angle, rotationAxis.x(), rotationAxis.y(), rotationAxis.z());
    startVector = endVector;
}

这个方法也是将鼠标坐标映射到屏幕中间,然后计算新的向量,并根据这两个向量计算旋转轴和角度。

然后我用

glMultMatrixf(ArcBall::rotation.data());

应用旋转

我建议将鼠标位置存储在您最初在视图中单击的位置。计算鼠标在window坐标中的移动量。移动的距离必须映射到一个角度。旋转轴垂直(垂直)于鼠标移动的方向。结果是对象的旋转类似于 this WebGL 演示。

将当前鼠标位置存储在startRotation中。注意存储位置鼠标位置的坐标不是归一化向量:

// xy normalized device coordinates:
float ndcX = 2.0f * xPos / context->getWidth() - 1.0f;
float ndcY = 1.0 - 2.0f * yPos / context->getHeight();

startVector = QVector3D(ndcX, ndcY, 0.0);

获取当前在updateRotation中的位置:

// xy normalized device coordinates:
float ndcX = 2.0f * xPos / context->getWidth() - 1.0f;
float ndcY = 1.0 - 2.0f * yPos / context->getHeight();

endVector = QVector3D(ndcX, ndcY, 0.0);

计算从起始位置到结束位置的向量:

QVector3D direction = endVector - startVector;

旋转轴垂直于运动方向:

rotationAxis = QVector3D(-direction.y(), direction.x(), 0.0).normalized();

注意即使direction的类型是QVector3D,它仍然是一个二维向量。它是视口 XY 平面中的一个向量,表示鼠标在视口上的移动。 z坐标为0,一个二维向量(x, y),可以逆时针旋转90度,通过(-y, x).

方向向量的长度表示旋转的角度。鼠标在整个屏幕上移动会产生一个长度为 2.0 的向量。因此,如果在整个屏幕上拖动会导致完全旋转,则矢量的长度必须乘以 PI。如果要进行hlf旋转,则由PI/2

angle = (float)qRadiansToDegrees(direction.length() * 3.141593);

最后,新旋转必须应用于现有旋转而不是模型:

QMatrix4x4 addRotation;
addRotation.rotate(angle, rotationAxis.x(), rotationAxis.y(), rotationAxis.z());
rotation = addRotation * rotation; 

startRotationupdateRotation 方法的最终代码清单:

void ArcBall::startRotation(int xPos, int yPos) {

    // xy normalized device coordinates:
    float ndcX = 2.0f * xPos / context->getWidth() - 1.0f;
    float ndcY = 1.0 - 2.0f * yPos / context->getHeight();

    startVector = QVector3D(ndcX, ndcY, 0.0);
    endVector   = startVector;
    rotating    = true;
}
void ArcBall::updateRotation(int xPos, int yPos) {

    // xy normalized device coordinates:
    float ndcX = 2.0f * xPos / context->getWidth() - 1.0f;
    float ndcY = 1.0 - 2.0f * yPos / context->getHeight();

    endVector = QVector3D(ndcX, ndcY, 0.0);

    QVector3D direction = endVector - startVector;
    rotationAxis        = QVector3D(-direction.y(), direction.x(), 0.0).normalized();
    angle               = (float)qRadiansToDegrees(direction.length() * 3.141593);

    QMatrix4x4 addRotation;
    addRotation.rotate(angle, rotationAxis.x(), rotationAxis.y(), rotationAxis.z());
    rotation = addRotation * rotation; 

    startVector = endVector;
}

如果你想绕着物体的向上轴旋转a使物体沿着视图spacex轴倾斜,那么计算是不同的。首先应用围绕 y 轴的旋转矩阵(向上向量),然后应用当前视图矩阵,最后应用 x 轴上的旋转:

view-matrix = rotate-X * view-matrix * rotate-Y

函数更新轮换必须如下所示:

void ArcBall::updateRotation(int xPos, int yPos) {

    // xy normalized device coordinates:
    float ndcX = 2.0f * xPos / context->getWidth() - 1.0f;
    float ndcY = 1.0 - 2.0f * yPos / context->getHeight();

    endVector = QVector3D(ndcX, ndcY, 0.0);

    QVector3D direction = endVector - startVector;

    float angleY = (float)qRadiansToDegrees(-direction.x() * 3.141593);
    float angleX = (float)qRadiansToDegrees(-direction.y() * 3.141593);

    QMatrix4x4 rotationX;
    rotationX.rotate(angleX, 1.0f 0.0f, 0.0f);

    QMatrix4x4 rotationUp;
    rotationX.rotate(angleY, 0.0f 1.0f, 0.0f);

    rotation = rotationX * rotation * rotationUp; 

    startVector = endVector;
}