如何使用OpenGL进行3D选择/拾取 [英] How to do 3D selection/picking using OpenGL

查看:103
本文介绍了如何使用OpenGL进行3D选择/拾取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在场景中有一些物体,有些可能会遮挡其他物体.当我单击鼠标或拖动选择以获取选择矩形时,我只想选择/拾取从该角度可以看到的对象.该应用程序当前使用GL_SELECT渲染模式,但众所周知,这也会选择被遮挡的对象.另外,我读到它在OpenGL 3中已被弃用.

I have some objects in the scene, some may occlude others. When I click the mouse or drag-select to get a selection rectangle, I want to select/pick only the objects that I can see from this perspective. The application currently uses GL_SELECT render mode but as we know, this selects occluded objects too. Also, I read that this is deprecated in OpenGL 3.

目前有两种吸引我的方法.首先是使用Back Buffer的对象选择(红皮书,第14章):将每个对象的颜色设置为其对象ID,并从Back Frame缓冲区读取像素的颜色.第二个是遮挡查询(superbible,第4版,第13章).

There are two methods that are currently appealing to me. First is Object Selection using the Back Buffer (Red book, chapter 14): setting the colour of each object to it's object id and reading the colour of pixels from the back frame buffer. The second is occlusion queries (superbible, 4th ed, chap 13).

我排除的其他方法是查看选择缓冲区中的最小/最大z值,并在GL之外进行自定义射线/对象检测.

Other approaches I have ruled out are looking at the min/max z values in the selection buffer and doing custom ray/object detection outside of GL.

我有一些问题: 1)如果在最近的OpenGL中不推荐使用GL_SELECT,那么开发人员应该使用哪些替代方法? 2)我只读过有关用于加速渲染的遮挡查询的信息.它们可以用于选择/挑选吗?有缺点吗? 3)现有的应用程序有少量的glColorXXX调用.如果我走了后缓冲路线,并使用了glColorMask(FALSE,FALSE,FALSE,FALSE),这将有效地将glColourXXX调用转换为无效调用,从而让我在选择模式下渲染时可以将颜色控制在一个位置? 4)哪条路线是最佳/规范的?

I have some questions: 1) If GL_SELECT is deprecated in recent OpenGL, what alternatives are developers supposed to be using? 2) I've only ever read about occlusion queries being employed to speed up rendering. Can they be used for selection/picking, and are there drawbacks? 3) The existing application has a handful of glColorXXX calls. If I went the back buffer route, and used glColorMask(FALSE,FALSE,FALSE,FALSE), will this effectively turn the glColourXXX calls into calls that have no effect, thereby letting me control the colour in a single place when rendering in select mode? 4) Which route is best/canonical?

推荐答案

我决定使用后台缓冲区来实现选择.这是我尝试回答我的问题的尝试:

I decided to implement the selection using the back buffer. Here's my attempt to answer my questions:

如果最近的OpenGL中不推荐使用GL_SELECT,那么开发人员应该使用哪些替代方案?
我认为最好不要使用OpenGL来执行此任务,而应使用空间加速结构作为对原始问题的注释中建议的用户chamber85.

If GL_SELECT is deprecated in recent OpenGL, what alternatives are developers supposed to be using?
I think it's best to not employ OpenGL to do this task but to use spatial acceleration structures as user chamber85 suggested in comments to the original question.

我只读过有关用于加速渲染的遮挡查询的信息.它们可以用于选择/挑选吗?有缺点吗?
我确定他们可以,但是需要在绘制之前知道他们要查询遮挡的所有对象.使用后备缓冲区和颜色选择,人们可以看到光标下方或矩形区域内的内容,然后从那里进行过滤.

I've only ever read about occlusion queries being employed to speed up rendering. Can they be used for selection/picking, and are there drawbacks?
I'm sure they could but one would need to know all the objects they want to query for occlusion before the draw. Using back buffer and colour selection, one can just see what is under the cursor or within a rectangular region and filter from there.

现有应用程序有少量glColorXXX调用.如果我走了后缓冲路线,并使用了glColorMask(FALSE,FALSE,FALSE,FALSE),这将有效地将glColorXXX调用转换为无效调用,从而让我在选择模式下渲染时可以在单个位置控制颜色?
答案是不.使用所有GL_FALSE参数调用glColorMask()将意味着将不遵守glColor3ub()调用(例如).它只是在将颜色写入颜色缓冲区之前指定颜色的过滤器/遮罩.最初的想法是将颜色设置为对象id,然后调用glColorMask()以忽略所有后续的glColorXXX()调用.这种策略注定要失败,因为代表对象ID的颜色也会被屏蔽掉.

The existing application has a handful of glColorXXX calls. If I went the back buffer route, and used glColorMask(FALSE,FALSE,FALSE,FALSE), will this effectively turn the glColorXXX calls into calls that have no effect, thereby letting me control the colour in a single place when rendering in select mode?
The answer is no. Calling glColorMask() with all GL_FALSE parameters will not mean that glColor3ub() calls (for example) will not be honoured. It simply specifies a filter/mask for colours just before they are written to the colour buffer. The original thought was to set the colour to the object id, then call glColorMask() to ignore all subsequent glColorXXX() calls. This strategy is doomed as the colour representing the object id would also be masked out.

4)哪条路线是最佳/规范的? 我想说,一般来说是最好的选择,因为它不需要在绘制之前/期间设置遮挡查询.

4) Which route is best/canonical? I would say the back buffer colour selection is generally best as it doesn't require setting up the occlusion queries before/during the draw.

这篇关于如何使用OpenGL进行3D选择/拾取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆