opengl - How attribute divisor works with indexed drawing -
from khronos:
gldrawelementsinstanced behaves identically gldrawelements except primcount instances of set of elements executed. attributes have divisor n n other 0 (as specified glvertexattribdivisor) advance once every n instances.
the 2 ideas of indexed rendering , attribute divisors seem incompatible me. how have index choose location fetch data, , @ same time have (instance count)/(divisor) index fetch data.
it make sense. when using instanced rendering, not want draw exactly same thing multiple times. in fact, not, because drawing same thing multiple times kind of pointless.
instanced rendering intended draw multiple instances of object similar, there still difference between each instance. example, may want draw lot of objects same, positioned differently. or in other words, each instance has own translation vector.
you have 2 main options draw each instance differently:
- you can use
gl_instanceid
in vertex shader. since has different value each instance, can use or calculate different values per instance. - you can use attribute divisor.
the divisor allows have vertex attributes have value per instance. example mentioned above, perfect. can have vertex attribute contains translation vector. in buffer source attribute from, store translation vector value per instance. set divisor attribute 1.
this allows draw multiple instances of object single draw call, different translation vectors. , don't have replicate vertex data.
for existing vertex attributes, vertex positions, normals, , texture coordinates, keep divisor @ default value of 0. means behave did before, value each vertex being fetched based on index in index buffer.
i have never used divisor values larger 1, it's valid. instead of using value each instance, can use same value n instances. imho, far useful values 0 , 1:
- 0, default, specifies attribute behaves usual, meaning value fetched based on vertex index.
- 1 specifies have 1 attribute value per instance, , value fetched based on instance id (
gl_instanceid
).
Comments
Post a Comment