实施 openGL 选取时遇到困难
我已经能够找到我单击的位置的世界坐标,并且它还使用深度缓冲区进行检查。为此,使用以下代码:
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX,winY;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
// obtain the Z position (not world coordinates but in range 0 - 1)
GLfloat z_cursor;
winX = (float)x_cursor;
winY = (float)viewport[3]-(float)y_cursor;
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z_cursor);
// obtain the world coordinates
GLdouble x, y, z;
gluUnProject(winX, winY, z_cursor, modelview, projection, viewport, &x, &y, &z);
其中 x_cursor 和 y_cursor 是我的光标坐标。
到目前为止一切都很好,当打印 x、y、z 时,它们似乎都很好!
但现在... 解析包含所有多边形/三角形的文件后。我把所有东西都放在 openGL DISPLAY 列表中。所以我的程序基本上只是调用列表,并翻译/旋转它们。每个对象都有一个唯一的名称。所以我保留的只是列表,我不保留每个对象的点/三角形
我的问题:
所以我有我单击的位置的世界坐标,但是我如何找出哪个对象与该位置匹配!?
I'm already able to find the world coordinates of the place i clicked, and it also checks this with the depth buffer. For this is used the following code :
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX,winY;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
// obtain the Z position (not world coordinates but in range 0 - 1)
GLfloat z_cursor;
winX = (float)x_cursor;
winY = (float)viewport[3]-(float)y_cursor;
glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z_cursor);
// obtain the world coordinates
GLdouble x, y, z;
gluUnProject(winX, winY, z_cursor, modelview, projection, viewport, &x, &y, &z);
with x_cursor and y_cursor being my cursor coordinates.
So far so good and when printing x,y,z they seem to be good !
But now...
After parsing my files containing all the polygons/triangles. I put everyting in openGL DISPLAY Lists. So my program basically just calls the lists, and translates/rotates them. Every object als has a unique name. So all i keep are the lists, I don't keep the points/triangles of every object
My Question :
So i have my world coordinates of where i clicked, but how can I figure out which object matches that position !?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您只想要对象 ID 并且愿意容忍 2x 过度绘制:
1) 清除颜色和深度缓冲区
2) 使用不同的纯色绘制所有对象(禁用照明和纹理),保持颜色 ->对象 ID 映射。
3) 读取鼠标位置处的颜色缓冲区,记下颜色并在颜色图中查找。
4) 清除颜色和深度缓冲区并正常重新渲染场景。
If you just want the object ID and are willing to tolerate 2x overdraw:
1) Clear color and depth buffers
2) Draw all objects with different solid colors (disable lighting and texturing), maintaining a color -> object ID map.
3) Read color buffer at mouse position, note color and look up in color map.
4) Clear color and depth buffers and re-render scene as normal.
我想您可能会对以下内容感兴趣:
http://www.gpwiki.org/index.php/OpenGL:Tutorials:Picking
I think you might be interested in:
http://www.gpwiki.org/index.php/OpenGL:Tutorials:Picking
请参阅OpenGL 拾取教程。它包含一个完整的工作示例。然而,如果您只想选择最靠近相机的对象(例如画在顶部的对象),似乎代码仍然可以改进。不过,您可以修改代码以获得此结果。
警告:我之前已经这样做过,但是这个特定的代码没有经过测试。
Refer to the OpenGL Picking Tutorial. It contains a fully working example. However, it seems that code can still be improved if you only want to select the object closest to the camera (e.g. the one painted on top). You can modify the code to get this result, however.
Caveat: I've done this before, but this particular code is not tested.
从眼睛的角度将光线投射到场景中。
使用向量算术计算鼠标在近平面上单击的世界空间 xyz 位置。
下面是您需要执行的操作的图表:
这比取消项目更有效,因为取消项目涉及矩阵逆(成本相当高)
这正是您想要的过程用于为所单击的像素将光线投射到光线跟踪器的场景中。 (在光线追踪器中,您正在检索颜色,在这里您正在检索对象的 id)。
Cast a ray into the scene from the point of view of the eye.
Compute the world space xyz location of the mouse click on the near plane by using vector arithmetic.
Here is a diagram of what you need to do:
This is more efficient than an unproject because unproject involves a matrix inverse (which is quite costly)
This is exactly the process you would use to cast a ray into a scene for a raytracer for the pixel that was clicked on. (In a ray tracer, you are retrieving the color, here you are retrieving the id of an object).