1

For one of my WebGL projects I need to generate lots of objects with indices. For examples, cones. When JavaScript generates not so many everything works fine, by adding another cones to the scene rendering becomes glitchy.

works fine with small number of cones

glitchy with lots of them

I am pretty sure that the problem is in defining index buffer:

*var iBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, iBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint8Array(glIndices), gl.STATIC_DRAW);*

To be more specific with Unit8Array or its transition to shader.

Can anyone help me with that?

2
  • Sorry, Uint8Array of course. Commented Nov 8, 2016 at 17:04
  • How about using Uint16Array?! Commented Nov 8, 2016 at 18:20

1 Answer 1

2

If you use Uint8 for indices you are limiting yourself to a maximum of 256 unique vertices per drawcall, so if you batch a draw it's likely you are exceeding the max value of a Uint8 and integer truncation likely means you just end up with effectvely random vertex connectivity across models.

Increase to a Uint16 indices will let you have 65536 unique vertices per draw.

Sign up to request clarification or add additional context in comments.

6 Comments

That's what I did firstly, but it seems that with changing Uint8Array to Uint16Array I need to change something else, because in this case script always renders the same mess. Maybe I need change this line somehow: gl.drawElements( gl.TRIANGLES, indices.length, gl.UNSIGNED_BYTE, 0 );
gl.UNSIGNED_BYTE != 16 bits.
Yeah, that's why I posted gl.drawElements line, but I could find by what it should be replaced. I have tried UNSIGNED_SHORT, but the script stops working and maybe I have to change variable type within shader also.
According to WebGL reference there is only two options: UNSIGNED_BYTE and UNSIGNED_SHORT (16-bit), so I have tried to implement short, but everything stops working returning Offscreen-For-WebGL-0x7f9227023400]GL ERROR :GL_INVALID_OPERATION : glDrawElements: range out of bounds for buffer
I have tried this way: var buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(glIndices), gl.STATIC_DRAW); var iBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buf); var indices = [3, 2, 1]; //or 2, 1, 0 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW); however it returns INVALID_OPERATION: bindBuffer: buffers can not be used with multiple targets and INVALID_OPERATION: bufferData: no buffer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.