2011-10-05 47 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