简体   繁体   English

错误剩余()<count <在GLSurfaceView.Renderer中需要

[英]Error remaining() < count < needed in GLSurfaceView.Renderer

I am trying to create a triangle in OpenGL ES. 我正在尝试在OpenGL ES中创建一个三角形。 But app is crashing for line gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff); 但应用程式因gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff); in below code inside draw method. 在下面的代码里面draw方法。

public class GLTriangleEx {

    private float vertices[] = {
        0f, 1f, // point 0
        1f, -1f, // point 1
        -1f, -1f // point 2
    };

    private  FloatBuffer vertBuff;

    private short[] pIndex = {0, 1, 2};

    private ShortBuffer pBuff;

    public GLTriangleEx() {
        ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
        bBuff.order(ByteOrder.nativeOrder());
        vertBuff = bBuff.asFloatBuffer();
        vertBuff.put(vertices);
        vertBuff.position(0);

        ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
        pbBuff.order(ByteOrder.nativeOrder());
        pBuff = pbBuff.asShortBuffer();
        pBuff.put(pIndex);
        pbBuff.position(0);
    }

    public void draw(GL10 gl){
        gl.glFrontFace(GL10.GL_CW);
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);

        // app crashes here.
        gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

Crash log cat is 崩溃日志猫是

            java.lang.ArrayIndexOutOfBoundsException: remaining() < count < needed
            at com.google.android.gles_jni.GLImpl.glDrawElements(Native Method)
            at com.mobility.opengleslearning.GLTriangleEx.draw(GLTriangleEx.java:45)
            at com.mobility.opengleslearning.GLRenderer.onDrawFrame(GLRenderer.java:38)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)

I have checked below links for help but of no use for my case: 我检查了以下链接,以寻求帮助,但对我的情况没有用:

  1. Android OpenGL error: "remaining() < needed" and Android 4.4 Android OpenGL错误:“ remaining()<needed”和Android 4.4

  2. Beginning to learn OpenGL ES. 开始学习OpenGL ES。 Drawing quad 图四

You need to rewind the ShortBuffer you use for the indices. 您需要倒回用于索引的ShortBuffer In this code: 在此代码中:

ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
pbBuff.order(ByteOrder.nativeOrder());
pBuff = pbBuff.asShortBuffer();
pBuff.put(pIndex);
pbBuff.position(0);

you're rewinding pbBuff , which is the underlying ByteBuffer . 您正在倒带pbBuff ,它是底层的ByteBuffer

asShortBuffer() returns a view buffer that shares the underlying data with the original buffer. asShortBuffer()返回与原始缓冲区共享基础数据的视图缓冲区。 From the documentation (emphasis added by me): 文档 (我添加了重点):

A view buffer is simply another buffer whose content is backed by the byte buffer. 视图缓冲区只是另一个缓冲区,其内容由字节缓冲区支持。 Changes to the byte buffer's content will be visible in the view buffer, and vice versa; 对字节缓冲区内容的更改将在视图缓冲区中可见,反之亦然; the two buffers' position , limit, and mark values are independent . 两个缓冲区的位置 ,限制和标记值是独立的

So pBuff , which is your view buffer, has its own position. 因此, pBuff (它是您的视图缓冲区)具有自己的位置。 You need to rewind the view buffer, which is the buffer you use later: 您需要倒退视图缓冲区,这是以后使用的缓冲区:

pBuff.position(0);

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

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