简体   繁体   中英

Android OpenGL Viewport same view on all devices and both horizontal and vertical

I am looking for the proper way to set up OpenGL so that the same information will be displayed on any screen oriented any way. I do understand that everything will appear squished when rotated, but the same information will appear. Is there a way to do that?

For more clarity, imagine a landscape tablet that has a circle in the center of the screen. When the user rotates the tablet and the height because the width and visa versa, instead of seeing a circle they will see an elongated oval.

When you initialize OpenGL ES you set a ratio for projection matrix based on screen size. If you set it to 1, it will modify projection matrix the way image will take the same size in portrait and landscape disregarding screen proportions. Should be the thing you need.

Example:

// correct proportion according width to height ratio
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);
    float ratio = (float) width / height;
    Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 2.0f, 250);
} 

// disregard screen width/height ratio
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);
    Matrix.frustumM(mProjMatrix, 0, -1, 1, -1, 1, 2.0f, 250);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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