Crash when switching to VBOs in OpenGL ES 2.0

94 Views Asked by At

I use this code to render single textured rectangle. I have 2 floats for vertex position, 2 floats for texture coordinates and 1 for texture id.This code works correctly and renders my rectangle (don't mind the weird float data):

public void renderNonVBO(){
    float [] data = new float[]{66.61024f, 27.520004f, 0.0f, 0.206f, 0.0f,
            77.950134f, 49.80019f, 0.141f, 0.206f, 0.0f,
            33.389763f, 72.479996f, 0.0f, 0.206f, 0.0f,
            22.049864f, 50.19981f, 0.0f, 0.206f, 0.0f};

    FloatBuffer b = ByteBuffer.allocateDirect(20 * FLOAT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
    b.put(data);
    b.position(0);

    GLES20.glEnableVertexAttribArray(aPosition);
    GLES20.glVertexAttribPointer(aPosition, POSITION_SIZE, GLES20.GL_FLOAT, false, TOTAL_SIZE * FLOAT_SIZE, b);

    b.position(2);
    GLES20.glEnableVertexAttribArray(aTexPos);
    GLES20.glVertexAttribPointer(aTexPos, TEXTURE_SIZE, GLES20.GL_FLOAT, false, TOTAL_SIZE * FLOAT_SIZE, b);

    b.position(4);
    GLES20.glEnableVertexAttribArray(aTexIdPos);
    GLES20.glVertexAttribPointer(aTexIdPos, TEXTURE_ID_SIZE, GLES20.GL_FLOAT, false, TOTAL_SIZE * FLOAT_SIZE, b);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
}

But when I try to switch to VBOs using this code, my app crashes:

public void renderVBO(){
    float [] data = new float[]{66.61024f, 27.520004f, 0.0f, 0.206f, 0.0f,
            77.950134f, 49.80019f, 0.141f, 0.206f, 0.0f,
            33.389763f, 72.479996f, 0.0f, 0.206f, 0.0f,
            22.049864f, 50.19981f, 0.0f, 0.206f, 0.0f};

    FloatBuffer b = ByteBuffer.allocateDirect(20 * FLOAT_SIZE).order(ByteOrder.nativeOrder()).asFloatBuffer();
    b.put(data);
    b.position(0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
    GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, b.capacity() * FLOAT_SIZE, b, GLES20.GL_STATIC_DRAW);

    GLES20.glEnableVertexAttribArray(aPosition);
    GLES20.glVertexAttribPointer(aPosition, POSITION_SIZE, GLES20.GL_FLOAT, false, TOTAL_SIZE * FLOAT_SIZE, 0);

    GLES20.glEnableVertexAttribArray(aTexPos);
    GLES20.glVertexAttribPointer(aTexPos, TEXTURE_SIZE, GLES20.GL_FLOAT, false, TOTAL_SIZE * FLOAT_SIZE, 8);

    GLES20.glEnableVertexAttribArray(aTexIdPos);
    GLES20.glVertexAttribPointer(aTexIdPos, TEXTURE_ID_SIZE, GLES20.GL_FLOAT, false, TOTAL_SIZE * FLOAT_SIZE, 16);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);
}

The only difference is using VBO and last parameter of glVertexAttribPointer, that is now numeric offset instead of FloatBuffer. buffer[0] was returned by glGenBuffers. When I look into logcat I see this message:

D/emuglGLESv2_enc: sendVertexAttributes: bad offset / len!!!!!

Which is weird, because my offsets are lengths must be correct here, they are the same for both methods. My constants:

final int FLOAT_SIZE = 4;
final int POSITION_SIZE = 2;
final int TEXTURE_SIZE = 2;
final int TEXTURE_ID_SIZE = 1;
final int TOTAL_SIZE = POSITION_SIZE + TEXTURE_SIZE + TEXTURE_ID_SIZE;

What am I doing wrong?

0

There are 0 best solutions below