1

I'm learning WebGL with haxe and I'm stuck on the part describing element arrays. What is suppose to be a square isn't showing up and I dont know why?

var verticesArray = [   
                             0.5, 0.5,      
                             0.5,-0.5,  
                            -0.5,-0.5, 
                            -0.5, 0.5   
                        ];

    var indicesArray = [0, 1, 3, 1, 2, 3];

    var VBO = GL.createBuffer();
    GL.bindBuffer(GL.ARRAY_BUFFER, VBO);
    GL.bufferData(GL.ARRAY_BUFFER,new Float32Array(verticesArray), GL.STATIC_DRAW);

    var EBO = GL.createBuffer();
    GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, EBO);
    GL.bufferData(GL.ELEMENT_ARRAY_BUFFER,  new UInt16Array(indicesArray), GL.STATIC_DRAW);

    GL.vertexAttribPointer(0, 2, GL.FLOAT, false, 0, 0);
    GL.enableVertexAttribArray(0);

    GL.useProgram(shaderProgram);


    GL.drawElements(GL.TRIANGLES, 6, GL.UNSIGNED_INT, 0);

    GL.bindBuffer(GL.ARRAY_BUFFER, null);
    GL.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, null);

Here's all the code that's suppose to draw the square I have got the shader programing working for sure

4
  • a 16 bit integer is usually referred to as short, not as int. in fact, for 32 bit integer indices you'd need the OES_element_index_uint extension. Commented Feb 4, 2015 at 18:42
  • also, you may not let that vertexAttribPointer wander off too far from the VBO it describes or you will be back tomorrow, after adding another VBO wondering why the pointers don't work any more after you've bound another buffer to that target. Commented Feb 4, 2015 at 18:59
  • @Winchestro thanks that worked, and I moved the vertexattribPointer back next to the VBO calls Commented Feb 4, 2015 at 22:02
  • glad I could help. I guess I could as well formulate it as an answer. Commented Feb 4, 2015 at 22:45

1 Answer 1

6

Make sure the type in your call to drawElements matches the provided index array type, which is

  • gl.UNSIGNED_BYTE for Uint8Array
  • gl.UNSIGNED_SHORT for Uint16Array
  • gl.UNSIGNED_INT for Uint32Array (needs OES_element_index_uint extension)

Also VertexAttribPointer and enableVertexAttribArray calls always operate on the buffer currently bound to the ARRAY_BUFFER target, which in this case isn't a problem, but very well become one the way you wrote it if you add additional VBOs. So either set them after creating and binding the buffer or make otherwise sure the correct buffer is bound.

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

Comments

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.