OpenGL裁剪 [英] OpenGL clipping

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

问题描述

我几个小时以来一直在阅读有关剪辑的文章,但似乎找不到解决我问题的方法.

I have been reading articles about clipping now for hours now but I dont seem to find a solution for my problem.

这是我的情况:

在OpenGL ES环境(IOS,Android)中,我有一个由可绘制对象组成的2D场景图,形成一棵树.

Within an OpenGL ES environment (IOS,Android) I am having a 2D scene graph consisting of drawable objects, forming a tree.

每个树节点都有自己的空间空间和变换矩阵,每个节点都将其坐标空间继承给子节点.每个节点都有一个矩形边界框,但是这些边界框不是轴对齐的.

Each tree node has its own spatial room with it's own transformation matrix, and each node inherits its coordinate space to its children. Each node has a rectangular bounding box, but these bounding boxes are not axis aligned.

此设置非常适合渲染2D场景图,遍历树,渲染每个对象然后是子对象.

This setup works perfect to render a 2D scene graph, iterating through the tree, rendering each object and then it's children.

现在出现了我的问题:我正在寻找一种在节点上启用剪切的方法.启用剪切功能后,离开父边界框的区域时应剪切节点的子项.

Now comes my problem: I am looking for a way enable clipping on a node. With clipping enabled, children of a node should be clipped when leaving the area of the parent's bounding box.

例如,我希望有一个节点,其中包含一组作为子节点的文本节点,可以将其与父边界框一起上下滚动,并且在离开父边界框的区域时应进行剪切.

For example I wand to have a node containing a set of text nodes as children, which can be scrolled up and down withing it's parents bounding box, and should be clipped when leaving the area of the parent's bounding box.

由于边界框未沿轴向对齐,因此我无法使用glScissor,这是最简单的方法.

Since bounding boxes are not axis aligned, I cannot use glScissor, which would be the easiest way.

我正在考虑使用模板缓冲区,并在启用裁剪功能后将填充的边界矩形绘制到其中,然后再启用模板缓冲区.这可能会起作用,但会导致另一个问题:如果被裁剪节点内的子节点再次进行裁剪会怎样? -必须为孩子设置模板遮罩,从而擦除父母的模板遮罩.

I was thinking about using the stencil buffer and draw the filled bounding rectangle into it, when clipping is enabled and then enable the stencil buffer. This might workd but leads to another problem: What happens if a child inside a clipped node has clipping again ? - The stencil mask would have to be setup for the child, erasing the parents stencil mask.

我想到的另一个解决方案是对软件进行裁剪.这是可能的,因为在每个节点内,可以在其自己的局部坐标空间中相对容易地进行裁剪.该解决方案的缺点是必须为实现的每个新节点类型实现裁剪.

Another solution I was thinking of, is to do the clipping in software. This would be possible, because within each node, clipping could be done relative easily in it's own local coordinate space. The backdraw of this solution would be that clipping has to be implemented for each new node type that is implemented.

这里有人可以指出正确的方向吗? 我正在寻找的是类似于glscissor的功能,用于裁剪无轴对齐的矩形区域.

Can anybody here point me into the right direction? What I am looking for is something like the functionality of glscissor for clipping non axis aligned rectangular areas.

推荐答案

剪刀盒是满足简单需求的简单工具.如果您的需求不那么简单,那么您将不得不使用不太简单的OpenGL机制进行裁剪:模具缓冲区.

The scissor box is a simple tool for simple needs. If you have less simple needs, then you're going to have to use the less simple OpenGL mechanism for clipping: the stencil buffer.

假设您当前不将模板缓冲区用于其他任何用途,则可以访问8个模板位.通过为场景图中的每个级别赋予比上一个更高的数字,您可以为每个级别分配自己的模板蒙版.当然,如果两个处于相同深度级别的兄弟姐妹重叠,或者只是太近,那么您就会遇到问题.

Assuming that you aren't using the stencil buffer for anything else at present, you have access to 8 stencil bits. By giving each level in your scene graph a higher number than the last, you can assign each level its own stencil mask. Of course, if two siblings at the same depth level overlap, or simply are too close, then you've got a problem.

另一种替代方法是为每个 node 分配一个1到255之间的数字.使用GL_EQUAL的模版测试,您将能够避免上述重叠问题.但是,这意味着整个场景图中最多只能有255个节点.根据您的特定需求,这可能是一个很大的限制.

Another alternative is to assign each node a number from 1 to 255. Using a stencil test of GL_EQUAL, you will be able to prevent the overlap issue above. However, this means that you can have no more than 255 nodes in your entire scene graph. Depending on your particular needs, this may be too much of a restriction.

当通过任何一种方法用完所有位时,您当然可以清除模具(和深度.不要尝试只清除其中一个).但这可能会花费很多,并且需要您清除深度,而您可能不想这样做.

You can of course clear the stencil (and depth. Don't try to clear just one of them) when you run out of bits via either method. But that can be somewhat costly, and it requires that you clear the depth, which you may not want to do.

另一种方法是对不与轴对齐的节点使用数量递增且仅对GL_EQUAL测试 .您可以对大多数节点使用剪刀框,但是如果有一些旋转(无论是在该节点上还是继承的),则可以使用模板蒙版,而在处理图形时会碰到蒙版计数器.同样,您被限制为256个节点,但至少这些仅适用于旋转的节点.根据您的需要以及旋转的节点数,这可能就足够了.

Another method is to use the increasing number and GL_EQUAL test only for non-axis-aligned nodes. You use the scissor box for most nodes, but if there is some rotation (whether on that node or inherited), then you use the stencil mask instead, bumping the mask counter as you process the graph. Again, you are limited to 256 nodes, but at least these are only for rotated nodes. Depending on your needs and how many rotated nodes there are, that may be sufficient.

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

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