将顶点放入VertexBuffer时的BufferOverFlowException

时间:2014-03-28 12:25:25

标签: java android opengl-es

我试图使用OpenGL绘制一个圆圈。我有以下代码:

int numberOfVertices = 100;
float[][] vertices = new float[numberOfVertices][2];
private FloatBuffer vertexBuffer; // Buffer for vertex-array

public Circle(){    
    setUpVertices(1.0f);
    // Setup vertex-array buffer. Vertices in float. A float has 4 bytes
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder()); // Use native byte order
    vertexBuffer = vbb.asFloatBuffer(); // Convert byte buffer to float

    //Loop through the vertices and put them in the vertexbuffer
    for (int i = 0; i < numberOfVertices; i++) {
        for (int j = 0; j <= 1; j++) {
            System.out.println("x,y: " + vertices[i][j] + " i,j: (" + i + "," + j + ")");
            vertexBuffer.put(vertices[i][j]); // Copy data into buffer
        }
    }
    vertexBuffer.position(0); // Rewind
}

setUpVertices:

private void setUpVertices(float radius) {
    float theta = (float) (2 * Math.PI / (numberOfVertices - 1));
    float c = (float) Math.cos(theta);
    float s = (float) Math.sin(theta);
    float x = radius;
    float y = 0;

    for (int i = 0; i < numberOfVertices; i++) {
        vertices[i][0] = x;
        vertices[i][1] = y;
        //indices[ii] = (byte) ii;
        float t = x;
        x = c * x - s * y;
        y = s * t + c * y;
    }
}

堆栈跟踪:

03-28 08:09:08.689: E/AndroidRuntime(4776): FATAL EXCEPTION: main
03-28 08:09:08.689: E/AndroidRuntime(4776): Process: se.saxman.oj, PID: 4776
03-28 08:09:08.689: E/AndroidRuntime(4776): java.lang.RuntimeException: Unable to start activity ComponentInfo{se.saxman.oj/se.saxman.oj.Graphics.OJGLActivity}: java.nio.BufferOverflowException
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.os.Looper.loop(Looper.java:136)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at java.lang.reflect.Method.invoke(Method.java:515)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at dalvik.system.NativeStart.main(Native Method)
03-28 08:09:08.689: E/AndroidRuntime(4776): Caused by: java.nio.BufferOverflowException
03-28 08:09:08.689: E/AndroidRuntime(4776):     at java.nio.ByteBufferAsFloatBuffer.put(ByteBufferAsFloatBuffer.java:142)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at se.saxman.oj.Graphics.Circle.<init>(Circle.java:26)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at se.saxman.oj.Graphics.OJGLRenderer.<init>(OJGLRenderer.java:33)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at se.saxman.oj.Graphics.OJGLActivity.onCreate(OJGLActivity.java:16)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.app.Activity.performCreate(Activity.java:5231)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 08:09:08.689: E/AndroidRuntime(4776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-28 08:09:08.689: E/AndroidRuntime(4776):     ... 11 more

为什么我不能把所有这些&#34;进入VB?我把它们打印出来,只有在碰撞之前才到达i = 50。删除for(int j = 0; j&lt; = 1; j ++)几乎可以使它工作,但似乎并不正确。有谁知道问题是什么?

2 个答案:

答案 0 :(得分:2)

我相信你没有为顶点分配足够的空间:

//2 float coordinates per vertex and 4 bytes per float
ByteBuffer vbb = ByteBuffer.allocateDirect(numberOfVertices * 2 * 4);

答案 1 :(得分:0)

使用此方法:

floatBuffer = ByteBuffer.allocateDirect(vertexData.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer().put(vertexData);

将任何float []数组作为vertexData。它将处理所需的大小并修复字节顺序。