我尝试使用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