Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
glReadPixels在Qt中的读取坐标似乎不正确_Qt_Opengl_Glreadpixels - Fatal编程技术网

glReadPixels在Qt中的读取坐标似乎不正确

glReadPixels在Qt中的读取坐标似乎不正确,qt,opengl,glreadpixels,Qt,Opengl,Glreadpixels,我正在尝试使用OpenGL4.0和Qt5.3开发一个应用程序,我想在QGLWidget中实现颜色选择来选择不同的模型。 基本上,当我检测到鼠标点击时,我: 获取鼠标的位置 渲染场景 设置白色背景(glClearColor然后glClear(GL\u COLOR\u BUFFER\u BIT | GL\u DEPTH\u BUFFER\u BIT) 我绑定我的着色器程序 我画我的模型(每一个都有不同的颜色和它自己的变换矩阵) 释放我的着色器程序 调用glFlush和glFinish以确保在调用g

我正在尝试使用OpenGL4.0和Qt5.3开发一个应用程序,我想在QGLWidget中实现颜色选择来选择不同的模型。 基本上,当我检测到鼠标点击时,我:

  • 获取鼠标的位置
  • 渲染场景
  • 设置白色背景(
    glClearColor
    然后
    glClear(GL\u COLOR\u BUFFER\u BIT | GL\u DEPTH\u BUFFER\u BIT)
  • 我绑定我的着色器程序
  • 我画我的模型(每一个都有不同的颜色和它自己的变换矩阵)
  • 释放我的着色器程序
  • 调用
    glFlush
    glFinish
    以确保在调用
    glReadPixels
  • 调用
    glReadPixels(mouse.x,window\u height-mouse.y,1,1,GL\u RGBA,GL\u UNSIGNED\u BYTE,data)
    数据是长度为4的
    GLubyte
    数组
  • 我的程序运行得“很好”,但当我想要选择一个对象时,我必须单击比模型实际位置更高的位置。我尝试交换缓冲区以检查模型是否在正确的位置渲染,是的,它们是。。。
    我还尝试在
    glReadPixels
    之前调用
    glPixelStorei(GL\u PACK\u ALIGNMENT,x)
    ,x=1,2,4,8,但似乎没有影响它。

    感谢您的评论Michael IV和jozxyqk。 最后,我在QOpenGLFramebufferObject中绘制了一幅图,并使用QImage读取像素颜色。如下所示:

    QImage image = fbo->toImage(); //fbo is the QOpenGLFramebufferObject
    QRgb rgb = image.pixel(mouse.x(), mouse.y());
    
    有关详细信息,请参阅:


    快乐的编码!

    有点晚了……晚了6年,但我在使用
    QOpenGLWidget
    时也遇到了同样的问题。我发现
    glReadPixels
    覆盖了整个窗口(或者可能只是“中央小部件”),而不是OpenGL小部件。如果不选择创建新FBO,则可以使用窗口偏移来获得正确的结果:

    void MyOpenGLWidget::mouseReleaseEvent(QMouseEvent *event)
    {
        if (!parentWidget()) //This widget needs to belong to a window or another widget
            return;
    
        QPointF wpos = event->windowPos();
    
        GLfloat color[4];
        //Note: If a menu or status bar is present the height of the bar(s) needs
        //      to be included in the y calc because the parentWidget is the "centralwidget",
        //      not the window.
        glReadPixels(wpos.x(), parentWidget()->height() - wpos.y(), 1, 1, GL_RGBA, GL_FLOAT, color); 
    
        QString msg = QString("%1 %2 %3 %4").arg(color[0]).arg(color[1]).arg(color[2]).arg(color[3]);
        qDebug() << "Clicked on pixel " << event->x() << ", " << event->y() << ", color " << msg;
    }
    
    void MyOpenGLWidget::mouseReleaseEvent(QMouseEvent*事件)
    {
    if(!parentWidget())//此小部件需要属于窗口或其他小部件
    回来
    QPointF wpos=event->windowPos();
    GLfloat颜色[4];
    //注意:如果存在菜单或状态栏,则需要该栏的高度
    //要包含在y计算中,因为parentWidget是“centralwidget”,
    //不是窗户。
    glReadPixels(wpos.x(),parentWidget()->height()-wpos.y(),1,1,GL_RGBA,GL_FLOAT,color);
    QString msg=QString(“%1%2%3%4”).arg(颜色[0]).arg(颜色[1]).arg(颜色[2]).arg(颜色[3]);
    
    qDebug()不要执行“glFlush和glFinish”,因为OpenGL将在您下载数据之前完成所有命令。关闭一个错误?
    window\u height-mouse.y-1
    ,因为
    window\u height
    在窗口之外。也可能是QTs鼠标原点的问题(例如,您获得的是与父元素相关的)。尝试打印坐标并尝试单击
    0,0