drawElements use

2019-08-28 04:33发布

I create a little application in WebGL, I have two objects which move, a cube and a sphere. I modify objects with shaders, each object have it shader. So I want update objects on the display at determine time, for this I use drawElements function.

For each object I have a buffer which contains indices of faces in vertices buffer :

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STREAM_DRAW);
indexCount = indices.length;

indices is an array which contains values of indices of each faces. (3 values per face, we work with triangles).

So, after this, for draw triangles of the object I do :

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.drawElements(gl.TRIANGLES, indexCount, gl.UNSIGNED_SHORT, 0);

But I have nothing on the screen, and I have this warning :

WebGL: INVALID_OPERATION: drawElements: attribs not setup correctly

What's could be my error ?

Thanks

标签: webgl
1条回答
Animai°情兽
2楼-- · 2019-08-28 05:35

I think you missed codes which link javascript vertex buffer to shader's attributes before drawElements().

e.g:

gl.bindBuffer(gl.ARRAY_BUFFER, meshVertexPositionBuffer); 
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, 
                     meshVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0); 
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, meshIndexBuffer);
gl.drawElements(gl.TRIANGLES, meshIndexBuffer.numberOfItems, gl.UNSIGNED_SHORT, 0);
查看更多
登录 后发表回答