缺少一圈圆webgl

时间:2016-10-23 09:24:58

标签: javascript webgl

我试图绘制一个圆圈,而不是得到一个完整的圆圈(360度),我得到几乎完整的圆圈和一个缺少的“切片”。
我错过了什么?

以下是代码:

//init:
            starsList[i].circlePositionBuffer = gl.createBuffer();
            gl.bindBuffer(gl.ARRAY_BUFFER, starsList[i].circlePositionBuffer);
            vertices = [1,0,1];
            for (j = 3; j < 360; j+=3) {        
                vertices[j] = starsList[i].factor * starsList[i].radius * Math.cos(j / 180.0 * Math.PI);
                vertices[j+1] = 0;
                vertices[j+2] = starsList[i].factor * starsList[i].radius * Math.sin(j / 180.0 * Math.PI);
            }

            gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
            starsList[i].circlePositionBuffer.itemSize = 3;
            starsList[i].circlePositionBuffer.numItems = 120;

//draw
            mat4.identity(mvMatrix);
            mat4.translate(mvMatrix, [x, y, z]);
            gl.bindBuffer(gl.ARRAY_BUFFER, starsList[i].circlePositionBuffer);
            gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, starsList[i].circlePositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
            setMatrixUniforms();
            gl.drawArrays(gl.LINE_LOOP, 0, starsList[i].circlePositionBuffer.numItems);

更新 - 已解决

 vertices = [1,0,1];

应该是:

vertices = [starsList[i].factor * starsList[i].radius,0,0];

1 个答案:

答案 0 :(得分:1)

你永远不会回到0°(或360°),尝试更换

for (j = 0; j < 360; j += 3)

通过

for (j = 0; j <= 360; j += 3)

您也可以移动

j / 180.0 * Math.PI

(j % 360) / 180.0 * Math.PI