glReadPixels和gluUnProject无法正常工作

时间:2018-09-17 10:25:57

标签: 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 给出

  

赢得XYZ 1190 53 0
  位置XYZ 137.762 -82.3627 100

点击点 40、10、10 给出

  

赢得XYZ 1239 51 0
  位置XYZ 147.552 -83.0283100。

你能指出我哪里错了吗?

1 个答案:

答案 0 :(得分:1)

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

  

在全局屏幕坐标中返回主屏幕的光标(热点)的位置

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

  

相对于收到事件的窗口小部件,返回鼠标光标的位置

这意味着您必须使用鼠标事件接收的位置而不是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;
}