2
\$\begingroup\$

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)));

\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

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));
\$\endgroup\$
3
  • \$\begingroup\$ I think, that he wants second quad. So last parameter should be .....*28)); \$\endgroup\$
    – zacharmarz
    Mar 30, 2012 at 17:03
  • \$\begingroup\$ @zacharmarz Thanks. Updated the answer accordingly. \$\endgroup\$ Mar 30, 2012 at 17:09
  • \$\begingroup\$ 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). \$\endgroup\$ Mar 30, 2012 at 20:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .