Search code examples
iphoneopengl-esvbotexture-mapping

iPhone OpenGL ES1 VBO Texture Coordinate Issues


I switched my code to use VBO and suddenly my sprites (2 triangle triangle-strip) have bad texture coordinates.

Disabling VBO makes the problem go away.

Note - I align my vertex and texture coordinate data to 4 bytes as specified in this link: https://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html

This is a printout of the vertex data, it goes: x, y, z, 0.0f, tu, tv, 0.0f, 0.0f

    Vertex 0 -0.500000 0.500000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 
    Vertex 1 -0.500000 -0.500000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 
    Vertex 2 0.500000 0.500000 0.000000 0.000000 1.000000 1.000000 0.000000 0.000000 
    Vertex 3 0.500000 -0.500000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 

This is how I generate the VBOs:

m_numVertices = 4
m_stride = 8

glGenBuffers( 1, &m_vboVertices); // Generate our Vertex Buffer Object
glBindBuffer( GL_ARRAY_BUFFER, m_vboVertices); // Bind our Vertex Buffer Object
glBufferData( GL_ARRAY_BUFFER, (m_numVertices * m_stride) * sizeof(float), m_pVertexData, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
glBindBuffer( GL_ARRAY_BUFFER, 0 );

glGenBuffers( 1, &m_vboIndices );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_vboIndices );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte)*4, m_pIndices, GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );

And this is how I render the sprite:

glBindBuffer(GL_ARRAY_BUFFER, m_vboVertices);
glVertexPointer( 3, GL_FLOAT, byteStride, BUFFER_OFFSET(0) );
glTexCoordPointer( 2, GL_FLOAT, byteStride, BUFFER_OFFSET(4) );
glBindBuffer(GL_ARRAY_BUFFER,0); // reset

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vboIndices );
glDrawElements(GL_TRIANGLE_STRIP, m_numVertices, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); // reset

BUFFER_OFFSET is defined as:

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

With VBO: http://i.imgur.com/zQ4GA.png

Without VBO: http://i.imgur.com/ByRVz.png

Thanks.

Edit - After messing around a bit with the code, it appears that this problem also occurs when drawing using GL_TRIANGLES.


Solution

  • It appears that the problem is that I am not calculating the glTexCoordPointer offset in bytes. It should be:

    glTexCoordPointer( 2, GL_FLOAT, byteStride, (char*)NULL + (4*sizeof(float)) );
    

    The Macro BUFFER_OFFSET was incorrect.

    Applying this fix solved the problem.