I'm using the following javascript function to create an array of vertices to pass to a buffer when creating a shape. It should calculate the vertices of an n-sided regular polygon with the first vertex located at the origin. The buffers are then created and stored as properties on the shape object that was passed to the function.
var vertices = [];
// Create each vertex by calculating the position of the new vertex relative
// to the old then adding on to the old position
// Begin by setting up the first vertex at the origin
vertices [0] = 0.0;
vertices [1] = 0.0;
vertices [2] = 0.0;
var oldX = 0.0;
var oldY = 0.0;
for (var i = 0; i < shape.numberOfSides; i++) {
var theta = i * 2 * Math.PI / shape.numberOfSides;
var y = shape.sideLength * Math.sin(theta);
var x = shape.sideLength * Math.cos(theta);
y += oldY;
x += oldX;
var start = (i+1) * 3;
vertices [start] = x;
vertices [start + 1] = y;
vertices [start + 2] = 0.0;
oldX = x;
oldY = y;
}
// Create a buffer and store the vertices on it
shape.verticesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, shape.verticesBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
This works absolutely fine for triangles, but for pentagons or above the shape is not complete - it looks like a crescent. I've included a screenshot below of the working triangle and a non-working hexagon, both created with the same function.
http://i39.tinypic.com/ndoj8z.png
Can anyone see what it is that I'm doing wrong?