0
votes

I've got a shape I'm trying to draw through OpenGL ES 2.0 on Android 3.2. I can get my scene rendering as intended with DrawArrays and a FloatBuffer, but not through glGenBuffers and a vbo/ibo.

Here are some snippets I'm using with irrelevant sections taken out

    GLES20.glGenBuffers(1, vbo, 0);
    GLES20.glGenBuffers(1, ibo, 0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[0]); 
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, this.vertexBuffer.capacity() * 4, vertexBuffer, GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo[0]);
    GLES20.glBufferData(GLES20.GL_ELEMENT_ARRAY_BUFFER, indexBuffer.capacity() * 2, indexBuffer, GLES20.GL_STATIC_DRAW);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

Above is my code for creating my arrays. Below is my code for rendering.

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[0]);
    GLES20.glEnableVertexAttribArray(positionHandle);
    GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, ibo[0]);

    GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, 1, GLES20.GL_UNSIGNED_SHORT, 0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, 0);

I know for a fact that my vertex float buffer is working correctly because If I replace the above code with the below code, it works as intended :

    GLES20.glEnableVertexAttribArray(positionHandle);
    GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 3);

While I was debugging to find out if DrawArrays would work (which it does if I remove all code pertaining to binding buffers), I found that I could leave all the gen/bind/data buffer calls EXCEPT *GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vbo[0]);*, this one exclusively would prevent rendering when using drawArrays

I've spent all day trying changing little details and calls in different orders to no avail.

At first I was trying to render a cube with the following data : private float[] pts = { -1f, 1f, -1f,

                         1f,    1f,     -1f,
                         -1f,   1f,     1f,
                         1f,    1f,     1f,
                         -1f,   -1f,    -1f,
                         1f,    -1f,    -1f,
                         -1f,   -1f,    1f,
                         1f,    -1f,    1f };
private short indecies[] = {0,1,2,3,6,7,4,5,0,1,1,5,3,7,7,6,6,2,4,0};

But I decided to go with this test data and this works in drawArrays without the use of the indicies

    private float[] pts = {     -1,0,0,
                            1f,0,0,
                            0f,1,0};
private short[] indecies = {0,1,2};
1
In this line "GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer)" last two arguments should probably be 0. In this line "GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP, 1, GLES20.GL_UNSIGNED_SHORT, 0);" replace "1" with indices count.Chiral Code
Thanks, I had tried the second thing you mentioned previously, but the first suggestion was exactly what I needed!!user1055947

1 Answers

2
votes

In case someone else struggles with this later, Chiral Code told me how to fix my problem by replacing

GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, vertexBuffer);

with

GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, 3*4, 0);

Also use the indicies count for the corresponding argument to glDrawElements.