glDrawElements and memory allocation

Started by
2 comments, last by zifox 19 years, 10 months ago
i have a question regarding the use of the glDrawElement routine. I'd like to know how does OpenGL expects to have the vertices in the array passed as an argument : - do i have to put all the X coordinates first, then all the Y coordinates, and finally the Z coordinates - or do i need to write the three coordinates one after another for each point ? I raise this question because i try to use glDrawElement and my app crashes on that particular call. Do i fill the array correctly ? I'm not sure Here is how i did: I defined a point, then i allocate an array. I wanted to be able the access the coordinates as if it was in a matrix ( a coordinate on each line)

typedef float Point[3];
 
Point * reducedVerticesArray;
reducedVerticesArray = (Point*)calloc (ReducedPointsContained , 3*sizeof(float))
then i fill the array by picking some values in another array. Here i can access every cell using reducedVerticesArray[j][k]. Then i call:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, reducedVerticesArray);
glDrawElements(GL_POINTS, reducedVerticesArraySize, GL_UNSIGNED_INT, reducedVerticesArray);
glDisableClientState(GL_VERTEX_ARRAY);
and here it crashes (back to windows). I'm wondering what i did wrong ? (i tried with &reducedVerticesArray[0][0], same result) [edited by - zifox on June 2, 2004 6:02:22 PM]
Advertisement
glDrawElements expects your indicies that access the vertex array. Your glDrawElements call should look like this:

glDrawElements( GL_POINTS, numIndices, GL_UNSIGNED_INT, indexArray );


So for a quick example concider these arrays:

float vertices[] =
{ 0, 5, 0,
-5, 0, 0,
5, 0, 0
};

UINT indices[] =
{
0, 1, 2
};

int numIndices = 3;

glVertexPointer( 3, GL_FLOAT, 0, vertices );
glDrawElements( GL_TRIANGLE, numIndices, GL_UNSIGNED_INT, indices );

And that should render a triangle using vertex arrays w/ indices.

If you do not have indices and would rather not make them, you can still use your vertex array but to render use glDrawArray. glDrawArray should be faster than glDrawElements unless you are reusing vertices, then glDrawElements may be faster. Also what could speed up performance (using VBOs this definately will) is to use the extension GL_EXT_draw_range_elements then your render call would be glDrawRangeElements which allows you to specify a valid range of indices which are in your index array.



-SirKnight
thanks for your hint, i'll use glDrawArray instead.

in the matrix example you gave, how is the order ?
Does the X coordinates are the three first values, or the 1st,4th and 7th ?



[edited by - zifox on June 3, 2004 6:49:53 AM]
the order is x,y,z,x,y,z,x,y,z,....

This topic is closed to new replies.

Advertisement