【问题标题】:Android OpenGL ES 2.0 - Using glDelete* in onDestroy()Android OpenGL ES 2.0 - 在 onDestroy() 中使用 glDelete*
【发布时间】:2012-08-16 13:03:54
【问题描述】:

我想在我的安卓游戏中释放纹理、缓冲区和着色器,如果用户点击后退按钮,调用 finish() 活动方法,进入 onDestroy() ,我重写了它来清理游戏数据、关闭服务器连接等等。
我在清单中设置了 android:launchMode="singleTask",因此调用活动的 finish() 总是会导致立即销毁。
但是要使用 glDeleteBuffers(...),例如,它必须从具有 EGL 上下文的线程(Renderer 线程)调用,但即使从 Renderer 类设置和调用此类方法,我得到 - 否OpenGL ES 上下文错误。

我使用 NDK,所以 NativeLib.* 调用了类似的 C/C++ 函数

JNIEXPORT void JNICALL /*class path*/_NativeLib_onDrawFrame(JNIEnv* env, jclass _class)
{
 glClear(GL_COLOR_BUFFER_BIT);
 ...
}

查看

public class OGLES2View extends GLSurfaceView
{

  private static class OGLES2Renderer implements GLSurfaceView.Renderer
  {
    public void onDrawFrame(GL10 unused)
    {
        NativeLib.onDrawFrame();
    }

    public void onSurfaceChanged(GL10 unused, int width, int height)
    {
        NativeLib.onSurfaceChanged(width, height);
    }

    public void onSurfaceCreated(GL10 unused, EGLConfig unusedConfig)
    {
        NativeLib.onSurfaceCreated();
    }

    public void onSurfaceDestroy()
    {
        Log.i(LOG_TAG, "Destroying Opengl objects");
        NativeLib.onGLDestroy();//Don't work - call to OpenGL API with current context
    }

}

public OGLES2Renderer renderer = null; 
public OGLES2View(Context context)
{
    super(context);
    setRenderer(renderer = new OGLES2Renderer());
}

在活动中

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    ogles2View = new OGLES2View(this);
    setContentView(ogles2View);
}

@Override
protected void onDestroy()
{
    Log.i(LOG_TAG, "Got finish request for game");
    super.onDestroy();
    ogles2View.render.onSurfaceDestroy(); // Don't not work
}

【问题讨论】:

    标签: java android c android-ndk opengl-es-2.0


    【解决方案1】:

    一旦 EGL 上下文被销毁,所有 GL 缓冲区等都会随之销毁,所以这是不必要的。

    【讨论】:

      【解决方案2】:

      如果有人确实需要在销毁 EGLContext 之前在 GLThread 上运行某些东西(或在给定方法之外的任何执行点),您可以使用 @ 的 queueEvent() 方法987654324@ 将Runnable 发布到 GLThread。请注意,在使用queueEvent() 之前,您必须先致电setRenderer()

      示例(针对 OP 的问题):

      ...
      
      public void onSurfaceDestroy()
      {
          queueEvent(new Runnable() {
              @Override
              public void run() {
                  Log.i(LOG_TAG, "Destroying Opengl objects");
                  NativeLib.onGLDestroy(); //Works since it's called on the GLThread
              }
          }
      }
      
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-08
        • 1970-01-01
        • 1970-01-01
        • 2012-01-24
        • 2012-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多