2014-11-24 87 views
0

我嘗試使用openGL和lwjgl呈現2D動畫。
我使用的圖像是896x128像素,由7個單幀組成,每個幀的大小均爲128x128像素。
現在,我嘗試使用glTexCoord2f得到紋理的正確部分來渲染這個圖像,如圖https://github.com/mattdesl/lwjgl-basics/wiki/Textures#texture-atlases
我的代碼如下所示:Lwjgl和OpenGL - glTexCoord2f無法正常工作

float srcX = 128*2; 
    float srcY = 0; 
    float srcWidth = 128; 
    float srcHeight = 128; 

    float u = srcX/animation.baseImage().width(); 
    float v = srcY/animation.baseImage().height(); 
    float u2 = (srcX + srcWidth)/animation.baseImage().width(); 
    float v2 = (srcY + srcHeight)/animation.baseImage().height(); 

    GL11.glTexCoord2f(u, v); // Upper Left 
    GL11.glVertex2f(upperLeft.x(), upperLeft.y()); 

    GL11.glTexCoord2f(u2, v); // Upper Right 
    GL11.glVertex2f(upperRight.x(), upperRight.y()); 

    GL11.glTexCoord2f(u2, v2); // Lower Right 
    GL11.glVertex2f(lowerRight.x(), lowerRight.y()); 

    GL11.glTexCoord2f(u, v2); // Lower Left 
    GL11.glVertex2f(lowerLeft.x(), lowerLeft.y()); 

animation.baseImage()的寬度和高度()是完整圖像的大小。
srcX = 128 * 2中的「2」實際上是當前幀。但我的結果是這樣的: wrong animation

是否有人知道什麼地方出了問題,怎麼做對不對?

+0

也許「srcWidth」不是128 – 2014-11-24 13:49:20

+0

我查了一下。 128是單個幀的寬度。 – M0rgenstern 2014-11-24 15:17:10

回答

-1

我發現問題:
OpenGL似乎將整個紋理縮放到2的下一個冪,這將是高度128,等於原始圖像,但是寬度爲1024.如果我做了所有計算animation.baseImage()。width();取而代之的是一切看起來像預期的那樣

有沒有辦法避免這種情況?

+1

「OpenGL」不這樣做。你的紋理加載器似乎這樣做。這是一個很常見的問題,例如「光滑」。這些東西完全是過時的 - 但固定功能管道和您正在使用的即時模式也是如此。 – derhass 2014-11-24 22:07:21