PDA

View Full Version : glDrawElements - multiple calls, one index array?



Robbie
11-29-2011, 07:10 AM
I have an array of indexes stored with VBO's and I'm currently calling glDrawElements like so:



glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, 0);


Is it possible to make an additional call to glDrawElements to render only a single point from my list of existing indices? I've tried doing it and keep getting an access violation. Here's the method I'm trying to use (with the existing line as well):



glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, 0);
glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, &m_indices[CurrCellIndex]);


Where CurrCellIndex is the index for the vertex that I want a point rendered. What am I doing wrong?

tksuoran
11-29-2011, 07:32 AM
Your last arg is wrong. When using VBO, it is an offset, but you are passing a whole pointer. Try something like CurrCellIndex * sizeof(unsigned int).

Robbie
11-29-2011, 07:45 AM
I don't think I'm following you. does that replace the entire last argument or is that supposed to be &m_indices[CurrCellIndex * sizeof(unsigned int)]?

the book I have says that the last argument is supposed to be a pointer to the start of the array. Should my second argument be the CurrCellIndex?

Robbie
11-29-2011, 08:56 AM
I've tried both glDrawElements and glDrawRangeElements and no matter what I do I can't get it to draw just one point from one index of my existing indices. :(

if I do:



glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, 0);
glDrawElements(GL_POINTS, 0, GL_UNSIGNED_INT, 0);

I get all the points drawn with all the lines. (it's probably drawing all the points twice because my indices are set up for lines (0,1,1,2,etc.))

if I do:



glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, 0);
glDrawRangeElements(GL_POINTS, CurrCellIndex, CurrCellIndex, 1, GL_UNSIGNED_INT, 0);

I get the first point drawn. It only draws the point represented by the first index, ignoring the start and end values I specify via CurrCellIndex.

Interestingly enough, if I put anything other than 0 in my last argument (such as &m_indices[0], or &m_indices[CurrCellIndex]) I get the access violation.

carsten neumann
11-29-2011, 08:59 AM
the last argument is supposed to be a pointer to the start of the array


yes and no. It is a pointer if you use Vertex Arrays (that is if your data is stored in client memory). You are using VBOs which means your data is stored in server memory. The draw functions are overloaded for both uses and depending on what you use (Vertex Arrays vs. VBO) the last argument is either a proper pointer or an offset into the VBO.



Should my second argument be the CurrCellIndex?


no, because you only want to use one index, not CurrCellIndex many.

Robbie
11-29-2011, 09:03 AM
How do I specify an offset then other than 0? I want my offset to be CurrCellIndex, but its not accepting that.

carsten neumann
11-29-2011, 09:10 AM
Actually the above is not entirely correct. It does not matter whether your vertex data is in client or server memory, but where your index data is.
So, if you have uploaded your indices to server memory (using something like glBufferData(GL_ELEMENT_ARRAY_BUFFER, ...) ) and have a buffer bound to GL_ELEMENT_ARRAY_BUFFER when making your draw calls, the "indices" pointers are not really pointers, but offsets into the index buffer.

carsten neumann
11-29-2011, 09:13 AM
As tksuoran said, it should be CurrCellIndex * sizeof(unsigned int):



glDrawElements(GL_LINES, m_indices.size(), GL_UNSIGNED_INT, 0);
glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, CurrCellIndex * sizeof(unsigned int));


Does that not work? Is CurrCellIndex < m_indices.size()?

Robbie
11-29-2011, 10:02 AM
I get an error stating that parameter 4 cannot ve converted from unsigned int to 'const GLvoid'

I tried putting it in parentheses also. CurrCellIndex is < m_indices.size()

tksuoran
11-29-2011, 10:05 AM
You need to cast it. See BUFFER_OFFSET macro on http://www.opengl.org/wiki/Vertex_Buffer_Object

Robbie
11-29-2011, 10:17 AM
Thats exactly what I needed! AND...my code that was built from a sample, already had that definition, it was just not using it.

Thanks so much for the help! Sorry for being so difficult.