我正在浏览 Hello GL Example在 Qt 文档中。
他们有一些代码可以帮助通过鼠标拖动来旋转场景。
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
int dx = event->x() - lastPos.x();
int dy = event->y() - lastPos.y();
if (event->buttons() & Qt::LeftButton) {
setXRotation(xRot + 8 * dy);
setYRotation(yRot + 8 * dx);
} else if (event->buttons() & Qt::RightButton) {
setXRotation(xRot + 8 * dy);
setZRotation(zRot + 8 * dx);
}
lastPos = event->pos();
}
和
void GLWidget::setXRotation(int angle)
{
qNormalizeAngle(angle);
if (angle != xRot) {
xRot = angle;
emit xRotationChanged(angle);
updateGL();
}
}
我能理解他们是在计算拖动过程中x/y坐标的变化。
但是他们如何将其映射到旋转场景?文档缺乏对正在做什么的解释。
还有,这些神奇的数字 8
是怎么回事?
最佳答案
OpenGL 基本上是一支用来画东西的铅笔。没有这样的场景。你,程序员创建了一堆变量,然后,当需要绘制一些东西时,使用这些变量在 OpenGL 的铅笔周围移动。
在您的特定情况下,有一些变量 xRot
、yRot
和 zRot
用于创建应用的转换链绘制对象。实际绘制发生在 GLWidget::paintGL
中;我给你注释了:
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // reset transformation state
glTranslatef(0.0, 0.0, -10.0); // apply translation to transformation
glRotatef(xRot / 16.0, 1.0, 0.0, 0.0); // apply rotation on x
glRotatef(yRot / 16.0, 0.0, 1.0, 0.0); // apply rotation on x
glRotatef(zRot / 16.0, 0.0, 0.0, 1.0); // apply rotation on x
logo->draw(); // draw logo using the currently set transformation
}
关于c++ - Qt OpenGL - 用鼠标拖动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28165062/