c++ - glReadPixels 和 gluUnProject 无法正常工作

标签 c++ qt opengl

我正在尝试使用 glReadPixelsgluUnProject 获取真实坐标,但它不起作用。

void GLWidget::resizeGL(int Width, int Height)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0);
    glViewport(0, 0, (GLint)Width, (GLint)Height);

    currentWidth = Width;
    currentHeight = Height;
}

...

void GLWidget::plotAxis()
{
    GLdouble xc,yc,zc;

    glPointSize(7.0);
    // Test points
        glColor3f(0, 0, 0);
        glBegin(GL_POINTS);
        glVertex3f(30, 10, 0);
        glVertex3f(40, 10, 10);
        glVertex3f(50, 10, -10);
        glEnd();

}
...
void GLWidget::pickGeometry()
{

    makeCurrent();
    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    GLfloat winX = 0.0, winY = 0.0, winZ = 0.0;
    GLdouble posX = 0.0, posY = 0.0, posZ = 0.0;

    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);

    this->cursorX=QCursor::pos().x();
    this->cursorY=QCursor::pos().y();

    winX = cursorX;
    winY = viewport[3] - cursorY;

// obtain Z position
    glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

// obtain global coordinates
    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

    qDebug() << "win XYZ" << winX << winY << winZ;
    qDebug() << "pos XYZ" << posX << posY << posZ;
}

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    pressPosition = event->pos();
    if(event->button()==Qt::LeftButton)
    {
    pickGeometry();

    }
}

点击点 30, 10, 0 给出

win XYZ 1190 53 0
pos XYZ 137.762 -82.3627 100

点击点 40, 10, 10 给出

win XYZ 1239 51 0
pos XYZ 147.552 -83.0283 100.

你能指出我错在哪里吗?

最佳答案

QCursor::pos()提供的坐标是屏幕坐标。
参见 QPoint QCursor::pos() :

Returns the position of the cursor (hot spot) of the primary screen in global screen coordinates.

但是,QMouseEvent::pos() 提供的坐标是相对于小部件的坐标。
参见 QPoint QMouseEvent::pos() const :

Returns the position of the mouse cursor, relative to the widget that received the event.

这意味着您必须使用鼠标事件接收到的位置而不是 QCursor::pos() 来解决您的问题:

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    pressPosition = event->pos();    

    .....
}

void OGLWidget::pickGeometry()
{
    makeCurrent();

    GLint viewport[4];
    GLdouble modelview[16];
    GLdouble projection[16];
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);

    cursorX = pressPosition.rx(); // <-- pressPosition instead of QCursor::pos()
    cursorY = pressPosition.ry();

    GLfloat winX = 0.0, winY = 0.0, winZ = 0.0;
    winX = (GLfloat)cursorX;
    winY = (GLfloat)(viewport[3] - cursorY);

    // obtain Z position
    glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

    // obtain global coordinates
    GLdouble posX = 0.0, posY = 0.0, posZ = 0.0;
    gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

    qDebug() << "win XYZ" << winX << winY << winZ;
    qDebug() << "pos XYZ" << posX << posY << posZ;
}

关于c++ - glReadPixels 和 gluUnProject 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52365624/

相关文章:

c++ - 如何将 std::string 长度转换为十六进制然后再转换回字符串?

c++ - 编写应用程序核心和 gui 分离的最佳实践?

opengl - OpenGL 和 GLSL 中的 glTexParameter 和过滤?

网络协议(protocol)解析的C++方法

c++ - 在 C++ 中堆重载和读取文件的二维矩阵

c++ - 未声明的标识符编译错误

c++ - QScrollArea 中 QBoxLayout 中固定大小的小部件

c++ - Qt Creator DEFAULT 项目模板和编译标志

c++ - 程序在一台机器上编译但不能在另一台机器上编译

OpenGL纹理颜色错误