# Duplicating vertices so the count is equal to UV Coordinates

This topic is 1535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I have a model which has a significantly larger UV count than vertex count and I need to know how I can know which vertices to duplicate so as the model displays correctly. I understand that this has something to do with the UV coordinate indices in the mesh but I don't really know this would work practically in code, so a short code example would be very helpful along with a short explanation of how it works.

Thanks in advance for the help.

##### Share on other sites

You should read up on the file format you are trying to read. At least tell its name, otherwise people can only speculate.

##### Share on other sites

I am loading a COLLADA model. Although personally I didn't think it has that much to do with the format specifics. But I may be wrong.....

##### Share on other sites
It doesn’t really matter which format, as far as the overall concept goes—they all work the same.

The vertices, normals, UV’s, etc., are all in what we call “pools”. There is some way in whatever API you are using to access indices into these pools. The total number of indices will be the same for each pool (or else there would be no logical way to recreate the mesh), but while the indices in the pool for vertices will have more repeats the indices into the pool of UV’s will have fewer (hence fewer total vertices than UV’s).

The only thing that is specific to the API is how you access the indices and the pools of vertices, normals, UV’s, etc. We leave that up to you.

The concept is that once you are able to access all of these things, you create a vertex buffer by simply accessing the indices in-order and creating each vertex as specified, repeats allowed.
You then create an index-buffer–vertex-buffer pair by checking your newly created vertex buffer for repeats, eliminating them, and creating an index buffer at the same time.

You need to keep in mind that whatever format serves as your source was not meant for games. It was meant to be an interchange format to exchange graphics data between Maya and 3DS Max (not strictly true, but 99% true). Its definition of an index buffer is not your game’s definition of an index buffer.
It must be deconstructed in this way and then reconstructed for your purposes.

L. Spiro

##### Share on other sites

Sorry, L. Spiro I don't really understand what you mean, are you saying that you use the indices to build the vertex array something like this:

EDIT: Don't worry I am only using COLLADA for interchange to my engine.

// This probably wouldn't work in practice but you get the idea
for( size_t i = 0; i < faces.size( ); i++ )
{
newArray[i].x = initialArray[faces[i]].x;
newArray[i].y = initialArray[faces[i]].y;
newArray[i].z = initialArray[faces[i]].z;
}

Edited by gchris6810

##### Share on other sites

I think there are a few possible reasons for the problem you describe.

1. The easiest.. you have multiple sets of texture coordinates and this should be easy to figure out by checking the counts ;)

2. You misunderstood the concept of the index buffer. The number of elements in the position buffer has nothing to do with the vertex count. It possible that you need different vertices at the same position (in the worst case one per adjacent triangle). You should think about the relations of the different buffer sizes and counts. E.g. IndexCount = PrimitiveCount * 3 (for triangles). If your model consists of multiple meshes you need to consider the offsets too. You may post the different buffer sizes and counts here and I’m sure that we will find out what’s wrong.

3. your model is messed up ;)

Edited by derKai

##### Share on other sites

OK, here is the counts for a single mesh:

number of positions: 342

number of uv coords: 1884

number of faces: 628

##### Share on other sites

I am loading a COLLADA model. Although personally I didn't think it has that much to do with the format specifics. But I may be wrong.....

Collada contains a list of indices into each of the vertex attributes, so yep, it absolutely does have to do with the format specifics

The index stream will say something like 1 2 3 4 5 6

That means build a vertex using position #1 and UV #2, then build a vertex using position #3 and UV #4, etc... (assuming the mesh only has positions and UV, and that positions are the first attribute and UV's are the second attribute).

Check out the spec.

What particular kind of COLLADA tag are you trying to parse?

Edited by Hodgman

##### Share on other sites

I am not trying to parse the tags. I am using OpenCOLLADA to load the model so therefore the information is passed to my application as separate UV index and position index lists. Perhaps I should have been clearer about how I obtained the data.

EDIT: Could we please try and cut through the generality so as I can have something more specific to work on. Thanks.

Edited by gchris6810

##### Share on other sites

OK, here is the counts for a single mesh:

number of positions: 342

number of uv coords: 1884

number of faces: 628

okay.. here you see the number of uv coords is 3 times the number of faces.. this is very typical for textured models..

i've never seen separete index lists per semantic. i guess all your index lists are of the same size (3 * 628)

if thats so you may allocate your vertexbuffer (or buffers if you don't want interleaved vertexbuffers), iterate over the index lists and fill your buffer with the data at the given indices:

positionBuffer[faceCount*3]
tecCoordsBuffer[faceCount*3]

for(int i=0; i<faceCount*3; i++)
{
positionBuffer[i] = positionData[positionIndices[i]];
texCoordBuffer[i] = texCoordData[texCoordIndices[i]];
}


this will flatten you buffers (you don't need the indexbuffer for drawing)

for a interleaved buffer you will create a struct containing fields for position and texcoords and an array of these structs in a similar way

Edited by derKai

##### Share on other sites

The source data indexes multiple streams/pools of vmaps (position, uv) for each individual vertex element. Your mesh may also store discontinuous uv-map data meaning a vertex may have different uv-values associated with it for different triangles connected to the vertex. You can only set a single index buffer, so you'll have to combine position- and uv-data into a single indexable stream.

So, you need to unweld/duplicate vertices. The most intuitive way is to construct both the vertex- and the index stream in a single pass:

- Set up an "unweld-buffer" of vertex-count number of elements, each element in this buffer stores a linked-list of possible position-uv-normal etc. sets for a specific vertex combined with an index into the vertex buffer.

- Loop over all vertices in all triangles. (foreach(mesh->triangles as triangle) foreach(triangle->vertices as vertex))

- Grab for each vertex the set of position, uv, etc. data (relative to the current triangle for discontinuous maps)

- Look for a compatible existing element in the unweld-buffer, if it exists place the existing index associated with this element into the index buffer, if it does not, create a new element, assign it the index "vertex-buffer count + 1" and add it to both the linked list and the vertex buffer.

Edited by eppo

##### Share on other sites

so yep, it absolutely does have to do with the format specifics

I guess what I meant was that every format requires you build up a vertex buffer and then deconstruct it, and that they all keep things in pools, though COLLADA’s pools seem to be 1-for-1 with the other pools. Rare, but still fully applicable.

But those details should be covered in the documentation.
Not only those details but everything else you need except to understand that those formats are not how they are represented in games and that you need to reconstruct the data for games (as described in detail by eppo).

It was my hope that any problems related to any specific API could be resolved by the poster on his or her own by checking the documentation for said API…on his or her own. The thing that caught me so many years ago was being in a mind-trap where I thought the original file format was something like what games use, and it was only when I escaped from that that I realized how to do it right, and I was hoping he’d be able to look at his API and take my words in combination and figure that out for himself.

L. Spiro Edited by L. Spiro

##### Share on other sites

This topic is 1535 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.