OpenGL-ArrayIndexOutOfBoundsException: remaining() < count < needed

来源:互联网 发布:模式识别 算法 编辑:程序博客网 时间:2024/06/05 17:03

解决方式就是

FloatBuffer.position(0);//FloatBuffer.flip();ShortBuffer.position(0);//ShortBuffer.flip();

无论如何这难以自圆其说,以下分析纯属扯淡。敬待高论!!

06-08 11:20:09.772 E/Adreno-ES11: <qglDrvAPI_glEnableClientState:1675>: GL_INVALID_ENUM06-08 11:20:09.794 E/AndroidRuntime: FATAL EXCEPTION: GLThread 3592                                     Process: com.example.a3d_study_2, PID: 15266                                     java.lang.ArrayIndexOutOfBoundsException: remaining() < count < needed                                         at com.google.android.gles_jni.GLImpl.glDrawElements(Native Method)                                         at com.example.a3d_study_2.No_4.NonRenderer.onDrawFrame(NonRenderer.java:115)                                         at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)                                         at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)

我的现状:目标是要绘制一个正方形。

private FloatBuffer vertexBuffer;private ShortBuffer orderBufferSquare, orderBufferRectange, orderBufferParallelogram;    private final float[] vertexArray = {            //遵循:右上→右下→左下→左上  的点序。            //First Square            4f, 4f, 0f,            4f, -4f, 0f,            -4f, -4f, 0f,            -4f, 4f, 0f,            //Second rectangle            8f, 7f, 0f,            8f, 6f, 0f,            6f, 6f, 0f,            6f, 7f, 0f,            //Third Parallelogram            15f, 13f, 0f,            14f, 9f, 0f,            10f, 9f, 0f,            11f, 13f, 0f,    };    private final short[] orderArraySquare = {0, 1, 2, 0, 2, 3};    private final short[] orderArrayRectange = {4, 5, 6, 4, 6, 7};    private final short[] orderArrayParallelogram = {8, 9, 10, 8, 10, 11};
  • 把定点坐标放到 FloatBuffer 中。
        ByteBuffer bbVertex = ByteBuffer.allocateDirect(this.vertexArray.length * 4);        bbVertex.order(ByteOrder.nativeOrder());        this.vertexBuffer = bbVertex.asFloatBuffer();        this.vertexBuffer.put(this.vertexArray);        ByteBuffer bbOrderSquare = ByteBuffer.allocateDirect(this.orderArraySquare.length * 2);        bbOrderSquare.order(ByteOrder.nativeOrder());        this.orderBufferSquare = bbOrderSquare.asShortBuffer();        this.orderBufferSquare.put(this.orderArraySquare);

FloatBuffer继承Buffer,并且通过getter/setter方法可以享用父类的参数position,limit等。
limit在ByteBuffer.allocateDirect(this.vertexArray.length * 4)已经确定。
伴随着this.vertexBuffer.put(this.vertexArray);操作 position还会变化为 vertexArray.length

所以执行完上述代码后

vertexBuffer.position == vertexArray.length//12orderBufferSquare.position == orderArraySquare.length//6
  • 执行到下述代码
    @Override    public void onDrawFrame(GL10 gl) {        // Clears the screen and depth buffer.        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);// OpenGL docs.        gl.glLoadIdentity();        gl.glTranslatef(0, 0, -14);        gl.glEnableClientState(GL10.GL_TRIANGLES);        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, this.vertexBuffer);        gl.glDrawElements(GL10.GL_TRIANGLES, this.orderArraySquare.length, GL10.GL_UNSIGNED_SHORT, this.orderBufferSquare);    }}

glVertexPointervertexBuffer.position()处开始读取顶点数据,所以从一开始就没有读到数据(也就是说要绘制的顶点并没有被系统获取到)。这到不会导致报错。
glDrawElements真正要绘制this.orderArraySquare.length个定点所构成的图形时候,就需要从上一步获取到的顶点按this.orderBufferSquare顺序逐个取出来了,发现我们给定的索引导致了 ArrayIndexOutOfBoundsException