本文介绍了OpenGL ES的模板操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在参考文章一个新的问题出现了。

In reference to the problem diskussed in article OpenGL clipping a new question arises.

我实施基于节点的2D场景图裁剪。每当裁剪框轴对准我使用glScissor像建议。我已经成功实施的那些轴对准盒节点剪裁。

I am implementing the clipping for a node based 2D scene graph. Whenever the clipping boxes are axis aligned I am using the glScissor like proposed in OpenGL clipping. I have sucessfully implemented node clipping of boxes that are axis aligned.

原来,每个节点都有相交它的剪切矩形与使用裁剪它的祖先的人。 (这在与祖先的裁剪框重叠的兄弟姐妹的情况下是必要的)。

It turns out that each node has to intersect it's clipping rectangle with the ones of it's ancestors that use clipping. (That is necessary in case of siblings that are overlapping with the clipping box of an ancestor).

相交的非轴对齐矩形的机构具有使用模板缓冲区来实现。我开始实施所提出的解决方案,但我与具有夹子rects与theyr祖先的人overlaping chidrens问题。

The mechanism of intersecting non axis aligned rectangles has to be implemented using the stencil buffer. I started implementing the proposed solution in OpenGL clipping but am having problems with chidrens having clip rects overlaping with theyr ancestors ones.

现在模板剪裁完美的作品只有一个裁剪框。
但是,在一个子或隆重子与一个祖先的algorith失败相交,因为将需要在这里(例如在轴线对准版本)的2涉及矩形的交集作为掩模和的情况下,不是孩子的全部矩形。

Right now stencil clipping works perfect with only one clipping box.But in case of a child or grand child that intersects with an ancestor the algorith fails, because the intersection of the 2 involved rectangles would be needed here (like in the axis aligned version) as mask and not the full rectangle of the child.

我已经想了如下算法:

最上面的节点写入开始为1的计数器值并绘制它的剪切矩形用1填充到模板缓冲区,使得它的孩子对一,各兄弟模板测试也已削波开启绘制它是由添加 1的模板缓存,然后与2等测试。现在,当一个同级剪裁矩形重叠与祖先之一,这里它们重叠将充满2秒的区域中,针对2测试时,该算法可以扩展到最大255嵌套裁剪节点提供完美裁剪

The topmost node writes starts with a counter value of 1 and draws it's clipping rectangle filled with 1s into the stencil buffer and renders it's children stencil-testing against 1. Each sibling that also has clipping turned on draws it's bounding rectangle by adding 1 to the stencil buffer and then testing against 2 and so on. Now when a siblings clipping rect overlaps with an ancestors one,the region where they overlap will be filled with 2s, giving perfect clipping when testing against 2. This algorithm can be extended to a maximum of 255 nested clipping nodes.

这里说到我的问题:

如何执行渲染到模板缓存的方式,而不是1秒被书写,1S被添加到当前模板缓冲值,每当进行渲染。

How do I perform rendering to the stencil buffer in a way that instead of 1s being writing,1s are added to the current stencil buffer value, whenever rendering is performed.

这是我的code我用prepare渲染到模板缓存:

This is my code I use to prepare for rendering to the stencil buffer:

glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, ref, mask);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

我要寻找一个设置将由裁判,而不是写REF到缓冲区中的价值增加了​​模板缓冲当前值。

I am looking for a setup that will increase the stencil buffers current value by the value of ref instead of writing ref into the buffer.

有人能帮助我吗?

推荐答案

glStencilFunc(GL_ALWAYS,楼盘,面罩); 说:


  • 第一个参数表示,模板对比测试总是成功

  • 第二和第三因而是忽略的,但在原则将设定的常数,从模版缓冲器来的值进行比较,以与该位被用于比较

glStencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE); 的大意是:


  • 当模板测试失败,则新值替换旧(根据第一个参数)

  • 当深度测试失败,但模板测试通过,新值取代旧的(每第二个参数)

  • 当模板和深度测试都通过,新值取代旧的(每第三个参数)

所以,你设置的东西了,让你尝试绘制一切都会过去的模板测试,并且在任何情况下,其价值将它是否通过测试或没有。被复制到模板缓冲

So you're setting things up so that everything you try to draw will pass the stencil test, and in any case its value will be copied into the stencil buffer whether it passes the test or not.

也许你想要做的是改变你的论点 glStencilOp ,让你上的像素执行 GL_INCR 该传球。因此,在你的模板缓存,你会用1由一个多边形,一个'2'的任何地方的任何地方感动,你画了两个连续的多边形,一个'3'端在任何地方你画3等。

Probably what you want to do is to change your arguments to glStencilOp so that you perform a GL_INCR on pixels that pass. So in your stencil buffer you'll end up with a '1' anywhere touched by a single polygon, a '2' anywhere that you drew two successive polygons, a '3' anywhere you drew 3, etc.

在绘制用户可见的东西,你就可以使用类似 glStencilFunc 设置为 GL_GEQUAL 和比较进入值'1','2','3'或任何

When drawing the user-visible stuff you can then use something like glStencilFunc set to GL_GEQUAL and to compare incoming values to '1', '2', '3' or whatever.

如果您preFER不会让你是多少级深的轨道,假设你有一个剪辑区域绘制成模板,你可以修改它到下一个剪辑面积:

If you prefer not to keep track of how many levels deep you are, supposing you have one clip area drawn into the stencil, you can modify it to the next clip area by:


  1. 绘制所有新的几何与 GL_INCR ;这会导致一个模版缓冲器,其中该人在这两个领域的所有像素具有2的值,并且是仅在一个区域中的所有像素具有值1

  2. 画一个全屏幕的多边形,通过只用蜡纸基金 GL_GREATER 和参考值为0, GL_DECR 。结果是0到处这是从来没有画或被涂一次,'1'无处不在的被涂了两次。

  1. drawing all new geometry with GL_INCR; this'll result in a stencil buffer where all pixels that were in both areas have a value of '2' and all pixels that were in only one area have a value of '1'
  2. draw a full screen polygon, to pass only with stencil fund GL_GREATER and reference value 0, with GL_DECR. Result is '0' everywhere that was never painted or was painted only once, '1' everywhere that was painted twice.

这篇关于OpenGL ES的模板操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:21