开发者

Using the geometry shader for instancing

开发者 https://www.devze.com 2023-02-10 16:20 出处:网络
So I want to draw lots of quads (or even cubes), and stumbled across this lovely thing called the geometry shader.

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 wh开发者_开发问答en making the vertex shader redundant.

Are there alternatives? Or is this really the 'right' way to do it?


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 or gl_InstanceID for coordinates.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号