2011-10-05 48 views
1

我在下面的代碼中得到了一些奇怪的寬度/高度值,導致拉伸的圖片或根本沒有圖片。在OpenGL(-ES)中設置縱橫比,得到奇怪的值

public void onSurfaceChanged(GL10 gl, int w, int h) { 

    screenHeight = h; // actual height in pixels 
    screenWidth = w; // actual width in pixels 
    worldWidth = 20; // width of the projection (20 units) 
    worldHeight = (int) (worldWidth * (screenHeight/screenWidth)); 

    gl.glMatrixMode(GL10.GL_PROJECTION); 
    gl.glViewport(0, 0, screenWidth, screenHeight); //new viewport 
    gl.glLoadIdentity(); 
    GLU.gluOrtho2D(gl, 0, worldWidth, 0, worldHeight); set the 2D projection 

所以我記錄的變量,這些都是從人像模式:

screenHeight = 455 
screenWidth = 320 
worldHeight = 20 
worldWidth = 20  

(worldWidth * (screenHeight/screenWidth)應該給20 *三百二十零分之四百五十五爲worldHeight = 28。

它會變得在橫向模式下的陌生人,其中worldHeight突然等於0

我在做什麼錯?

回答

4

讓我猜,screenHeightscreenWidth都是int s?在這種情況下,除法將是整數除法,導致舍入/截斷的整數,因此如果比率爲<,則爲0. 1.將除法的至少一個操作數強制轉換爲浮點數以執行實際浮點師:

worldHeight = (int) (worldWidth * ((float)screenHeight/(float)screenWidth)); 
+0

哦ofcoarse,適合症狀完美!明天去試試看。 – user717572