简体   繁体   中英

opengles java nio buffers

My game has two screens. Each screen is rendered with a class named SpriteBatcher. The first screen renders fine. The second fails and throws me the above error.

 11-30 13:10:46.530: E/AndroidRuntime(1621): java.lang.IllegalArgumentException: Bad position (limit 0): 2
 11-30 13:10:46.530: E/AndroidRuntime(1621): at java.nio.Buffer.positionImpl(Buffer.java:357)
 11-30 13:10:46.530: E/AndroidRuntime(1621): at java.nio.Buffer.position(Buffer.java:351)
 11-30 13:10:46.530: E/AndroidRuntime(1621):
 atcom.rim.framework.gl.Vertices.bind(Vertices.java:66)

So i am trying to access buffer's position 2 while the buffer is 0 sized. In my code though i dont see any bug.

    this.vertexSize = (2 + (hasColor?4:0) + (hasTexCoords?2:0)) * 4;

    ByteBuffer buffer = ByteBuffer.allocateDirect(maxVertices * vertexSize); ///8 bit = 1 byte, 4 byte = 32bit float
    buffer.order(ByteOrder.nativeOrder());
    vertices = buffer.asFloatBuffer();

The problem as the error states is in my bind() method:

    public void bind() {
    GL10 gl = glGraphics.getGL();

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    vertices.position(0);
    gl.glVertexPointer(2, GL10.GL_FLOAT, vertexSize, vertices);

    if(hasColor) {
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        vertices.position(2);
        gl.glColorPointer(4, GL10.GL_FLOAT, vertexSize, vertices);
    }

    if(hasTexCoords) {
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        vertices.position(hasColor?6:2);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, vertexSize, vertices);
    }
}

The bug was caused by the spritebatcher.bind method. I was passing zero sprites so i ended up rendering nulls...

NIO ByteBuffers have a limit and a capacity . The limit is a "soft" limit; the capacity defines how much the buffer can actually hold. You're trying to position past the soft limit.

On a newly-created buffer, the limit will be equal to the capacity. It sounds like something changed the limit to zero. You can reset the limit (and mark, and position) with the clear() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM