OpenGL的模板(剪辑实体) [英] OpenGL stencil (Clip Entity)

查看:236
本文介绍了OpenGL的模板(剪辑实体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用精灵剪贴实体。这意味着我想,所有它的子实体被裁剪,以便只重叠剪辑实体(父)子的部分都是可见的。

I am trying to use a sprite as clip-entity. This means I want that all it's child entities are clipped so that only the parts of the children that overlap the clip-entity (parent) are visible.

我想,使用OpenGL的模板函数来完成。在我的模拟器我得到了它的工作,但我的Andr​​oid手机上没有。我在做什么错了?

I'm trying to do that using the OpenGL stencil function. On my emulator I got it working, but on my Android phone it doesn't. What am I doing wrong?

GLES20.glEnable(GLES20.GL_STENCIL_TEST);
GLES20.glClearStencil(0);
GLES20.glStencilMask(1);

GLES20.glClear(GLES20.GL_STENCIL_BUFFER_BIT);

// pre draw
GLES20.glStencilFunc(GLES20.GL_NEVER, 1, 1);
GLES20.glStencilOp(GLES20.GL_REPLACE, GLES20.GL_KEEP, GLES20.GL_KEEP);

// draw clip-entity (this) (sprite)
this.draw();

// post draw
GLES20.glStencilFunc(GLES20.GL_LESS, 2, 1);
GLES20.glStencilOp(GLES20.GL_KEEP, GLES20.GL_KEEP, GLES20.GL_KEEP);

// draw children of clip-entity (this) which should now be clipped
this.drawChildren();

GLES20.glDisable(GLES20.GL_STENCIL_TEST);

推荐答案

使用为你描述听起来是个不错的方法是什么模板缓存。

Using a stencil buffer for what you're describing sounds like a good approach.

您需要确保您请求帧缓冲带的配置模板缓冲区。当使用 GLSurfaceView 在Android上,这是通过重载 setEGLConfigChooser()方法之一完成的。最简单的形式是一个需要红,绿,蓝,阿尔法,深度,和模版的位深度。如果您还需要一个深度缓冲,你可以尝试这样的组合,以获得一个配置与模板:

You need to make sure that you request a framebuffer configuration with a stencil buffer. When using GLSurfaceView on Android, this is done with one of the overloaded setEGLConfigChooser() methods. The simplest form is the one that takes a bit depth for Red, Green, Blue, Alpha, Depth, and Stencil. If you also need a depth buffer, you can try combinations like these to get a config with stencil:

setEGLConfigChooser(8, 8, 8, 8, 24, 8);
setEGLConfigChooser(8, 8, 8, 8, 16, 8);
setEGLConfigChooser(8, 8, 8, 0, 24, 8);
setEGLConfigChooser(8, 8, 8, 0, 16, 8);
setEGLConfigChooser(5, 6, 5, 0, 24, 8);
setEGLConfigChooser(5, 6, 5, 0, 16, 8);

要更加彻底,你也可以使用 setEGLConfigChooser()覆盖,让您注册您自己的 EGLConfigChooser 的实施。利用这一点,你可以枚举配置,并实现自己的逻辑来选择最适合您的需要之一。

To be even more thorough, you can also use the setEGLConfigChooser() override that lets you register your own EGLConfigChooser implementation. Using that, you can enumerate configuration, and implement your own logic to choose the one that best meets your need.

要验证是否确实得到了一个模板缓存,你可以使用:

To verify that you indeed got a stencil buffer, you can use:

GLint stencilBits = 0;
glGetIntegerv(GL_STENCIL_BITS, &stencilBits);

我认为模板支持是标准,但我的方式读取ES 2.0规范,它不是必需的:

I thought that stencil support was standard, but the way I read the ES 2.0 spec, it's not required:

另外,实施或上下文可能不提供深度或模版缓冲器。然而,OpenGL ES的实现必须至少支持一个骗子网络摹与深度位深度。

Further, an implementation or context may not provide depth or stencil buffers. However, an OpenGL ES implementation must support at least one config with a depth bit depth.

我相信至少相对较新的GPU支持模板。如果你必须得到在GPU上,确实你尝试了上述所有后不提供模板这个工作,事情变得更加困难。为您勾勒,你用一个矩形剪辑的例子,那当然可以简单地用剪刀矩形完成。但我敢肯定,你只使用了作说明。

I believe at least relatively recent GPUs support stencil. If you have to get this working on a GPU that indeed does not provide stencil after you tried all of the above, things get more difficult. For the example you sketched, where you clip by a rectangle, that could of course simply be done by using a scissor rectangle. But I'm sure that you only used that for illustration.

根据你使用多少深度缓存为您实际的渲染,可以用深度缓冲玩一些技巧来代替。例如,它可能看起来像这样:

Depending how much you use the depth buffer for your actual rendering, you could play some tricks with the depth buffer instead. For example, it could look like this:

glClearDepthf(0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
// draw clip entity
glEnable(GL_DEPTH_TEST);
// draw children

这样做的缺点是,你需要禁用深度测试您的绘图的一部分,如果需要绘制深度测试,以正确地呈现这可能是完全不能接受的。

The downside of this is that you need to disable depth testing for part of your drawing, which might be completely unacceptable if that drawing needs depth testing to render properly.

这篇关于OpenGL的模板(剪辑实体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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