Search code examples
androidopengl-es3dlandscapecube

Android OpenGL ES 3D cube is scewed when in landscape mode


So I have looked absolutely everywhere and can't find the anser. When I draw a 3d cube in opengl es for android it seems to look fine as long as I'm in portrait mode but when I switch to landscape it looks more like a diamond. I assume the issue is with the ratio but I really cant say so here's the code i'm using for the ratios.

public void onSurfaceChanged(GL10 gl, int width, int height){
    gl.glViewport(0, 0, width, height);

    float ratio = (float) width/height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1, 1, 11);
}

The other option that may be the issue I believe is the cube its self but again not sure so here's the vertices and indices.

private int vertices[] = {
        -one, -one, -one,
        one, -one, -one,
        one, one, -one,
        -one, one, -one,
        -one, -one, one,
        one, -one, one,
        one, one, one,
        -one, one, one
};

private byte indices[] = {
        0, 4, 5, 0, 5, 1,
        1, 5, 6, 1, 6, 2,
        2, 6, 7, 2, 7, 3,
        3, 7, 4, 3, 4, 0,
        4, 7, 6, 4, 6, 5,
        3, 0, 1, 3, 1, 2
};

Solution

  • Alright so I guess I'm gonna answer my own question here and for all who need it. I used this ratio code instead under the onSurfaceChanged method.

        double w = width/4.5;
        gl.glViewport(0, (int)-w, width, width);
    
        float ratio = (float) width/width;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 14);