PDA

View Full Version : Efficient use of vertex arrays.



B_old
12-07-2002, 05:35 AM
Hello.
I want to start using vertex arrays and am not quite sure how to best use them.



static GLfloat g_cube_vertices[] = { 1.0f, 1.0f, 1.0f, //0
-1.0f, 1.0f, 1.0f, //1
1.0f, -1.0f, 1.0f, //2
-1.0f, -1.0f, 1.0f, //3

1.0f, 1.0f, -1.0f, //4
-1.0f, 1.0f, -1.0f, //5
1.0f, -1.0f, -1.0f, //6
-1.0f, -1.0f, -1.0f}; //7


static GLubyte g_cube_strip[] = {0, 1, 2,
3, 6, 7,
3, 5, 1,
0, 5, 4,
0, 6, 3,
7, 4, 5};

//somewhere in the drawing funtion
glVertexPointer(3, GL_FLOAT, 0, g_cube_vertices);
glEnableClientState(GL_VERTEX_ARRAY);

glDrawElements(GL_TRIANGLE_STRIP, 18, GL_UNSIGNED_BYTE, g_cube_strip);

This will draw a cube. If I want to draw another cube I simply call glDrawElements again.
Now, when I had never used the vertex array yet I had the impression that you would feed more or less all of your vertex data in the array and then draw it. Now I still draw every cube alone, so I cannot see why I should use it instead of display lists.
Can you show the best way to draw more than 1 object, using the vertex array?
Also I am very confused about how to apply textures, normals and other stuff to the array.
Any help is very appreciated. Thanks.

titan
12-07-2002, 12:52 PM
Check out http://www.cs.rit.edu/~ncs/Courses/570/UserGuide/OpenGLonWin-15.html for how to use VAs.

You're on the right track. For drawing a simple cube chances are vertex arrays will be slightly worse than display lists.

Of course you can use vertex arrarys in your display lists... But unless you use compiled VA's, or even better, nVidia's VAR or ATI's VAO, they might not match display lists for performance.

The reason you would use a VA over a Display List is because once you've made a display list you can't change it. If you're doing skinning or normal mapping on older hardware (no ARB_vertex_program support) you have to manually change your verticies all the time, and VA's become your only option.

Another reason may be for a particle system. Rather than using a matrix transform for each particle, just move each in a VA and then draw the entire array without push/popping a matrix for each particle.

But for static geometry, like a cube or anymechanical object like a robot, Displays lists are awesome!
http://www.fl-tw.com/opengl/GeomBench/ gives you an idea of what kind of performance to expect.

B_old
12-07-2002, 03:00 PM
Another reason may be for a particle system. Rather than using a matrix transform for each particle, just move each in a VA and then draw the entire array without push/popping a matrix for each particle.

Yes! A particle system is excactly what I want to do. I think I understand the basic theory behind your suggestion but I am having a hard time to imagine what the actual code would look like.
Any help here would be extremly cool!
Thanks for your reply!


[This message has been edited by B_old (edited 12-07-2002).]