How to stop Open GL renderer

For some reason, I need to restart GLSurfaceView.Renderer, so I want some time in my application to call setRenderer (newRenderer) a second time that Android doesn't like it, and throw an IllegalStateException: "setRenderer is already called" ...

Now I know that this is because the renderer is attached to the GLSurfaceView, and I need to unlock this renderer to the surface view, so I can call setRenderer again.

Does anyone have a solution for this?

PS: The code is as follows:

render = new Renderer(this); setContentView(R.layout.main); graphicView = (GLSurfaceView) this.findViewById(R.id.graphicView); //DO STUFF graphicView.setRenderer(render); //DO STUFF Renderer newRender = new Renderer(); graphicView.setRenderer(newRender); <= ...and Android hates this line sooo much 

Thanks!

+6
source share
3 answers

Found the answer here , this "LemonDev" saved my life:

You can stop / destroy the OpenGL surface view and set the new GLSurfaceView to a new renderer by calling OnPause () and postDelayed destruction with Handler and Runnable, for example:

 final Renderer newRender = new GRenderer(this); final LinearLayout ll = (LinearLayout) this.findViewById(R.id.main); graphicView.onPause(); Handler handler = new Handler(); class RefreshRunnable implements Runnable{ public RefreshRunnable(){ } public void run(){ ll.removeView(findViewById(R.id.graphicView)); GLSurfaceView surfaceview = new GLSurfaceView(getApplication()); surfaceview.setId(R.id.graphicView); surfaceview.setRenderer(newRender); ll.addView(surfaceview); graphicView = (GLSurfaceView) findViewById(R.id.graphicView); } }; RefreshRunnable r = new RefreshRunnable(this); handler.postDelayed(r, 500); 

This will create a new renderer and stop GLSurfaceView. Then the application will โ€œdestroyโ€ some internal things that you cannot touch using the SDK ... After that, you can destroy GLSurfaceView, create another one and set a new rendering for it.

My problem is that the textures have not been updated yet, but nonetheless, this is great!

+5
source

Do you really need to call setRenderer twice?

android documentation mentions:

This method should be called once and only once in the GLSurfaceView life cycle.

Instead, I think you want to pause the rendering?

Use setRenderMode(RENDERMODE_WHEN_DIRTY); to pause rendering

+4
source

I found an easier way to update textures. In the Activity class call invalidate (), and then in the class of your surface view, your onDrawFrame will constantly receive calls every time. So you can load the texture here, and thus your work will be done!

+2
source

Source: https://habr.com/ru/post/907587/


All Articles