OpenGL ES 2.0 中的剪切平面 [英] Clipping-planes in OpenGL ES 2.0

查看:17
本文介绍了OpenGL ES 2.0 中的剪切平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 OpenGL ES 2.0 中的一个剪切平面下剪切数百个对象,我会感谢对这个 OpenGL 子集更有经验的人的想法.

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.

在 OpenGL ES 1.x 中有 glClipPlane.在桌面上,您的着色器中有 glClipPlane 或 gl_ClipDistance.这两个在 OpenGL ES 2.0 中都不可用.似乎这种功能在 2.0 中完全消失了.

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.

似乎唯一的方法是 A) 在片段着色器中运行平面方程,或者 B) 编写一个非常复杂的顶点着色器,如果顶点在平面后面,则将它们定位在平面上.

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) 与 glClipPlane 相比会很慢,因为常规"剪辑是在顶点着色器之后和片段着色器之前完成的,因此每个片段仍然必须进行部分处理和丢弃.

(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) 很难在着色器之间实现兼容,因为我们不能丢弃顶点,我们必须将它们与平面对齐并调整那些剪切"的属性.如果不发送纹理中的所有顶点并对其进行采样,就不可能在着色器中的顶点之间进行插值,这将非常昂贵.通常,无论如何都可能无法正确地插入数据.

(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).

适用于单个对象的方法是将平面绘制到深度缓冲区,然后使用 glDepthFunc(GL_GREATER) 渲染对象,但正如预期的那样,当其中一个对象位于另一个对象后面时,它不起作用.我试图在这个概念的基础上再接再厉,但最终得到了与影子卷非常相似且同样昂贵的东西.

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.

那么我错过了什么?您将如何在 OpenGL ES 2.0 中进行平面裁剪?

So what am I missing? How would you do plane clipping in OpenGL ES 2.0?

推荐答案

这是我在 Vuforia SDK 论坛.

  1. 使用 Harri Smatt 的着色器:

  1. Using shaders by Harri Smatt:

uniform mat4 uModelM;
uniform mat4 uViewProjectionM;
attribute vec3 aPosition;
varying vec3 vPosition;
void main() {
  vec4 pos = uModelM * vec4(aPosition, 1.0);
  gl_Position = uViewProjectionM * pos;
  vPosition = pos.xyz / pos.w;
}

<小时>

precision mediump float;
varying vec3 vPosition;
void main() {
  if (vPosition.z < 0.0) {
    discard;
  } else {
    // Choose actual color for rendering..
  }
}

  • Alessandro Boccalatte 在深度缓冲区中使用四边形:

  • Using quad in depth buffer by Alessandro Boccalatte:

    • 禁用颜色写入(即设置glColorMask(false, false, false, false);)
    • 渲染一个与标记形状相匹配的四边形(即,只是一个与标记的大小和位置/方向相同的四边形);这只会被渲染到深度缓冲区中(因为我们在上一步中禁用了颜色缓冲区写入)
    • 启用颜色遮罩 (glColorMask(true, true, true, true);)
    • 渲染您的 3D 模型

  • 这篇关于OpenGL ES 2.0 中的剪切平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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