3
votes

I have a varray full of triangles that make a large square. I'm trying to texture this large square and all i'm getting (it seems) is a one pixel column that is then stretched across the primitive.

I'm texturing each right angled triangle with UV coords (0,1), (0,0), (1,0) and (0,1), (1,1), (1,0) depending on which way up the triangle is.

rendering code.

    glBindTexture(GL_TEXTURE_2D, m_texture.id());

    //glEnableClientState(GL_INDEX_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    //glIndexPointer(GL_INT, 0, &m_indices[0]);
    glVertexPointer(3, GL_FLOAT, sizeof(VertexData), &m_vertexData[0].x);
    glNormalPointer(GL_FLOAT, sizeof(VertexData), &m_vertexData[0].nx);
    glTexCoordPointer(2, GL_FLOAT, sizeof(UVData), &m_uvData[0].u);

    glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, &m_indices[0]);
    glBindTexture(GL_TEXTURE_2D, 0);

texture is wrap mode is GL_CLAMP_TO_EDGE

Screenshot

EDIT

outputting the first two triangles i get values ...

Triangle 1:

Indices(0,1,31)

Vertex0 (-15, 0, -15), Vertex1 (-15,0,-14), Vertex31 (-14, 0, -14)

UV (0,1), (0,0), (1,0)

Triangle 2:

Indices(0, 30, 31)

Vertex0(-15, 0, -15), Vertex30(-14, 0, -15) Vertex31 (-14, 0, -14)

UV (0, 1), (1, 1), (1, 0)

For posterity the full code is here

The two triangles make up a square with the diagonal cut from top left to bottom right.

2
You mean UV0, UV1, UV31, and UV0, UV30, and UV31, right? - Andrew Rasmussen
@arasmussen, Your right about UV0, 1, 31, I was passing 0,1,2. I missunderstood the indices. I think I can only have 1 UV value per vertex. - user245019
Right. So really, you should pack it in with your VertexData. - Andrew Rasmussen

2 Answers

4
votes

Don't use the glIndexPointer, is not what you think it is, is used for color-index mode, to do indexed meshes just use glDrawElements.

0
votes

Looks like you're associating the UV data with the indices, not with the vertices. UV data is part of the vertex data, so you should probably pack it in the VertexData object, rather than keeping it separate. You can keep it separate, but you need one per vertex, not one per index.