Losing OpenGL Textures in Android after a resume

戏子无情 提交于 2019-12-07 01:29:24

问题


My game is working correctly except in the case where I press the HOME button then resume. What needs to be done to use the textures again? I have tried calling onPause and onResume on the GLSurfaceView (when the activity's onPause and onResume are called).

Any ideas what I could be doing wrong?


回答1:


If all else fails, reload the textures:

Pseudocode

for tex in textures:
    if glIsTexture(tex.opengl_name) == false:
        glGenTextures(1, &tex.opengl_name)

    glBindTexture(tex.texture_target);
    glTexImage(..., texture.image);



回答2:


Even if you fixed your problem, just to give a bit of explanation that might help others.

Android does not guaranty to keep the OpenGL context alive when the activity is paused.

You have to recreate every OpenGL-resources on resume (texture in you case, but also VBOs etc etc).

Since API 11, you can ask kindly Android to keep the context, but there is no guaranty it would.




回答3:


After trying:

  1. do not call GLSurfaceView#onPause/onResume in Activity's onPause/onResume
  2. call GLSurfaceView#onPause/onResume, but also set GLSurfaceView#setPreserveEGLContextOnPause(true)

Both of cases fix the HOME-resume-black-texture issue. Guess Android implementation failed to re-create the EGL context when resume. Since onPause/onResume are required to call, should always set setPreserveEGLContextOnPause to true.



来源:https://stackoverflow.com/questions/5965899/losing-opengl-textures-in-android-after-a-resume

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