Search code examples
c++openglvbotexture-mappingtexture2d

How to add textures to vertex buffer objects in OpenGL (2.1)


What is the correct way to texture map an object in OpenGL 2.1? At the moment I am performing the following steps but am unable to get the texture to appear correctly on the object.

  1. Generate a buffer and store the ID
  2. Bind the buffer
  3. Upload the data to the buffer
  4. Every time the object from the buffer is rendered:

    4.1. Enable GL_TEXTURE_2D, GL_VERTEX_ARRAY and GL_TEXTURE_COORD_ARRAY

    4.2. Bind the texture then bind the buffer

    4.3 Setup glTexCoordPointer then setup glVertexPointer

    4.4 Use glDrawArrays to render the object

    4.5 Call glFlush

Code for creating the VBO:

GLuint flagVBO;
glGenBuffers(1, &flagVBO);
glBindBuffer(GL_ARRAY_BUFFER, flagVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(flag), (void*) flag, GL_STATIC_DRAW);

Code for rendering the VBO:

glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glBindTexture(GL_TEXTURE_2D, gameManager->getTextureManager().getTexture("poleEndFlag")); //The get texture method returns 2 which is the correct texture
glColor3f(1.0, 1.0, 1.0);

glBindBuffer(GL_ARRAY_BUFFER, flagVBO);
glTexCoordPointer(2, GL_FLOAT, sizeof(float)*5, (GLvoid*)3);
glVertexPointer(3, GL_FLOAT, sizeof(float)*5, (GLvoid*)0);
glDrawArrays(GL_QUADS, 0, sizeof(flag) / sizeof(float) / 5);
glFlush();

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_TEXTURE_2D);

flagVBO array:

flag[120] = {
    //Bottom
    -0.1, 1.4, 0.025, 1, 1,
    -0.8, 1.4, 0.025, 0, 1,
    -0.8, 1.4, -0.025, 0, 0,
    -0.1, 1.4, -0.025, 1, 0,
    //Top
    -0.1, 1.95, 0.025, 1, 1,
    -0.8, 1.95, 0.025, 0, 1,
    -0.8, 1.95, -0.025, 0, 0,
    -0.1, 1.95, -0.025, 1, 0,
    //Front
    -0.1, 1.4, 0.025, 1, 0,
    -0.8, 1.4, 0.025, 0, 0,
    -0.8, 1.95, 0.025, 0, 1,
    -0.1, 1.95, 0.025, 1, 1,
    //Back
    -0.1, 1.4, -0.025, 1, 0,
    -0.8, 1.4, -0.025, 0, 0,
    -0.8, 1.95, -0.025, 0, 1,
    -0.1, 1.95, -0.025, 1, 1,
    //Left
    -0.8, 1.95, 0.025, 1, 1,
    -0.8, 1.4, 0.025, 0, 1,
    -0.8, 1.4, -0.025, 0, 0,
    -0.8, 1.95, -0.025, 1, 0,
    //Right
    -0.1, 1.95, 0.025, 1, 1,
    -0.1, 1.4, 0.025, 0, 1,
    -0.1, 1.4, -0.025, 0, 0,
    -0.1, 1.95, -0.025, 1, 0
}

It appears the vertex co-ordinates render correctly but the tex co-ordinates don't. The object is rendered the same color as the main color in the texture but the whole texture is not there.

The image shows the rendered object on the left and the texture it should have on the right

The image shows the rendered object on the left and the texture it should have on the right


Solution

  • Your texture coord pointer is wrong. The offset is in bytes. And the 3 bytes your are specifying are somewhere inbetween the x and y coordinate of your vertex positions. The correct value would be

    glTexCoordPointer(2, GL_FLOAT, sizeof(float)*5, (GLvoid*) (3*sizeof(GLfloat)) );