GLFW picking with multiple viewports


I have 4 viewports which shares single GLFW context same as below.

I have a problem with picking here that I’m getting z-depth as 1.0 every time I run following codes.

GLfloat winZ = 0.1f;
glm::vec4 viewport;
glm::vec3 screen;

viewport = glm::vec4(glWindow.glViews[CurViewport].x, glWindow.glViews[CurViewport].y, glWindow.glViews[CurViewport].width, glWindow.glViews[CurViewport].height);
glReadPixels(io.MousePos.x - glWindow.glViews[CurViewport].area.left, glWindow.glViews[CurViewport].area.bottom - io.MousePos.y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

screen = glm::vec3(io.MousePos.x, io.MousePos.y, winZ);
glMouse = glm::unProject(screen, glWindow.glViews[CurViewport].camera->viewMatrix, glWindow.glViews[CurViewport].camera->projectionMatrix, viewport); 

All drawing routine clears up screen with glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);, so I’m wondering if there is anything I’m doing wrong here

When I check the values from above code, I can obtain below values which is totally wrong :sob:

But if I skip glReadPixels and set winZ as 0.1f, I can obtain OpenGL coordinates but I can find that it has some tolerance from actual location of mouse cursor.

Can you please help me out?
Please also let me know if there’s anything to be provided further to understand/resolve the problem.

Thanks in advance,

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.