vertex array optimization

Posts: 320
Joined: 2003.06
Post: #1

I'm drawing terrain using an instance variable vertex array, but I dynamically create vertex arrays if the edges do not match in level of detail.

The thing is, that when I make these dynamic vertex arrays, I'm spending most of my CPU time copying individual vertices from my points array.

Geez... this is hard to explain....
I have an array of points/normals/texture coords named _geoMipMaps
I have an array in the form that OpenGL likes of exactly the same points, that I use if the neighbouring patches have the same detail level
Otherwise, I generate an openGL vertex array each frame based on the neighbours. This dynamic array is generated from the initial _geoMipMaps array.

heres a code snippet that shows what I'm doing when I dynamically create this vertex array:

vertex_array[vert1] = _geoMipMaps[detailLevel].columns[x].points[z].x;
            vertex_array[vert1 + 1] = _geoMipMaps[detailLevel].columns[x].points[z].y;
            vertex_array[vert1 + 2] = _geoMipMaps[detailLevel].columns[x].points[z].z;
            vertex_array[vert2] = _geoMipMaps[detailLevel].columns[x].points[zPlusOne].x;
            vertex_array[vert2 + 1] = _geoMipMaps[detailLevel].columns[x].points[zPlusOne].y;
            vertex_array[vert2 + 2] = _geoMipMaps[detailLevel].columns[x].points[zPlusOne].z;
            vertex_array[vert3] = _geoMipMaps[detailLevel].columns[xPlusOne].points[zPlusOne].x;
            vertex_array[vert3 + 1] = _geoMipMaps[detailLevel].columns[xPlusOne].points[zPlusOne].y;
            vertex_array[vert3 + 2] = _geoMipMaps[detailLevel].columns[xPlusOne].points[zPlusOne].z;
            vertex_array[vert4] = _geoMipMaps[detailLevel].columns[xPlusOne].points[z].x;
            vertex_array[vert4 + 1] = _geoMipMaps[detailLevel].columns[xPlusOne].points[z].y;
            vertex_array[vert4 + 2] = _geoMipMaps[detailLevel].columns[xPlusOne].points[z].z;

I then reconcile the edges if they need it, but a lot of the vertices don't change.

So after all that, my question it this. Is there a fast way to initiallize one array with another? If I could stick my instance OpenGL friendly array into the dynamically allocated array initially, I could then only change what is required, rather than rebuilding the thing from scratch.

maybe this is a stupid question.... maybe there is a far better way of doing this whole thing... Please let me know Smile

Chopper, iSight Screensavers, DuckDuckDuck:
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
Probably most of your problem here is unnecessary memory accesses. Take a look in shark at the assembly Smile

Try storing some of those intermediate pointers in locals; ie _geoMipMaps[detailLevel].columns, _geoMipMaps[detailLevel].columns[x].points, _geoMipMaps[detailLevel].columns[xPlusOne].points.

There are some other odd-looking things going on here... looks like you've got a dynamically-allocated 2D array (bad monkey, use a 1D array). It looks like you're copying by column rather than row (bad monkey, copy by row for the cache's sake). It looks like your arrays are of structs; perhaps you can either avoid that or pass them directly to the GL anyway...
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Particle Vertex Array bonanza 9 6,799 Jun 27, 2008 07:46 AM
Last Post: bonanza
  Vertex Array & Interleaved Array troubles TomorrowPlusX 5 7,326 Nov 17, 2007 09:59 AM
Last Post: TomorrowPlusX
  Simple ray-face intersect optimization NYGhost 8 9,790 Aug 17, 2007 12:01 PM
Last Post: NYGhost
  Vertex Array Range bonanza 3 5,354 Jun 19, 2007 12:42 PM
Last Post: OneSadCookie
  Compiled vertex array problem Frank C. 1 3,140 Feb 7, 2006 10:06 PM
Last Post: Frogblast