簡體   English   中英

在OpenGL(-ES)中設置寬高比,獲得奇怪的值

[英]setting aspect ratio in OpenGL(-ES), getting strange values

我在下面的代碼中得到一些奇怪的寬度/高度值,導致拉伸圖片或根本沒有圖片。

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    

while (worldWidth * (screenHeight / screenWidth)應該為(worldWidth * (screenHeight / screenWidth)提供20 * 455/320 = 28。

它在橫向模式下變得更加奇怪,其中worldHeight突然等於0。

我究竟做錯了什么?

我猜, screenHeightscreenWidth都是int 在這種情況下,除法將是整數除法,從而產生舍入/截斷的整數,因此如果比率<1則為0。 將除法的至少一個操作數轉換為浮點數以執行實際浮點除法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM