1
\$\begingroup\$

I am trying to map UV coordinates to indexed vertices with this code. I am using a vertex buffer, texture buffer, and index buffer (note the vertex and texture buffers are indexed in the same order)

glBindTexture(GL_TEXTURE_2D, staticTexture);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
glTexCoordPointer(2, GL_FLOAT, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, staticVertexBuffer);
glVertexPointer(3, GL_FLOAT, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);

glDrawElements(GL_QUADS, indices.size(), GL_UNSIGNED_INT, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindTexture(GL_TEXTURE_2D, 0);

but when I run the code I get this (compared to the actual image that is supposed to appear on each face)

My code for setting the indices and uv coords (one face of the cube)

uv.push_back(Vector2(0, 1));
uv.push_back(Vector2(1, 1));
uv.push_back(Vector2(1, 0));
uv.push_back(Vector2(0, 0));
indices.push_back(vertexIndexFromCoordinates(x, y, z));
indices.push_back(vertexIndexFromCoordinates(x, y, z + 1));
indices.push_back(vertexIndexFromCoordinates(x, y + 1, z + 1));
indices.push_back(vertexIndexFromCoordinates(x, y + 1, z));

run uv mapping

The only thing I can think of is that the UV's are being mapped by the vertices not the indices.

\$\endgroup\$

closed as off-topic by Josh Feb 14 '14 at 17:25

  • This question does not appear to be about game development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Shouldn't this be on Stack Overflow? \$\endgroup\$ – The Guy with The Hat Feb 14 '14 at 13:21
  • 1
    \$\begingroup\$ This question appears to be off-topic because it is about debugging your code for you. \$\endgroup\$ – Josh Feb 14 '14 at 17:25
4
\$\begingroup\$

Yes, your UVs are being mapped to the vertices, not to the indices -- that's the way that vertex buffers work.

Your vertex isn't just the position -- it's also the color, normal, texture coordinate, and any other attributes which you've attached to it.

In a case like the one here, you'll actually need a separate copy of each vertex for each face, because they don't share texture coordinates.

\$\endgroup\$
  • \$\begingroup\$ So there is no way of doing this like that, where the uv coords are mapped to the indices? \$\endgroup\$ – Jesse Laning Dec 24 '13 at 14:47
  • \$\begingroup\$ That is correct -- there is no way of associating vertex attributes such as texture coordinates with the indices used to initiate the draw call. Vertex attributes may only be associated with the vertex data itself. \$\endgroup\$ – Trevor Powell Dec 24 '13 at 19:17
  • \$\begingroup\$ Dang, I cut my memory usage in half and the time it takes to generate the cubes in half (When I render a lot more cube) by using indices... welp back to the original way I had it, vertex arrays... \$\endgroup\$ – Jesse Laning Dec 24 '13 at 19:19
  • \$\begingroup\$ Do note that you really only need one set of vertex buffer objects to draw as many cubes as you want (as long as they have the same texture coordinates, etc). Just use different matrices for each one, and you can draw the single set of VBOs over and over again, perhaps setting different textures for each, if you want them to have different appearances. \$\endgroup\$ – Trevor Powell Dec 24 '13 at 19:23
  • \$\begingroup\$ Well it is Minecraft like rendering, I'm not just drawing single cubes. \$\endgroup\$ – Jesse Laning Dec 24 '13 at 19:32

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