Question

As far as I know, when the orientation of an Android device is changed, Open GL ES destroys the onSurfaceCreated() and similar methods and restarts them. In other words, when the orientation is changed, everything gets reset as if it just started because as far as opengl es is concerned, it did. The issue I'm having is I don't want it to wipe the render surface clean. I want it to continue, only in landscape. To reiterate: It stays the same, all the matrices and object positions, only now the orientation is different. I'm not sure how I'd go about this. I thought of maybe preserving the last state before rotation and then applying it to the new render surface - Would this be a good way to about it? So I guess my question is: How would I keep OpenGL ES(2.0) from clearing the render surface(or the illusion of) when the screen orientation is switched?

Was it helpful?

Solution

OpenGL ES does not destroy contexts or surfaces during orientation changes. The View hierarchy does do this; GLSurfaceView is particularly fond of releasing everything it can find.

The onSurfaceCreated() callbacks are associated with Surfaces and Views. When the orientation changes you go through the activity lifecycle events, which tear down the View, and they in turn tear down the Surface. If this didn't happen, and you continued to render into the same Surface, then everything you did would appear to be rotated 90 degrees. (Remember, SurfaceView surfaces are sent directly to the system compositor, not composited with the UI by the application.)

The usual practice is just to let everything get torn down and then build it back up again.

If this doesn't work for some reason, I would recommend that you do your rendering off-screen and then blit the texture to the current surface. You can see an example of this in Grafika's "Double decode" activity, which renders into a pair of SurfaceTextures that it connects (and re-connects) to a pair of TextureViews. Another way to do the same thing would be to create an FBO, render into that, and blit that to your SurfaceView (you can see an example of FBO setup / blit in Grafika's "Record GL app" activity).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top