Skip to main content
gamedev.net
🔒 Locked

Reducing Vertex Array

Started by pikebu Nov 21, 2004 at 8:38 AM 0 replies 550+ views
Original Post
pikebu
pikebu
hi, i have an huge array with triangles. every triangle contains 3 vertices. now i don't want to store every vertex for every triangle. e.g. when i have a square i only need 4 vertices. in my array i have 6 vertices - so i want to find these two vertices which both triangles use together and just save a reference to the vertices. so i have something like this: triangle one: 1, 4, 5 triangle two: 1, 4, 3 and i want something like this: vertices: 1, 4, 5, 3 triangle one: vertices[0], vertices[1], vertices[2] triangle two: vertices[0], vertices[1], vertices[3] has anyone an algorithm to create the vertices array and then get the positions in it for the triangle references? hope that someone out there can help me out of this
grhodes_at_work
grhodes_at_work
Its best to create an indexed mesh to begin with, but here is a naive way to convert a non-indexed mesh to an indexed mesh:

vertex_array = list of final vertices
index_array = list of triplets of indices indicating the triangles

In the end, index_array contains triplets of indices that point into vertex_array, e.g., one triangle is given by the vertices vertex_array[index_array[0]], vertex_array[index_array[1]], vertex_array[index_array[2]]. The next triangle is given by vertex_array[index_array[3]], vertex_array[index_array[4]], vertex_array[index_array[5]], etc.

To build the arrays do this:

int index_accumulator = 0;int found_index = 0;int tri_index = 0;for (i = 0; i < number_triangles; i++){  for (j = 0; j < 3; j++)  {    found_index = find_vertex_in_vertex_array(trivert.x,                         trivert.y, trivert.z)    if (found_index >= 0)    {      index_array[tri_index++] = found_index    }    else    {      vertex_array[index_accumulator] = new_vert(trivert.x,                               trivert.y, trivert.z);      index_array[tri_index++] = index_accumulator;      index_accumulator++;    }  }}// epsilon is a tolerance that will fuse nearly-coincident// verts, to allow for roundoff error.float epsilon = 1.0e-5;int find_vertex_in_vertex_array(x, y, z){  for (i = 0; i < index_accumulator; i++)  {    if (distance(vertex_array[index_accumulator] - vert(x,y,z)           < epsilon)      return(i);  }  return(-1);}


Now, the problem with this is the find_vertex_in_vertex_array method will eventually be too slow if your mesh is complex. If you have meshes with more than a few triangles/vertices, you could more rapidly search for the existing vertices using a hash table of some sort, or a spatial partitioning scheme such as a quadtree.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.