Android OpenGL ES 1.1:从模板缓冲区读取

发布于 2025-01-01 09:26:27 字数 514 浏览 6 评论 0原文

使用 Android OpenGL ES 1.1 (HTC Desire)...

我遇到的一般问题是:

我在复杂的场景中渲染了各种 3D 对象。我想检查用户是否单击了特定对象。该对象可能部分隐藏,因此可能在场景中显示为几乎任何形状。我只想在用户单击场景中可见的对象的一部分时允许“选择”对象。这意味着我无法对对象相交使用基于向量的计算,因为这些计算无法轻松考虑对象的隐藏区域。

所以我想出了一个主意...

我设置了模板缓冲区,以便在对象可见的任何地方,模板缓冲区都填充 1,而模板缓冲区中的其他任何位置都填充 0。在场景中,我只需要检查模板缓冲区,看看它是否包含 1 或 0,这表明该对象是否被单击。

这在 PC 上完美运行,但在 Android OpenGL ES 1.1 上,我似乎无法使用 glReadPixels() 从模板缓冲区读取,因为不支持 GL_STENCIL_INDEX。

有谁知道是否有办法从模板缓冲区读取这个 0/1?或者有人能想到更好的算法来确定我的对象是否已被单击?

非常感谢

Using Android OpenGL ES 1.1 (HTC Desire)...

The problem I have in general is this:

I have various 3D objects rendered in a complex scene. I want to check if the user has clicked on a particular object. This object may be partially hidden and therefore may appear as almost any shape in the scene. I only want to allow the object to be "selected" if the user clicks on a part of the object that is visible in the scene. This means that I cannot use vector-based calculations for the object intersection since these could not easily take into account the hidden areas of the object.

So I came up with an idea...

I set up the stencil buffer such that wherever the object was visible the stencil buffer was filled with 1s and everywhere else in the stencil buffer is 0. When the user clicks on a particular pixel in the scene I just need to check the stencil buffer to see if it contains a 1 or 0 which indicates if the object was clicked or not.

This works perfectly on a PC, but on Android OpenGL ES 1.1 it seems that I cannot read from the stencil buffer by using glReadPixels() as GL_STENCIL_INDEX is not supported.

Does anyone know if there is a way to read this 0/1 from the stencil buffer? Or can anyone think of a better algorithm for determining if my object has been clicked?

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2025-01-08 09:26:27

您可以使用颜色缓冲区实现相同的算法。

创建一个帧缓冲区。将场景渲染到其中,但用不同的颜色绘制每个对象。颜色不必与对象的实际颜色相同,因为您在帧缓冲区中绘制的内容不是用于眼睛的,而仅用于鼠标查找的。绘制整个场景后,使用 glReadPixels 读取像素。当您从鼠标事件获取鼠标坐标时,在像素图中查找它们。您找到的颜色可以指向鼠标当前所在的对象。

使用模板缓冲区可能更有效,但在 OpenGL ES 中似乎不可能将模板缓冲区读入内存。另一方面,此方法的优点是您不限于 8 位模板缓冲区的 255 个对象。

You can implement the same algorithm using color buffer.

Create a FrameBuffer. Render the scene into it, but draw each object with a distinct color. The color doesn't have to be same as the actual color of the object, because what you draw in framebuffer is not for eyes, but only for mouse lookup. Once entire scene is drawn, read the pixels using glReadPixels. When you get coordinates of the mouse from mouse event, look them up in the pixel map. The color you find can point back to the object the mouse is currently on.

It may be more efficient to use stencil buffer, but reading stencil buffer into memory doesn't seem possible in OpenGL ES. On the other hand, this method's advantage is you are not limited to 255 objects as in case of 8-bit stencil buffer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文