简体   繁体   English

尝试使用 openGL 从屏幕上的 2d 单击中获取 3d 点

[英]Trying to get 3d point from 2d click on screen with openGL

i have a PyOpenGL project.我有一个 PyOpenGL 项目。 i render a stadium and some pcd clouds.我渲染了一个体育场和一些 PCD 云。 and i want to extract a point from the screen.我想从屏幕上提取一个点。 i have the X,Y values and i am trying to use gluUnProject or glReadPixelsf to get the point in x,y,z.我有 X、Y 值,我正在尝试使用gluUnProjectglReadPixelsf来获取 x、y、z 中的点。

but i am getting weird numbers for example: i ZOOMED as much as i can to the 0,0,0 point with my camera and used the但我得到了奇怪的数字,例如:我用我的相机尽可能地放大到 0,0,0 点并使用

 worldCoordinate1 = gluUnProject(x, realy, 0.0, mvmatrix, projmatrix, vport)

gets :得到:

(-79.71705354658924, 3.024572101740394, -1.8905707759570176)

which looks fine.看起来不错。 this is truly the camera location.这确实是相机的位置。 but my problem starts with the far plane.但我的问题始于远平面。

worldCoordinate2 = gluUnProject(x, realy, 1.0, mvmatrix, projmatrix, vport)

ideally i want this to get me the point colliding with any buffer object drawn.理想情况下,我希望这能让我知道与绘制的任何缓冲区对象发生冲突。 but i am getting但我得到

  (2098.1759117202328, -121.52711517535181, 77.12367509703613)

my function looks like this:我的功能是这样的:

def get_clicked_location(x, y):
    vport = glGetIntegerv(GL_VIEWPORT)
    mvmatrix = glGetDoublev(GL_MODELVIEW_MATRIX)
    projmatrix = glGetDoublev(GL_PROJECTION_MATRIX)
    realy = vport[3] - y
    worldCoordinate1 = gluUnProject(x, realy, 0.0, mvmatrix, projmatrix, vport)
    worldCoordinate2 = gluUnProject(x, realy, 1.0, mvmatrix, projmatrix, vport)
    print worldCoordinate1
    print worldCoordinate2
    data = glReadPixelsf(x, realy, 1, 1, GL_RGB, GL_UNSIGNED_BYTE)
    pick = map(ord, data)
    print pick

what i want is to get the x,y,z point colliding with the buffer drawn on screen.我想要的是让 x,y,z 点与屏幕上绘制的缓冲区发生碰撞。 but i cant seem to find the way.但我似乎找不到路。 also i found some people using glreadpixelsf but i don't think i understand how to combine them thanks for the help.我还发现有些人使用 glreadpixelsf,但我认为我不明白如何将它们结合起来,感谢您的帮助。

You can use glReadPixels to read the depth from the depth buffer of the default framebuffer.您可以使用glReadPixels从默认帧缓冲区的深度缓冲区读取深度。 Use the format GL_DEPTH_COMPONENT to do so:使用格式GL_DEPTH_COMPONENT这样做:

depth = glReadPixels(x, realy, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)

This depth can be used with gluUnProject , to find the world (or model) coordinate of the fragment:此深度可与gluUnProject一起使用,以查找片段的世界(或模型)坐标:

vport      = glGetIntegerv(GL_VIEWPORT)
mvmatrix   = glGetDoublev(GL_MODELVIEW_MATRIX)
projmatrix = glGetDoublev(GL_PROJECTION_MATRIX)

worldCoordinate1 = gluUnProject(x, realy, depth, mvmatrix, projmatrix, vport)

See also get 3d point in space using 2d point in image in python opengl另请参阅在 python opengl 中使用图像中的 2d 点获取空间中的 3d 点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM