2013-08-27 42 views
3

我有面指数(指向点)和点,并希望绘制一个循环中的三角形。 Web控制台提供了我这个错误:绘图与webGL的元素

WebGL: drawElements: bound element array buffer is too small for given count and offset 

这是我的代码:

for(var i=1;i<38000;i++){ 
var vtx = new Float32Array(
[points[faces[i][1]][1],points[faces[i][1]][2],points[faces[i][1]][3], 
    points[faces[i][2]][1],points[faces[i][2]][2],points[faces[i][2]][3], 
    points[faces[i][3]][1],points[faces[i][3]][2],points[faces[i][3]][3] 
] 
); 
var idx = new Uint16Array([0, 1]); 
initBuffers(vtx, idx); 
gl.lineWidth(1.0); 
gl.uniform4f(shaderProgram.colorUniform, 0, 0, 0, 1); 
gl.drawElements(gl.LINES, 3, gl.UNSIGNED_SHORT, 0); 
unbindBuffers(); 
} 
} 

例程不画任何东西。我该如何解决这个问题?

回答

5

您的drawElements调用需要第二个参数(count)的不同值。

首先:它表示您正在绘制的顶点数目(不是基元!)。所以(gl.TRIANGLES, 3...)会画一个三角形。 (gl.LINES, 2...)会画一条线,(gl.LINES, 4...)会画2条,但是(gl.LINES, 3...)会画一条线,一条线和一条半?确保你的计数符合原始类型。其次,假设您正在将idx正确上传到缓冲区并对其进行绑定,则只有在您的绘制调用表明您正在绘制三个时才提供两个索引。这可能是你错误的原因。将计数更改为2,并且您的代码应该可以工作(假设您已正确设置了其他所有设置)。至少你会避免你得到的错误信息。