我嘗試使用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」實際上是當前幀。但我的結果是這樣的:
是否有人知道什麼地方出了問題,怎麼做對不對?
也許「srcWidth」不是128 – 2014-11-24 13:49:20
我查了一下。 128是單個幀的寬度。 – M0rgenstern 2014-11-24 15:17:10