使用几何着色器进行实例化
所以我想绘制很多四边形(甚至立方体),并偶然发现了这个叫做几何着色器的可爱东西。
我现在有点了解它是如何工作的,我可能可以将其操纵为为顶点缓冲区中的每个顶点绘制一个立方体,但我不确定这是否是正确的方法。几何着色器发生在顶点着色器和片段着色器之间,因此它作用于屏幕空间中的顶点。但我需要它们在世界空间中进行转换。
那么,让我的顶点着色器简单地将输入传送到几何着色器,并在创建图元后让几何着色器乘以 modelviewproj 矩阵,可以吗?统一着色器架构应该没有问题,但是当顶点着色器变得多余时我仍然感到恶心。
还有其他选择吗?或者这真的是“正确”的做法吗?
So I want to draw lots of quads (or even cubes), and stumbled across this lovely thing called the geometry shader.
I kinda get how it works now, and I could probably manipulte it into drawing a cube for every vertex in the vertex buffer, but I'm not sure if it's the right way to do it. The geometry shader happens between the vertex shader and the fragment shader, so it works on the vertices in screen space. But I need them in world space to do transformations.
So, is it OK to have my vertex shader simply pipe the inputs to the geometry shader, and have the geometry shader multiply by the modelviewproj matrix after creating the primitives? It should be no problem with the unified shader architecture, but I still feel queasy when making the vertex shader redundant.
Are there alternatives? Or is this really the 'right' way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
完全没问题。
除此之外,请考虑使用实例化渲染(
glDrawArraysInstanced
、glDrawElementsInstanced
)和顶点属性除数(glVertexAttribDivisor
)。这样你就可以在没有几何着色器的情况下完成相同的任务。例如,您可以有一个规则的立方体几何体边界。然后,您将拥有一个特殊的顶点属性,其中包含每个实例所需的立方体位置。您应该将其与 divisor=1 绑定,这将使其针对绘制的每个实例提前。然后使用
glDraw*Instanced
绘制立方体,并指定实例数。您还可以使用
gl_VertexID
或gl_InstanceID
作为坐标,从纹理中采样输入数据。It is perfectly OK.
Aside from that, consider using instanced rendering (
glDrawArraysInstanced
,glDrawElementsInstanced
) with vertex attribute divisor (glVertexAttribDivisor
). This way you can accomplish the same task without geometry shader at all.For example, you can have a regular cube geometry bound. Then you have a special vertex attribute carrying cube positions you want for each instance. You should bind it with a divisor=1, what will make it advance for each instance drawn. Then draw the cube using
glDraw*Instanced
, specifying the number of instances.You can also sample input data from textures, using
gl_VertexID
orgl_InstanceID
for coordinates.