OpenGL ES 2.0 中的剪裁平面
我需要在 OpenGL ES 2.0 中的剪切平面下剪切数百个对象,并且希望了解对 OpenGL 的这个子集更有经验的人们的想法。
在 OpenGL ES 1.x 中有 glClipPlane。在桌面上,着色器中有 glClipPlane 或 gl_ClipDistance。这两者在 OpenGL ES 2.0 中都不可用。似乎这种功能在2.0中就完全消失了。
似乎唯一的方法是 A)在片段着色器中运行平面方程,或者 B)编写一个非常复杂的顶点着色器,将顶点放置在平面上(如果它们位于平面后面)。
(A) 与 glClipPlane 相比会很慢,因为“常规”裁剪是在顶点着色器之后和片段着色器之前完成的,每个片段仍然需要部分处理和丢弃。
(B) 很难在着色器之间兼容,因为我们不能丢弃顶点,我们必须将它们与平面对齐并调整“剪切”的属性。如果不发送纹理中的所有顶点并对其进行采样,则无法在着色器中的顶点之间进行插值,这将非常昂贵。通常,无论如何都可能无法正确插入数据。
我还考虑过将近平面与裁剪平面对齐,这将是一个有效的解决方案。
在渲染整个场景并检查深度失败后绘制平面也不起作用(除非您看起来接近垂直于平面)。
对于单个对象有效的方法是将平面绘制到深度缓冲区,然后使用 glDepthFunc(GL_GREATER) 渲染对象,但正如预期的那样,当一个对象位于另一个对象后面时,它不起作用。我尝试以这个概念为基础,但最终得到了与影子卷非常相似的东西,而且同样昂贵。
那么我错过了什么?如何在 OpenGL ES 2.0 中进行平面裁剪?
I need to clip a few hundred objects under a clipping plane in OpenGL ES 2.0 and would appreciate ideas from people more experienced with this subset of OpenGL.
In OpenGL ES 1.x there is glClipPlane. On the desktop you have glClipPlane, or gl_ClipDistance in your shader. Neither of these two are available in OpenGL ES 2.0. It seems that this kind of functionality disappeared completely with 2.0.
It seems the only way to do this is either A) run the plane equation in the fragment shader, or B) write a very complex vertex shader that positions vertices on the plane if they are behind it.
(A) would be slow compared to glClipPlane, since "regular" clipping is done after the vertex shader and before the fragment shader, each fragment would still have to be partially processed and discarded.
(B) would be very hard to make compatible between shaders, since we can't discard vertices we have to align them with the plane and adjust attributes for those that are "cut". It's not possible to interpolate between vertices in the shader without sending all vertices in a texture and sample it, which would be extremely expensive. Often it would probably be impossible to interpolate the data correcly anyway.
I've also thought of aligning the near plane with the clipping plane which would be an efficient solution.
And drawing a plane after rendering the whole scene and checking for depth-fail will not work either (unless you are looking close to perpendicular to the plane).
What works for a single object is to draw the plane to the depth buffer and then render the object with glDepthFunc(GL_GREATER), but as expected it does not work when one of the objects is behind another. I tried to build on to this concept but eventually ended up with something very similar to shadow volumes and just as expensive.
So what am I missing? How would you do plane clipping in OpenGL ES 2.0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我在 Vuforia SDK 上找到的两个解决方案论坛。
使用 Harri Smatt 的着色器:
<小时>
在 Alessandro Boccalatte 的深度缓冲区中使用四边形:
<块引用>
glColorMask(false, false, false, false);
)glColorMask(true, true, true, true);
)Here is two solutions I've found on Vuforia SDK forums.
Using shaders by Harri Smatt:
Using quad in depth buffer by Alessandro Boccalatte:
由于扩展 EXT_clip_cull_distance 在 OpenGL ES 2.0 中不可用(因为此扩展需要 OpenGL ES 3.0),因此必须模拟裁剪。它可以通过丢弃片段在片段着色器中进行模拟。请参阅片段着色器 - 特殊操作。
另请参阅 OpenGL ES 着色语言 1.00 规范; 6.4 跳跃;第 58 页:
模拟
gl_ClipDistance
的着色器程序可能如下所示:顶点着色器:
片段着色器:
以下 WebGL 示例演示了这一点。请注意,WebGL 1.0 上下文与 OpenGL ES 2.0 非常一致API。
Since the extension EXT_clip_cull_distance is not available in OpenGL ES 2.0 (because for this extension is OpenGL ES 3.0 is required), clipping has to be emulated. It can be emulated in the fragment shader, by discarding fragments. See Fragment Shader - Special operations.
See also OpenGL ES Shading Language 1.00 Specification; 6.4 Jumps; page 58:
A shader program which emulates
gl_ClipDistance
may look like this:Vertex shader:
Fragment shader:
The following WebGL example demonstrates this. Note, the WebGL 1.0 context conforms closely to the OpenGL ES 2.0 API.
我不知道这是否适用于 OpenGL ES,但 OpenGL 具有由 glEnable(GL_CLIP_DISTANCE0) 启用的 gl_ClipDistance 变化输出。启用后,图元将被剪裁,使得在顶点和几何着色器之后 gl_ClipDistance[0] >= 0。
剪辑距离可以指定为与世界空间平面方程的点积:
http://github .prideout.net/clip-planes/
I don't know if this applies to OpenGL ES, but OpenGL has the gl_ClipDistance varying output enabled by glEnable(GL_CLIP_DISTANCE0). Once enabled, the primitive is clipped such that gl_ClipDistance[0] >= 0 after the vertex and geometry shaders.
The clip distance can be specified as just a dot product with a world-space plane equation:
http://github.prideout.net/clip-planes/