0
votes

So I created a quad using glBegin(GL_QUADS) and then drawing vertices and now I want to pass into my shader an array of texture coordinates so that I can apply a texture to my quad.

So I'm having some trouble getting the syntax right.

First I made a 2D array of values

GLfloat coords[4][2];
coords[0][0] = 0;
coords[0][1] = 0;
coords[1][0] = 1;
coords[1][1] = 0;
coords[2][0] = 1;
coords[2][1] = 1;
coords[3][0] = 0;
coords[3][1] = 1;

and then I tried to put it into my shader where I have a attribute vec2 texcoordIn

GLint texcoord = glGetAttribLocation(shader->programID(), "texcoordIn");
glEnableVertexAttribArray(texcoord);
glVertexAttribPointer(texcoord, ???, GL_FLOAT, ???, ???, coords);

So I'm confused as to what I should put in for parameters to glVertexAttribPointer that I marked with '???' and I'm also wondering if I'm even allowed to represent the texture coordinates as a 2d array like I did in the first place.

1

1 Answers

0
votes

The proper values would be

glVertexAttribPointer(
 texcoord,
 2, /* two components per element */
 GL_FLOAT,
 GL_FALSE, /* don't normalize, has no effect for floats */
 0, /* distance between elements in sizeof(char), or 0 if tightly packed */
 coords);

and I'm also wondering if I'm even allowed to represent the texture coordinates as a 2d array like I did in the first place.

If you write it in the very way you did above, i.e. using a statically allocated array, then yes, because the C standard asserts that the elements will be tightly packed in memory. However if using a dynamically allocated array of pointers to pointers, then no.