Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


glDrawElements and memory allocation

This topic is 5281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
glVertexPointer(3, GL_FLOAT, 0, reducedVerticesArray);
glDrawElements(GL_POINTS, reducedVerticesArraySize, GL_UNSIGNED_INT, reducedVerticesArray);
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]

Share this post

Link to post
Share on other sites
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.


Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!