OpenGL - auto generation of indices / stride parameter for glDrawArrays

爱⌒轻易说出口 提交于 2019-12-09 23:00:50

问题


I'm rendering a grid-structure with lots of data-points (>1M). The structure of my data is in the picture.

So the content of my index buffer looks like this 0, 100, 1, 101, 2, 102, 3, 103, ...

I'm a bit annoyed by the huge size of my index-buffer which I need to define my triangle strip. Is there a possibility to tell OpenGL to generate these indices automatically in the showed way? Or maybe some trick with glVertexAttribPointer I haven't though of?


回答1:


There is indeed a function for something like this: glDrawElementsBaseVertex

so you can draw them all with:

for(int i=0; i < height-1; i++){
    glDrawElementsBaseVertex(GL_TRIANGLE_STRIP, 200, GL_UNSIGNED_INT, 0, i*100);
}

the index buffer is then just: 0, 100, 1, 101, 2, 102, 3, 103,... 98, 198, 99, 199

With some tweaking you can even use glMultiDrawElementsBaseVertex:

GLsizei *count = new GLsizei[height-1];
GLvoid **indices = new GLvoid[height-1];
GLint *basevertex​ = new GLint[height-1];
for(int i = 0; i< height-1; i++){
    count[i]=200;
    indices[i]=0;
    basevertex​[i]=i*100;
}
glMultiDrawElementsBaseVertex(GL_TRIANGLE_STRIP, count, indices, height-1, basevertex​);



回答2:


The best solution I was able to find is to use glMultiDrawElementsIndirect. The buffer size is lots of smaller than the original one and you can pass all draw-commands with one gl-call. Thanks to derhass for the hint!



来源:https://stackoverflow.com/questions/26755248/opengl-auto-generation-of-indices-stride-parameter-for-gldrawarrays

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