JNI DETECTED ERROR IN APPLICATION: jarray was NULL

项目中用到了opengles去展示三维点云数据,个人比较生疏,所以打算学一学。根据官方的示例先画了一个三角形,后面在画方形的时候怎么都不成功,报错:JNI DETECTED ERROR IN APPLICATION: jarray was NULL。可是对比别人的代码和自己的代码,根本看不出有什么区别,从别人的博客中看到以下内容:

在Java中当我们要对数据进行更底层的操作时,一般是操作数据的字节(byte)形式,这时经常会用到
ByteBuffer这样一个类。ByteBuffer提供了两种静态实例方式:
    public static ByteBuffer allocate(int capacity)  
    public static ByteBuffer allocateDirect(int capacity)  
为什么要提供两种方式呢?这与Java的内存使用机制有关。第一种分配方式产生的内存开销是在JVM中的,
而另外一种的分配方式产生的开销在JVM之外,以就是系统级的内存分配。当Java程序接收到外部传来的数据时,
首先是被系统内存所获取,然后在由系统内存复制复制到JVM内存中供Java程序使用。所以在另外一种分配方式
中,能够省去复制这一步操作,效率上会有所提高。可是系统级内存的分配比起JVM内存的分配要耗时得多,所以
并非不论什么时候allocateDirect的操作效率都是最高的

前面在写三角形之前已经了解到了,opengles是不能直接使用java jvm分配的内存的,要将数组转换到系统内存才能使用,

我的示例里面因为用了这个所以一直报数组是空的

改成这样,方形就绘制出来了。

三角形和方形源码

public class Triangle {
    private FloatBuffer vertextBuffer;

    //number of coordinates per vertex in this array
    //每个顶点的坐标包括x,y,z 三个
    static final int COORDS_PER_VERTEXT = 3;

    //顺时针的顺序
    static float triangleCoords[] = {   // in counterclockwise order:
            0.0f, 0.622008459f, 0.0f, // top
            -0.5f, -0.311004243f, 0.0f, // bottom left
            0.5f, -0.311004243f, 0.0f  // bottom right
    };

    // Set color with red, green, blue and alpha (opacity) values
    float color[] = {0.63671875f, 0.76953125f, 0.22265625f, 1.0f};

    //1.为顶点在native层分配缓冲区内存,然后将顶点坐标放进缓冲区

    //2.绘制需要顶点着色器和片段着色器

    //绘制形状需要一个顶点着色程序和一个片段着色程序
    private final String vertexShaderCode =
            "attribute vec4 vPosition;" +
                    "void main() {" +
                    "  gl_Position = vPosition;" +
                    "}";

    private final String fragmentShaderCode =
            "precision mediump float;" +
                    "uniform vec4 vColor;" +
                    "void main() {" +
                    "  gl_FragColor = vColor;" +
                    "}";

    private final int mProgram;


    private int positionHandle;
    private int colorHandle;

    //顶点数
    private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEXT;

    //每个顶点占的字节数
    private final int vertexStride = COORDS_PER_VERTEXT * 4;

    public Triangle() {
        // 初始化顶点字节的缓冲区
        //需要在native 层分配内存
        ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
        // 使用设备硬件的本机字节顺序
        bb.order(ByteOrder.nativeOrder());
        // 创建float缓冲区
        vertextBuffer = bb.asFloatBuffer();
        //将顶点坐标放进float缓冲区
        vertextBuffer.put(triangleCoords);
        // 设置缓冲区以读取第一个坐标
        vertextBuffer.position(0);


        int vertextShader = GlRender_1.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = GlRender_1.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
        // create empty OpenGL ES Program
        mProgram = GLES20.glCreateProgram();

        //将顶点着色器添加到项目
        GLES20.glAttachShader(mProgram, vertextShader);
        GLES20.glAttachShader(mProgram, fragmentShader);
        // creates OpenGL ES program executables
        GLES20.glLinkProgram(mProgram);
    }


    public void draw() {
        // Add program to OpenGL ES environment
        GLES20.glUseProgram(mProgram);

        // get handle to vertex shader's vPosition member
        positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
        // Enable a handle to the triangle vertices
        GLES20.glEnableVertexAttribArray(positionHandle);

        // Prepare the triangle coordinate data
        GLES20.glVertexAttribPointer(positionHandle,
                vertexCount,
                GLES20.GL_FLOAT,
                false,
                vertexStride,
                vertextBuffer);
        // get handle to fragment shader's vColor member
        colorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
        // Set color for drawing the triangle
        GLES20.glUniform4fv(colorHandle, 1, color, 0);

        // Draw the triangle
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
        // Disable vertex array
        GLES20.glDisableVertexAttribArray(positionHandle);
    }

}
public class Square {
    //1.顶点坐标缓冲区
    private FloatBuffer vertextBuffer;
    //2.绘制顺序缓冲区
    private ShortBuffer drawListBuffer;

    //每个顶点包含坐标个数
    static final int COORDS_PER_VERTEXT = 3;

    static float squareCoords[] = {
            -0.5f, 0.5f, 0.0f,   // top left
            -0.5f, -0.5f, 0.0f,   // bottom left
            0.5f, -0.5f, 0.0f,   // bottom right
            0.5f, 0.5f, 0.0f}; // top right

    static short drawOrder[] = {0, 1, 2, 0, 2, 3};

    private final int vertexCount = squareCoords.length / COORDS_PER_VERTEXT;
    private final int vertextStride = COORDS_PER_VERTEXT * 4;

    private final int mProgram;

    //绘制形状需要一个顶点着色程序和一个片段着色程序
    private final String vertexShaderCode =
            "attribute vec4 vPosition;" +
                    "void main() {" +
                    "  gl_Position = vPosition;" +
                    "}";

    private final String fragmentShaderCode =
            "precision mediump float;" +
                    "uniform vec4 vColor;" +
                    "void main() {" +
                    "  gl_FragColor = vColor;" +
                    "}";

    // Set color with red, green, blue and alpha (opacity) values
    float color[] = {0.63671875f, 0.76953125f, 0.22265625f, 1.0f};
    private int positionHandle;
    private int colorHandle;

    public Square() {
        //1.在native 层分配内存 float 占4个字节
        ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
        //字节顺序
        bb.order(ByteOrder.nativeOrder());

        vertextBuffer = bb.asFloatBuffer();
        vertextBuffer.put(squareCoords);
        vertextBuffer.position(0);

        ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
        dlb.order(ByteOrder.nativeOrder());

        drawListBuffer = dlb.asShortBuffer();
        drawListBuffer.put(drawOrder);
        drawListBuffer.position(0);

        int vertexShader = GlRender_1.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = GlRender_1.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        mProgram = GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgram, vertexShader);
        GLES20.glAttachShader(mProgram, fragmentShader);
        GLES20.glLinkProgram(mProgram);
    }


    public void draw() {
        GLES20.glUseProgram(mProgram);
        //顶点
        positionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
        GLES20.glEnableVertexAttribArray(positionHandle);
        GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEXT, GLES20.GL_FLOAT,
                false, vertextStride, vertextBuffer);
        //颜色
        colorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
        GLES20.glUniform4fv(colorHandle, 1, color, 0);
        /**索引法绘制矩形*/
        Log.i("test", "draw: "+drawListBuffer.toString());
        Log.i("test", "draw: "+ Arrays.toString(drawOrder));
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
        //Disable vertex array
        GLES20.glDisableVertexAttribArray(positionHandle);
    }


}

 

说明:博客的所有内容均为自己探索实现的,受自身知识点的限制很多地方不一定正确的

你可能感兴趣的:(Opengl,es)