0
\$\begingroup\$

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 Vector3fs to represent each vertex
  • 3 Edges to represent each edge
  • an equals and hashcode implementation to be able to compare

And the Edge class stores

  • 2 Vector3fs 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?

\$\endgroup\$
  • \$\begingroup\$ Edit: Oh, thats java... nevermind. \$\endgroup\$ – tkausl Jul 23 '15 at 13:00
1
\$\begingroup\$
for (int i = 0; i < vertices.length / 3; i++)
{
    vertexList[indices[i]] = new Vector3f(vertices[i], vertices[i + 1], vertices[i + 2]);
}

In your first run, you're taking vertices[0], vertices[1] and vertices[2], in your second run you're taking vertices[1], vertices[2] and vertices[3], see where the problem is?

And i don't get why you're using vertexList[indices[i]], this makes no sense.

for (int i = 0; i < vertices.length; i+=3)
{
    vertexList[i/3] = 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)));
}

Here you're iterating from 0 to vertexList.length, but you can have more indicies than vertices.

for(int i = 0; i < indices; i+=3)
{
    result.add(new Triangle(vertexList[indices[i]], vertexList[indices[i+1]], vertexList[indices[i+2]], new Vector3f(0, 1, 0)));
}

If the indices start counting from 1, like the wavefront .obj-format does it, you have to subtract 1 from every index.


If this still doesn't work, try passing { 1, 2, 3, 4} as vertices and {0, 1, 2, 0, 2, 3} as indices and step through it with the debugger.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.