Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL texturing Triangles

Tags:

c++

opengl

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.


1 Answers

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.

like image 185
Dr. Snoopy Avatar answered May 24 '26 23:05

Dr. Snoopy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!