1

I am using QGLWidget and QtOpenGL to display my point clouds and glReadPixels and gluUnProject to pick a point from a poiint cloud. The problem is that the glReadPixels does not seem to pick pixels of my points.

I've tried to use different point sizes as well as different block sizes in glReadPixels but the "ray" seems to go through the points. Im wondering if I need to calculate the closes point to the ray since its almost impossible to click right on the point.

The points are drawn with (just and example of a point in origon )

`
GLuint list = glGenLists(1);
glNewList(list, GL_COMPILE);

glPointSize(10.0f);
glBegin(GL_POINTS);
glColor3f(0.0f, 255.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glEnd();
glEndList();
updateScene();`

The point picking is done by the getObejctCoords function below.

`
void pclView::getObjectCoords(QMouseEvent *event)
GLdouble projection[16];
GLdouble modelView[16];
GLint viewPort[4];

GLdouble obj_coords0, obj_coords1, obj_coords2;
GLdouble pt_coords0, pt_coords1, pt_coords2;

glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetDoublev(GL_MODELVIEW_MATRIX, modelView);
glGetIntegerv(GL_VIEWPORT, viewPort);
// Window parameters
winX = event->pos().x();
winY = viewPort[3] - event->pos().y();

// get Window Z
glReadPixels( event->pos().x(), int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);


// Unproject 2D click to 3D location
gluUnProject( winX, winY, winZ, modelView, projection, viewPort, &obj_coords0, &obj_coords1, &obj_coords2);

std::cout << "x: " << obj_coords0;
std::cout << " y: " << obj_coords1;
std::cout << " z: " << obj_coords2 << std::endl;
`

At camera position (0,0,-50) rotation: (0, 0) (By clicking at the point at almost at origon (but on the point ) the function produces the following output

´ x: 0 y: -0.578724 z: -950 `

And the actual result should (as I've understood it) should be something like x: 0 y: -0.578724 z: -0

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Marras
  • 11
  • 2
  • see [OpenGL 3D-raypicking with high poly meshes](https://stackoverflow.com/a/51764105/2521214) and [depth buffer got by glReadPixels is always 1](https://stackoverflow.com/a/51130948/2521214). Anyway make sure your screen coordinates passed to `glUnproject` are correct. The (0,0) is bottom left corner in OpenGL screen !!! – Spektre May 22 '19 at 08:23
  • @Spektre thanks for the tips. You were correct the depth is always 1 and the screen coordinates were correct. Im still working on a solution, the examples provided are a bit hard for me to understand and the "glReadDepth" produces a non number value. – Marras May 22 '19 at 10:00
  • Hmm that could mean your projection matrix is different than the equations are for ... – Spektre May 22 '19 at 10:16
  • I tested the glReadPixels function with different color with different data types such as GL_GREEN and the values returned were correct so its only the GL_DEPTH_COMPONENT that is causing the problem but I can not find a way to resolve it – Marras May 29 '19 at 06:05
  • Is your Projection a `gluPerspective` and Modelview an `ortonormal` or `ortogonal` matrix? If not then the unproject might not work. I do not use it as I use my own implementation (see the links above) but the unprojecting iitself is just inverse of the math behind rendering pipeline so if you are rendering with different type of matrices it will not work. if you read the depth as `~1` that is OK you just need to linearize it back from logarithmic NDC back to world coordinates. If you print the depth with high precision it will not be `1` but something like `0.9999999999???` instead. – Spektre May 29 '19 at 06:47
  • I printed the depth with high precision of 16 and it still returns 1, but float only supports up to 7 decimals. I set up the view model view this way: ` glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluOrtho2D(0, 1, 0, 1); glShadeModel(GL_SMOOTH); glDisable(GL_TEXTURE_2D);glLoadIdentity(); gluPerspective(30.0f, GLfloat(aspectR), 1.0f, viewDistance); ` and I dont do anything to the projection before calling it in the function getObjectCoords – Marras May 29 '19 at 08:11
  • If I see it right your problem is that you are combining `gluOrtho2D` with `gluPerspective` that is non standard and corrupts the math of the unproject ... use only one of them. – Spektre May 29 '19 at 08:28

0 Answers0