简体   繁体   English

opengles Java Nio缓冲区

[英]opengles java nio buffers

My game has two screens. 我的游戏有两个屏幕。 Each screen is rendered with a class named SpriteBatcher. 每个屏幕都有一个名为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. 所以我试图在缓冲区大小为0时访问缓冲区的位置2。 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: 错误状态的问题在我的bind()方法中:

    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. 该错误是由spritebatcher.bind方法引起的。 I was passing zero sprites so i ended up rendering nulls... 我传递了零个精灵,所以我最终渲染了空值...

NIO ByteBuffers have a limit and a capacity . NIO ByteBuffers有一个限制和一个容量 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. 您可以使用clear()方法重置限制(以及标记和位置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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