drawElements use

与世无争的帅哥 提交于 2020-01-16 19:45:11

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/13510520/drawelements-use

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!