我正在使用以下 javascript 函数来创建顶点数组,以便在创建形状时传递给缓冲区。它应该计算第一个顶点位于原点的 n 边正多边形的顶点。然后创建缓冲区并将其存储为传递给函数的形状对象的属性。
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);
这对三角形非常有效,但对于五边形或以上的形状并不完整——它看起来像一个新月形。我在下面包含了一个工作三角形和一个非工作六边形的屏幕截图,两者都是使用相同的功能创建的。
http://i39.tinypic.com/ndoj8z.png
谁能看到我做错了什么?