Questions about glDrawRangeElements()
Asked Answered
H

1

7

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?

Hijacker answered 26/9, 2011 at 1:58 Comment(0)
M
28

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.

Melva answered 26/9, 2011 at 2:19 Comment(8)
You should teach a class as you do a great job of explaining. I am going to correct some errors in my code and try again.Hijacker
One more question for further clarification. Imagine I had indices {2, 11, 6, 4, 0, 3} for two triangles I wanted to render and just for the purpose of the example, I wanted to render them in two batches. The first triangle would be (assuming the index type is byte): glDrawRangeElements(GL_TRIANGLES, 2, 11, 3, GL_UNSIGNED_BYTE, indices); and the second would be glDrawRangeElements(GL_TRIANGLES, 0, 4, 3, GL_UNSIGNED_BYTE, indices + 3); -- Is that correct? If that is the case, I need know the highest and lowest index in each rendering batch.Hijacker
It isn't true that glDrawElements will raise INVALID_OP on 29. It draws with 27, and drops the last 2.Slone
From reading the spec your statement that the start to end range is "half-open" seems incorrect. The spec says that "All vertices referenced by indices must lie between start and end inclusive." Therefore the end value should be set to the highest referenced index value, not the highest referenced index value + 1.Contortive
Are these two equivalent: glDrawElements(mode, end-start, type, data + start) and glDrawRangeElements(mode, start, end, end-start, type, data)?Thadthaddaus
@Thadthaddaus - From the answer provided and the docs, it sounds like those are equivalently in that both are wrong. If you have an index buffer with 3 elements in it, and the index values contained in the index buffer are 200, 201, and 202, a proper call would look like: glDrawElements(mode, 200, 202, 3, type, data); It could also look like this: glDrawElements(mode, 100, 500, 3, type, data);, but it cannot look like this: glDrawElements(mode, 0, 2, 3, type, data); or glDrawElements(mode, 0, 3, 3, type, data); because the actual values are not between 0 and 2 (or 3).Carpentry
I realized the "Range" in the name "glDrawRangeElements" bears the same meaning as in "Range Query" --- that is to find a subset of values that fall in the range, not the subset of indices that fall in the range !Asp
can glDrawRangeElements(mode, start, end, count, type, data) be replaced with glDrawElements(mode, count, type, data) and get the same rendering result?Lynnelle

© 2022 - 2024 — McMap. All rights reserved.