Search code examples
optimizationopengl-esduplicatestexture-mappingvertex

OpenGL texture mapping without duplicate vertices


I've been using OpenGL for some time now, and I am at a point where I'm doing optimizing of the various aspects of my application.

Here's the problem: In some cases I end up duplicating vertixes for texture mapping. I've been using this book a lot: http://staff.fit.ac.cy/eng.ap/FALL2017/3d-game-development-with-lwjgl.pdf

You can go to pages 70-71 and see the issue and the suggested solution. Basically, it says that when a vertex needs to have a different texture coord for different faces - you've got to duplicate that vertex. And that that's the only solution.

But I hesitate - is it? Lots of UV maps will have cases where the coords are cut apart a bit, for vertexes. I.e. vertexes end up having different coords. Duplicating them seems like a huge waste.

Is there a way to do it a different way?


Solution

  • A vertex is not a just a position. A vertex is the whole vector of the position, color, texture coordinate and whatever else attribute you're putting into it.

    But I hesitate - is it?

    Yes it is. It is also the most efficient way to do this, as is allows for easier caching.

    Is there a way to do it a different way?

    You could use different arrays for each attribute, then have a primary index array to address into secondary index arrays, one for each attribute, that then do the lookup for each attribute.

    Two indirections and very little cache coherence. Overall the overhead of secondary index arrays will consume more memory, than the little overhead of a "few" duplicate position vertices. And you're avoiding double indirection.

    Overall, the way some duplicated vertex positions is also the most performant one.