# What is the correct way to use glTexCoordPointer?

I'm trying to work out how to use this function glTexCoordPointer.

The man page states that I must set a pointer to the first element of the array that uses the texture cordinate.

Here is my array:

static const GLfloat GUIVertices[] = {

//FIRST QUAD
1.0f,  1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
-1.0f,  1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-1.0f, 0.94f, 0.0f, 1.0f, 0.0f, 1.0f,
1.0f, 0.94f, 0.0f, 1.0f, 1.0f, 1.0f,

//2ND QUAD
// x     y      z    w      X    Y
1.0f,  -1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
-1.0f,  -1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-1.0f, -0.94f, 0.0f, 1.0f, 0.0f, 1.0f,
1.0f, -0.94f, 0.0f, 1.0f, 1.0f,  1.0,
};


But how do I set the pointer correctly for the fifth element on the 2nd quad first row? I was thinking something like this:

glTexCoordPointer(1, GL_FLOAT, 6, reinterpret_cast<const GLvoid *>(29 * sizeof(float)));

## 1 Answer

The call you're looking for is

glTexCoordPointer(2, GL_FLOAT, sizeof(float)*6, (GLvoid*)(sizeof(float)*4));


The first argument is the number of elements that need to be read per element (vertex), and it's two because texture coordinates are just u and v coordinates. The second argument describes the type of data being passed. In this case, they're floats.

The third argument is the stride, which is the byte offset between consecutive texture coordinates. In this case, it's simply the size of one of your vertex-texcoord pairs (if I'm understanding the way you packed the data correctly). In other words, there are 6 floats between the start of two texture coordinates and the start of the next two.

Finally, the buffer offset specifies how far into the structure OpenGL must look before it finds the first values. You set it to 4 because you have to instruct OpenGL to skip the first 4 float it reads, as they are the vertex coordinates. Also note that this last pointer is casted to a GLvoid*.

Edit: If you want to point only to the texture coordinates of only the second quad, then use:

glTexCoordPointer(2, GL_FLOAT, sizeof(float)*6, (GLvoid*)(sizeof(float)*28));

• I think, that he wants second quad. So last parameter should be .....*28)); Mar 30, 2012 at 17:03
• @zacharmarz Thanks. Updated the answer accordingly. Mar 30, 2012 at 17:09
• From the looks of it he's also not using VBOs (but is further confused by the documentation relating to them) so you should probably change your answer to reflect this too (or at least add an "if you are not using VBOs" version). Mar 30, 2012 at 20:21