如何使用glReadPixels和gluUnproject产生正确的点坐标?

时间:2019-05-22 06:40:11

标签: c++ qt opengl opengl-compat qtopengl

我正在使用QGLWidget和QtOpenGL显示我的点云,并使用glReadPixels和gluUnProject从点云中选择一个点。问题在于glReadPixels似乎没有拾取我的点的像素。

我尝试在glReadPixels中使用不同的点大小以及不同的块大小,但是“射线”似乎通过了这些点。我想知道是否需要计算光线的闭合点,因为它几乎不可能右键单击该点。

用(只是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();`

点拾取是由下面的getObejctCoords函数完成的。

`
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;
`

在摄像机位置(0,0,-50)旋转:(0,0)(通过单击几乎在origon处的点(但在该点上),该函数将产生以下输出

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

实际结果(据我所知)应该类似于     x: 0 y: -0.578724 z: -0

0 个答案:

没有答案
相关问题