How to minimize glVertexAttribPointer calls when using Instanced Arrays?

谁都会走 提交于 2019-12-04 13:46:06

If you have access to base-instance rendering (requires GL 4.2 or ARB_base_instance), then you could do this. Put the instanced attribute stuff in the setup with the non-instanced attribute stuff:

GLuint loc = glGetAttribLocation(myprogram, "model_matrix_column_1");

for(int count = 0; count < 4; ++count, ++loc)
{
    GLsizei matrixbytes = 4*4*sizeof(GLfloat);
    GLsizei columnbytes = 4*sizeof(GLfloat);
    glVertexAttribPointer(
          loc, 
          4, 
          GL_FLOAT, 
          GL_FALSE, 
          matrixbytes,
          (GLvoid*) (count*columnbytes)
    );
    glEnableVertexAttribArray(loc);
    glVertexAttribDivisor(loc, 1); // matrices are in instanced array
}

Then you just bind the VAO when you're ready to render these models. Your draw call becomes:

glDrawArraysInstancedBaseInstance​(GL_TRIANGLES, vertices_offset, models[i]->num_vertices(), models[i]->num_instances(), matrix_offset);

This feature is surprisingly widely available, even on pre-GL 4.x hardware (as long as it has recent drivers).

Without base instance rendering however, there's nothing you can do. You will have to adjust the instance pointers for each new set of instances you want to render. This is in fact why base instance rendering exists.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!