Why Buffers over Arrays?

Started by thinker, October 14, 2003, 17:44:05

Previous topic - Next topic

thinker

The data structures for storing which faces of my mesh use which material and correspond to which vertices is stored as arrays. I use to loop over for collision detection, rendering, etc. I could store it as a Buffer but i believe looping over a buffer would be harder/slower then looping over an array. I could be wrong, and i'll test it at some point, but that is how my data structure works right now.

Do you suggest storing all internal data as Buffers instead of arrays? What technique do you find fastest for looping over the items in a buffer on the Java side?
...
thinker

thinker

I'm new, so help me out here. I need to pass everything as Buffers to LWJGL, but i also need to deal with the data in my game on the Java side. I only see the following options:

1) Store everything as both an array and a Buffer. Pass the Buffer to LWJGL when needed and use the array in Java when needed. High memory cost for duplicate storage.

2) Store everything as a Buffer. Pass the Buffer to LWJGL when needed and loop over the Buffer in Java when needed. High processing cost looping over a Buffer for things like collision detection, etc.

3) Store everything as an array. Covert array to Buffer and pass to LWJGL when needed and use the array in Java when needed. Conversion cost.

Now i'll likely use a mixture of all 3 of these, but i am wondering what other people are doing, and what they find works best/fastest for them.
...
thinker

princec

There's more ways of killing a cat than skinning it, but the general concensus of opinion is that OpenGL is for drawing things fast, and therefore what you feed to OpenGL needs to be accessible by OpenGL, fast. Arrays are not fast, incurring a big JNI overhead compared to buffers.

If you're drawing something, you write it to a buffer, and forget about it. That buffer is likely to exist in AGP RAM anyway if you're doing it right, which means all you can do is simply blat out data to it sequentially.

If you need to think about something, then you don't want to be drawing it; it needs to be stored in an alternative representation which is suitable for thinking, not drawing. Arrays are a great choice for this. Objects are even better.

Cas :)

UlfJack

Have you considered using display lists?

In that case you have one int display list name which is essentially the reference to the rendering data. You can keep the "thinking" data in a separate data structure. You may even consider having different meshes for rendering and collision detection/shadow calculation, etc.

The reason for using display lists is simple: speed. I wrote an OpenGL game for a computer graphics class last year and played around a bit with the different rendering methods and the result was that rendering single polygons is _real_ slow. Using glDrawElements is faster, using the special somthing something extensions for direct AGP buffers and stuff is faster, but MUCH faster is rendering everything to a display list and just calling that list. And I am talking about 1 or 2 orders of magnitude here.

It is even faster if you render to a display list the first frame and use that display list for at least the next 2 (two) frames. (Render ONLY to display list, DON'T render to display list and screen, thats real slow again.)

I did my testing on a linux box with a GeForce 3 card. You should not consider this comprehensive, but OpenGL display lists can really make a HUGE difference.

The impact on your lwjgl code is to keep everything in an array (or whatever structure you like best and is fastest for you cd/sd/whatever) and render it ONCE to an OpenGL display list. Conversion cost is not an issue since your only doing it once anyway.

-- UlfJack