gpt4 book ai didi

javascript - 使用 webGL 绘制元素

转载 作者:行者123 更新时间:2023-11-29 09:52:59 25 4
gpt4 key购买 nike

我有面索引(指向点)和点,想循环绘制三 Angular 形。 Web 控制台给我这个错误:

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();
}
}

例程不绘制任何东西。我该如何解决?

最佳答案

您的 drawElements 调用需要为第二个 arg(计数)设置不同的值。

首先:它表示您正在绘制的顶点(不是图元!)的数量。所以 (gl.TRIANGLES, 3...) 会绘制一个三 Angular 形。 (gl.LINES, 2...) 会画一条线,(gl.LINES, 4...) 会画 2 条线,但是 (gl. LINES, 3...) 会画,什么,一条半线?确保您的计数与原始类型匹配。

其次,假设您正确地将 idx 上传到缓冲区并绑定(bind)它,您只提供了两个索引,而您的绘图调用表明您正在绘制三个。这可能是您错误的原因。将计数更改为 2 并且您的代码应该可以工作(假设您已正确设置所有其他内容)。至少您会避免收到错误消息。

关于javascript - 使用 webGL 绘制元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18464085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com