gpt4 book ai didi

java - 将顶点放入顶点缓冲区时出现 BufferOverFlowException

转载 作者:行者123 更新时间:2023-12-01 23:06:12 24 4
gpt4 key购买 nike

我正在尝试使用 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
}

设置顶点:

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

为什么我不能将它们“全部放入”VB 中?我已经把它们打印出来了,直到 i = 50 才崩溃。删除 for( int j = 0; j <= 1; j++) 几乎可以使其工作,但似乎不正确。有谁知道问题出在哪里吗?

最佳答案

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

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

关于java - 将顶点放入顶点缓冲区时出现 BufferOverFlowException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22712810/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com