Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about glDrawRangeElements()

Tags:

c++

render

opengl

I am trying to render some old level data using the glDrawRangeElements() command. My vertices are set up correctly, my indices are set up correctly, but I can't seem to get it to render. I finally checked online and came across the example found here: http://www.songho.ca/opengl/gl_vertexarray.html

From the example, I think I have been doing it incorrectly. Apparently the start is an index value and the ending is an index value, rather than an index into the indices array. I assumed that for example if you wanted to render 10 triangles, the start would be 0 and the ending would be 29 and the count would be 30. But I am apparently wrong?

That would only be correct if the index value at 0, and 29 were in fact 0 and 29. So if the indices started with 400 and ended with 452, the call to that same array would instead be

glDrawRangeElements(GL_TRIANGLES, 400, 452, 29, GL_UNSIGNED_BYTE, indices);

Is that correct? Does anyone else think this is a little counter intuitive? Any other advice about vertex arrays?

like image 620
Satchmo Brown Avatar asked Sep 26 '11 01:09

Satchmo Brown


1 Answers

First, let's talk about glDrawElements, because the Range version is just a modification of that. The count is the number of indices to pull from the source index array to render. Each index pulled maps to a vertex. So if your count is "29", then you are trying to render 29 vertices. This will only render 27 vertices if you use GL_TRIANGLES, since each triangle requires three vertices. OpenGL will discard the extras.

So if you want to render 30 indices, you put 30 in as the count.

Now that we know how to use glDrawElements, let's talk about glDrawRangeElements. When normally using glDrawElements, you specify a location in the source index array to pull from. The indices and count parameters tell OpenGL where to find the indices. But the actual indices pulled from this array could be anywhere within the boundaries of the source vertex array indices.

glDrawRangeElements allows you to give OpenGL a range (inclusive, because that makes sense) of vertex index values. What you are saying is that the indices it gets during this draw call will not exceed that range. This could allow the driver to perform useful optimizations. The start value should be the lowest index value that will be gotten from the index array, and the end should be the highest. It should not simply be the index of the first and last vertices.

like image 95
Nicol Bolas Avatar answered Nov 04 '22 12:11

Nicol Bolas