c++opengltexturing

Issue using glTexCoordPointer()


I'm fairly new to OpenGL (and GLSL) and I have an issue using glTexCoordPointer().

I have the texture loaded in and it is rendering on the object correctly (a single quad) but I also get another quad appearing which is a single colour not a part of the loaded texture.

The arrays are defined as follows:

static const GLfloat obj_vert_buf[] = {
    -1, 0, -1,
    -1, 0, 1,
    1, 0, 1,
    1, 0, -1
};

static const GLfloat obj_tex_buf[] = {
    0, 0, 
    0, 1, 
    1, 1,
    1, 0
};

And the relevant excerpt from the draw function:

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);

glGenBuffers(1, &obj_id);

glTexCoordPointer(2, GL_FLOAT, 0, obj_tex_buf);
glVertexPointer(3, GL_FLOAT, 0, obj_vert_buf);
glDrawArrays(GL_QUADS, 0, sizeof(obj_vert_buf) / sizeof(GLfloat));

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY_EXT);

To my understanding glTexCoordPointer()'s first argument specifies the number of elements per vertex which would be two as in:

glTexCoord2f(0.0, 0.0);

The second argument is the type, GLfloat.

The third argument is the offset between each set of elements pertaining to each vertex, so zero after the two stated before (I have also tried it with 2 * sizeof(GLfloat) to no change).

And the fourth argument is a pointer to the start of the data, i.e. obj_tex_buf.

The quad renders correctly and the texture is drawn on it correctly, but I get another random shape coming off from its centre and textured incorrectly, any thoughts would be great. The additional quad isn't visible without the glTexCoordPointer() line.


Solution

  • From the docs:

    count
    Specifies the number of indices to be rendered.
    

    Thus you have to call glDrawArrays(GL_QUADS, 0, 4);

    Please note that GL_QUADS isn't officially supported anymore as of OpenGL 3.1.