Color picking with Qt in OpenGLWidget

37 views Asked by At

I've been reading tutorials about picking and that color picking is far the most popular and simplest form.

For some reason, when I add glEnable(GL_DEPTH_TEST), the objects stop being selected correctly. When I remove this function, it selects objects normally, but there is a problem with overlapping objects. How can I fix this?

int WidgetGL::selectObject(int xx, int yy, QVector<Transformational *> &objs)
{
    m_selectBuffer->bind();

    glViewport(0, 0, width(), height());
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    m_selectProgram.bind();
    m_selectProgram.setUniformValue("u_projectionMatrix", m_projectionMatrix);
    m_camera->draw(&m_selectProgram);

    for (int i = 0; i < objs.size(); i++) {
        m_selectProgram.setUniformValue("u_code", (float) (i + 1));
        objs[i]->draw(&m_selectProgram, context()->functions());
    }
    m_selectProgram.release();


    GLuint viewport[4];
    GLubyte res[4];
    glGetIntegerv(GL_VIEWPORT, (GLint *) viewport);
    glReadPixels(xx, viewport[3] - yy, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &res);
    // m_selectBuffer->toImage().save("select.bmp");


    m_selectBuffer->release();

    return res[0];
}

I tried to fix it in every way, but still nothing worked

0

There are 0 answers