0
votes

As I understand it, glDrawArraysInstanced() will draw a VBO n times. glVertexAttribDivisor() using 1, so that each instance has a unique attribute in the shader. So far I can pass a different color for each instance, with vec4*instances, and each vertex in that instance will share that attribute, hence each instance of a triangle has a different color.

However, what I'm looking for is a type of divisor that will advance the attribute per vertex for each instance, and the best example would be a different color for each vertex in a triangle, for each instance. I would fill a VBO with 3*vec4*instances.

Eg. I want to draw 2 triangles using instancing:

color_vbo [] = {
    vec4, vec4, vec4, // first triangle's vertex colors
    vec4, vec4, vec4 // second triangle's vertex colors
}; // Imagine this is data in a VBO

glDrawArraysInstanced(GL_TRIANGLES, 0, 3(vertexes), 2(instances));

If I set the attribute divisor to 0, it will use the first 3 colors of the color_vbo everywhere, rather than advancing. Effectively each vertex should get the attribute from the VBO as if it were:

color_attribute = color_vbo[(vertex_count * current_instance) + current_vertex];
1
I am in the same situation. I need one vec2 for each vertex. And each instance has 4 vertexes, using GL_TRIANGLE_STRIP. So I have a mat4x2 to put the instanced data to one instance. And then using mat4x2[gl_VertexID] to access only one vec2 for each vertex. I think the theory should be correct. Unfortunately, the render result is not desired, very random.user1914692

1 Answers

2
votes

I don't think what you're asking for is possible using vertex attribute divisors.

It is possible to do this sort of thing using a technique described in OpenGL Insights as "Programmable Vertex Pulling", where you read vertex data from a texture buffer using whatever calculation you like (using gl_VertexID and gl_InstanceID).

Another possibility would be to use three vertex attributes (each with a divisor of 1) to store the colours of each triangle point and use gl_VertexID to choose which of the attributes to use for any given vertex. Obviously this solution doesn't scale to having much more than three vertices per instance.