c++ - Qt OpenGL - 用鼠标拖动旋转

标签 c++ qt opengl qtopengl

我正在浏览 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 的铅笔周围移动。

在您的特定情况下,有一些变量 xRotyRotzRot 用于创建应用的转换链绘制对象。实际绘制发生在 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/

相关文章:

c++ - 你会如何计算这个函数中的j?

c++ - 在 Qt 中访问父对象

c++ - 已部署的 Mac 应用程序无法在 2014 年底之前的 Mac 上打开

qt - 使用 CMake 构建 Qt Qml 插件

opengl - 这个简单的FxAA如何工作?

c++ - OpenGL - GLM 和 GLSL,它们有何不同?

opengl - "gladLoadGL"有问题。我收到一个错误,说它不需要 1 个参数

c++ - OpenCV cv::Mat 的深拷贝

c++ - 在主循环外的多个文件上定义全局变量

c++ - vim 不高亮某些 c++ 关键字