There was such a problem: when you change the orientation from portrait to landscape, all the objects being drawn disappear and only one fill remains. I draw on GLSurfaceView using OpenGL ES 2.0 functions. The onSurfaceChange method looks like this:

@Override public void onSurfaceChanged(GL10 arg0, int width, int height) { glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, width, height); GraphicTricks.initializeFrustumMatrix(width, height); GraphicTricks.bindMatrixForColorShader(); } 

In the initializeFrustumMatrix method (width, height) I create a projection matrix, here is its code:

 public static void initializeFrustumMatrix(int width, int height) { // Отношение большей стороны телефона к меньшей float ratio = 1.0f; // Координаты сторон near границы // Ширина и высота near границы равна 2 float left = -1.0f; float right = 1.0f; float bottom = -1.0f; float top = 1.0f; // Расстояние от камеры до ближайшей границы float near = 1.0f; // Расстояние от камеры до дальнейшей границы float far = 8.0f; if (width > height) { ratio = (float) width / height; left *= ratio; right *= ratio; } else { ratio = (float) height / width; bottom *= ratio; top *= ratio; } Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far); } 

Those. the matrix is ​​written into the float [16] array mProjectionMatrix Well, the last method, bindMatrixForColorShader (), looks like this:

 public static void bindMatrixForColorShader() { Matrix.multiplyMM(mMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); Matrix.multiplyMM(mMatrix, 0, mProjectionMatrix, 0, mMatrix, 0); glUniformMatrix4fv(uMatrixLocationColor, 1, false, mMatrix, 0); } 

Those. simply multiplies all the matrices and associates the result with the shader. Everything works well, until I turn the screen of the phone, when turning all the drawn objects disappear ... Tell me, what could be the problem?

    2 answers 2

    Override the onSurfaceCreated method and draw in it. And add to the activity tag in the manifest

     android:configChanges="orientation|screenSize" 
    • The onSurfaceCreated method is overridden, in it I fill the array of vertices, transfer it to the buffer, create the program from the shaders, get the variable positions from the shaders + set the view matrix. - PashaKrizskiy
    • The onDrawFrame method is also redefined, it directly draws, everything works while the phone is in portrait orientation ... - PashaKrizskiy

    You must use onSaveInstanceState and onRestoreInstanceState .

    • Those. When changing orientation, everything that was created in onSurfaceCreated disappears? - PashaKrizskiy
    • Please try to write more detailed answers. - Nicolas Chabanovsky