Warning: trying to learn OpenGL …
I have drawn a square with the following code.
static const GLfloat squareVertices[] = {
-0.5f, -0.33f,
0.5f, -0.33f,
-0.5f, 0.33f,
0.5f, 0.33f,
};
...
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
...
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
So naturally, I thought to add two more vertices and get a hexagon. Instead, I have square that is folded like origami:
static const GLfloat hexagonVertices[] = {
-0.5f, -0.33f,
+0.0f, -0.66f,
+0.5f, -0.33f,
+0.75f, +0.33f,
-0.1f, +0.5f,
+0.25f, +0.33f,
};
...
glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, hexagonVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX);
...
glVertexPointer(3, GL_FLOAT, 0, hexagonVertices);
I have even tried changing the vertices to effect the shape. I can morph the square into a irregular, four-sided polygon, and I can skew the “hexagon”, but it is still just a folded piece of paper.
Can I do it this way?
What is the preferred alternate way?
Your problem comes from sending a value of 3 in the second parameter of glVertexAttribPointer
This tells opengl to use 3 coordinates per vertex instead of the two that you want. Since you have 12 floats, this becomes 4 vertices, hence the square.
Edit: oh, and you need the first parameter of glVertexPointer to be 2 also.