Should my mesh have more indices than vertices?

Started by
4 comments, last by LorenzoGatti 2 years, 7 months ago

Hi, I'm trying to implement assimp into my game engine. I'm loading the models using the ‘triangulate’ and ‘join identical vertices’ flags. The result of this is that one of my meshes ends up with 3709 vertices and 17226 indices. Is this correct? I thought that the point of indexing was to not have the GPU iterate over a large list of vertices by creating a smaller list of indices instead. In this case wouldn't it be better just to draw the vertices without submitting an index list?

Advertisement

adam7 said:
I thought that the point of indexing was to not have the GPU iterate over a large list of vertices by creating a smaller list of indices instead

No, the idea of indices is the opposite - you have mesh with a a smaller number of shared vertices, which will then be reused via an index (instead of having to duplicate the vertices). Think of a cube (*) - it only has 8 vertices, one for each corner, but you have to draw 2 quads per side. So you then have 3 indices per triangle = 6 per side = 36 in total.

(*) A cube practically is a bad example since the sides will most likely have separate normals; but for smooth edges reusing via indices is normally possible

Some numbers to compare to:

Consider a simple plane tesselated into 10x10 quads. You will have (10+1)*(10+1) vertices, or 121 vertices.

Each quad is 2 triangles, so with a triangle list, you will get 600 indices for 200 triangles.

With a triangle strip formulation, you will get (2+N) indices per strip, and an additional 2 indices between each strip, for a total of (2+20)*10+2*9 indices, or 238 indices for 200 triangles.

However, with a good vertex cache ordering, the indexed triangle list formulation will transform each vertex exactly once, whereas the strip formulation will generally have to re-transform vertices because it can't order “cache optimally,” so the index list will often render faster anyway. (The number 10 may be too small to show this difference well on modern hardware, though, so feel free to scale up as appropriate.)

enum Bool { True, False, FileNotFound };

adam7 said:
The result of this is that one of my meshes ends up with 3709 vertices and 17226 indices. Is this correct?

Let's do some math…

In a typical triangle mesh, each vertex is shared by 6 triangles. (With a quad mesh it's 4 quads per vertex, which is easier to imagine by thinking of a plane tessellated to a regular grid.)

So the expected number of indices = 3709 * 6 = 22,254 indices. Your number is a bit smaller than that, which is expected, and everything seems fine.

Why is the triangle number smaller than expected in most cases?
Think of a cube of quads. 6 quads, 8 vertices. But each vertex has only 3 quads, not 4 as said above. This is because all 8 vertices are irregular and represent a singularity where the surface around the vertex forms a cone, not a plane. And the angle sum of triangles adjacent to a vertex is 270 degrees, not 360 degrees like we would have on the plane.
To make more sense, we now tessellate the cube, so each face becomes a regular grid of 8x8 quads. Now, almost all vertices are regular with 4 quads per vertex, but the 6 irregular corner vertices remain as well. And it#s those 6 vertices which makes our total indices sum a bit smaller than expected in practice.

In general, if a vertex is used it has at least one index, and if it is reused it has more than one index.

Vertices have only one index in specific, not too usual geometry, like triangles that don't share any vertex or edge with one another, or triangle strips and fans where about ⅔ of the required indices are implicit.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement