Using OpenGL from the main thread on Android

混江龙づ霸主 提交于 2021-02-05 20:31:14

问题


I would like to call a GLES20 method when an item from the options menu is selected.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            break;
        // ...
    }
}

This does not work since I am in the main thread and not in GLThread. It says:

call to OpenGL ES API with no current context (logged once per thread)

But what do I have to do to get things working?


回答1:


I found the answer on my own:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.clear:
            // GLSurfaceView.queueEvent
            surface.queueEvent(new Runnable() {
                @Override
                public void run() {
                    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
                }
            });
            break;
        // ...
    }
}


来源:https://stackoverflow.com/questions/5234867/using-opengl-from-the-main-thread-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!