gpt4 book ai didi

java - glDrawElements() 会导致 Lollipop 崩溃,但在 KitKat 上正常

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:23 31 4
gpt4 key购买 nike

我正在尝试学习适用于 Android 的 OpenGL ES 1.0。我的应用程序运行良好,直到今天早上我将设备升级到 Android 5.0.1 Lollipop。我最初尝试调试这个问题很快就发现我的应用程序仍然可以在运行 KitKat 的模拟器上运行,但在 Lollipop 上崩溃,无论是在我的设备上还是在模拟器上。

我的应用程序使用 OpenGL 绘制一个简单的立方体,每一面都有不同的纹理。我已经对其进行了故障排除,直到它在 glDrawElements() 行崩溃的地方。

package com.briansworld.gravitycubestep7;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

// draw a cube
// store the cube's position within the multicube
// bind the textures here
class Cube
{
private FloatBuffer mVertexBuffer;
private FloatBuffer myTexBuffer;
private ByteBuffer myIndexBuffer;
public int x, y, z; // used to keep track of which cube is which, not cube position

// constructor
public Cube(int x, int y, int z) // need to add texture ID's
{
this.x = x;
this.y = y;
this.z = z;

float vertices[] =
{
-1.0f, -1.0f, 1.0f, // front
1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,

1.0f, -1.0f, 1.0f, // right
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,

1.0f, -1.0f, -1.0f, // rear
-1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,

-1.0f, -1.0f, -1.0f, // left
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,

-1.0f, -1.0f, -1.0f, // bottom
1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,

-1.0f, 1.0f, -1.0f, // top
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f
};

float texBuffer[] =
{
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,

0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,

0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,

0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,

0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,

0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};

byte indexBuffer[] =
{
0, 1, 3, 0, 3, 2,
4, 5, 7, 4, 7, 6,
8, 9, 11, 8, 11, 10,
12, 13, 15, 12, 15, 14,
16, 17, 19, 16, 19, 18,
20, 21, 23, 20, 23, 22
};

ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
mVertexBuffer = byteBuf.asFloatBuffer();
mVertexBuffer.put(vertices);
mVertexBuffer.position(0);

byteBuf = ByteBuffer.allocateDirect(texBuffer.length * 4);
byteBuf.order(ByteOrder.nativeOrder());
myTexBuffer = byteBuf.asFloatBuffer();
myTexBuffer.put(texBuffer);
myTexBuffer.position(0);

myIndexBuffer = ByteBuffer.allocate(indexBuffer.length);
myIndexBuffer.put(indexBuffer);
myIndexBuffer.position(0);
}


// need to add functionality to only draw viewable/outside textures
public void draw(GL10 gl, int[] texture)
{
// enable vertex and texture states
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

// set the font face rotation
gl.glFrontFace(GL10.GL_CW);

// set the pointers to the buffers
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, myTexBuffer);

// step each face of the cube and attach a different texture to each side
for (int i = 0; i < 6; i++)
{
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture[i]); // bind the textures
myIndexBuffer.position(6 * i); // step through the buffer
gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, myIndexBuffer);
}

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}

目录中错误消息的前几行是:

JNI 在应用程序中检测到错误:无效元素指针 0x12ce4382,数组元素为 0x12ce437c

in call to ReleaseArrayElements

from void com.google.android.gles_jni.GLImpl.glDrawElements(int, int, int, java.nio.Buffer)

“GLThread 147”prio=5 tid=12 可运行

| group="main"sCount=0 dsCount=0 obj=0x12c72430 self=0xae286400

| sysTid=2100 Nice=0 cgrp=default sched=0/0 句柄=0xb4559f00

|状态=R schedstat=( 0 0 0 ) utm=1 stm=16 core=0 HZ=100

|堆栈=0xa6832000-0xa6834000堆栈大小=1036KB

| held mutexes=“mutator lock”(共享持有)

它通过了 for 循环的第一次迭代,OK。应用程序在第二遍时在 glDrawElements 行崩溃。为什么这在 KitKat Android 4.4 上可以正常工作,而在 Lollipop、Android 5.0 上却崩溃?我需要做哪些不同的事情才能让我的代码与 Lollipop 一起使用?

最佳答案

使用allocateDirect分配索引缓冲区:

myIndexBuffer = ByteBuffer.allocateDirect(indexBuffer.length);

传递给 OpenGLES 的所有缓冲区都应该是直接缓冲区。我不知道为什么它会在 Kit Kat 上起作用,也许某些东西在幕后得到了优化,现在它产生了影响。

关于java - glDrawElements() 会导致 Lollipop 崩溃,但在 KitKat 上正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30472118/

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