freakinpenguin
freakinpenguin

Reputation: 867

OpenGL - auto generation of indices / stride parameter for glDrawArrays

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

Structure of data-points

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?

Upvotes: 3

Views: 1671

Answers (2)

freakinpenguin
freakinpenguin

Reputation: 867

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!

Upvotes: 1

ratchet freak
ratchet freak

Reputation: 48226

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​);

Upvotes: 3

Related Questions