所以,我已经开始了一个相当即时的视觉,三维编程世界之旅.我目前在webgl中具有相当强大的
javascript和大多数面向Web的语言背景,但这是我的第一个图形语言.
在尝试绘制我的第一个相当简单的形状时,我遇到了一个我无法找到解决方案的错误.它读取chrome作为:
WebGL:INVALID_OPERATION:drawElements:attribs设置不正确(重复n次)
其中n是一个看似随机变化的数字.有问题的代码在这里:
var tessVertexPositionBuffer;
var tessVertexColorBuffer;
var tessVertexIndexBuffer;
function initBuffers () {
tessVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexPositionBuffer);
var vertices = [
//innerfront
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
//innerleft
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
//innerback
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
//innerright
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
//topfront
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-2.0, 2.0, 2.0,
2.0, 2.0, 2.0,
//topleft
-1.0, 1.0, 1.0,
-2.0, 2.0, 2.0,
-1.0, 1.0, -1.0,
-2.0, 2.0, -2.0,
//topback
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
-2.0, 2.0, -2.0,
2.0, 2.0, -2.0,
//topright
1.0, 1.0, 1.0,
2.0, 2.0, 2.0,
1.0, 1.0, -1.0,
2.0, 2.0, -2.0,
//outerfront
-2.0, 2.0, 2.0,
2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
2.0, -2.0, 2.0,
//outerleft
-2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
-2.0, 2.0, -2.0,
-2.0, -2.0, -2.0,
//outerback
-2.0, 2.0, -2.0,
2.0, 2.0, -2.0,
-2.0, -2.0, -2.0,
2.0, -2.0, -2.0,
//outerright
2.0, 2.0, 2.0,
2.0, -2.0, 2.0,
2.0, 2.0, -2.0,
2.0, -2.0, -2.0,
//bottomfront
2.0, 2.0, 2.0,
-2.0, 2.0, 2.0,
-2.0, -2.0, 2.0,
2.0, -2.0, 2.0,
//bottomleft
-1.0, -1.0, 1.0,
-2.0, -2.0, 2.0,
-1.0, -1.0, -1.0,
-2.0, -2.0, -2.0,
//bottomback
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
-2.0, -2.0, -2.0,
2.0, -2.0, -2.0,
//bottomright
1.0, -1.0, 1.0,
2.0, -2.0, 2.0,
1.0, -1.0, -1.0,
2.0, -2.0, -2.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
tessVertexPositionBuffer.itemSize = 3;
tessVertexPositionBuffer.numItems = 64;
tessVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexColorBuffer);
var colors = [
[0.7, 0.7, 0.7, 1.0], //all inner sides
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.7, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0], //all top sides
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.0, 0.7, 1.0],
[0.7, 0.7, 0.0, 1.0], //all outer sides
[0.7, 0.7, 0.0, 1.0],
[0.7, 0.7, 0.0, 1.0],
[0.7, 0.7, 0.0, 1.0],
[0.0, 0.7, 0.7, 1.0], //all bottom sides
[0.0, 0.7, 0.7, 1.0],
[0.0, 0.7, 0.7, 1.0],
[0.0, 0.7, 0.7, 1.0],
];
var unpackedColors = [];
for (var i in colors) {
var color = colors[i];
for (var j=0; j< 4; j++) {
unpackedColors = unpackedColors.concat(color);
}
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(unpackedColors), gl.STATIC_DRAW);
tessVertexColorBuffer.itemSize = 4;
tessVertexColorBuffer.numItems = 64;
tessVertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tessVertexIndexBuffer);
var tessVertexIndices = [
0, 1, 2, 0, 2, 3,
4, 5, 6, 5, 6, 7,
8, 9, 10, 9, 10, 11,
12, 13, 14, 13, 14, 15,
16, 17, 18, 17, 18, 19,
20, 21, 22, 21, 22, 23,
24, 25, 26, 25, 26, 27,
28, 29, 30, 29, 30, 31,
32, 33, 34, 33, 34, 35,
36, 37, 38, 37, 38, 39,
40, 41, 42, 41, 42, 43,
44, 45, 46, 45, 46, 47,
48, 49, 50, 48, 50, 51,
52, 53, 54, 53, 54, 55,
56, 57, 58, 57, 58, 59,
60, 61, 62, 61, 62, 63
];
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(tessVertexIndices), gl.STATIC_DRAW);
tessVertexIndexBuffer.itemSize = 1;
tessVertexIndexBuffer.numItems = 96;
}
并且缓冲区的实际绘图如下:
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vetexPositionAttribute, tessVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, tessVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, tessVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tessVertexIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, tessVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
现在,这是相对逐字逐句来自learnwebgl,我只是想绘制一个简单的形状.我相对肯定我的问题在于我的缓冲区类型,因为我真的不太了解它们(我发现的大多数关于webGl的文献都是对语言或HYPERSUPERPRO类型的一般理解的新手).
我真的被卡住了.我已经多次检查了实际的顶点位置/颜色/索引,除非我已经熟悉了足够的代码,我对简单的错误视而不见,我在那里找不到错误.
我非常感谢你能在这个主题上发光的任何灯光.
最佳答案 看起来你的问题只是一个小错字. “实际绘制缓冲区”代码的第二行应该是:
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, tessVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
您错过了该行的vertexPositionAttribute中的第一个r.
我修改了第4课LearningWebGL教程以使用你的代码(使用固定的拼写错误),我有一个链接here.
如果您注意到有一个蓝色三角形,并且在空间中完全相同的位置绘制一个黄色三角形,这会导致一些闪烁.我不确定这是否是故意的.