2015-10-06 107 views
0

所以我絕對到處找,找不到安塞。當我在android的opengl中繪製一個3d立方體時,只要我處於肖像模式,但是當我切換到橫向時,它看起來更像鑽石。我認爲問題與比例,但我真的不能這樣說,這裏是我用於比率的代碼。安卓OpenGL ES 3D立方體在橫向模式下呈現

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); 
} 

,可能是我認爲這個問題的另一種選擇是立方體自身卻又不知道所以這裏的頂點和索引。

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 
}; 

回答

0

好吧,所以我想我會回答我自己的問題在這裏,併爲所有需要它的人。我在onSurfaceChanged方法下使用了這個比率代碼。

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);