简体   繁体   English

使用GLES后onResume后的空白屏幕

[英]blank screen after onResume when using GLES

I'm having a problem where when I get an onResume, I re create the EGL and GLES state from scratch, but it won't draw anything any longer. 我有一个问题,当我得到一个onResume时,我从头开始创建EGL和GLES状态,但它不会再绘制任何东西了。 The important code is identical between the initial create and the resume code. 初始创建和恢复代码之间的重要代码是相同的。 I'm currently stumped as to what could cause this. 我目前难以理解可能导致这种情况的原因。 Things work perfectly with the initial creation and display of my EGL surface. 事情与我的EGL表面的初始创建和显示完美配合。 Only when the app is resumed (press home button, then go back into the app) does it fail to display anything. 只有当应用程序恢复时(按主页按钮,然后返回应用程序)它是否无法显示任何内容。 I've verified that everything else is behaving as it should, input is received, and android callbacks happen as normal (no blocking or locking up in either the dalvik or rendering threads). 我已经验证了其他一切都表现得如此,收到输入,并且android回调正常发生(在dalvik或渲染线程中没有阻塞或锁定)。 The EGL and GLES state are completely destroyed and re-created between onPause and onResume. EGL和GLES状态在onPause和onResume之间被完全销毁并重新创建。

What I do is load all my 3D model vertices, textures etc into memory (not the GL handle) when the app starts or when I need them. 我做的是在应用程序启动或需要时将所有3D模型顶点,纹理等加载到内存(而不是GL句柄)中。

I have a relatively simple game app written in openGL-es 1.1 (no lighting, shaders, particles etc, just textured models). 我有一个相对简单的游戏应用程序用openGL-es 1.1编写(没有光照,着色器,粒子等,只是纹理模型)。 The only things I need to load into the openGL handle ahead of time are the textures, because they takes so long to load. 我需要提前加载到openGL句柄中的唯一内容是纹理,因为它们需要很长时间才能加载。 Everything else can be done at run-time in the onDraw method of the renderer 其他所有内容都可以在渲染器的onDraw方法的运行时完成

So my code looks a bit like this: 所以我的代码看起来有点像这样:

@override
public void onPause(){

    findViewById(R.id.loading_screen).setVisibility(View.VISIBLE); 

}

@Override
public void onResume(){
    super.onResume();

    findViewById(R.id.loading_screen).setVisibility(View.VISIBLE);

    myRenderer.reLoadTextures();   

    findViewById(R.id.loading_screen).setVisibility(View.GONE);     
}

@Override
public void onRestart(){
    super.onRestart();

    findViewById(R.id.loading_screen).setVisibility(View.VISIBLE);

    myRenderer.reLoadTextures();

    findViewById(R.id.loading_screen).setVisibility(View.GONE); 
}

class MyRenderer implements Renderer{

    private HashMap<String,Texture> textures;
    private boolean reloadTextures = false;

    // flag to the renderer that textures need to be reloaded
    public void reLoadTextures(){
        reloadTextures = true;
    }

    public void onDraw(GL10 gl){

        if(reloadTextures){

            // loop through hashmap and reload textures
            Iterator it = textures.entrySet().iterator();
            while(it.hasNext()){
                Entry set = it.next();
                set.getValue().loadTexture(gl, set.getKey(), context);
            }

            reloadTextures = false;
        }

        // draw models as normal

    }

}

... which I know is not what you're really looking for. ...我知道的不是你真正想要的。 Doing it this way means it doesn't matter that the GL state is destroyed. 这样做意味着GL状态被破坏并不重要。 Android will automatically give you a fresh GL state as though it is starting the app for the first time. Android将自动为您提供全新的GL状态,就好像它是第一次启动应用程序一样。 It also means you never have to extend SurfaceView, just implement Renderer 这也意味着你永远不必扩展SurfaceView,只需实现渲染器

This is the simplified version, and it gets a lot more complex once you factor in threading. 这是简化版本,一旦您考虑到线程,它就会变得复杂得多。 There is also a bit of work in Texture.loadTexture() to make sure that the references that models use and the Id's that you get from glGenTextures still match after the app / activity is resumed. Texture.loadTexture()还有一些工作要确保模型使用的引用和从glGenTextures获得的Id在恢复应用程序/活动后仍然匹配。

Also, if you have android:screenOrientation="landscape" in your manifest file, make sure to handle orientation changes, as often, as is the case with my phone, the home screen and lock screen are always in portrait, and on resumption of the app, the orientation will be wrong. 此外,如果你的清单文件中有android:screenOrientation="landscape" ,请确保处理方向更改,就像我的手机一样,主屏幕和锁定屏幕始终处于纵向状态,并且恢复时应用程序,方向将是错误的。 Not overriding onConfigurationChange() can cause problems with the GLSurfaceView (like some textures not showing) even if you've re-loaded them in onResume 不覆盖onConfigurationChange()可能会导致GLSurfaceView出现问题(就像某些纹理没有显示),即使你已经在onResume重新加载了它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM