android ArCore分段-更新时出错

vlf7wbxs  于 2023-03-28  发布在  Android
关注(0)|答案(1)|浏览(105)

我试图使用谷歌的ArCore套件,但我的应用程序崩溃的Session.update()与错误

A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 20724 (GLThread 529), pid 20685 (...)
  • AndroidManifest.xml*
...
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-feature android:name="android.hardware.camera.ar" />
<uses-permission android:name="android.permission.CAMERA" />
...
<application>
    ...
    <meta-data android:name="com.google.ar.core" android:value="required" />
</application>
...
  • MainActivity.kt*

我按照描述检查可用性here

  • MainFragment.kt*
class MainFragment : Fragment(R.layout.fragment_main), GLSurfaceView.Renderer {
    private lateinit var surfaceView: GLSurfaceView

    private lateinit var session: Session

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        surfaceView = view.findViewById(R.id.surface_view)
        surfaceView.setRenderer(this)
        surfaceView.renderMode = GLSurfaceView.RENDERMODE_CONTINUOUSLY

        session = Session(requireContext())
        val config = Config(session)
        session.configure(config)
    }

    override fun onResume() {
        super.onResume()
        session.resume()
    }

    override fun onPause() {
        super.onPause()
        session.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        session.close()
    }

    override fun onSurfaceCreated(gl: GL10, cfg: EGLConfig) {
        GLES20.glClearColor(1f, 1f, 1f, 1f)
        val texture = intArrayOf(-1)
        GLES20.glGenTextures(1, texture, 0)
        if (texture[0] < 0) {
            throw Exception("Failed to Create OpenGL texture")
        }
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0)
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0])
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE)
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE)
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR_MIPMAP_LINEAR )
        GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR)
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, 0)
        session.setCameraTextureName(texture[0])
    }

    override fun onSurfaceChanged(gl: GL10, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(gl: GL10) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        session.update()
    }
}

当我在一个单独的线程中使用EGL手动创建一个OpenGL上下文并初始化时,在其中使用ArCore会话,一切正常,但这是一段冗余的代码,因为我们有GLSurfaceView.Renderer

esbemjvw

esbemjvw1#

在查看了google提供的sample之后,我发现了导致崩溃的原因,看起来在调用GLSurfaceView.setRenderer()之前,必须调用GLSurfaceView.setEGLContextClientVersion()才能使ar.core.Session正常工作。

相关问题