使用 OpenGL 实例渲染具有对象深度和 alpha 混合的 2D 场景 [英] Using OpenGL instancing for rendering 2D scene with object depths and alpha blending

查看:48
本文介绍了使用 OpenGL 实例渲染具有对象深度和 alpha 混合的 2D 场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:我想使用实例化渲染一个由许多对象(四边形)组成的 2D 场景.具有较低 y 值(朝向屏幕底部)的对象需要在具有较高 y 值的对象之前渲染.阿尔法混合也需要工作.

Here's what I'm trying to do: I want to render a 2D scene, consisting of a number of objects (quads), using instancing. Objects with a lower y value (towards the bottom of the screen) need to be rendered in front of the ones with higher y values. And alpha blending also needs to work.

所以我的第一个想法是使用 Z 值作为深度,但我很快意识到 alpha 混合将不起作用,除非以正确的顺序绘制对象.但我不会为每个四边形发出一个调用,而是使用单个实例化调用来渲染整个场景.将实例数据按正确的排序顺序排列似乎对我有用,但我怀疑这是我可以依赖的东西,因为 GPU 应该尽可能并行地运行这些计算.

So my first idea was to use the Z value for depth, but I soon realized alpha blending will not work unless the objects are drawn in the right order. But I'm not issuing one call for each quad, but use a single instanced call to render the whole scene. Putting the instance data in the correct sorted order seems to work for me, but I doubt this is something I can rely on, since the GPU is supposed to run those computations in parallel as much as possible.

所以问题是,有没有办法让这个工作?我现在能想到的最好的事情是为每个单独的 y 值发出一个实例化调用(并按顺序发出,从后到前).有没有更好的方法来做到这一点?

So the question is, is there a way to make this work? The best thing I can think of right now is to issue an instanced call for each separate y value (and issue those in order, back to front). Is there a better way to do this?

推荐答案

实例化最适用于每个实例都是中等大小的情况:成百上千个三角形.四边形不是实例化的好选择.

Instancing is best used for cases where each instance is medium-sized: hundreds or maybe thousands of triangles. Quads are not a good candidate for instancing.

只需构建和渲染一系列三角形.甚至还有一些方法可以有效地解决现代 OpenGL 中缺少 GL_QUADS 原始类型的问题.

Just build and render a sequence of triangles. There are even ways to efficiently get around the lack of a GL_QUADS primitive type in modern OpenGL.

以正确的排序顺序放置实例数据似乎对我有用,但我怀疑这是我可以依赖的东西,因为 GPU 应该尽可能并行地运行这些计算.

Putting the instance data in the correct sorted order seems to work for me, but I doubt this is something I can rely on, since the GPU is supposed to run those computations in parallel as much as possible.

GPU 不是这样工作的.

That's not how GPUs work.

当您发出渲染命令时,您(最终)得到的是一系列基元.因为赋予该命令的顶点是有序的(从头到尾),并且该命令中的实例也是有序的,甚至单个绘制命令中的绘制也是有序的,所以可以为绘制调用中的每个图元分配一个顺序相对于基于顶点、实例和绘制顺序的所有其他图元.

When you issue a rendering command, what you (eventually) get is a sequence of primitives. Because the vertices that were given to that command are ordered (first to last), and the instances in that command are ordered, and even the draws within a single draw command are ordered, an order can be assigned to every primitive in the draw call with respect to every other primitive based on the order of vertices, instances, and draws.

这定义了绘图命令的原始顺序.GPU 保证混合(以及逻辑操作和其他可见的后片段着色器操作)将尊重渲染命令的原始顺序以及渲染命令之间的顺序.也就是说,如果您在一次调用中绘制 2 个三角形,并且第一个在第二个之后(关闭深度测试),则第二个三角形的混合将尊重第一个写入的数据.

This defines the primitive order for a drawing command. GPUs guarantee that blending (and logical operations and other visible post-fragment shader operations) will respect the primitive order of a rendering command and between rendering commands. That is, if you draw 2 triangles in a single call, and the first is behind the second (with depth testing turned off), then blending for the second triangle will respect the data written by the first.

基本上,如果您按顺序将基元提供给 GPU,则 GPU 将尊重该顺序进行混合等.

Basically, if you give primitives to the GPU in an order, the GPU will respect that order with regard to blending and such.

同样,只需构建一个有序的三角形流来表示您的四边形并渲染它们.

So again, just build a ordered stream of triangles to represent your quads and render them.

这篇关于使用 OpenGL 实例渲染具有对象深度和 alpha 混合的 2D 场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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