I would like to implement a LoD algorithm to my 3D game in order to simplify meshes at certain distances from the player. My meshes are created / loaded using a VBO, so the vertex data is stored in a very large float array as
float[] vertices = {x1, y1, z1, x2, y2, z2, x3, y3, z3 ... xn, yn, zn};
How would I use this data in something like edge colapse
? Would I need to convert this data into a more usable data structure such as a list of faces, or would it be possible to use this directly?
I feel like it would be very difficult to use the vertex array directly, so I have a simple class to represent a triangle using a Vector3f
for each vertex, and an Edge
class for each edge. The data structure for these are pretty simple, I dont feel that I need to show the whole code, so just a simple overview:
The Triangle
class stores
- 3
Vector3f
s to represent each vertex - 3
Edge
s to represent each edge - an equals and hashcode implementation to be able to compare
And the Edge
class stores
- 2
Vector3f
s to represent each vertex - a float representing the length of the edge:
abs(magnitude(subtract(v1, v2)))
- an equals and hashCode implementation to be comparable
I have a method to convert the vertex array into a list of triangles, but it isn't working properly.
public static List<Triangle> vertexArrayToTriangles(float[] vertices, int[] indices)
{
List<Triangle> result = new ArrayList();
Vector3f[] vertexList = new Vector3f[vertices.length / 3];
for (int i = 0; i < vertices.length / 3; i++)
{
vertexList[indices[i]] = new Vector3f(vertices[i], vertices[i + 1], vertices[i + 2]);
}
int i = 0;
while (i < vertexList.length / 3)
{
result.add(new Triangle(vertexList[indices[++i]], vertexList[indices[++i]], vertexList[indices[++i]], new Vector3f(0, 1, 0)));
}
return result;
}
This just makes a spiky jumbled mess of triangles that is extremely laggy to render. Also, during rendering, the triangles are converted back into a float array of vertices.
How would I convert the vertex array into a List<Triangle>
and keep the indices in order?