problems with glVertexPointer


Im storing my vertices in an array, where each vertex is x,y,z,w: float

now, if i pass the w component like this

glVertexPointer(4, GL_FLOAT, 0, my_vertices)

all looks good, however if i do something like this

glVertexPointer(3, GL_FLOAT, 4, my_vertices)

my mesh gets messed up.

since i got the w for my normals too, using
glNormalPointer(GL_FLOAT, 4, my_normals)

makes it all messed up

any ideas as to what can be wrong?

glVertexPointer(3, GL_FLOAT, 4, my_vertices) is incorrect. The stride argument should be in bytes, so it should be 16 not 4.

That should fix it.


Also, there is no w component for normals. Normals will always be 3 elements each. The value 4 that you are passing for glNormalPointer is wrong too. That’s the stride, not the number of components. It should be 12. (3 floats * 4 bytes/float)

Using glVertexPointer(3, GL_FLOAT, 4, my_vertices) Is saying that you only have x,y,z (the first parameter is how many components) and as Nutty pointed out, sets the stride to 4. So unless you changed your vertices to not have a w, you should do this.

glVertexPointer(4, GL_FLOAT, 16, my_vertices)

Using glVertexPointer(3, GL_FLOAT, 16, my_verts); will also work, it will just ignore the w component.

Get rid of the W, as you rarely need to specify that anyway, and use

glVertexPointer(3, GL_FLOAT, 0, myverts);
glVertexPointer(3, GL_FLOAT, 12, myverts);

both will work, as your xyz will be tightly packed with no w.

personally I prefer to specify the stride explicitly.


if you’re going to do matrix transformations yourself, you need the W component in both cases. (im using ogl for this, just reusing my matrix lib since i just started)

i see now what i missinterpreted, thanks for the heads up.

the third value is the stride. ie the byte offset between the vertices. i assume u supply the data without any gaps so you need to use zero offset

glVertexPointer(4, GL_FLOAT, 0, my_vertices);

The byte offset between vertices on 4 floats, is 16. The reason you can put 0 in, is because of a tiny little extra feature of that function. If verts are tightly packed, you can put 0 in, and gl will automatically calculate the correct stride.

As I said, I personally prefer to specify it explicity.

I think alot of people get confused on this. They know it works with 0, so they think gl is measuring the byte difference between vert(n).z and vert(n+1).x When it’s not, it’s the difference between vert(n).x and vert(n+1).x


P.S. Why do you need to specify the W component if you write your own matrix code?

that was exactly what i was confusing it with. and the wording in the msdn doc’s doesnt quite help (in my view): “The byte offset between consecutive vertices. When stride is zero, the vertices are tightly packed in the array”

the last part could be rewritten to cause less confusion.

anyway, why i need W? and so i can transform the vectors and vertices with the same matrix (and matrix function) and perform perspective correction.

thanks for the help, its all running perfectly now