使用 OpenGL 着色语言进行拾取
我正在尝试找出选择使用着色器渲染的信息的最佳方法。 窗口管理器缓冲区(窗口)用于输出渲染。在用户事件中,我应该选择渲染的几何图形。使用 gluPick 很简单,但我尝试仅使用着色器进行选择。
着色器的灵活性并不能帮助我找到正确的解决方案。我找到了以下解决方案:
- 使用系统提供的缓冲区进行渲染(实际上使用颜色)。使用 glScissor 限制特定区域的渲染,并使用 glReadPixels 获取颜色,并使用几何标识符
- 映射使用单独的帧缓冲区对象进行渲染。需要定义渲染缓冲区格式。也许我可以使用普通整数来放置名称?
- 使用帧缓冲区对象进行渲染,既用于渲染又用于拾取。包含渲染名称的附加渲染缓冲区对象。
似乎第二种解决方案是最好的,因为不会改变窗口缓冲区,并且可以定义自定义方式来指定几何名称。
有什么建议吗?
PS:
由于在片段着色器中我声明了一个 vec4 数组来定义片段数据,因此我如何设置一个帧缓冲区来保存仅具有 R 组件的渲染缓冲区,并以大小整数内部格式存储?
是否可以将片段着色器输出变量与特定的附加渲染缓冲区链接起来?
I'm trying to figure out the best way to pick informations rendered using shaders.
A window manager buffer (the window) is used for outputting rendering. On user event I should pick the geometry rendered. Using gluPick is easy, but I'm trying to pick using only shaders.
Flexibility of shaders doesn'y aid me to find a correct solution. I've found the following solutions:
- Rendering using the system provided buffer (indeed using colors). Use glScissor to limit the rendering on a certain region and use glReadPixels for fetching colors, mapped with geometry identifiers
- Rendering using a separate framebuffer object. Renderbuffer formats need to defined. Maybe can I use plain integers for putting names?
- Rendering using a framebuffer object, both for rendering and picking. An additionally renderbuffer object containing rendered names.
It seems that the second solution is the best, since doesn't alterate the window buffers and it's possible to define a custom way to specify geometry names.
Any suggestion?
P.S:
Since in a fragment shader i declare an array of vec4 for defining the fragment data, how I could setup a framebuffer holding an render buffer having only the R component, stored in a sized integer internal format?
Is it possible to link a fragment shader output variable with a specific attached render buffer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定着色器在这里是否真的有任何帮助,除了作为某种形式的优化,您可以渲染到多个目标,从而能够一次性生成您的拾取值和颜色值。
如果您使用光栅化进行拾取,那么您总是会被 glReadPixels() 调用或等效调用所困扰,并且您总是会基本上将场景绘制到某种具有拾取值的缓冲区中。
如果您确实想要发挥创意并偏离常规,您可以尝试使用着色器与场景中的形状执行光线相交。这需要大量的专门着色器开发,并且除非您进行大量选择(也许是光线追踪器?),否则很难看到好处。
I am not sure that shaders are really any help here, except maybe as some form of optimization whereby you render to multiple targets and thus are able to generate your picking values and color values in one pass.
If you are doing picking using rasterization, you're always going to be stuck with a glReadPixels() call or equivalent, and you're always going to be basically drawing your scene to a buffer of some sort with picking values.
If you really wanted to get creative and diverge from the norm, you could try using shaders to perform a ray intersection with the shapes in your scene. This would require a good bit of specialized shader development, and it's hard to see the benefit unless you're doing a LOT of picking (maybe a raytracer?).