Skip to main content
GameDev.net gamedev.net
🔒 Locked

Index UV coordinates!

Started by golgoth Sep 27, 2004 at 2:41 PM 0 replies 1.2k views
Original Post
golgoth
golgoth
to passed uv coordinates to opengl we use something like this: UV *l_uv = in_primitive->GetUV(); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, l_uv); glMatrixMode(GL_TEXTURE); glPushMatrix(); glLoadIdentity(); but how do we passe indexed uv coordinates???? THX
iNsAn1tY
iNsAn1tY
Put simply, you can't. You can only have one index, the array that you pass to glDrawElements, and all your arrays must be indexed by the indices it contains.

As an example, take a model of a cube, that has 8 vertices and 12 UV texture coords. It also has two index buffers: one for the vertices, one for the texture coords, each of which contain 12 * 3 indices (12 triangles in this cube model, three vertices each). There are no duplicate values in the vertex and texture coord arrays, which means that they take up less space on disk. However, when you come to draw this cube model, each vertex has three triangles that use it, and therefore three (potentially different) sets of texture coordinates. With only one index buffer, this can't occur. The solution is to create new arrays and one index buffer that references them all. The new vertex and texture coord arrays will contain some duplicates, but usually, they won't be any more than about 10-20% bigger than they were before. The following bit of code shows how to do this:

// Create the new vertex and texture coord arrays...pNewMesh->m_pvTexCoords = new vec2_t[pNewMesh->m_uiNumTris * 3];pNewMesh->m_pvVertices = new vec3_t[pNewMesh->m_uiNumTris * 3];// Copy all the values from the old arrays to the new ones...for(x = 0; x < pNewMesh->m_uiNumTris * 3; x++){		pNewMesh->m_pvTexCoords[x][0] = pvTexCoords[ pTexCoordIndicesList[x] ][0];	pNewMesh->m_pvTexCoords[x][1] = pvTexCoords[ pTexCoordIndicesList[x] ][1];		pNewMesh->m_pvVertices[x][0] = pvVertices[ pVertexIndicesList[x] ][0];	pNewMesh->m_pvVertices[x][1] = pvVertices[ pVertexIndicesList[x] ][1];	pNewMesh->m_pvVertices[x][2] = pvVertices[ pVertexIndicesList[x] ][2];	}// Create the new index array...pNewMesh->m_piIndices = new unsigned short[pNewMesh->m_uiNumTris * 3];for(x = 0; x < pNewMesh->m_uiNumTris * 3; x += 3){		pNewMesh->m_piIndices[x] = x;	pNewMesh->m_piIndices[x + 1] = x + 1;	pNewMesh->m_piIndices[x + 2] = x + 2;	}

Then, when you come to render, you just load the arrays with the glVertexPointer and glTexCoordPointer functions, and send the index buffer to glDrawElements, as usual...
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.